xref: /onnv-gate/usr/src/cmd/devmgmt/cmds/devreserv.c (revision 644:bca1986a1faa)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*644Sakaplan  * Copyright 2005 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 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include	<sys/types.h>
340Sstevel@tonic-gate #include	<sys/param.h>
350Sstevel@tonic-gate #include	<stdio.h>
360Sstevel@tonic-gate #include	<errno.h>
370Sstevel@tonic-gate #include	<stdlib.h>
380Sstevel@tonic-gate #include	<string.h>
390Sstevel@tonic-gate #include	<fmtmsg.h>
400Sstevel@tonic-gate #include	<devmgmt.h>
410Sstevel@tonic-gate #include	<devtab.h>
420Sstevel@tonic-gate #include	<values.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  *  Local definitions
470Sstevel@tonic-gate  *	TRUE		Boolean TRUE value
480Sstevel@tonic-gate  *	FALSE		Boolean FALSE value
490Sstevel@tonic-gate  *	TOKDELIMS	Char string of delimiters for lists
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #ifndef		TRUE
530Sstevel@tonic-gate #define		TRUE		('t')
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #ifndef		FALSE
570Sstevel@tonic-gate #define		FALSE		0
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define		TOKDELIMS	", \t\n"
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  *  Exit codes:
650Sstevel@tonic-gate  *	EX_OK		Exit code for all went well
660Sstevel@tonic-gate  *	EX_ERROR	Exit code for something failed
670Sstevel@tonic-gate  *	EX_TABLES	A table couldn't be accessed
680Sstevel@tonic-gate  *	EX_NOALLOC	Exit code for allocation failed
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #define		EX_OK		0
720Sstevel@tonic-gate #define		EX_ERROR	1
730Sstevel@tonic-gate #define		EX_TABLES	2
740Sstevel@tonic-gate #define		EX_NOALLOC	3
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  *  Messages:
780Sstevel@tonic-gate  *	M_USAGE		Usage error
790Sstevel@tonic-gate  *	M_INVKEY	Invalid key specified
800Sstevel@tonic-gate  *	M_ERROR		Some strange error
810Sstevel@tonic-gate  *	M_UNABLE	A list of devices is unavailable
820Sstevel@tonic-gate  *	M_DEVTAB	Can't access device table (for reading)
830Sstevel@tonic-gate  *	M_RSVTAB	Can't access device reservation table (for r/w)
840Sstevel@tonic-gate  *	M_NODEV		A list of devices is invalid
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #define	M_USAGE		"usage: devreserv [key [devicelist [...]]]"
880Sstevel@tonic-gate #define	M_INVKEY	"Invalid key: %s"
890Sstevel@tonic-gate #define	M_ERROR		"Internal error, errno=%d"
900Sstevel@tonic-gate #define	M_UNABLE	"Cannot reserve devices"
910Sstevel@tonic-gate #define	M_DEVTAB	"Cannot open the device table: %s"
920Sstevel@tonic-gate #define	M_RSVTAB	"Cannot open the device-reservation table: %s"
930Sstevel@tonic-gate #define	M_NODEV		M_UNABLE
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  *  Local functions and static data
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  *	buildreqlist()	Builds the list of requested devices for devreserv()
1000Sstevel@tonic-gate  *	freereqlist()	Free space allocated to the list of requested devices
1010Sstevel@tonic-gate  *	ndevsin()	Get number of elements in a list
1020Sstevel@tonic-gate  *	stdmsg(r,l,s,m)	Standard message generation
1030Sstevel@tonic-gate  *			r	Recoverability flag
1040Sstevel@tonic-gate  *			l	Label
1050Sstevel@tonic-gate  *			s	Severity
1060Sstevel@tonic-gate  *			m	Message
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  *	lbl		Buffer for the label-component of a message
1090Sstevel@tonic-gate  *	txt		Buffer for the text-component of a message
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static char  ***buildreqlist();
1130Sstevel@tonic-gate static void	freereqlist();
1140Sstevel@tonic-gate static int	ndevsin();
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #define	stdmsg(r,l,s,m)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,m,MM_NULLACT,MM_NULLTAG)
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static	char	lbl[MM_MXLABELLN+1];
1190Sstevel@tonic-gate static	char	txt[MM_MXTXTLN+1];
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  *  devreserv [key [devlist [devlist [...]]]]
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  *	This command reserves sets of devices known to the OA&M device
1250Sstevel@tonic-gate  *	management system.  It reserves a device from each of the device
1260Sstevel@tonic-gate  *	lists presented to it, reserving them on the key (<key>).  If no
1270Sstevel@tonic-gate  *	device-lists are provided, the command lists those devices reserved
1280Sstevel@tonic-gate  *	on the given key (<key>).  If no key (<key>) is provided, the
1290Sstevel@tonic-gate  *	command lists all devices currently reserved.
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  *  Options:  None
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  *  Arguments:
1340Sstevel@tonic-gate  *	key		Key to lock the devices on
1350Sstevel@tonic-gate  *	devlist		A comma-, space-, or tab-list containing devices
1360Sstevel@tonic-gate  *			(pathnames or aliases).  For typical shells, space-
1370Sstevel@tonic-gate  *			and tab-lists should be quoted or the separator should
1380Sstevel@tonic-gate  *			be somehow escaped.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  *  Command Values:
1410Sstevel@tonic-gate  *	EX_OK		0	Device(s) successfully allocated
1420Sstevel@tonic-gate  *	EX_ERROR	1	A syntax or other error occurred
1430Sstevel@tonic-gate  *	EX_TABLES	2	Either the device-table or the device-
1440Sstevel@tonic-gate  *				reservation table couldn't be opened as needed
1450Sstevel@tonic-gate  *	EX_NOALLOC	3	The device-reservation request couldn't be
1460Sstevel@tonic-gate  *				fulfilled.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate 
149*644Sakaplan int
main(int argc,char * argv[])150*644Sakaplan main(int argc, char *argv[])
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Automatics */
1540Sstevel@tonic-gate 	char		     ***reqlist;	/* * to list of lists */
1550Sstevel@tonic-gate 	char		      **argp;		/* Ptr to current argument */
1560Sstevel@tonic-gate 	char		      **alloclist;	/* List of allocated devices */
1570Sstevel@tonic-gate 	char		      **pp;		/* Temp ptr to char ptrs */
1580Sstevel@tonic-gate 	struct reservdev      **rsvd;		/* Ptr to list of rsvd devs */
1590Sstevel@tonic-gate 	struct reservdev      **plk;		/* Running ptr to locks */
1600Sstevel@tonic-gate 	char		       *p;		/* Temp char ptr */
1610Sstevel@tonic-gate 	char		       *devtab;		/* Device table pathname */
1620Sstevel@tonic-gate 	char		       *rsvtab;		/* Dev-rsv tbl pathname */
1630Sstevel@tonic-gate 	int			argcount;	/* Number of args on cmd */
1640Sstevel@tonic-gate 	long			lkey;		/* Key for locking (long) */
1650Sstevel@tonic-gate 	int			key;		/* Key for locking */
1660Sstevel@tonic-gate 	int			exitcode;	/* Value to return */
1670Sstevel@tonic-gate 	int			sev;		/* Message severity */
1680Sstevel@tonic-gate 	int			syntaxerr;	/* Flag, TRUE if syntax error */
1690Sstevel@tonic-gate 	int			c;		/* Option character */
1700Sstevel@tonic-gate 	int			i;		/* Temp counter */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * Initializations
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* Build a message label */
1780Sstevel@tonic-gate 	if (p = strrchr(argv[0], '/')) p++;
1790Sstevel@tonic-gate 	else p = argv[0];
1800Sstevel@tonic-gate 	(void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl));
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/*
1840Sstevel@tonic-gate 	 * Allow only the text component of messages to be written
1850Sstevel@tonic-gate 	 * (this will probably go away in SVR4.1)
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Parse the options from the command line
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	opterr = 0;
1960Sstevel@tonic-gate 	syntaxerr = FALSE;
1970Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "")) != EOF) switch(c) {
1980Sstevel@tonic-gate 	default:
1990Sstevel@tonic-gate 	    syntaxerr = FALSE;
2000Sstevel@tonic-gate 	    break;
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* If there's (an obvious) syntax error, write a message and quit */
2040Sstevel@tonic-gate 	if (syntaxerr) {
2050Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
2060Sstevel@tonic-gate 	    exit(EX_ERROR);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/* Argument initializations */
2100Sstevel@tonic-gate 	argcount = argc - optind;
2110Sstevel@tonic-gate 	argp = &argv[optind];
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 *  devreserv
2160Sstevel@tonic-gate 	 *
2170Sstevel@tonic-gate 	 *  	If euid == 0, write a list of all currently allocated devices.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (argcount == 0) {
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	    /* Get the list of reserved devices */
2230Sstevel@tonic-gate 	    if (rsvd = reservdev()) {
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		/* Write the list of reserved devices with the key
2260Sstevel@tonic-gate 		 * that the device was locked on.  The key should go
2270Sstevel@tonic-gate 		 * in column 16, but separate it from the alias with at
2280Sstevel@tonic-gate 		 * least one space */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		exitcode = EX_OK;
2310Sstevel@tonic-gate 		for (plk = rsvd ; *plk ; plk++) {
2320Sstevel@tonic-gate 		    if ((i = fputs((*plk)->devname, stdout)) >= 0) do
2330Sstevel@tonic-gate 			(void) fputc(' ', stdout);
2340Sstevel@tonic-gate 		    while (++i < 16);
2350Sstevel@tonic-gate 		    (void) fprintf(stdout, "%ld\n", (*plk)->key);
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	    } else {
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		/* Problems getting the list of reserved devices */
2410Sstevel@tonic-gate 		if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
2420Sstevel@tonic-gate 		    (void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
2430Sstevel@tonic-gate 		    exitcode = EX_TABLES;
2440Sstevel@tonic-gate 		    sev = MM_ERROR;
2450Sstevel@tonic-gate 		} else {
2460Sstevel@tonic-gate 		    (void) sprintf(txt, M_ERROR, errno);
2470Sstevel@tonic-gate 		    exitcode = EX_ERROR;
2480Sstevel@tonic-gate 		    sev = MM_HALT;
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, sev, txt);
2510Sstevel@tonic-gate 	    }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	    /* Finished */
2540Sstevel@tonic-gate 	    exit(exitcode);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 *  devreserv key
2600Sstevel@tonic-gate 	 *
2610Sstevel@tonic-gate 	 *  	Generate a list of the devices allocated on a specific key.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (argcount == 1) {
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	    /* Extract the key from the command */
2670Sstevel@tonic-gate 	    lkey = strtol(*argp, &p, 10);
2680Sstevel@tonic-gate 	    if (*p || (lkey <= 0) || (lkey > MAXINT)) {
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		/* <key> argument invalid */
2710Sstevel@tonic-gate 		(void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
2720Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
2730Sstevel@tonic-gate 		exitcode = EX_ERROR;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	    } else {
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		key = (int) lkey;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		/* Get the list of reserved devices ... */
2800Sstevel@tonic-gate 		if (rsvd = reservdev()) {
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		    /* For each reserved device, write the alias to stdout */
2830Sstevel@tonic-gate 		    exitcode = EX_OK;
2840Sstevel@tonic-gate 		    for (plk = rsvd ; *plk ; plk++) {
2850Sstevel@tonic-gate 			if ((*plk)->key == key) (void) puts((*plk)->devname);
2860Sstevel@tonic-gate 		    }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		} else {
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		    /* Problems getting the list of reserved devices */
2910Sstevel@tonic-gate 		    if (((errno == EINVAL) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
2920Sstevel@tonic-gate 			(void) snprintf(txt, sizeof(txt), M_RSVTAB, rsvtab);
2930Sstevel@tonic-gate 			exitcode = EX_TABLES;
2940Sstevel@tonic-gate 			sev = MM_ERROR;
2950Sstevel@tonic-gate 		    } else {
2960Sstevel@tonic-gate 			(void) sprintf(txt, M_ERROR, errno);
2970Sstevel@tonic-gate 			exitcode = EX_ERROR;
2980Sstevel@tonic-gate 			sev = MM_HALT;
2990Sstevel@tonic-gate 		    }
3000Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, sev, txt);
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 	    }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	    /* Finished */
3050Sstevel@tonic-gate 	    exit(exitcode);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/*
3100Sstevel@tonic-gate 	 *  devreserv key devlist [...]
3110Sstevel@tonic-gate 	 *
3120Sstevel@tonic-gate 	 *	Reserve specific devices
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/* Open the device file (if there's one to be opened) */
3160Sstevel@tonic-gate 	if (!_opendevtab("r")) {
3170Sstevel@tonic-gate 	    if (devtab = _devtabpath()) {
3180Sstevel@tonic-gate 		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
3190Sstevel@tonic-gate 		exitcode = EX_TABLES;
3200Sstevel@tonic-gate 		sev = MM_ERROR;
3210Sstevel@tonic-gate 	    } else {
3220Sstevel@tonic-gate 		(void) sprintf(txt, M_ERROR, errno);
3230Sstevel@tonic-gate 		exitcode = EX_ERROR;
3240Sstevel@tonic-gate 		sev = MM_HALT;
3250Sstevel@tonic-gate 	    }
3260Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, sev, txt);
3270Sstevel@tonic-gate 	    exit(exitcode);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* Extract the key from the command */
3310Sstevel@tonic-gate 	lkey = strtol(*argp, &p, 10);
3320Sstevel@tonic-gate 	if (*p || (lkey <= 0) || (lkey > MAXINT)) {
3330Sstevel@tonic-gate 	    (void) snprintf(txt, sizeof(txt), M_INVKEY, *argp);
3340Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, txt);
3350Sstevel@tonic-gate 	    exit(EX_ERROR);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	key = (int) lkey;
3390Sstevel@tonic-gate 	argp++;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/* Build the device request list from the command arguments */
3420Sstevel@tonic-gate 	if (reqlist = buildreqlist(argp)) {
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	    /* Attempt to allocate the devices */
3450Sstevel@tonic-gate 	    if (alloclist = devreserv(key, reqlist)) {
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * For each allocated device, write the alias to stdout
3490Sstevel@tonic-gate 		 * and free the space allocated for the string.
3500Sstevel@tonic-gate 		 */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 		for (pp = alloclist; *pp; pp++) {
3530Sstevel@tonic-gate 		    (void) puts(*pp);
3540Sstevel@tonic-gate 		    free(*pp);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		/* Free the list of allocated devices */
3580Sstevel@tonic-gate 		free((char *) alloclist);
3590Sstevel@tonic-gate 		exitcode = EX_OK;
3600Sstevel@tonic-gate 	    }
3610Sstevel@tonic-gate 	    else {
3620Sstevel@tonic-gate 		/* Device allocation failed */
3630Sstevel@tonic-gate 		if (errno == EAGAIN) {
3640Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_UNABLE);
3650Sstevel@tonic-gate 		    exitcode = EX_NOALLOC;
3660Sstevel@tonic-gate 		} else if (errno == ENODEV) {
3670Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_NODEV);
3680Sstevel@tonic-gate 		    exitcode = EX_NOALLOC;
3690Sstevel@tonic-gate 		} else {
3700Sstevel@tonic-gate 		    (void) sprintf(txt, M_ERROR, errno);
3710Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_HALT, txt);
3720Sstevel@tonic-gate 		    exitcode = EX_ERROR;
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	    }
3750Sstevel@tonic-gate 	    freereqlist(reqlist);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/* Exit with the appropriate code */
3800Sstevel@tonic-gate 	return(exitcode);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * char ***buildreqlist(args)
3850Sstevel@tonic-gate  *	char   **args
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  *	Build the list of lists of devices to request, as described by the
3880Sstevel@tonic-gate  *	arguments on the command line.
3890Sstevel@tonic-gate  *
3900Sstevel@tonic-gate  *  Arguments:
3910Sstevel@tonic-gate  *	char **args	The address of the first argument of the list of
3920Sstevel@tonic-gate  *			lists of devices to allocate.   (This list is
3930Sstevel@tonic-gate  *			terminated with a (char *) NULL.)
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  *  Returns:  char ***
3960Sstevel@tonic-gate  *	A pointer to a list containing addresses of lists of pointers to
3970Sstevel@tonic-gate  *	character-strings, as expected by "devreserv()"
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  *  Notes:
4000Sstevel@tonic-gate  *    -	Assuming that strtok() won't return "".  If it does, the
4010Sstevel@tonic-gate  *	parsing algorithm needs to be enhanced a bit to eliminate
4020Sstevel@tonic-gate  *	these cases.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static char ***
buildreqlist(args)4060Sstevel@tonic-gate buildreqlist(args)
4070Sstevel@tonic-gate 	char  **args;
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	/* Local automatic data */
4100Sstevel@tonic-gate 	char	     ***addrlist;	/* Addr of space for ptrs to lists */
4110Sstevel@tonic-gate 	char	     ***ppp;		/* Pointer to pointers to pointers */
4120Sstevel@tonic-gate 	char	      **pp;		/* Pointer to pointers */
4130Sstevel@tonic-gate 	char	      **qq;		/* Pointer to pointers */
4140Sstevel@tonic-gate 	int		noerror;	/* FLAG, TRUE if all's well */
4150Sstevel@tonic-gate 	int		i;		/* Counter */
4160Sstevel@tonic-gate 	int		n;		/* Another counter */
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/* Count the number of lists we have to work with */
4200Sstevel@tonic-gate 	i = 1;
4210Sstevel@tonic-gate 	for (pp = args ; *pp ; pp++) i++;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/* If we can allocate space for the list of lists ... */
4250Sstevel@tonic-gate 	if (addrlist = (char ***) malloc(i*sizeof(char **))) {
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	    /* Parse each list, putting that list in the list of lists */
4280Sstevel@tonic-gate 	    ppp = addrlist;
4290Sstevel@tonic-gate 	    noerror = TRUE;
4300Sstevel@tonic-gate 	    for (pp = args ; noerror && *pp ; pp++) {
4310Sstevel@tonic-gate 		n = ndevsin(*pp, TOKDELIMS);
4320Sstevel@tonic-gate 		if (*ppp = (char **) malloc((n+1)*sizeof(char *))) {
4330Sstevel@tonic-gate 		     qq = *ppp++;
4340Sstevel@tonic-gate 		     if (*qq++ = strtok(*pp, TOKDELIMS))
4350Sstevel@tonic-gate 			 while (*qq++ = strtok((char *) NULL, TOKDELIMS));
4360Sstevel@tonic-gate 		} else noerror = FALSE;
4370Sstevel@tonic-gate 	    }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	    /* If there was an error, clean up the malloc()s we've made */
4400Sstevel@tonic-gate 	    if (!noerror) {
4410Sstevel@tonic-gate 		freereqlist(addrlist);
4420Sstevel@tonic-gate 		addrlist = (char ***) NULL;
4430Sstevel@tonic-gate 	    }
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	/* Return ptr to the list of addresses of lists (or NULL if none) */
4470Sstevel@tonic-gate 	return(addrlist);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  *  void freereqlist(list)
4520Sstevel@tonic-gate  *	char ***list
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  *	This function frees the space allocated to the list of lists
4550Sstevel@tonic-gate  *	referenced by <list>
4560Sstevel@tonic-gate  *
4570Sstevel@tonic-gate  *  Arguments:
4580Sstevel@tonic-gate  *	char ***list	Address of the list of lists
4590Sstevel@tonic-gate  *
4600Sstevel@tonic-gate  *  Returns:  void
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate static void
freereqlist(list)4640Sstevel@tonic-gate freereqlist(list)
4650Sstevel@tonic-gate 	char ***list;
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	char ***ppp;
4680Sstevel@tonic-gate 	if (list) {
4690Sstevel@tonic-gate 	    for (ppp = list ; *ppp ; ppp++) free((char *) *ppp);
4700Sstevel@tonic-gate 	    free((char *) list);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * int ndevsin(list, delims)
4760Sstevel@tonic-gate  *	char   *list
4770Sstevel@tonic-gate  *	char   *delims
4780Sstevel@tonic-gate  *
4790Sstevel@tonic-gate  *	This function determines how many tokens are in the list <list>.
4800Sstevel@tonic-gate  *	The tokens are delimited by fields of characters in the string
4810Sstevel@tonic-gate  *	<delims>.  It returns the number of tokens in the list.
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  *  Arguments:
4840Sstevel@tonic-gate  *	char *list	The <delims>list of tokens to scan
4850Sstevel@tonic-gate  *	char *delims	The list of delimiters that define the list
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  *  Returns: int
4880Sstevel@tonic-gate  *	The number of elements in the list.
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  *  Notes:
4910Sstevel@tonic-gate  *    -	This function does not recognize "null" elements.  For example,
4920Sstevel@tonic-gate  *	a,b,,,,c,,d contains 4 elememts (if delims contains a ',')
4930Sstevel@tonic-gate  */
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static int
ndevsin(list,delims)4960Sstevel@tonic-gate ndevsin(list, delims)
4970Sstevel@tonic-gate 	char   *list;			/* List to scan */
4980Sstevel@tonic-gate 	char   *delims;			/* Delimiters */
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	char   *p;			/* Running character pointer */
5010Sstevel@tonic-gate 	int	count;			/* Number of tokens seen so far */
5020Sstevel@tonic-gate 	int	tokflag;		/* TRUE if we're parsing a token */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	count = 0;			/* None seen yet */
5050Sstevel@tonic-gate 	tokflag = FALSE;		/* Not in a token */
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/* Scan the character-string containing the list of tokens */
5080Sstevel@tonic-gate 	for (p = list ; *p ; p++) {
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	    /* If a delimiter, we're not in a token */
5110Sstevel@tonic-gate 	    if (strchr(delims, *p)) tokflag = FALSE;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	    /* Otherwise, if we weren't in a token, we've found one */
5140Sstevel@tonic-gate 	    else if (!tokflag) {
5150Sstevel@tonic-gate 		tokflag = TRUE;
5160Sstevel@tonic-gate 		count++;
5170Sstevel@tonic-gate 	    }
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/* Return the number of elements in the list */
5210Sstevel@tonic-gate 	return(count);
5220Sstevel@tonic-gate }
523