blob: 4420c2d77a74c31a08ee7bff1b0b9ecf8884d17b [file] [log] [blame]
rsc2277c5d2004-03-21 04:33:13 +00001#include <u.h>
2#include <libc.h>
3#include <fcall.h>
4#include <thread.h>
5#include <9p.h>
6#include <auth.h>
7#include "post.h"
8
9Postcrud*
10_post1(Srv *s, char *name, char *mtpt, int flag)
11{
12 Postcrud *p;
13
14 p = emalloc9p(sizeof *p);
15 if(!s->nopipe){
16 if(pipe(p->fd) < 0)
17 sysfatal("pipe: %r");
18 s->infd = s->outfd = p->fd[1];
19 s->srvfd = p->fd[0];
20 }
21 if(name)
22 if(postfd(name, s->srvfd) < 0)
23 sysfatal("postfd %s: %r", name);
24 p->s = s;
25 p->mtpt = mtpt;
26 p->flag = flag;
27 return p;
28}
29
30void
31_post2(void *v)
32{
33 Srv *s;
34
35 s = v;
rsc2277c5d2004-03-21 04:33:13 +000036 if(!s->leavefdsopen){
rsc5c8a0422004-12-26 21:46:26 +000037 rfork(RFNOTEG);
rsc2277c5d2004-03-21 04:33:13 +000038 rendezvous((ulong)s, 0);
39 close(s->srvfd);
40 }
41 srv(s);
42}
43
44void
45_post3(Postcrud *p)
46{
47 /*
48 * Normally the server is posting as the last thing it does
49 * before exiting, so the correct thing to do is drop into
50 * a different fd space and close the 9P server half of the
51 * pipe before trying to mount the kernel half. This way,
52 * if the file server dies, we don't have a ref to the 9P server
53 * half of the pipe. Then killing the other procs will drop
54 * all the refs on the 9P server half, and the mount will fail.
55 * Otherwise the mount hangs forever.
56 *
57 * Libthread in general and acme win in particular make
58 * it hard to make this fd bookkeeping work out properly,
59 * so leaveinfdopen is a flag that win sets to opt out of this
60 * safety net.
61 */
62 if(!p->s->leavefdsopen){
63 rfork(RFFDG);
64 rendezvous((ulong)p->s, 0);
65 close(p->s->infd);
66 if(p->s->infd != p->s->outfd)
67 close(p->s->outfd);
68 }
69
70 if(p->mtpt){
71 if(amount(p->s->srvfd, p->mtpt, p->flag, "") == -1)
72 sysfatal("mount %s: %r", p->mtpt);
73 }else
74 close(p->s->srvfd);
75 free(p);
76}
77