9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

post9p.c (1523B)


      1 #include <u.h>
      2 #include <libc.h>
      3 
      4 int chattyfuse;
      5 
      6 int
      7 post9pservice(int fd, char *name, char *mtpt)
      8 {
      9 	int i, pid;
     10 	char *ns, *addr;
     11 	Waitmsg *w;
     12 
     13 	if(name == nil && mtpt == nil){
     14 		close(fd);
     15 		werrstr("nothing to do");
     16 		return -1;
     17 	}
     18 
     19 	if(name){
     20 		if(strchr(name, '!'))	/* assume is already network address */
     21 			addr = strdup(name);
     22 		else{
     23 			if((ns = getns()) == nil)
     24 				return -1;
     25 			addr = smprint("unix!%s/%s", ns, name);
     26 			free(ns);
     27 		}
     28 		if(addr == nil)
     29 			return -1;
     30 		switch(pid = fork()){
     31 		case -1:
     32 			return -1;
     33 		case 0:
     34 			dup(fd, 0);
     35 			dup(fd, 1);
     36 			for(i=3; i<20; i++)
     37 				close(i);
     38 			execlp("9pserve", "9pserve", "-u", addr, (char*)0);
     39 			fprint(2, "exec 9pserve: %r\n");
     40 			_exits("exec");
     41 		}
     42 		close(fd);
     43 		w = waitfor(pid);
     44 		if(w == nil)
     45 			return -1;
     46 		if(w->msg && w->msg[0]){
     47 			free(w);
     48 			werrstr("9pserve failed");
     49 			return -1;
     50 		}
     51 		free(w);
     52 		if(mtpt){
     53 			/* reopen */
     54 			if((fd = dial(addr, nil, nil, nil)) < 0){
     55 				werrstr("cannot reopen for mount: %r");
     56 				return -1;
     57 			}
     58 		}
     59 		free(addr);
     60 	}
     61 	if(mtpt){
     62 		switch(pid = rfork(RFFDG|RFPROC|RFNOWAIT)){
     63 		case -1:
     64 			return -1;
     65 		case 0:
     66 			dup(fd, 0);
     67 			for(i=3; i<20; i++)
     68 				close(i);
     69 
     70 			/* Try v9fs on Linux, which will mount 9P directly. */
     71 			execlp("mount9p", "mount9p", "-", mtpt, (char*)0);
     72 
     73 			if(chattyfuse)
     74 				execlp("9pfuse", "9pfuse", "-D", "-", mtpt, (char*)0);
     75 			else
     76 				execlp("9pfuse", "9pfuse", "-", mtpt, (char*)0);
     77 			fprint(2, "exec 9pfuse: %r\n");
     78 			_exits("exec");
     79 		}
     80 		close(fd);
     81 	}
     82 	return 0;
     83 }