xref: /onnv-gate/usr/src/cmd/autopush/autopush.c (revision 10585:e59699fcc4b6)
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
5*10585SDhanaraj.M@Sun.COM  * Common Development and Distribution License (the "License").
6*10585SDhanaraj.M@Sun.COM  * 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  */
210Sstevel@tonic-gate /*
22*10585SDhanaraj.M@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * autopush(1) is the command interface to the STREAMS autopush
320Sstevel@tonic-gate  * mechanism.  The autopush command can be used to configure autopush
330Sstevel@tonic-gate  * information about a STREAMS driver, remove autopush information,
340Sstevel@tonic-gate  * and report on current configuration information.  Its use is as
350Sstevel@tonic-gate  * follows:
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *	autopush -f file
380Sstevel@tonic-gate  *	autopush -r -M major -m minor
390Sstevel@tonic-gate  *	autopush -g -M major -m minor
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * The -f option allows autopush information to be set from a file.  The
420Sstevel@tonic-gate  * format of the file is as follows:
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * # Comment lines begin with a # in column one.
450Sstevel@tonic-gate  * # The fields are separated by white space and are:
460Sstevel@tonic-gate  * # major	minor	lastminor	module1 module2 ... module8
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * "lastminor" is used to configure ranges of minor devices, from "minor"
490Sstevel@tonic-gate  * to "lastminor" inclusive.  It should be set to zero when not in use.
500Sstevel@tonic-gate  * The -r option allows autopush information to be removed for the given
510Sstevel@tonic-gate  * major/minor pair.  The -g option allows the configuration information
520Sstevel@tonic-gate  * to be printed.  The format of printing is the same as for the file.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Use autopush version 1; keep before #include <sys/sad.h>.
570Sstevel@tonic-gate  * See <sys/sad.h> for details.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate #define	AP_VERSION	1
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <sys/types.h>
620Sstevel@tonic-gate #include <sys/conf.h>
630Sstevel@tonic-gate #include <sys/modctl.h>
640Sstevel@tonic-gate #include <sys/sad.h>
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include <fcntl.h>
670Sstevel@tonic-gate #include <errno.h>
680Sstevel@tonic-gate #include <ctype.h>
690Sstevel@tonic-gate #include <stdlib.h>
700Sstevel@tonic-gate #include <unistd.h>
710Sstevel@tonic-gate #include <string.h>
720Sstevel@tonic-gate #include <locale.h>
730Sstevel@tonic-gate #include <sys/stat.h>
74*10585SDhanaraj.M@Sun.COM #include <zone.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #define	OPTIONS	"M:f:gm:r"	/* command line options for getopt(3C) */
770Sstevel@tonic-gate #define	COMMENT	'#'
780Sstevel@tonic-gate #define	MINUS	'-'
790Sstevel@tonic-gate #define	SLASH	'/'
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Output format.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #define	OHEADER		"     Major      Minor  Lastminor\tModules\n"
850Sstevel@tonic-gate #define	OFORMAT1_ONE	"%10ld %10ld      -    \t"
860Sstevel@tonic-gate #define	OFORMAT1_RANGE	"%10ld %10ld %10ld\t"
870Sstevel@tonic-gate #define	OFORMAT1_ALL	"%10ld       ALL       -    \t"
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	AP_ANCHOR	"[anchor]"
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	Openerr		gettext("%s: ERROR: Could not open %s: ")
920Sstevel@tonic-gate #define	Digiterr	gettext("%s: ERROR: argument to %s option must be " \
930Sstevel@tonic-gate 			    "numeric\n")
940Sstevel@tonic-gate #define	Badline		gettext("%s: WARNING: File %s: bad input line %d " \
950Sstevel@tonic-gate 			    "ignored\n")
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static void	usage();
980Sstevel@tonic-gate static int	rem_info(), get_info(), set_info();
990Sstevel@tonic-gate static int	is_white_space(), parse_line();
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static char	*Cmdp;		/* command name */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * main():
1050Sstevel@tonic-gate  *	process command line arguments.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate int
main(int argc,char * argv[])1080Sstevel@tonic-gate main(int argc, char *argv[])
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	int		c;		/* character read by getopt(3C) */
1110Sstevel@tonic-gate 	char		*filenamep;	/* name of configuration file */
1120Sstevel@tonic-gate 	major_t		major;		/* major device number */
1130Sstevel@tonic-gate 	minor_t		minor;		/* minor device number */
1140Sstevel@tonic-gate 	char		*cp;
1150Sstevel@tonic-gate 	int		exitcode;
1160Sstevel@tonic-gate 	ushort_t	minflag = 0;	/* -m option used */
1170Sstevel@tonic-gate 	ushort_t	majflag = 0;	/* -M option used */
1180Sstevel@tonic-gate 	ushort_t	fflag = 0;	/* -f option used */
1190Sstevel@tonic-gate 	ushort_t	rflag = 0;	/* -r option used */
1200Sstevel@tonic-gate 	ushort_t	gflag = 0;	/* -g option used */
1210Sstevel@tonic-gate 	ushort_t	errflag = 0;	/* options usage error */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1240Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1250Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/*
1300Sstevel@tonic-gate 	 * Get command name.
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	Cmdp = argv[0];
1330Sstevel@tonic-gate 	for (filenamep = argv[0]; *filenamep; filenamep++)
1340Sstevel@tonic-gate 		if (*filenamep == SLASH)
1350Sstevel@tonic-gate 			Cmdp = filenamep + 1;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * Get options.
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	while (!errflag && ((c = getopt(argc, argv, OPTIONS)) != -1)) {
1410Sstevel@tonic-gate 		switch (c) {
1420Sstevel@tonic-gate 		case 'M':
1430Sstevel@tonic-gate 			if (fflag|majflag)
1440Sstevel@tonic-gate 				errflag++;
1450Sstevel@tonic-gate 			else {
1460Sstevel@tonic-gate 				majflag++;
1470Sstevel@tonic-gate 				for (cp = optarg; *cp; cp++)
1480Sstevel@tonic-gate 					if (!isdigit(*cp)) {
1490Sstevel@tonic-gate 						(void) fprintf(stderr,
1500Sstevel@tonic-gate 						    Digiterr, Cmdp, "-M");
1510Sstevel@tonic-gate 						exit(1);
1520Sstevel@tonic-gate 					}
1530Sstevel@tonic-gate 				major = (major_t)atol(optarg);
1540Sstevel@tonic-gate 			}
1550Sstevel@tonic-gate 			break;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		case 'm':
1580Sstevel@tonic-gate 			if (fflag|minflag)
1590Sstevel@tonic-gate 				errflag++;
1600Sstevel@tonic-gate 			else {
1610Sstevel@tonic-gate 				minflag++;
1620Sstevel@tonic-gate 				for (cp = optarg; *cp; cp++)
1630Sstevel@tonic-gate 					if (!isdigit(*cp)) {
1640Sstevel@tonic-gate 						(void) fprintf(stderr,
1650Sstevel@tonic-gate 						    Digiterr, Cmdp, "-m");
1660Sstevel@tonic-gate 						exit(1);
1670Sstevel@tonic-gate 					}
1680Sstevel@tonic-gate 				minor = (minor_t)atol(optarg);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 			break;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		case 'f':
1730Sstevel@tonic-gate 			if (fflag|gflag|rflag|majflag|minflag)
1740Sstevel@tonic-gate 				errflag++;
1750Sstevel@tonic-gate 			else {
1760Sstevel@tonic-gate 				fflag++;
1770Sstevel@tonic-gate 				filenamep = optarg;
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			break;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		case 'r':
1820Sstevel@tonic-gate 			if (fflag|gflag|rflag)
1830Sstevel@tonic-gate 				errflag++;
1840Sstevel@tonic-gate 			else
1850Sstevel@tonic-gate 				rflag++;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		case 'g':
1890Sstevel@tonic-gate 			if (fflag|gflag|rflag)
1900Sstevel@tonic-gate 				errflag++;
1910Sstevel@tonic-gate 			else
1920Sstevel@tonic-gate 				gflag++;
1930Sstevel@tonic-gate 			break;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		default:
1960Sstevel@tonic-gate 			errflag++;
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 		} /* switch */
1990Sstevel@tonic-gate 		if (errflag) {
2000Sstevel@tonic-gate 			usage();
2010Sstevel@tonic-gate 			exit(1);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	} /* while */
2040Sstevel@tonic-gate 	if (((gflag || rflag) && (!majflag || !minflag)) || (optind != argc)) {
2050Sstevel@tonic-gate 		usage();
2060Sstevel@tonic-gate 		exit(1);
2070Sstevel@tonic-gate 	}
208*10585SDhanaraj.M@Sun.COM 
209*10585SDhanaraj.M@Sun.COM 	if (getzoneid() != GLOBAL_ZONEID) {
210*10585SDhanaraj.M@Sun.COM 		(void) fprintf(stderr, gettext("autopush "
211*10585SDhanaraj.M@Sun.COM 		    "can only be run from the global zone.\n"));
212*10585SDhanaraj.M@Sun.COM 		exit(1);
213*10585SDhanaraj.M@Sun.COM 	}
214*10585SDhanaraj.M@Sun.COM 
2150Sstevel@tonic-gate 	if (fflag)
2160Sstevel@tonic-gate 		exitcode = set_info(filenamep);
2170Sstevel@tonic-gate 	else if (rflag)
2180Sstevel@tonic-gate 		exitcode = rem_info(major, minor);
2190Sstevel@tonic-gate 	else if (gflag)
2200Sstevel@tonic-gate 		exitcode = get_info(major, minor);
2210Sstevel@tonic-gate 	else {
2220Sstevel@tonic-gate 		usage();
2230Sstevel@tonic-gate 		exit(1);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	return (exitcode);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * usage():
2310Sstevel@tonic-gate  *	print out usage statement.
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate static void
usage()2340Sstevel@tonic-gate usage()
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	(void) fprintf(stderr,	gettext("%s: USAGE:\n\t%s -f filename\n"
2370Sstevel@tonic-gate 	    "\t%s -r -M major -m minor\n"
2380Sstevel@tonic-gate 	    "\t%s -g -M major -m minor\n"), Cmdp, Cmdp, Cmdp, Cmdp);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * set_info():
2430Sstevel@tonic-gate  *	set autopush configuration information.
2440Sstevel@tonic-gate  *	namep: autopush configuration filename
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static int
set_info(char * namep)2470Sstevel@tonic-gate set_info(char *namep)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	int		line;		/* line number of file */
2500Sstevel@tonic-gate 	FILE		*fp;		/* file pointer of config file */
2510Sstevel@tonic-gate 	char		buf[256];	/* input buffer */
2520Sstevel@tonic-gate 	struct strapush push;		/* configuration information */
2530Sstevel@tonic-gate 	int		sadfd;		/* file descriptor to SAD driver */
2540Sstevel@tonic-gate 	int		retcode = 0;	/* return code */
2550Sstevel@tonic-gate 	int		parsecode;	/* return value from parse function */
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if ((sadfd = open(ADMINDEV, O_RDWR)) < 0) {
2580Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, ADMINDEV);
2590Sstevel@tonic-gate 		perror("");
2600Sstevel@tonic-gate 		return (1);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	if ((fp = fopen(namep, "r")) == NULL) {
2630Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, namep);
2640Sstevel@tonic-gate 		perror("");
2650Sstevel@tonic-gate 		return (1);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	line = 0;
2680Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
2690Sstevel@tonic-gate 		line++;
2700Sstevel@tonic-gate 		if ((buf[0] == COMMENT) || is_white_space(buf))
2710Sstevel@tonic-gate 			continue;
2720Sstevel@tonic-gate 		(void) memset(&push, 0, sizeof (struct strapush));
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		parsecode = parse_line(buf, line, namep, &push);
2750Sstevel@tonic-gate 		if (parsecode != 0) {
2760Sstevel@tonic-gate 			retcode = parsecode;
2770Sstevel@tonic-gate 			continue;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		if (push.sap_minor == (minor_t)-1)
2810Sstevel@tonic-gate 			push.sap_cmd = SAP_ALL;
2820Sstevel@tonic-gate 		else if (push.sap_lastminor == 0)
2830Sstevel@tonic-gate 			push.sap_cmd = SAP_ONE;
2840Sstevel@tonic-gate 		else
2850Sstevel@tonic-gate 			push.sap_cmd = SAP_RANGE;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		if (ioctl(sadfd, SAD_SAP, &push) < 0) {
2880Sstevel@tonic-gate 			int error = errno;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 			retcode = 1;
2910Sstevel@tonic-gate 			(void) fprintf(stderr,
2920Sstevel@tonic-gate 			    gettext("%s: ERROR: File %s: could not configure "
2930Sstevel@tonic-gate 			    "autopush for line %d\n"), Cmdp, namep, line);
2940Sstevel@tonic-gate 			switch (error) {
2950Sstevel@tonic-gate 			case EPERM:
2960Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
2970Sstevel@tonic-gate 				    "You don't have permission to set autopush "
2980Sstevel@tonic-gate 				    "information\n"), Cmdp);
2990Sstevel@tonic-gate 				break;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			case EINVAL:
3020Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3030Sstevel@tonic-gate 				    "Invalid major device number or invalid "
3040Sstevel@tonic-gate 				    "module name or too many modules\n"), Cmdp);
3050Sstevel@tonic-gate 				break;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			case ENOSTR:
3080Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3090Sstevel@tonic-gate 				    "Major device is not a STREAMS "
3100Sstevel@tonic-gate 				    "driver\n"), Cmdp);
3110Sstevel@tonic-gate 				break;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 			case EEXIST:
3140Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3150Sstevel@tonic-gate 				    "Major/minor already configured\n"), Cmdp);
3160Sstevel@tonic-gate 				break;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			case ENOSR:
3190Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: Ran "
3200Sstevel@tonic-gate 				    "out of autopush structures\n"), Cmdp);
3210Sstevel@tonic-gate 				break;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 			case ERANGE:
3240Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3250Sstevel@tonic-gate 				    "lastminor must be greater than minor\n"),
3260Sstevel@tonic-gate 				    Cmdp);
3270Sstevel@tonic-gate 				break;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 			default:
3300Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "),
3310Sstevel@tonic-gate 				    Cmdp);
3320Sstevel@tonic-gate 				(void) fprintf(stderr, "%s\n", strerror(error));
3330Sstevel@tonic-gate 				break;
3340Sstevel@tonic-gate 			} /* switch */
3350Sstevel@tonic-gate 		} /* if */
3360Sstevel@tonic-gate 	} /* while */
3370Sstevel@tonic-gate 	return (retcode);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * rem_info():
3420Sstevel@tonic-gate  *	remove autopush configuration information.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate static int
rem_info(major_t maj,minor_t min)3450Sstevel@tonic-gate rem_info(major_t maj, minor_t min)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	struct strapush push;		/* configuration information */
3480Sstevel@tonic-gate 	int		sadfd;		/* file descriptor to SAD driver */
3490Sstevel@tonic-gate 	int		retcode = 0;	/* return code */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((sadfd = open(ADMINDEV, O_RDWR)) < 0) {
3520Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, ADMINDEV);
3530Sstevel@tonic-gate 		perror("");
3540Sstevel@tonic-gate 		return (1);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	push.sap_cmd = SAP_CLEAR;
3570Sstevel@tonic-gate 	push.sap_minor = min;
3580Sstevel@tonic-gate 	push.sap_major = maj;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (ioctl(sadfd, SAD_SAP, &push) < 0) {
3610Sstevel@tonic-gate 		int error = errno;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		retcode = 1;
3640Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: ERROR: Could not remove "
3650Sstevel@tonic-gate 		    "autopush information\n"), Cmdp);
3660Sstevel@tonic-gate 		switch (error) {
3670Sstevel@tonic-gate 		case EPERM:
3680Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: You don't "
3690Sstevel@tonic-gate 			    "have permission to remove autopush "
3700Sstevel@tonic-gate 			    "information\n"), Cmdp);
3710Sstevel@tonic-gate 			break;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		case EINVAL:
3740Sstevel@tonic-gate 			if ((min != 0) && (ioctl(sadfd, SAD_GAP, &push) == 0) &&
3750Sstevel@tonic-gate 			    (push.sap_cmd == SAP_ALL))
3760Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3770Sstevel@tonic-gate 				    "When removing an entry for ALL minors, "
3780Sstevel@tonic-gate 				    "minor must be set to 0\n"), Cmdp);
3790Sstevel@tonic-gate 			else
3800Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
3810Sstevel@tonic-gate 				    "Invalid major device number\n"), Cmdp);
3820Sstevel@tonic-gate 			break;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		case ENODEV:
3850Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major/minor "
3860Sstevel@tonic-gate 			    "not configured for autopush\n"), Cmdp);
3870Sstevel@tonic-gate 			break;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		case ERANGE:
3900Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: minor must "
3910Sstevel@tonic-gate 			    "be set to begining of range when clearing\n"),
3920Sstevel@tonic-gate 			    Cmdp);
3930Sstevel@tonic-gate 			break;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		default:
3960Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: "), Cmdp);
3970Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", strerror(error));
3980Sstevel@tonic-gate 			break;
3990Sstevel@tonic-gate 		} /* switch */
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	return (retcode);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  * get_info():
4060Sstevel@tonic-gate  *	get autopush configuration information.
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate static int
get_info(major_t maj,minor_t min)4090Sstevel@tonic-gate get_info(major_t maj, minor_t min)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	struct strapush push;	/* configuration information */
4120Sstevel@tonic-gate 	int		i;	/* counter */
4130Sstevel@tonic-gate 	int		sadfd;	/* file descriptor to SAD driver */
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((sadfd = open(USERDEV, O_RDWR)) < 0) {
4160Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, USERDEV);
4170Sstevel@tonic-gate 		perror("");
4180Sstevel@tonic-gate 		return (1);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	push.sap_major = maj;
4210Sstevel@tonic-gate 	push.sap_minor = min;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (ioctl(sadfd, SAD_GAP, &push) < 0) {
4240Sstevel@tonic-gate 		int error = errno;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: ERROR: Could not get "
4270Sstevel@tonic-gate 		    "autopush information\n"), Cmdp);
4280Sstevel@tonic-gate 		switch (error) {
4290Sstevel@tonic-gate 		case EINVAL:
4300Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Invalid "
4310Sstevel@tonic-gate 			    "major device number\n"), Cmdp);
4320Sstevel@tonic-gate 			break;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		case ENOSTR:
4350Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major "
4360Sstevel@tonic-gate 			    "device is not a STREAMS driver\n"), Cmdp);
4370Sstevel@tonic-gate 			break;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		case ENODEV:
4400Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major/minor "
4410Sstevel@tonic-gate 			    "not configured for autopush\n"), Cmdp);
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		default:
4450Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: "), Cmdp);
4460Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", strerror(error));
4470Sstevel@tonic-gate 			break;
4480Sstevel@tonic-gate 		} /* switch */
4490Sstevel@tonic-gate 		return (1);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 	(void) printf(OHEADER);
4520Sstevel@tonic-gate 	switch (push.sap_cmd) {
4530Sstevel@tonic-gate 	case SAP_ONE:
4540Sstevel@tonic-gate 		(void) printf(OFORMAT1_ONE, push.sap_major, push.sap_minor);
4550Sstevel@tonic-gate 		break;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	case SAP_RANGE:
4580Sstevel@tonic-gate 		(void) printf(OFORMAT1_RANGE, push.sap_major, push.sap_minor,
4590Sstevel@tonic-gate 		    push.sap_lastminor);
4600Sstevel@tonic-gate 		break;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	case SAP_ALL:
4630Sstevel@tonic-gate 		(void) printf(OFORMAT1_ALL, push.sap_major);
4640Sstevel@tonic-gate 		break;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	default:
4670Sstevel@tonic-gate 		(void) fprintf(stderr,
4680Sstevel@tonic-gate 		    gettext("%s: ERROR: Unknown configuration type\n"), Cmdp);
4690Sstevel@tonic-gate 		return (1);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	for (i = 0; i < push.sap_npush; i++) {
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		(void) printf("%s", push.sap_list[i]);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		if (push.sap_anchor == (i + 1))
4770Sstevel@tonic-gate 			(void) printf(" %s", AP_ANCHOR);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		if (i < push.sap_npush - 1)
4800Sstevel@tonic-gate 			(void) printf(" ");
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	(void) printf("\n");
4850Sstevel@tonic-gate 	return (0);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * is_white_space():
4900Sstevel@tonic-gate  *	Return 1 if buffer is all white space.
4910Sstevel@tonic-gate  *	Return 0 otherwise.
4920Sstevel@tonic-gate  */
4930Sstevel@tonic-gate static int
is_white_space(char * bufp)4940Sstevel@tonic-gate is_white_space(char *bufp)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate 	while (*bufp) {
4970Sstevel@tonic-gate 		if (!isspace(*bufp))
4980Sstevel@tonic-gate 			return (0);
4990Sstevel@tonic-gate 		bufp++;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	return (1);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * parse_line():
5060Sstevel@tonic-gate  *	Parse input line from file and report any errors found.  Fill
5070Sstevel@tonic-gate  *	strapush structure along the way.  Returns 1 if the line has
5080Sstevel@tonic-gate  *	errors and 0 if the line is well-formed.  Another hidden
5090Sstevel@tonic-gate  *	dependency on MAXAPUSH. `linep' is the input buffer, `lineno'
5100Sstevel@tonic-gate  *	is the current line number, and `namep' is the filename.
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate static int
parse_line(char * linep,int lineno,char * namep,struct strapush * pushp)5130Sstevel@tonic-gate parse_line(char *linep, int lineno, char *namep, struct strapush *pushp)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	char		*wp;		/* word pointer */
5160Sstevel@tonic-gate 	char		*cp;		/* character pointer */
5170Sstevel@tonic-gate 	int		midx;		/* module index */
5180Sstevel@tonic-gate 	int		npush;		/* number of modules to push */
5190Sstevel@tonic-gate 	char		c;
5200Sstevel@tonic-gate 	major_t		major_num;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	pushp->sap_anchor = 0;		/* by default, no anchor */
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/*
5250Sstevel@tonic-gate 	 * Find the major device number.
5260Sstevel@tonic-gate 	 */
5270Sstevel@tonic-gate 	for (wp = linep; isspace(*wp); wp++)
5280Sstevel@tonic-gate 		;
5290Sstevel@tonic-gate 	for (cp = wp; !isspace(*cp); cp++)
5300Sstevel@tonic-gate 		;
5310Sstevel@tonic-gate 	if (!isspace(*cp)) {
5320Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
5330Sstevel@tonic-gate 		return (1);
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 	c = *cp;
5360Sstevel@tonic-gate 	*cp = '\0';
5370Sstevel@tonic-gate 	if (modctl(MODGETMAJBIND, wp, strlen(wp) + 1, &major_num) != 0) {
5380Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
5390Sstevel@tonic-gate 		return (1);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 	*cp = c;
5420Sstevel@tonic-gate 	pushp->sap_major = major_num;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * Find the minor device number.  Must handle negative values here.
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	for (wp = cp; isspace(*wp); wp++)
5480Sstevel@tonic-gate 		;
5490Sstevel@tonic-gate 	for (cp = wp; (isdigit(*cp) || (*cp == MINUS)); cp++)
5500Sstevel@tonic-gate 		;
5510Sstevel@tonic-gate 	if (!isspace(*cp)) {
5520Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
5530Sstevel@tonic-gate 		return (1);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	pushp->sap_minor = (minor_t)atol(wp);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * Find the lastminor.
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	for (wp = cp; isspace(*wp); wp++)
5610Sstevel@tonic-gate 		;
5620Sstevel@tonic-gate 	for (cp = wp; isdigit(*cp); cp++)
5630Sstevel@tonic-gate 		;
5640Sstevel@tonic-gate 	if (!isspace(*cp)) {
5650Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
5660Sstevel@tonic-gate 		return (1);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	pushp->sap_lastminor = (minor_t)atol(wp);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Read the list of module names.
5720Sstevel@tonic-gate 	 */
5730Sstevel@tonic-gate 	npush = 0;
5740Sstevel@tonic-gate 	while ((npush < MAXAPUSH) && (*cp)) {
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		while (isspace(*cp))
5770Sstevel@tonic-gate 			cp++;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		if (strncasecmp(cp, AP_ANCHOR, sizeof (AP_ANCHOR) - 1) == 0) {
5800Sstevel@tonic-gate 			if (pushp->sap_anchor != 0) {
5810Sstevel@tonic-gate 				(void) fprintf(stderr,
5820Sstevel@tonic-gate 				    gettext("%s: ERROR: File %s: more than "
5830Sstevel@tonic-gate 				    "one anchor in line, line %d ignored\n"),
5840Sstevel@tonic-gate 				    Cmdp, namep, lineno);
5850Sstevel@tonic-gate 				return (1);
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 			if (npush == 0)
5880Sstevel@tonic-gate 				(void) fprintf(stderr,
5890Sstevel@tonic-gate 				    gettext("%s: WARNING: File %s: anchor at "
5900Sstevel@tonic-gate 				    "beginning of stream on line %d ignored\n"),
5910Sstevel@tonic-gate 				    Cmdp, namep, lineno);
5920Sstevel@tonic-gate 			pushp->sap_anchor = npush;
5930Sstevel@tonic-gate 			cp += sizeof (AP_ANCHOR) - 1;
5940Sstevel@tonic-gate 			continue;
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		for (midx = 0; !isspace(*cp) && *cp; midx++) {
5980Sstevel@tonic-gate 			if (midx == FMNAMESZ) {
5990Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
6000Sstevel@tonic-gate 				    "File %s: module name too long, line %d "
6010Sstevel@tonic-gate 				    "ignored\n"), Cmdp, namep, lineno);
6020Sstevel@tonic-gate 				return (1);
6030Sstevel@tonic-gate 			}
6040Sstevel@tonic-gate 			pushp->sap_list[npush][midx] = *cp++;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		if (midx > 0) {
6080Sstevel@tonic-gate 			pushp->sap_list[npush][midx] = '\0';
6090Sstevel@tonic-gate 			npush++;
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	pushp->sap_npush = npush;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * We have everything we want from the line.
6160Sstevel@tonic-gate 	 * Now make sure there is no extra garbage on the line.
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	while (isspace(*cp))
6190Sstevel@tonic-gate 		cp++;
6200Sstevel@tonic-gate 	if (*cp) {
6210Sstevel@tonic-gate 		(void) fprintf(stderr,
6220Sstevel@tonic-gate 		    gettext("%s: ERROR: File %s: too many modules, line %d "
6230Sstevel@tonic-gate 		    "ignored\n"), Cmdp, namep, lineno);
6240Sstevel@tonic-gate 		return (1);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	return (0);
6270Sstevel@tonic-gate }
628