hlfw.ca

task

ref: c9cdd2ed16e9cc218a675addc421e2c59b8f1528
dir: /ns.c/

View raw version
#include <u.h>
#include <libc.h>
#include <auth.h>

#include "pro.h"

static void
usage(char *argv0)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	Proconf *conf;
	int i, fd, pipefd[2];

	if(argc != 1)
		usage(argv[0]);
	conf = pconf();
	if(conf == nil){
		fprint(2, "can't parse: %r\n");
		exits("config");
	}
	for(i = 0; i < conf->nstub; i++){
		print(conf->stubs[i]);
		if(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFNAMEG) == 0){
			execl("/bin/aux/stub", "aux/stub", "-d", conf->stubs[i]);
			fprint(2, "can't exec aux/stub: %r\n");
		}
	}
	if(addns(getuser(), ".pro/ns") < 0){
		fprint(2, "can't addns: %r\n");
		exits("addns");
	}
	if(pipe(pipefd) < 0){
		fprint(2, "can't pipe: %r\n");
		exits("pipe");
	}
	if(access(conf->sfile, 0) == 0)
		remove(conf->sfile);
	fd = create(conf->sfile, OWRITE|ORCLOSE, 0600);
	if(fd < 0){
		fprint(2, "can't create %s: %r\n", conf->sfile);
		exits("create");
	}

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

		execl("/bin/exportfs", "exportfs", "-r", ".");
		fprint(2, "can't exec exportfs: %r\n");
		exits("exec");
	default:
		break;
	}
	exits(0);
}