shithub: drawcpu

ref: 56e529d5e9a213be7a9738f2254fc4c31f46359f
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";
int  debug;

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 [-d] -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);

	addnote(note);
	return;
}

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

	fs = auth = nil;
	debug 		= 0;
	kerndate 	= seconds();
	eve 		= getuser();

	ARGBEGIN {
	case 'd':
		debug++;
		break;
	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();
	/* We don't use argv for the root loadtext */
	argv[0] = "";

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

	chandevreset();
	chandevinit();
	quotefmtinstall();
	notify(notehandler);

	/* 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);

	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);

	/* Used for `os` */
	bind("#C", "/mnt/term/", MAFTER);
	bind("#U", "/mnt/cpu", MAFTER);
	bind("/mnt/cpu/tmp", "/tmp", MCREATE|MAFTER);
	bind("/root", "/", MCREATE|MAFTER);

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

	loadtext("/bin/rc", 1, argv);
	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", "cpu", 0);
	ksetenv("sysname", sysname, 0);
	ksetenv("prompt", "unix\% ", 0);
	ksetenv("nvram", "/tmp/nvram", 0); /* Set up in the host /tmp */

	/* Run machine */
	procrun(0);
	exits(0);
}