xref: /csrg-svn/old/init/init.c (revision 29836)
121135Sdist /*
228801Skarels  * Copyright (c) 1980,1986 Regents of the University of California.
321135Sdist  * All rights reserved.  The Berkeley software License Agreement
421135Sdist  * specifies the terms and conditions for redistribution.
521135Sdist  */
621135Sdist 
712682Ssam #ifndef lint
8*29836Ssam static char sccsid[] = "@(#)init.c	5.7 (Berkeley) 10/13/86";
921135Sdist #endif not lint
1012682Ssam 
111029Sbill #include <signal.h>
121029Sbill #include <sys/types.h>
131029Sbill #include <utmp.h>
141029Sbill #include <setjmp.h>
151403Sbill #include <sys/reboot.h>
162821Swnj #include <errno.h>
1713021Ssam #include <sys/file.h>
1816452Sroot #include <ttyent.h>
1928801Skarels #include <sys/syslog.h>
2023147Sbloom #include <sys/stat.h>
211029Sbill 
221029Sbill #define	LINSIZ	sizeof(wtmp.ut_line)
2324494Skarels #define	CMDSIZ	200	/* max string length for getty or window command*/
2423147Sbloom #define	ALL	p = itab; p ; p = p->next
251029Sbill #define	EVER	;;
261029Sbill #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
271029Sbill #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
281029Sbill 
291029Sbill char	shell[]	= "/bin/sh";
301029Sbill char	minus[]	= "-";
311029Sbill char	runc[]	= "/etc/rc";
3223147Sbloom char	utmpf[]	= "/etc/utmp";
331029Sbill char	wtmpf[]	= "/usr/adm/wtmp";
341029Sbill char	ctty[]	= "/dev/console";
351029Sbill 
361029Sbill struct utmp wtmp;
371029Sbill struct	tab
381029Sbill {
391029Sbill 	char	line[LINSIZ];
4018542Sralph 	char	comn[CMDSIZ];
411029Sbill 	char	xflag;
421029Sbill 	int	pid;
4318542Sralph 	int	wpid;		/* window system pid for SIGHUP	*/
4418542Sralph 	char	wcmd[CMDSIZ];	/* command to start window system process */
455971Sroot 	time_t	gettytime;
465971Sroot 	int	gettycnt;
4722181Skarels 	time_t	windtime;
4822181Skarels 	int	windcnt;
4923147Sbloom 	struct	tab *next;
5023147Sbloom } *itab;
511029Sbill 
521029Sbill int	fi;
531029Sbill int	mergflag;
541029Sbill char	tty[20];
551429Sbill jmp_buf	sjbuf, shutpass;
562821Swnj time_t	time0;
571029Sbill 
581029Sbill int	reset();
592821Swnj int	idle();
601029Sbill char	*strcpy(), *strcat();
611029Sbill long	lseek();
621029Sbill 
6318542Sralph struct	sigvec rvec = { reset, sigmask(SIGHUP), 0 };
6413021Ssam 
6523147Sbloom 
66*29836Ssam #if defined(vax) || defined(tahoe)
671029Sbill main()
681029Sbill {
69*29836Ssam #if defined(tahoe)
70*29836Ssam 	register int r12;		/* make sure r11 gets bootflags */
71*29836Ssam #endif
721403Sbill 	register int r11;		/* passed thru from boot */
7313021Ssam #else
749869Spugs main(argc, argv)
759869Spugs 	char **argv;
769869Spugs {
7713021Ssam #endif
781403Sbill 	int howto, oldhowto;
791403Sbill 
802821Swnj 	time0 = time(0);
81*29836Ssam #if defined(vax) || defined(tahoe)
821403Sbill 	howto = r11;
8313021Ssam #else
849869Spugs 	if (argc > 1 && argv[1][0] == '-') {
859869Spugs 		char *cp;
869869Spugs 
879869Spugs 		howto = 0;
889869Spugs 		cp = &argv[1][1];
899869Spugs 		while (*cp) switch (*cp++) {
909869Spugs 		case 'a':
919869Spugs 			howto |= RB_ASKNAME;
929869Spugs 			break;
939869Spugs 		case 's':
949869Spugs 			howto |= RB_SINGLE;
959869Spugs 			break;
969869Spugs 		}
979869Spugs 	} else {
989869Spugs 		howto = RB_SINGLE;
999869Spugs 	}
10013021Ssam #endif
10124854Seric 	openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH);
10213021Ssam 	sigvec(SIGTERM, &rvec, (struct sigvec *)0);
1032821Swnj 	signal(SIGTSTP, idle);
1041029Sbill 	signal(SIGSTOP, SIG_IGN);
1051029Sbill 	signal(SIGTTIN, SIG_IGN);
1061029Sbill 	signal(SIGTTOU, SIG_IGN);
10713021Ssam 	(void) setjmp(sjbuf);
10813021Ssam 	for (EVER) {
1091403Sbill 		oldhowto = howto;
1101403Sbill 		howto = RB_SINGLE;
1111429Sbill 		if (setjmp(shutpass) == 0)
1121429Sbill 			shutdown();
1131403Sbill 		if (oldhowto & RB_SINGLE)
1141403Sbill 			single();
1151403Sbill 		if (runcom(oldhowto) == 0)
1161403Sbill 			continue;
1171029Sbill 		merge();
1181029Sbill 		multiple();
1191029Sbill 	}
1201029Sbill }
1211029Sbill 
1221429Sbill int	shutreset();
1231429Sbill 
1241029Sbill shutdown()
1251029Sbill {
1261029Sbill 	register i;
12723147Sbloom 	register struct tab *p, *p1;
1281029Sbill 
12923147Sbloom 	close(creat(utmpf, 0644));
1301029Sbill 	signal(SIGHUP, SIG_IGN);
13123147Sbloom 	for (p = itab; p ; ) {
1321029Sbill 		term(p);
13323147Sbloom 		p1 = p->next;
13423147Sbloom 		free(p);
13523147Sbloom 		p = p1;
1361029Sbill 	}
13723147Sbloom 	itab = (struct tab *)0;
1381429Sbill 	signal(SIGALRM, shutreset);
13928801Skarels 	(void) kill(-1, SIGTERM);	/* one chance to catch it */
14028801Skarels 	sleep(5);
1411429Sbill 	alarm(30);
14213021Ssam 	for (i = 0; i < 5; i++)
1431029Sbill 		kill(-1, SIGKILL);
14413021Ssam 	while (wait((int *)0) != -1)
1451029Sbill 		;
1461029Sbill 	alarm(0);
1471429Sbill 	shutend();
1481429Sbill }
1491429Sbill 
1501429Sbill char shutfailm[] = "WARNING: Something is hung (wont die); ps axl advised\n";
1511429Sbill 
1521429Sbill shutreset()
1531429Sbill {
1541429Sbill 	int status;
1551429Sbill 
1561429Sbill 	if (fork() == 0) {
1571429Sbill 		int ct = open(ctty, 1);
1581429Sbill 		write(ct, shutfailm, sizeof (shutfailm));
1591429Sbill 		sleep(5);
1601429Sbill 		exit(1);
1611429Sbill 	}
1621429Sbill 	sleep(5);
1631429Sbill 	shutend();
1641429Sbill 	longjmp(shutpass, 1);
1651429Sbill }
1661429Sbill 
1671429Sbill shutend()
1681429Sbill {
1692821Swnj 	register i, f;
1701429Sbill 
1712821Swnj 	acct(0);
1721029Sbill 	signal(SIGALRM, SIG_DFL);
17313021Ssam 	for (i = 0; i < 10; i++)
1741029Sbill 		close(i);
17513021Ssam 	f = open(wtmpf, O_WRONLY|O_APPEND);
1762821Swnj 	if (f >= 0) {
1772821Swnj 		SCPYN(wtmp.ut_line, "~");
1782821Swnj 		SCPYN(wtmp.ut_name, "shutdown");
17912682Ssam 		SCPYN(wtmp.ut_host, "");
1802821Swnj 		time(&wtmp.ut_time);
1812821Swnj 		write(f, (char *)&wtmp, sizeof(wtmp));
1822821Swnj 		close(f);
1832821Swnj 	}
18413021Ssam 	return (1);
1851029Sbill }
1861029Sbill 
1871029Sbill single()
1881029Sbill {
1891029Sbill 	register pid;
1902821Swnj 	register xpid;
1912821Swnj 	extern	errno;
1921029Sbill 
19313021Ssam 	do {
19413021Ssam 		pid = fork();
19513021Ssam 		if (pid == 0) {
19613021Ssam 			signal(SIGTERM, SIG_DFL);
19713021Ssam 			signal(SIGHUP, SIG_DFL);
19813021Ssam 			signal(SIGALRM, SIG_DFL);
19916452Sroot 			signal(SIGTSTP, SIG_IGN);
20013021Ssam 			(void) open(ctty, O_RDWR);
20113021Ssam 			dup2(0, 1);
20213021Ssam 			dup2(0, 2);
20313021Ssam 			execl(shell, minus, (char *)0);
20413021Ssam 			exit(0);
20513021Ssam 		}
20613021Ssam 		while ((xpid = wait((int *)0)) != pid)
20713021Ssam 			if (xpid == -1 && errno == ECHILD)
20813021Ssam 				break;
20913021Ssam 	} while (xpid == -1);
2101029Sbill }
2111029Sbill 
2121403Sbill runcom(oldhowto)
2131403Sbill 	int oldhowto;
2141029Sbill {
2151029Sbill 	register pid, f;
2161403Sbill 	int status;
2171029Sbill 
2181029Sbill 	pid = fork();
21913021Ssam 	if (pid == 0) {
22013021Ssam 		(void) open("/", O_RDONLY);
22113021Ssam 		dup2(0, 1);
22213021Ssam 		dup2(0, 2);
2231403Sbill 		if (oldhowto & RB_SINGLE)
2241403Sbill 			execl(shell, shell, runc, (char *)0);
2251403Sbill 		else
2261403Sbill 			execl(shell, shell, runc, "autoboot", (char *)0);
2271403Sbill 		exit(1);
2281029Sbill 	}
22913021Ssam 	while (wait(&status) != pid)
2301029Sbill 		;
23113021Ssam 	if (status)
23213021Ssam 		return (0);
23313021Ssam 	f = open(wtmpf, O_WRONLY|O_APPEND);
2341029Sbill 	if (f >= 0) {
2351029Sbill 		SCPYN(wtmp.ut_line, "~");
2361029Sbill 		SCPYN(wtmp.ut_name, "reboot");
23712682Ssam 		SCPYN(wtmp.ut_host, "");
2382821Swnj 		if (time0) {
2392821Swnj 			wtmp.ut_time = time0;
2402821Swnj 			time0 = 0;
2412821Swnj 		} else
2422821Swnj 			time(&wtmp.ut_time);
2431029Sbill 		write(f, (char *)&wtmp, sizeof(wtmp));
2441029Sbill 		close(f);
2451029Sbill 	}
24613021Ssam 	return (1);
2471029Sbill }
2481029Sbill 
24918542Sralph struct	sigvec	mvec = { merge, sigmask(SIGTERM), 0 };
25013021Ssam /*
25113021Ssam  * Multi-user.  Listen for users leaving, SIGHUP's
25213021Ssam  * which indicate ttys has changed, and SIGTERM's which
25313021Ssam  * are used to shutdown the system.
25413021Ssam  */
2551029Sbill multiple()
2561029Sbill {
2571029Sbill 	register struct tab *p;
2581029Sbill 	register pid;
25923147Sbloom 	int omask;
2601029Sbill 
26113021Ssam 	sigvec(SIGHUP, &mvec, (struct sigvec *)0);
26213021Ssam 	for (EVER) {
2631029Sbill 		pid = wait((int *)0);
26413021Ssam 		if (pid == -1)
2651029Sbill 			return;
26623147Sbloom 		omask = sigblock(SIGHUP);
26718542Sralph 		for (ALL) {
26818542Sralph 			/* must restart window system BEFORE emulator */
26918542Sralph 			if (p->wpid == pid || p->wpid == -1)
27018542Sralph 				wstart(p);
27113021Ssam 			if (p->pid == pid || p->pid == -1) {
27218542Sralph 				/* disown the window system */
27318542Sralph 				if (p->wpid)
27418542Sralph 					kill(p->wpid, SIGHUP);
2751029Sbill 				rmut(p);
2761029Sbill 				dfork(p);
2771029Sbill 			}
27818542Sralph 		}
27923147Sbloom 		sigsetmask(omask);
2801029Sbill 	}
2811029Sbill }
2821029Sbill 
28313021Ssam /*
28413021Ssam  * Merge current contents of ttys file
28513021Ssam  * into in-core table of configured tty lines.
28613021Ssam  * Entered as signal handler for SIGHUP.
28713021Ssam  */
28813021Ssam #define	FOUND	1
28913021Ssam #define	CHANGE	2
29018542Sralph #define WCHANGE 4
29113021Ssam 
29213021Ssam merge()
29313021Ssam {
29413021Ssam 	register struct tab *p;
29516452Sroot 	register struct ttyent *t;
29623147Sbloom 	register struct tab *p1;
29713021Ssam 
29813021Ssam 	for (ALL)
29913021Ssam 		p->xflag = 0;
30016452Sroot 	setttyent();
30116452Sroot 	while (t = getttyent()) {
30216452Sroot 		if ((t->ty_status & TTY_ON) == 0)
30316452Sroot 			continue;
30413021Ssam 		for (ALL) {
30516452Sroot 			if (SCMPN(p->line, t->ty_name))
30613021Ssam 				continue;
30713021Ssam 			p->xflag |= FOUND;
30816452Sroot 			if (SCMPN(p->comn, t->ty_getty)) {
30913021Ssam 				p->xflag |= CHANGE;
31016452Sroot 				SCPYN(p->comn, t->ty_getty);
31113021Ssam 			}
31218542Sralph 			if (SCMPN(p->wcmd, t->ty_window)) {
31318542Sralph 				p->xflag |= WCHANGE|CHANGE;
31418542Sralph 				SCPYN(p->wcmd, t->ty_window);
31518542Sralph 			}
31613021Ssam 			goto contin1;
31713021Ssam 		}
31818542Sralph 
31923147Sbloom 		/*
32023147Sbloom 		 * Make space for a new one
32123147Sbloom 		 */
32223147Sbloom 		p1 = (struct tab *)calloc(1, sizeof(*p1));
32323147Sbloom 		if (!p1) {
32423147Sbloom 			syslog(LOG_ERR, "no space for '%s' !?!", t->ty_name);
32513021Ssam 			goto contin1;
32613021Ssam 		}
32723147Sbloom 		/*
32823147Sbloom 		 * Put new terminal at the end of the linked list.
32923147Sbloom 		 */
33023147Sbloom 		if (itab) {
33123147Sbloom 			for (p = itab; p->next ; p = p->next)
33223147Sbloom 				;
33323147Sbloom 			p->next = p1;
33423147Sbloom 		} else
33523147Sbloom 			itab = p1;
33623147Sbloom 
33723147Sbloom 		p = p1;
33823147Sbloom 		SCPYN(p->line, t->ty_name);
33923147Sbloom 		p->xflag |= FOUND|CHANGE;
34023147Sbloom 		SCPYN(p->comn, t->ty_getty);
34123147Sbloom 		if (strcmp(t->ty_window, "") != 0) {
34223147Sbloom 			p->xflag |= WCHANGE;
34323147Sbloom 			SCPYN(p->wcmd, t->ty_window);
34423147Sbloom 		}
34513021Ssam 	contin1:
34613021Ssam 		;
34713021Ssam 	}
34816452Sroot 	endttyent();
34923147Sbloom 	p1 = (struct tab *)0;
35013021Ssam 	for (ALL) {
35113021Ssam 		if ((p->xflag&FOUND) == 0) {
35213021Ssam 			term(p);
35318542Sralph 			wterm(p);
35423147Sbloom 			if (p1)
35523147Sbloom 				p1->next = p->next;
35623147Sbloom 			else
35723147Sbloom 				itab = p->next;
35823147Sbloom 			free(p);
35923147Sbloom 			p = p1 ? p1 : itab;
36023147Sbloom 		} else {
36123147Sbloom 			/* window system should be started first */
36223147Sbloom 			if (p->xflag&WCHANGE) {
36323147Sbloom 				wterm(p);
36423147Sbloom 				wstart(p);
36523147Sbloom 			}
36623147Sbloom 			if (p->xflag&CHANGE) {
36723147Sbloom 				term(p);
36823147Sbloom 				dfork(p);
36923147Sbloom 			}
37013021Ssam 		}
37123147Sbloom 		p1 = p;
37213021Ssam 	}
37313021Ssam }
37413021Ssam 
3751029Sbill term(p)
37613021Ssam 	register struct tab *p;
3771029Sbill {
3781029Sbill 
37913021Ssam 	if (p->pid != 0) {
3801029Sbill 		rmut(p);
3811029Sbill 		kill(p->pid, SIGKILL);
3821029Sbill 	}
3831029Sbill 	p->pid = 0;
38418542Sralph 	/* send SIGHUP to get rid of connections */
38518542Sralph 	if (p->wpid > 0)
38618542Sralph 		kill(p->wpid, SIGHUP);
3871029Sbill }
3881029Sbill 
3896816Ssam #include <sys/ioctl.h>
3906816Ssam 
3911029Sbill dfork(p)
39213021Ssam 	struct tab *p;
3931029Sbill {
3941029Sbill 	register pid;
3955971Sroot 	time_t t;
3965971Sroot 	int dowait = 0;
3971029Sbill 
3985971Sroot 	time(&t);
3995971Sroot 	p->gettycnt++;
4005971Sroot 	if ((t - p->gettytime) >= 60) {
4015971Sroot 		p->gettytime = t;
4025971Sroot 		p->gettycnt = 1;
40318542Sralph 	} else if (p->gettycnt >= 5) {
40418542Sralph 		dowait = 1;
40518542Sralph 		p->gettytime = t;
40618542Sralph 		p->gettycnt = 1;
4075971Sroot 	}
4081029Sbill 	pid = fork();
40913021Ssam 	if (pid == 0) {
4106816Ssam 		signal(SIGTERM, SIG_DFL);
4116816Ssam 		signal(SIGHUP, SIG_IGN);
41222181Skarels 		sigsetmask(0);	/* since can be called from masked code */
4135971Sroot 		if (dowait) {
41418542Sralph 			syslog(LOG_ERR, "'%s %s' failing, sleeping", p->comn, p->line);
41518542Sralph 			closelog();
4165971Sroot 			sleep(30);
4175971Sroot 		}
41818542Sralph 		execit(p->comn, p->line);
4191029Sbill 		exit(0);
4201029Sbill 	}
4211029Sbill 	p->pid = pid;
4221029Sbill }
4231029Sbill 
42413021Ssam /*
42513021Ssam  * Remove utmp entry.
42613021Ssam  */
4271029Sbill rmut(p)
42813021Ssam 	register struct tab *p;
4291029Sbill {
4301029Sbill 	register f;
4313608Swnj 	int found = 0;
43223147Sbloom 	static unsigned utmpsize;
43323147Sbloom 	static struct utmp *utmp;
43423147Sbloom 	register struct utmp *u;
43523147Sbloom 	int nutmp;
43623147Sbloom 	struct stat statbf;
4371029Sbill 
43823147Sbloom 	f = open(utmpf, O_RDWR);
43913021Ssam 	if (f >= 0) {
44023147Sbloom 		fstat(f, &statbf);
44123147Sbloom 		if (utmpsize < statbf.st_size) {
44223147Sbloom 			utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
44323147Sbloom 			if (utmp)
44423147Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
44523147Sbloom 			else
44623147Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
44723147Sbloom 			if (!utmp)
44823147Sbloom 				syslog(LOG_ERR, "utmp malloc failed");
4491029Sbill 		}
45023147Sbloom 		if (statbf.st_size && utmp) {
45123147Sbloom 			nutmp = read(f, utmp, statbf.st_size);
45223147Sbloom 			nutmp /= sizeof(struct utmp);
45323147Sbloom 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
45423147Sbloom 				if (SCMPN(u->ut_line, p->line) ||
45523147Sbloom 				    u->ut_name[0]==0)
45623147Sbloom 					continue;
45723147Sbloom 				lseek(f, ((long)u)-((long)utmp), L_SET);
45823147Sbloom 				SCPYN(u->ut_name, "");
45923147Sbloom 				SCPYN(u->ut_host, "");
46023147Sbloom 				time(&u->ut_time);
46123147Sbloom 				write(f, (char *)u, sizeof(*u));
46223147Sbloom 				found++;
46323147Sbloom 			}
46423147Sbloom 		}
4651029Sbill 		close(f);
4661029Sbill 	}
4673608Swnj 	if (found) {
46813021Ssam 		f = open(wtmpf, O_WRONLY|O_APPEND);
4693608Swnj 		if (f >= 0) {
4703608Swnj 			SCPYN(wtmp.ut_line, p->line);
4713608Swnj 			SCPYN(wtmp.ut_name, "");
47212682Ssam 			SCPYN(wtmp.ut_host, "");
4733608Swnj 			time(&wtmp.ut_time);
4743608Swnj 			write(f, (char *)&wtmp, sizeof(wtmp));
4753608Swnj 			close(f);
4763608Swnj 		}
47717582Ssam 		/*
47817582Ssam 		 * After a proper login force reset
47917582Ssam 		 * of error detection code in dfork.
48017582Ssam 		 */
48117582Ssam 		p->gettytime = 0;
48222181Skarels 		p->windtime = 0;
4831029Sbill 	}
4841029Sbill }
4851029Sbill 
4861029Sbill reset()
4871029Sbill {
48813021Ssam 
4891029Sbill 	longjmp(sjbuf, 1);
4901029Sbill }
4912821Swnj 
49213021Ssam jmp_buf	idlebuf;
49313021Ssam 
49413021Ssam idlehup()
49513021Ssam {
49613021Ssam 
49713021Ssam 	longjmp(idlebuf, 1);
49813021Ssam }
49913021Ssam 
5002821Swnj idle()
5012821Swnj {
5022821Swnj 	register struct tab *p;
5032821Swnj 	register pid;
5042821Swnj 
50513021Ssam 	signal(SIGHUP, idlehup);
50618542Sralph 	for (EVER) {
50713021Ssam 		if (setjmp(idlebuf))
50813021Ssam 			return;
5092821Swnj 		pid = wait((int *) 0);
51013021Ssam 		if (pid == -1) {
51113021Ssam 			sigpause(0);
51213021Ssam 			continue;
5132821Swnj 		}
51418542Sralph 		for (ALL) {
51518542Sralph 			/* if window system dies, mark it for restart */
51618542Sralph 			if (p->wpid == pid)
51718542Sralph 				p->wpid = -1;
51813021Ssam 			if (p->pid == pid) {
51913021Ssam 				rmut(p);
52013021Ssam 				p->pid = -1;
52113021Ssam 			}
52218542Sralph 		}
5232821Swnj 	}
5242821Swnj }
52518542Sralph 
52618542Sralph wterm(p)
52718542Sralph 	register struct tab *p;
52818542Sralph {
52918542Sralph 	if (p->wpid != 0) {
53018542Sralph 		kill(p->wpid, SIGKILL);
53118542Sralph 	}
53218542Sralph 	p->wpid = 0;
53318542Sralph }
53418542Sralph 
53518542Sralph wstart(p)
53618542Sralph 	register struct tab *p;
53718542Sralph {
53822181Skarels 	register pid;
53922181Skarels 	time_t t;
54022181Skarels 	int dowait = 0;
54118542Sralph 
54222181Skarels 	time(&t);
54322181Skarels 	p->windcnt++;
54422181Skarels 	if ((t - p->windtime) >= 60) {
54522181Skarels 		p->windtime = t;
54622181Skarels 		p->windcnt = 1;
54722181Skarels 	} else if (p->windcnt >= 5) {
54822181Skarels 		dowait = 1;
54922181Skarels 		p->windtime = t;
55022181Skarels 		p->windcnt = 1;
55122181Skarels 	}
55222181Skarels 
55322181Skarels 	pid = fork();
55422181Skarels 
55522181Skarels 	if (pid == 0) {
55618542Sralph 		signal(SIGTERM, SIG_DFL);
55722181Skarels 		signal(SIGHUP,  SIG_IGN);
55822181Skarels 		sigsetmask(0);	/* since can be called from masked code */
55922181Skarels 		if (dowait) {
56022181Skarels 			syslog(LOG_ERR, "'%s %s' failing, sleeping", p->wcmd, p->line);
56122181Skarels 			closelog();
56222181Skarels 			sleep(30);
56322181Skarels 		}
56418542Sralph 		execit(p->wcmd, p->line);
56518542Sralph 		exit(0);
56618542Sralph 	}
56722181Skarels 	p->wpid = pid;
56818542Sralph }
56918542Sralph 
57022181Skarels #define NARGS	20	/* must be at least 4 */
57118542Sralph #define ARGLEN	512	/* total size for all the argument strings */
57218542Sralph 
57318542Sralph execit(s, arg)
57418542Sralph 	char *s;
57518542Sralph 	char *arg;	/* last argument on line */
57618542Sralph {
57718542Sralph 	char *argv[NARGS], args[ARGLEN], *envp[1];
57818542Sralph 	register char *sp = s;
57918542Sralph 	register char *ap = args;
58018542Sralph 	register char c;
58118542Sralph 	register int i;
58218542Sralph 
58318542Sralph 	/*
58418542Sralph 	 * First we have to set up the argument vector.
58518542Sralph 	 * "prog arg1 arg2" maps to exec("prog", "-", "arg1", "arg2").
58618542Sralph 	 */
58718542Sralph 	for (i = 1; i < NARGS - 2; i++) {
58818542Sralph 		argv[i] = ap;
58918542Sralph 		for (EVER) {
59018542Sralph 			if ((c = *sp++) == '\0' || ap >= &args[ARGLEN-1]) {
59118542Sralph 				*ap = '\0';
59218542Sralph 				goto done;
59318542Sralph 			}
59418542Sralph 			if (c == ' ') {
59518542Sralph 				*ap++ = '\0';
59618542Sralph 				while (*sp == ' ')
59718542Sralph 					sp++;
59818542Sralph 				if (*sp == '\0')
59918542Sralph 					goto done;
60018542Sralph 				break;
60118542Sralph 			}
60218542Sralph 			*ap++ = c;
60318542Sralph 		}
60418542Sralph 	}
60518542Sralph done:
60618542Sralph 	argv[0] = argv[1];
60718542Sralph 	argv[1] = "-";
60818542Sralph 	argv[i+1] = arg;
60918542Sralph 	argv[i+2] = 0;
61018542Sralph 	envp[0] = 0;
61118542Sralph 	execve(argv[0], &argv[1], envp);
61218542Sralph 	/* report failure of exec */
61318542Sralph 	syslog(LOG_ERR, "%s: %m", argv[0]);
61418542Sralph 	closelog();
61518542Sralph 	sleep(10);	/* prevent failures from eating machine */
61618542Sralph }
617