Plan 9 from Bell Labs’s /usr/web/sources/contrib/mycroftiv/root/sys/src/cmd/iosrv/iosrv.c

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


#include <u.h>
#include <libc.h>
#include <ctype.h>
#include "dat.h"
#include "fns.h"

/* Provide access to pipe(3) files for persistent buffered multiplexed i/o */
/* Can be used to provide similar functionality to gnu screen for persistent rc sessions */
/* The 'io' wrapper script is usually used rather than calling iosrv directly */

void
main(int argc, char *argv[])
{
	if(argc < 2){
		print("USAGE: iosrv [-dhprsv] [-t sleeptime] srvname\n");
		exits(nil);
	}
	char tmpstr[SMBUF];
	char noram;
	char nosrv;
	ulong mode;
	mode = 0777L;
	sleepdefault = 10;
	noram = 'n';
	nosrv = 'n';
	nohold = 'n';
	verbosity = DOWN;
	paranoia = DOWN;
	masterswitch = GO;
	for(int i = 0; i < HUBNUM; i++){
		hubmon[i] = STOP;
	}
	memset(extrafds, 0, (2048 * sizeof(int)));
	fdcounter = 0;

	print("Starting iosrv-");
	ARGBEGIN {
	case 'h':
		nohold = 'y';
		break;
	case 'p':
		paranoia = UP;
		break;
	case 'r':
		noram = 'y';
		break;
	case 's':
		nosrv = 'y';
		break;
	case 't':
		sprint(tmpstr, ARGF());
		if(isdigit(*tmpstr)){
			sleepdefault = atoi(ARGF());
		} else {
			print("-t needs numeric parameter to set default sleeptime in milliseconds\n");
		}
		break;
	case 'v':
		verbosity = UP;
		break;	
	default:
		print(" badflag('%c')", ARGC());
	} ARGEND

/* create a /srv of a ramfs that we will use to provide pipe (3) files to clients for input and output */
/* we always make one file descriptor for control input on fdctl */

	srvname = *argv;
	snprint(dirname, SMBUF, "/tmp/%s", srvname);
	if(create(dirname, OREAD, DMDIR | mode) <0){
		sysfatal("couldnt create tmp dir %s\n", dirname);
	}
	if(noram != 'y'){
		if((rfork(RFPROC|RFMEM|RFNOWAIT|RFNOTEG)) == 0){
			print("-using ramfs for temp files-");
			execl("/bin/ramfs", "ramfs", "-m", dirname, nil);
		}
		sleep(500);
	}
	sprint(tmpstr, "%s/ctl", dirname);
	fdctl = mkrdfd(tmpstr);
	sprint(tmpstr, "%s/ctl/data", dirname);
	extrafds[fdcounter++] = open(tmpstr, OWRITE);
	if(nosrv != 'y'){
		print("-starting srvfs at /srv/%s-", srvname);
		if((rfork(RFPROC|RFMEM|RFNOWAIT|RFNOTEG)) == 0){
			execl("/bin/srvfs", "srvfs", srvname, dirname, 0);
		}
		sleep(500);
	}

/* The ctlsrv listens on a separate file descriptor for control messages */
/* It parses them, and dispatches them to the Hubs and/or creates additional procs as needed */

	if(rfork(RFPROC|RFMEM|RFNOWAIT|RFNOTEG) == 0){
		ctlsrv(fdctl);
		exits(nil);
	}

/* We exit but backgrounded processes will be printing output on /fd/1 by default */
/* the wrapper scripts will generally either redirect this elsewhere or attach to the ctl fd */

	print("\nrforked everything, main exiting.\n");
	exits(nil);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.