Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/newns.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/*
	newns : This program follows the same sequence as ip/httpd/httpd to create a new namespace from /lib/namespace and then add any actions supplied on the command line.
		Primarily this is as a debugging tool for ip/httpd/httpd and a little bit of curiosity. Once I'd come across newns(2) I immediately tried to run it from the cmd line so I figured that was a niche because I had no idea how to run a shell that didn't inherit a namespace.

	8c newns.c && 8l newns.8 &&  mv 8.out newns

*/
#include <u.h>
#include <libc.h>
#include <auth.h>

static	char		*nsfile;
static	char		*user;

void
usage(void)
{
	fprint(2, "usage: newns [-u user] [-a nsfile] \n");
	exits("usage");
}

void
change_user(void) {
	int fd;
	if(user != nil) {
		fd = open("#c/user", OWRITE);
		if(fd < 0 || write(fd, user, strlen(user)) < 0)
			sysfatal("can't become user");
		close(fd);
	}
}

void
main(int argc, char **argv)
{
	int fd;
	nsfile = nil;
	user = nil;

	ARGBEGIN{
	case 'a':
		nsfile = ARGF();
		break;
	case 'u':
		user = ARGF();
		break;

	}ARGEND

	if(argc)
		usage();

	if(user)
		change_user();
	else
		user = getuser();

	switch(rfork(RFFDG|RFNOWAIT|RFNAMEG|RFPROC)) {
	case -1:
		sysfatal("fork");
	case 0:
		break;
	default:
		exits(nil);
	}


	if(newns(user, nil) < 0)
		sysfatal("newns failed on /lib/namespace");
	if(nsfile != nil && addns(user, nsfile) < 0)
		sysfatal("addns failed on nsfile");

	execl("/bin/rc", "rc");
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].