hlfw.ca

task

ref: a23f89cda579f886e1c843db3baf4fbe158a28c5
dir: /proj.c/

View raw version
#include <u.h>
#include <libc.h>
#include <auth.h>
static void
usage(void)
{
	fprint(2, "usage: %s [-i] [-n nsdir] [-w wdir] [-s srvfile] project\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *ename, *arglist[16], **argp;
	char *user, *home, *wdir, *sfile;
	int n, fd, stdin, pipefd[2];
	char buf[64], nsdir[64];

	argp = arglist;
	home = nil;
	ename = "/bin/exportfs";
	wdir = "/mnt/work";
	sfile = "/srv/work";
	stdin = 0;
	
	*argp++ = "exportfs";
	ARGBEGIN{
	default:
		usage();
	case 'i':
		stdin = 1;
		break;
	case 'n':
		home = EARGF(usage());
		strcpy(nsdir, home);
		break;
	case 'w':
		wdir = EARGF(usage());
		break;
	case 's':
		sfile = EARGF(usage());
		break;
	}ARGEND

	if(argc != 1 && stdin != 1)
		usage();
	*argp++ = "-r";
	*argp++ = wdir;
	*argp = 0;

	user = getuser();
	if(!home){
		home = getenv("home");
		snprint(nsdir, sizeof nsdir, "%s/lib", home);
	}

	if(stdin){
		n = snprint(buf, sizeof buf, "%s/namespace.", nsdir);
		n += read(0, buf+n, sizeof buf - n);
		buf[n-1] = 0;
	} else
		snprint(buf, sizeof buf, "%s/namespace.%s", nsdir, argv[0]);
	if(addns(user, buf) < 0){
		fprint(2, "can't addns: %r\n");
		exits("addns");
	}
	if(pipe(pipefd) < 0){
		fprint(2, "can't pipe: %r\n");
		exits("pipe");
	}
	// Probably check for /srv/work existing and delete if so
	if(access(sfile, 0) == 0)
		remove(sfile);
	fd = create(sfile, OWRITE|ORCLOSE, 0600);
	if(fd < 0){
		fprint(2, "can't create /srv/work: %r\n");
		exits("create");
	}
	fprint(fd, "%d", pipefd[1]);
	close(pipefd[1]);

	switch(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFFDG)){
	case -1:
		fprint(2, "can't rfork: %r\n");
		exits("rfork");
	case 0:
		dup(pipefd[0], 0);
		dup(pipefd[0], 1);
		close(pipefd[0]);

		exec(ename, arglist);
		fprint(2, "can't exec exportfs: %r\n");
		exits("exec");
	default:
		break;
	}
	exits(0);
}