shithub: drawcpu

ref: be21af8515dcebab0ae2991e8e952af04e06baad
dir: /main.c/

View raw version
#include "u.h"
#include "lib.h"
#include "kern/dat.h"
#include "kern/fns.h"
#include "user.h"
#include "drawcpu.h"
#include "auth.h"
#include "args.h"
#include "proc.h"

char *argv0;
char cons[] = "/dev/cons";

void
sizebug(void)
{
	/*
	 * Needed by various parts of the code.
	 * This is a huge bug.
	 */
	assert(sizeof(char)==1);
	assert(sizeof(short)==2);
	assert(sizeof(ushort)==2);
	assert(sizeof(int)==4);
	assert(sizeof(uint)==4);
	assert(sizeof(long)==4);
	assert(sizeof(ulong)==4);
	assert(sizeof(vlong)==8);
	assert(sizeof(uvlong)==8);
}

static void
usage(void)
{
	fprintf(stderr, "usage: drawcpu -f <fileserver> -a <authserver>\n");
	exit(1);
}

void
postsrv(int fd, char *name)
{
	int sfd;

	sfd = create(smprint("/srv/%s", name), OWRITE, 0666);
	fprint(sfd, "%d", fd);
	close(sfd);
}

void
notehandler(void *d, char *note)
{
	USED(d);
print("note: %s\n", note);
	if(strncmp(note, "sys:", 4) == 0)
		return;
	
	if(strncmp(note, "emu:", 4) == 0)
		exits(note);

	return;
}

int
main(int argc, char **argv)
{
	extern ulong kerndate;
	char *fs, *auth, *sysname;
	int sfd, fd, tls = 0;

	fs = auth = nil;
	kerndate 	= seconds();
	eve 		= getuser();
	sysname = "drawcpu";
	
	ARGBEGIN {
	case 'a':
		auth = EARGF(usage());
		break;
	case 'f':
		fs = EARGF(usage());
		break;
	case 'u':
		eve = EARGF(usage());
		break;
	case 's':
		sysname = EARGF(usage());
		break;
	case 't':
		tls++;
		break;
	default:
		usage();
	} ARGEND;
		
	if(!auth || !fs)
		usage();

	sizebug();
	osinit();
	procinit0();
	printinit();

	chandevreset();
	chandevinit();
	quotefmtinstall();

	/* 
	 * This ordering is based on /lib/namespace 
	 * - Our host fs connection lives in /srv/boot
	 * - Everything else should eventually get a /srv
	 */
	bind("#c", "/dev", MREPL);
	bind("#d", "/fd", MREPL);
	bind("#e", "/env", MREPL|MCREATE);
	bind("#p", "/proc", MREPL);
	bind("#s", "/srv", MREPL|MCREATE);
	//bind -q #σ /shr MREPL
	bind("#¤", "/dev", MAFTER);
	bind("#I", "/net", MAFTER);
	bind("#a", "/net", MAFTER);

	/* Environment */
	ksetenv("home", smprint("/usr/%s", eve), 0);
	ksetenv("cputype", "arm", 0); // cputype being anything but arm should fail for now
	ksetenv("objtype", "arm", 0);
	ksetenv("rootdir", "/root", 0);
	ksetenv("auth", auth, 0);
	ksetenv("user", eve, 0);
	ksetenv("service", "unix", 0);
	ksetenv("sysname", sysname, 0);
	ksetenv("prompt", "unix\% ", 0);
	ksetenv("nvram", "/tmp/nvram", 0); /* Set up in the host /tmp */

	open(cons, OREAD);
	open(cons, OWRITE);
	open(cons, OWRITE);

	fd = dial(netmkaddr(fs, tls ? "tls" : "tcp", "9fs"), nil, nil, nil);
	if(fd < 0)
		panic("dial fs: %r");
	postsrv(fd, "boot");

	/* Set up our root */
	sfd = open("/srv/boot", ORDWR);
	mount(sfd, -1, "/root", MREPL|MCREATE|MCACHE, "");
	close(sfd);

	/* Build our /bin directory */
	bind("/root", "/", MAFTER);
	if(bind("/arm/bin", "/bin", MCREATE|MREPL) < 0 || bind("/rc/bin", "/bin", MAFTER) < 0)
		panic("bind bin: %r");

	/* Run machine */
	loadtext(argv[0], argc, argv);
	procrun(0);

	/* Clean up */
	close(fd);
	exits(0);
}