+static int
+srvfd(char *s, int mode, int sfd)
+{
+ int fd;
+ char buf[32];
+
+ fd = create(s, ORCLOSE|OWRITE, mode);
+ if(fd < 0){
+ remove(s);
+ fd = create(s, ORCLOSE|OWRITE, mode);
+ if(fd < 0)
+ panic(s);
+ }
+ sprint(buf, "%d", sfd);
+ if(write(fd, buf, strlen(buf)) != strlen(buf))
+ panic("srv write");
+ return sfd;
+}
+
+static void
+postservice(void)
+{
+ char buf[3*NAMELEN];
+ int p[2];
+
+ if(service[0] == 0)
+ panic("no service name");
+
+ /* serve 9p for -s */
+ if(sfd >= 0){
+ srvchan(sfd, "stdio");
+ sfd = -1;
+ }
+
+ /* post 9p service */
+ if(pipe(p) < 0)
+ panic("can't make a pipe");
+ snprint(buf, sizeof(buf), "#s/%s", service);
+ srvfd(buf, 0666, p[0]);
+ close(p[0]);
+ srvchan(p[1], buf);
+
+ /* post cmd service */
+ if(pipe(p) < 0)
+ panic("can't make a pipe");
+ snprint(buf, sizeof(buf), "#s/%s.cmd", service);
+ srvfd(buf, 0660, p[0]);
+ close(p[0]);
+
+ /* use it as stdin */
+ dup(p[1], 0);
+ close(p[1]);
+}
+