⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
					"ipmask%d %I\n", i, ipaddr[i].defmask);		}		putbuf(p);		f.modconf = 0;		f.newconf = 0;		print("config block written\n");		goto start;	}	putbuf(p);	print("service    %s\n", service);	print("ipauth  %I\n", authip);	print("ipsntp  %I\n", sntpip);	for(i=0; i<10; i++) {		if(isvalidip(ipaddr[i].sysip)) {			print("ip%d     %I\n", i, ipaddr[i].sysip);			print("ipgw%d   %I\n", i, ipaddr[i].defgwip);			print("ipmask%d %I\n", i, ipaddr[i].defmask);		}	}loop:	/*	 * part 2 -- squeeze out the deleted filesystems	 */	for(fs=filsys; fs->name; fs++)		if(fs->conf == 0) {			for(; fs->name; fs++)				*fs = *(fs+1);			goto loop;		}	if(filsys[0].name == 0)		panic("no filsys");	/*	 * part 3 -- compile the device expression	 */	error = 0;	for(fs=filsys; fs->name; fs++) {		print("filsys %s %s\n", fs->name, fs->conf);		fs->dev = iconfig(fs->conf);		if(f.error) {			error = 1;			continue;		}	}	if(error)		panic("fs config");	/*	 * part 4 -- initialize the devices	 */	for(fs=filsys; fs->name; fs++) {		delay(3000);		print("sysinit: %s\n", fs->name);		if(fs->flags & FREAM)			devream(fs->dev, 1);		if(fs->flags & FRECOVER)			devrecover(fs->dev);		devinit(fs->dev);	}	floppyhalt();			/* don't wear out the floppy */	if (copyworm) {		dowormcopy();		/* can return if user quits early */		panic("copyworm bailed out!");	}	if (copydev)		if (dodevcopy() < 0)			panic("copydev failed!");		else			panic("copydev done.");}/* an unfinished idea.  a non-blocking rawchar() would help. */static intuserabort(char *msg){#ifdef IdeaIsFinished	if (consgetcifany() == 'q') {		print("aborting %s\n", msg);		return 1;	}#else	USED(msg);#endif /* IdeaIsFinished */	return 0;}static intblockok(Device *d, Off a){	Iobuf *p = getbuf(d, a, Bread);	if (p == 0) {		print("i/o error reading %Z block %lld\n", d, (Wideoff)a);		return 0;	}	putbuf(p);	return 1;}/* * special case for fake worms only: * we need to size the inner cw's worm device. * in particular, we want to avoid copying the fake-worm bitmap * at the end of the device. * * N.B.: for real worms (e.g. cw jukes), we need to compute devsize(cw(juke)), * *NOT* devsize(juke). */static Device *wormof(Device *dev){	Device *worm = dev, *cw;	if (dev->type == Devfworm) {		cw = dev->fw.fw;		if (cw != nil && cw->type == Devcw)			worm = cw->cw.w;	}	// print("wormof(%Z)=%Z\n", dev, worm);	return worm;}/* * return the number of the highest-numbered block actually written, plus 1. * 0 indicates an error. */static Devsizewrittensize(Device *worm){	Devsize lim = devsize(worm);	Iobuf *p;	print("devsize(%Z) = %lld\n", worm, (Wideoff)lim);	if (!blockok(worm, 0) || !blockok(worm, lim-1))		return 0;	delay(5*1000);	if (userabort("sanity checks"))		return 0;	/* find worm's last valid block in case "worm" is an (f)worm */	while (lim > 0) {		if (userabort("sizing")) {			lim = 0;		/* you lose */			break;		}		--lim;		p = getbuf(worm, lim, Bread);		if (p != 0) {			/* actually read one okay? */			putbuf(p);			break;		}	}	print("limit(%Z) = %lld\n", worm, (Wideoff)lim);	return lim <= 0? 0: lim + 1;}/* copy worm fs from "main"'s inner worm to "output" */static voiddowormcopy(void){	Filsys *f1, *f2;	Device *fdev, *from, *to = nil;	Iobuf *p;	Off a;	Devsize lim;	/*	 * convert file system names into Filsyss and Devices.	 */	f1 = fsstr("main");	if(f1 == nil)		panic("main file system missing");	fdev = f1->dev;	from = wormof(fdev);			/* fake worm special */	if (from->type != Devfworm && from->type != Devcw) {		print("main file system is not a worm; copyworm may not do what you want!\n");		print("waiting for 20 seconds...\n");		delay(20000);	}	f2 = fsstr("output");	if(f2 == nil) {		print("no output file system - check only\n\n");		print("reading worm from %Z (worm %Z)\n", fdev, from);	} else {		to = f2->dev;		print("\ncopying worm from %Z (worm %Z) to %Z, starting in 8 seconds\n",			fdev, from, to);		delay(8000);	}	if (userabort("preparing to copy"))		return;	/*	 * initialise devices, size them, more sanity checking.	 */	devinit(from);	if (0 && fdev != from) {		devinit(fdev);		print("debugging, sizing %Z first\n", fdev);		writtensize(fdev);	}	lim = writtensize(from);	if(lim == 0)		panic("no blocks to copy on %Z", from);	if (to) {		print("reaming %Z in 8 seconds\n", to);		delay(8000);		if (userabort("preparing to ream & copy"))			return;		devream(to, 0);		devinit(to);		print("copying worm: %lld blocks from %Z to %Z\n",			(Wideoff)lim, from, to);	}	/* can't read to's blocks in case to is a real WORM device */	/*	 * Copy written fs blocks, a block at a time (or just read	 * if no "output" fs).	 */	for (a = 0; a < lim; a++) {		if (userabort("copy"))			break;		p = getbuf(from, a, Bread);		/*		 * if from is a real WORM device, we'll get errors trying to		 * read unwritten blocks, but the unwritten blocks need not		 * be contiguous.		 */		if (p == 0) {			print("%lld not written yet; can't read\n", (Wideoff)a);			continue;		}		if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {			print("out block %lld: write error; bailing",				(Wideoff)a);			break;		}		putbuf(p);		if(a % 20000 == 0)			print("block %lld %T\n", (Wideoff)a, time());	}	/*	 * wrap up: sync target, loop	 */	print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);	sync("wormcopy");	delay(2000);	print("looping; reset the machine at any time.\n");	for (; ; )		continue;		/* await reset */}/* copy device from src to dest */static intdodevcopy(void){	Device *from, *to;	Iobuf *p;	Off a;	Devsize lim, tosize;	/*	 * convert config strings into Devices.	 */	from = iconfig(src);	if(f.error || from == nil) {		print("bad src device %s\n", src);		return -1;	}	to = iconfig(dest);	if(f.error || to == nil) {		print("bad dest device %s\n", dest);		return -1;	}	/*	 * initialise devices, size them, more sanity checking.	 */	devinit(from);	lim = devsize(from);	if(lim == 0)		panic("no blocks to copy on %Z", from);	devinit(to);	tosize = devsize(to);	if(tosize == 0)		panic("no blocks to copy on %Z", to);	/* use smaller of the device sizes */	if (tosize < lim)		lim = tosize;	print("copy %Z to %Z in 8 seconds\n", from, to);	delay(8000);	if (userabort("preparing to copy"))		return -1;	print("copying dev: %lld blocks from %Z to %Z\n", (Wideoff)lim,		from, to);	/*	 * Copy all blocks, a block at a time.	 */	for (a = 0; a < lim; a++) {		if (userabort("copy"))			break;		p = getbuf(from, a, Bread);		/*		 * if from is a real WORM device, we'll get errors trying to		 * read unwritten blocks, but the unwritten blocks need not		 * be contiguous.		 */		if (p == 0) {			print("%lld not written yet; can't read\n", (Wideoff)a);			continue;		}		if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {			print("out block %lld: write error; bailing",				(Wideoff)a);			break;		}		putbuf(p);		if(a % 20000 == 0)			print("block %lld %T\n", (Wideoff)a, time());	}	/*	 * wrap up: sync target	 */	print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);	sync("devcopy");	return 0;}voidgetline(char *line){	char *p;	int c;	p = line;	for(;;) {		c = rawchar(0);		if(c == 0 || c == '\n') {			*p = 0;			return;		}		if(c == '\b') {			p--;			continue;		}		*p++ = c;	}}voidarginit(void){	int verb, c;	char line[2*Maxword], word[Maxword+1], *cp;	uchar localip[Pasize];	Filsys *fs;	if(nvrcheck() == 0){		print("for config mode hit a key within 5 seconds\n");		c = rawchar(5);		if(c == 0) {			print("	no config\n");			return;		}	}	for (;;) {		print("config: ");		getline(line);		cp = getwd(word, line);		if (word[0] == '\0' || word[0] == '#')			continue;		if(strcmp(word, "end") == 0)			return;		if(strcmp(word, "halt") == 0) {			floppyhalt();			exit();		}		if(strcmp(word, "allow") == 0) {			wstatallow = 1;			writeallow = 1;			continue;		}		if(strcmp(word, "copyworm") == 0) {			copyworm = 1;			continue;		}		if(strcmp(word, "copydev") == 0) {	/* not yet documented */			cp = getwd(word, cp);			if(testconfig(word))				continue;			src = strdup(word);			getwd(word, cp);			if(testconfig(word))				continue;			dest = strdup(word);			copydev = 1;			continue;		}		if(strcmp(word, "noauth") == 0) {			noauth = !noauth;			continue;		}		if(strcmp(word, "noattach") == 0) {			noattach = !noattach;			continue;		}		if(strcmp(word, "readonly") == 0) {			readonly = 1;			continue;		}		if(strcmp(word, "ream") == 0) {			verb = FREAM;			goto gfsname;		}		if(strcmp(word, "recover") == 0) {			verb = FRECOVER;			goto gfsname;		}		if(strcmp(word, "filsys") == 0) {			verb = FEDIT;			goto gfsname;		}		if(strcmp(word, "nvram") == 0) {			getwd(word, cp);			if(testconfig(word))				continue;			/* if it fails, it will complain */			nvrsetconfig(word);			continue;		}		if(strcmp(word, "config") == 0) {			getwd(word, cp);			if(!testconfig(word) && nvrsetconfig(word) == 0)				f.newconf = 1;			continue;		}		if(strcmp(word, "service") == 0) {			getwd(word, cp);			strcpy(service, word);			f.modconf = 1;			continue;		}		if(strcmp(word, "ipauth") == 0) {			f.ipauthset = 1;			verb = 2;		} else if(astrcmp(word, "ip") == 0)			verb = 0;		else if(astrcmp(word, "ipgw") == 0)			verb = 1;		else if(astrcmp(word, "ipmask") == 0)			verb = 3;		else if(astrcmp(word, "ipsntp") == 0)			verb = 4;		else {			print("unknown config command\n");			print("	type end to get out\n");			continue;		}		getwd(word, cp);		if(chartoip(localip, word)) {			print("bad ip address\n");			continue;		}		switch(verb) {		case 0:			memmove(ipaddr[aindex].sysip, localip,				sizeof(ipaddr[aindex].sysip));			break;		case 1:			memmove(ipaddr[aindex].defgwip, localip,				sizeof(ipaddr[aindex].defgwip));			break;		case 2:			memmove(authip, localip, sizeof(authip));			break;		case 3:			memmove(ipaddr[aindex].defmask, localip,				sizeof(ipaddr[aindex].defmask));			break;		case 4:			memmove(sntpip, localip, sizeof(sntpip));			break;		}		f.modconf = 1;		continue;	gfsname:		cp = getwd(word, cp);		for(fs=filsys; fs->name; fs++)			if(strcmp(word, fs->name) == 0)				break;		if (fs->name == nil) {			memset(fs, 0, sizeof(*fs));			fs->name = strdup(word);		}		switch(verb) {		case FREAM:			if(strcmp(fs->name, "main") == 0)				wstatallow = 1;	/* only set, never reset */		case FRECOVER:			fs->flags |= verb;			break;		case FEDIT:			f.modconf = 1;			getwd(word, cp);			fs->flags |= verb;			if(word[0] == 0)				fs->conf = nil;			else if(!testconfig(word))				fs->conf = strdup(word);			break;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -