xref: /onnv-gate/usr/src/lib/libnsl/saf/doconfig.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21132Srobinson 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*6812Sraf 
321219Sraf #include "mt.h"
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <ulimit.h>
390Sstevel@tonic-gate #include <wait.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <stropts.h>
430Sstevel@tonic-gate #include <ctype.h>
440Sstevel@tonic-gate #include <sys/conf.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include "sac.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	COMMENT	'#'
500Sstevel@tonic-gate #define	NOWAIT	0
510Sstevel@tonic-gate #define	WAIT	1
520Sstevel@tonic-gate 
53132Srobinson static char	*eatwhite(char *);
54132Srobinson static int	doassign(char *);
55132Srobinson static int	dopush(int, char *);
56132Srobinson static int	dopop(int, char *);
57132Srobinson static int	dorun(char *, int);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * doconfig - the configuration script interpreter, if all is ok,
610Sstevel@tonic-gate  *	      return 0.  If there is a "system" error, return -1.
620Sstevel@tonic-gate  *	      If there is an error performing a command, or there
630Sstevel@tonic-gate  *	      is a syntax error, return the line number in error.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  *	args:	fd - file descriptor to push and pop from
660Sstevel@tonic-gate  *		script - name of the configuration script
670Sstevel@tonic-gate  *		rflag - restriction flag to determine what "commands"
680Sstevel@tonic-gate  *			can be run
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate int
doconfig(int fd,char * script,long rflag)720Sstevel@tonic-gate doconfig(int fd, char *script, long rflag)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int line;		/* line counter */
750Sstevel@tonic-gate 	struct stat statbuf;	/* place for stat */
760Sstevel@tonic-gate 	FILE *fp;		/* file pointer for config script */
770Sstevel@tonic-gate 	char buf[BUFSIZ + 1];	/* scratch buffer */
780Sstevel@tonic-gate 	char *bp;		/* scratch pointer */
790Sstevel@tonic-gate 	char *p;		/* scratch pointer */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	/* if the script does not exist, then there is nothing to do */
821219Sraf 	if (stat(script, &statbuf) < 0)
830Sstevel@tonic-gate 		return (0);
840Sstevel@tonic-gate 
851914Scasper 	fp = fopen(script, "rF");
86132Srobinson 	if (fp == NULL)
870Sstevel@tonic-gate 		return (-1);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	line = 0;
900Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp)) {
910Sstevel@tonic-gate 		line++;
920Sstevel@tonic-gate 		p = strchr(buf, '\n');
930Sstevel@tonic-gate 		/* if no \n, then line is too long */
940Sstevel@tonic-gate 		if (p == NULL) {
950Sstevel@tonic-gate 			(void) fclose(fp);
960Sstevel@tonic-gate 			return (line);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		*p = '\0';
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		/* remove comments */
1010Sstevel@tonic-gate 		p = strchr(buf, COMMENT);
1020Sstevel@tonic-gate 		if (p)
1030Sstevel@tonic-gate 			*p = '\0';
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		/* remove leading whitespace */
1060Sstevel@tonic-gate 		bp = eatwhite(buf);
1070Sstevel@tonic-gate 		/* see if anything is left */
1080Sstevel@tonic-gate 		if (*bp == '\0')
1090Sstevel@tonic-gate 			continue;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		/* remove trailing whitespace */
1120Sstevel@tonic-gate 		p = &buf[strlen(buf) - 1];
1130Sstevel@tonic-gate 		while (*p && isspace(*p))
1140Sstevel@tonic-gate 			*p-- = '\0';
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		/* get the command */
1170Sstevel@tonic-gate 		p = bp;
1180Sstevel@tonic-gate 		while (*p && !isspace(*p))
1190Sstevel@tonic-gate 			p++;
1200Sstevel@tonic-gate 		if (*p)
1210Sstevel@tonic-gate 			*p++ = '\0';
1220Sstevel@tonic-gate 		/* skip any whitespace here too (between command and args) */
1230Sstevel@tonic-gate 		p = eatwhite(p);
1240Sstevel@tonic-gate 
125132Srobinson 		if (strcmp(bp, "assign") == 0) {
1260Sstevel@tonic-gate 			if ((rflag & NOASSIGN) || doassign(p)) {
1270Sstevel@tonic-gate 				(void) fclose(fp);
1280Sstevel@tonic-gate 				return (line);
1290Sstevel@tonic-gate 			}
130132Srobinson 		} else if (strcmp(bp, "push") == 0) {
1310Sstevel@tonic-gate 			if (dopush(fd, p)) {
1320Sstevel@tonic-gate 				(void) fclose(fp);
1330Sstevel@tonic-gate 				return (line);
1340Sstevel@tonic-gate 			}
135132Srobinson 		} else if (strcmp(bp, "pop") == 0) {
1360Sstevel@tonic-gate 			if (dopop(fd, p)) {
1370Sstevel@tonic-gate 				(void) fclose(fp);
1380Sstevel@tonic-gate 				return (line);
1390Sstevel@tonic-gate 			}
140132Srobinson 		} else if (strcmp(bp, "run") == 0) {
1410Sstevel@tonic-gate 			if ((rflag & NORUN) || dorun(p, NOWAIT)) {
1420Sstevel@tonic-gate 				(void) fclose(fp);
1430Sstevel@tonic-gate 				return (line);
1440Sstevel@tonic-gate 			}
145132Srobinson 		} else if (strcmp(bp, "runwait") == 0) {
1460Sstevel@tonic-gate 			if ((rflag & NORUN) || dorun(p, WAIT)) {
1470Sstevel@tonic-gate 				(void) fclose(fp);
1480Sstevel@tonic-gate 				return (line);
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 		} else {
1510Sstevel@tonic-gate 			/* unknown command */
1520Sstevel@tonic-gate 			(void) fclose(fp);
1530Sstevel@tonic-gate 			return (line);
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	if (!feof(fp)) {
1570Sstevel@tonic-gate 		(void) fclose(fp);
1580Sstevel@tonic-gate 		return (-1);
1590Sstevel@tonic-gate 	}
160132Srobinson 	(void) fclose(fp);
161132Srobinson 	return (0);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * doassign - handle an `assign' command
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *	args:	p - assignment string
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate static int
doassign(char * p)1730Sstevel@tonic-gate doassign(char *p)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	char *var;		/* environment variable to be assigned */
1760Sstevel@tonic-gate 	char val[BUFSIZ];	/* and the value to be assigned to it */
1770Sstevel@tonic-gate 	char scratch[BUFSIZ];	/* scratch buffer */
1780Sstevel@tonic-gate 	char delim;		/* delimiter char seen (for quoted strings ) */
1790Sstevel@tonic-gate 	char *tp;		/* scratch pointer */
1800Sstevel@tonic-gate 
181132Srobinson 	if (*p == '\0')
1820Sstevel@tonic-gate 		return (-1);
1830Sstevel@tonic-gate 	var = p;
1840Sstevel@tonic-gate 	/* skip first token, but stop if we see a '=' */
1850Sstevel@tonic-gate 	while (*p && !isspace(*p) && (*p != '='))
1860Sstevel@tonic-gate 		p++;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* if we found end of string, it's an error */
189132Srobinson 	if (*p == '\0')
1900Sstevel@tonic-gate 		return (-1);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/* if we found a space, look for the '=', otherwise it's an error */
1930Sstevel@tonic-gate 	if (isspace(*p)) {
1940Sstevel@tonic-gate 		*p++ = '\0';
1950Sstevel@tonic-gate 		while (*p && isspace(*p))
1960Sstevel@tonic-gate 			p++;
197132Srobinson 		if (*p == '\0')
1980Sstevel@tonic-gate 			return (-1);
1990Sstevel@tonic-gate 		if (*p == '=')
2000Sstevel@tonic-gate 			p++;
201132Srobinson 		else
2020Sstevel@tonic-gate 			return (-1);
2030Sstevel@tonic-gate 	} else {
2040Sstevel@tonic-gate 		/* skip over '=' */
2050Sstevel@tonic-gate 		*p = '\0';
2060Sstevel@tonic-gate 		p++;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/* skip over any whitespace */
2100Sstevel@tonic-gate 	p = eatwhite(p);
2110Sstevel@tonic-gate 	if (*p == '\'' || *p == '"') {
2120Sstevel@tonic-gate 		/* handle quoted values */
2130Sstevel@tonic-gate 		delim = *p++;
2140Sstevel@tonic-gate 		tp = val;
2150Sstevel@tonic-gate 		for (;;) {
2160Sstevel@tonic-gate 			if (*p == '\0') {
2170Sstevel@tonic-gate 				return (-1);
2180Sstevel@tonic-gate 			} else if (*p == delim) {
2190Sstevel@tonic-gate 				if (*(p - 1) != '\\')
2200Sstevel@tonic-gate 					break;
2210Sstevel@tonic-gate 				else
2220Sstevel@tonic-gate 					*(tp - 1) = *p++;
2230Sstevel@tonic-gate 			} else
2240Sstevel@tonic-gate 				*tp++ = *p++;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		*tp = '\0';
227132Srobinson 		/*
228132Srobinson 		 * these assignments make the comment below true
229132Srobinson 		 * (values of tp and p
230132Srobinson 		 */
2310Sstevel@tonic-gate 		tp = ++p;
2320Sstevel@tonic-gate 		p = val;
2330Sstevel@tonic-gate 	} else {
2340Sstevel@tonic-gate 		tp = p;
2350Sstevel@tonic-gate 		/* look for end of token */
2360Sstevel@tonic-gate 		while (*tp && !isspace(*tp))
2370Sstevel@tonic-gate 			tp++;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * at this point, p points to the value, and tp points to the
2420Sstevel@tonic-gate  * end of the token.  check to make sure there is no garbage on
2430Sstevel@tonic-gate  * the end of the line
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate 
246132Srobinson 	if (*tp)
2470Sstevel@tonic-gate 		return (-1);
248132Srobinson 	(void) snprintf(scratch, sizeof (scratch), "%s=%s", var, p);
2490Sstevel@tonic-gate 	/* note: need to malloc fresh space so putenv works */
2500Sstevel@tonic-gate 	tp = malloc(strlen(scratch) + 1);
251132Srobinson 	if (tp == NULL)
2520Sstevel@tonic-gate 		return (-1);
253132Srobinson 	(void) strcpy(tp, scratch);
254132Srobinson 	if (putenv(tp))
2550Sstevel@tonic-gate 		return (-1);
256132Srobinson 	return (0);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate  * dopush - handle a `push' command
2620Sstevel@tonic-gate  *
2630Sstevel@tonic-gate  *	args:	fd - file descriptor to push on
2640Sstevel@tonic-gate  *		p - list of modules to push
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate static int
dopush(int fd,char * p)2690Sstevel@tonic-gate dopush(int fd, char *p)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	char *tp;	/* scratch pointer */
2720Sstevel@tonic-gate 	int i;		/* scratch variable */
2730Sstevel@tonic-gate 	int npush;	/* count # of modules pushed */
2740Sstevel@tonic-gate 
275132Srobinson 	if (*p == '\0')
2760Sstevel@tonic-gate 		return (-1);
2770Sstevel@tonic-gate 	npush = 0;
2780Sstevel@tonic-gate 	for (;;) {
279132Srobinson 		if (*p == '\0')		/* found end of line */
2800Sstevel@tonic-gate 			return (0);
2810Sstevel@tonic-gate 		p = eatwhite(p);
282132Srobinson 		if (*p == '\0')
2830Sstevel@tonic-gate 			return (-1);
2840Sstevel@tonic-gate 		tp = p;
2850Sstevel@tonic-gate 		while (*tp && !isspace(*tp) && (*tp != ','))
2860Sstevel@tonic-gate 			tp++;
2870Sstevel@tonic-gate 		if (*tp)
2880Sstevel@tonic-gate 			*tp++ = '\0';
2890Sstevel@tonic-gate 		if (ioctl(fd, I_PUSH, p) < 0) {
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * try to pop all that we've done, if pop fails it doesn't matter because
2930Sstevel@tonic-gate  * nothing can be done anyhow
2940Sstevel@tonic-gate  */
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 			for (i = 0; i < npush; ++i)
297132Srobinson 				(void) ioctl(fd, I_POP, 0);
2980Sstevel@tonic-gate 			return (-1);
2990Sstevel@tonic-gate 		}
300132Srobinson 		/* count the number of modules we've pushed */
301132Srobinson 		npush++;
302132Srobinson 		p = tp;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * dopop - handle a `pop' command
3090Sstevel@tonic-gate  *
3100Sstevel@tonic-gate  *	args:	fd - file descriptor to pop from
3110Sstevel@tonic-gate  *		p - name of module to pop to or ALL (null means pop top only)
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
dopop(int fd,char * p)3160Sstevel@tonic-gate dopop(int fd, char *p)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	char *modp;		/* module name from argument to pop */
3190Sstevel@tonic-gate 	char buf[FMNAMESZ + 1];	/* scratch buffer */
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (*p == '\0') {
3220Sstevel@tonic-gate 		/* just a pop with no args */
323132Srobinson 		if (ioctl(fd, I_POP, 0) < 0)
3240Sstevel@tonic-gate 			return (-1);
325132Srobinson 		return (0);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/* skip any whitespace in between */
3290Sstevel@tonic-gate 	p = eatwhite(p);
3300Sstevel@tonic-gate 	modp = p;
3310Sstevel@tonic-gate 	/* find end of module name */
3320Sstevel@tonic-gate 	while (*p && !isspace(*p))
3330Sstevel@tonic-gate 		p++;
3340Sstevel@tonic-gate 
335132Srobinson 	if (*p)		/* if not end of line, extra junk on line */
3360Sstevel@tonic-gate 		return (-1);
337132Srobinson 	if (strcmp(modp, "ALL") == 0) {
3380Sstevel@tonic-gate 		/* it's the magic name, pop them all */
3390Sstevel@tonic-gate 		while (ioctl(fd, I_POP, 0) == 0)
3400Sstevel@tonic-gate 			;
3410Sstevel@tonic-gate 		/* After all popped, we'll get an EINVAL, which is expected */
342132Srobinson 		if (errno != EINVAL)
3430Sstevel@tonic-gate 			return (-1);
344132Srobinson 		return (0);
345132Srobinson 	}
346132Srobinson 	/* check to see if the named module is on the stream */
347132Srobinson 	if (ioctl(fd, I_FIND, modp) != 1)
348132Srobinson 		return (-1);
3490Sstevel@tonic-gate 
350132Srobinson 	/* pop them until the right one is on top */
351132Srobinson 	for (;;) {
352132Srobinson 		if (ioctl(fd, I_LOOK, buf) < 0)
353132Srobinson 			return (-1);
354132Srobinson 		if (strcmp(modp, buf) == 0)
355132Srobinson 			/* we're done */
356132Srobinson 			return (0);
357132Srobinson 		if (ioctl(fd, I_POP, 0) < 0)
358132Srobinson 			return (-1);
3590Sstevel@tonic-gate 	}
360132Srobinson 	/* NOTREACHED */
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * dorun - handle a `run' command
3660Sstevel@tonic-gate  *
3670Sstevel@tonic-gate  *	args:	p - command line to run
3680Sstevel@tonic-gate  *		waitflag - flag indicating whether a wait should be done
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static int
dorun(char * p,int waitflg)3730Sstevel@tonic-gate dorun(char *p, int waitflg)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	char *tp;		/* scratch pointer */
3760Sstevel@tonic-gate 	char *ep;		/* scratch pointer (end of token) */
3770Sstevel@tonic-gate 	char savech;		/* hold area */
3780Sstevel@tonic-gate 	int status;		/* return status from wait */
3790Sstevel@tonic-gate 	pid_t pid;		/* pid of child proc */
3800Sstevel@tonic-gate 	pid_t rpid;		/* returned pid from wait */
3810Sstevel@tonic-gate 	void (*func)();		/* return from signal */
3820Sstevel@tonic-gate 
383132Srobinson 	if (*p == '\0')
3840Sstevel@tonic-gate 		return (-1);
3850Sstevel@tonic-gate 
386132Srobinson 	/*
387132Srobinson 	 * get first token
388132Srobinson 	 */
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	for (tp = p; *tp && !isspace(*tp); ++tp)
3910Sstevel@tonic-gate 		;
3920Sstevel@tonic-gate 	savech = '\0';
3930Sstevel@tonic-gate 	if (*tp) {
3940Sstevel@tonic-gate 		savech = *tp;
3950Sstevel@tonic-gate 		*tp = '\0';
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 
398132Srobinson 	/*
399132Srobinson 	 * look for built-in's
400132Srobinson 	 */
4010Sstevel@tonic-gate 
402132Srobinson 	if (strcmp(p, "cd") == 0) {
4030Sstevel@tonic-gate 		*tp = savech;
4040Sstevel@tonic-gate 		tp = eatwhite(tp);
4050Sstevel@tonic-gate 		if (*tp == '\0')
4060Sstevel@tonic-gate 			/* if nothing there, try to cd to $HOME */
4070Sstevel@tonic-gate 			tp = getenv("HOME");
408132Srobinson 		if (chdir(tp) < 0)
4090Sstevel@tonic-gate 			return (-1);
410132Srobinson 	} else if (strcmp(p, "ulimit") == 0) {
4110Sstevel@tonic-gate 		*tp = savech;
4120Sstevel@tonic-gate 		tp = eatwhite(tp);
4130Sstevel@tonic-gate 		/* must have an argument */
414132Srobinson 		if (*tp == '\0')
4150Sstevel@tonic-gate 			return (-1);
4160Sstevel@tonic-gate 		/* make sure nothing appears on line after arg */
4170Sstevel@tonic-gate 		for (ep = tp; *ep && !isspace(*ep); ++ep)
4180Sstevel@tonic-gate 			;
4190Sstevel@tonic-gate 		ep = eatwhite(ep);
420132Srobinson 		if (*ep)
4210Sstevel@tonic-gate 			return (-1);
422132Srobinson 		if (!isdigit(*tp))
4230Sstevel@tonic-gate 			return (-1);
4240Sstevel@tonic-gate 
425132Srobinson 		if (ulimit(2, atoi(tp)) < 0)
4260Sstevel@tonic-gate 			return (-1);
427132Srobinson 	} else if (strcmp(p, "umask") == 0) {
4280Sstevel@tonic-gate 		*tp = savech;
4290Sstevel@tonic-gate 		tp = eatwhite(tp);
4300Sstevel@tonic-gate 		/* must have an argument */
431132Srobinson 		if (*tp == '\0')
4320Sstevel@tonic-gate 			return (-1);
4330Sstevel@tonic-gate 		/* make sure nothing appears on line after arg */
4340Sstevel@tonic-gate 		for (ep = tp; *ep && !isspace(*ep); ++ep)
4350Sstevel@tonic-gate 			;
4360Sstevel@tonic-gate 		ep = eatwhite(ep);
437132Srobinson 		if (*ep)
4380Sstevel@tonic-gate 			return (-1);
439132Srobinson 		if (!isdigit(*tp))
4400Sstevel@tonic-gate 			return (-1);
4410Sstevel@tonic-gate 		(void) umask(strtol(tp, NULL, 8));
4420Sstevel@tonic-gate 	} else {
4430Sstevel@tonic-gate 		/* not a built-in */
4440Sstevel@tonic-gate 		*tp = savech;
4450Sstevel@tonic-gate 		func = signal(SIGCLD, SIG_DFL);
4460Sstevel@tonic-gate 		if ((pid = fork()) < 0) {
447132Srobinson 			(void) signal(SIGCLD, func);
4480Sstevel@tonic-gate 			return (-1);
449132Srobinson 		}
450132Srobinson 		if (pid) {
4510Sstevel@tonic-gate 			if (waitflg == WAIT) {
4520Sstevel@tonic-gate 				status = 0;
4530Sstevel@tonic-gate 				rpid = -1;
4540Sstevel@tonic-gate 				while (rpid != pid)
4550Sstevel@tonic-gate 					rpid = wait(&status);
4560Sstevel@tonic-gate 				if (status) {
4570Sstevel@tonic-gate 					/* child failed */
458132Srobinson 					(void) signal(SIGCLD, func);
4590Sstevel@tonic-gate 					return (-1);
4600Sstevel@tonic-gate 				}
4610Sstevel@tonic-gate 			}
462132Srobinson 			(void) signal(SIGCLD, func);
4630Sstevel@tonic-gate 		} else {
4640Sstevel@tonic-gate 			/* set IFS for security */
4650Sstevel@tonic-gate 			(void) putenv("IFS=\" \"");
4660Sstevel@tonic-gate 			/*
4670Sstevel@tonic-gate 			 * need to close all files to prevent unauthorized
4680Sstevel@tonic-gate 			 * access in the children.  Setup stdin, stdout,
4690Sstevel@tonic-gate 			 * and stderr to /dev/null.
4700Sstevel@tonic-gate 			 */
4710Sstevel@tonic-gate 			closefrom(0);
4720Sstevel@tonic-gate 			/* stdin */
473132Srobinson 			if (open("/dev/null", O_RDWR) != 0)
4740Sstevel@tonic-gate 				return (-1);
4750Sstevel@tonic-gate 			/* stdout */
476132Srobinson 			if (dup(0) != 1)
4770Sstevel@tonic-gate 				return (-1);
4780Sstevel@tonic-gate 			/* stderr */
479132Srobinson 			if (dup(0) != 2)
4800Sstevel@tonic-gate 				return (-1);
481*6812Sraf 			(void) execl("/usr/bin/sh", "sh", "-c", p, NULL);
482132Srobinson 			/*
483132Srobinson 			 * if we get here, there is a problem - remember that
484132Srobinson 			 * this is the child
485132Srobinson 			 */
4860Sstevel@tonic-gate 			exit(1);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 	return (0);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * eatwhite - swallow any leading whitespace, return pointer to first
4950Sstevel@tonic-gate  *	      non-white space character or to terminating null character
4960Sstevel@tonic-gate  *	      if nothing else is there
4970Sstevel@tonic-gate  *
4980Sstevel@tonic-gate  *	args:	p - string to parse
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static char *
eatwhite(char * p)5020Sstevel@tonic-gate eatwhite(char *p)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	while (*p && isspace(*p))
5050Sstevel@tonic-gate 		p++;
5060Sstevel@tonic-gate 	return (p);
5070Sstevel@tonic-gate }
508