xref: /titanic_54/usr/src/cmd/autopush/autopush.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * autopush(1) is the command interface to the STREAMS autopush
35*7c478bd9Sstevel@tonic-gate  * mechanism.  The autopush command can be used to configure autopush
36*7c478bd9Sstevel@tonic-gate  * information about a STREAMS driver, remove autopush information,
37*7c478bd9Sstevel@tonic-gate  * and report on current configuration information.  Its use is as
38*7c478bd9Sstevel@tonic-gate  * follows:
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  *	autopush -f file
41*7c478bd9Sstevel@tonic-gate  *	autopush -r -M major -m minor
42*7c478bd9Sstevel@tonic-gate  *	autopush -g -M major -m minor
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  * The -f option allows autopush information to be set from a file.  The
45*7c478bd9Sstevel@tonic-gate  * format of the file is as follows:
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * # Comment lines begin with a # in column one.
48*7c478bd9Sstevel@tonic-gate  * # The fields are separated by white space and are:
49*7c478bd9Sstevel@tonic-gate  * # major	minor	lastminor	module1 module2 ... module8
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  * "lastminor" is used to configure ranges of minor devices, from "minor"
52*7c478bd9Sstevel@tonic-gate  * to "lastminor" inclusive.  It should be set to zero when not in use.
53*7c478bd9Sstevel@tonic-gate  * The -r option allows autopush information to be removed for the given
54*7c478bd9Sstevel@tonic-gate  * major/minor pair.  The -g option allows the configuration information
55*7c478bd9Sstevel@tonic-gate  * to be printed.  The format of printing is the same as for the file.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Use autopush version 1; keep before #include <sys/sad.h>.
60*7c478bd9Sstevel@tonic-gate  * See <sys/sad.h> for details.
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate #define	AP_VERSION	1
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/sad.h>
68*7c478bd9Sstevel@tonic-gate #include <stdio.h>
69*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
70*7c478bd9Sstevel@tonic-gate #include <errno.h>
71*7c478bd9Sstevel@tonic-gate #include <ctype.h>
72*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
73*7c478bd9Sstevel@tonic-gate #include <unistd.h>
74*7c478bd9Sstevel@tonic-gate #include <string.h>
75*7c478bd9Sstevel@tonic-gate #include <locale.h>
76*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define	OPTIONS	"M:f:gm:r"	/* command line options for getopt(3C) */
79*7c478bd9Sstevel@tonic-gate #define	COMMENT	'#'
80*7c478bd9Sstevel@tonic-gate #define	MINUS	'-'
81*7c478bd9Sstevel@tonic-gate #define	SLASH	'/'
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * Output format.
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate #define	OHEADER		"     Major      Minor  Lastminor\tModules\n"
87*7c478bd9Sstevel@tonic-gate #define	OFORMAT1_ONE	"%10ld %10ld      -    \t"
88*7c478bd9Sstevel@tonic-gate #define	OFORMAT1_RANGE	"%10ld %10ld %10ld\t"
89*7c478bd9Sstevel@tonic-gate #define	OFORMAT1_ALL	"%10ld       ALL       -    \t"
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define	AP_ANCHOR	"[anchor]"
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate #define	Openerr		gettext("%s: ERROR: Could not open %s: ")
94*7c478bd9Sstevel@tonic-gate #define	Digiterr	gettext("%s: ERROR: argument to %s option must be " \
95*7c478bd9Sstevel@tonic-gate 			    "numeric\n")
96*7c478bd9Sstevel@tonic-gate #define	Badline		gettext("%s: WARNING: File %s: bad input line %d " \
97*7c478bd9Sstevel@tonic-gate 			    "ignored\n")
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate static void	usage();
100*7c478bd9Sstevel@tonic-gate static int	rem_info(), get_info(), set_info();
101*7c478bd9Sstevel@tonic-gate static int	is_white_space(), parse_line();
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static char	*Cmdp;		/* command name */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * main():
107*7c478bd9Sstevel@tonic-gate  *	process command line arguments.
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate int
110*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	int		c;		/* character read by getopt(3C) */
113*7c478bd9Sstevel@tonic-gate 	char		*filenamep;	/* name of configuration file */
114*7c478bd9Sstevel@tonic-gate 	major_t		major;		/* major device number */
115*7c478bd9Sstevel@tonic-gate 	minor_t		minor;		/* minor device number */
116*7c478bd9Sstevel@tonic-gate 	char		*cp;
117*7c478bd9Sstevel@tonic-gate 	int		exitcode;
118*7c478bd9Sstevel@tonic-gate 	ushort_t	minflag = 0;	/* -m option used */
119*7c478bd9Sstevel@tonic-gate 	ushort_t	majflag = 0;	/* -M option used */
120*7c478bd9Sstevel@tonic-gate 	ushort_t	fflag = 0;	/* -f option used */
121*7c478bd9Sstevel@tonic-gate 	ushort_t	rflag = 0;	/* -r option used */
122*7c478bd9Sstevel@tonic-gate 	ushort_t	gflag = 0;	/* -g option used */
123*7c478bd9Sstevel@tonic-gate 	ushort_t	errflag = 0;	/* options usage error */
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
126*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
127*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
128*7c478bd9Sstevel@tonic-gate #endif
129*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/*
132*7c478bd9Sstevel@tonic-gate 	 * Get command name.
133*7c478bd9Sstevel@tonic-gate 	 */
134*7c478bd9Sstevel@tonic-gate 	Cmdp = argv[0];
135*7c478bd9Sstevel@tonic-gate 	for (filenamep = argv[0]; *filenamep; filenamep++)
136*7c478bd9Sstevel@tonic-gate 		if (*filenamep == SLASH)
137*7c478bd9Sstevel@tonic-gate 			Cmdp = filenamep + 1;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/*
140*7c478bd9Sstevel@tonic-gate 	 * Get options.
141*7c478bd9Sstevel@tonic-gate 	 */
142*7c478bd9Sstevel@tonic-gate 	while (!errflag && ((c = getopt(argc, argv, OPTIONS)) != -1)) {
143*7c478bd9Sstevel@tonic-gate 		switch (c) {
144*7c478bd9Sstevel@tonic-gate 		case 'M':
145*7c478bd9Sstevel@tonic-gate 			if (fflag|majflag)
146*7c478bd9Sstevel@tonic-gate 				errflag++;
147*7c478bd9Sstevel@tonic-gate 			else {
148*7c478bd9Sstevel@tonic-gate 				majflag++;
149*7c478bd9Sstevel@tonic-gate 				for (cp = optarg; *cp; cp++)
150*7c478bd9Sstevel@tonic-gate 					if (!isdigit(*cp)) {
151*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
152*7c478bd9Sstevel@tonic-gate 						    Digiterr, Cmdp, "-M");
153*7c478bd9Sstevel@tonic-gate 						exit(1);
154*7c478bd9Sstevel@tonic-gate 					}
155*7c478bd9Sstevel@tonic-gate 				major = (major_t)atol(optarg);
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 			break;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		case 'm':
160*7c478bd9Sstevel@tonic-gate 			if (fflag|minflag)
161*7c478bd9Sstevel@tonic-gate 				errflag++;
162*7c478bd9Sstevel@tonic-gate 			else {
163*7c478bd9Sstevel@tonic-gate 				minflag++;
164*7c478bd9Sstevel@tonic-gate 				for (cp = optarg; *cp; cp++)
165*7c478bd9Sstevel@tonic-gate 					if (!isdigit(*cp)) {
166*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
167*7c478bd9Sstevel@tonic-gate 						    Digiterr, Cmdp, "-m");
168*7c478bd9Sstevel@tonic-gate 						exit(1);
169*7c478bd9Sstevel@tonic-gate 					}
170*7c478bd9Sstevel@tonic-gate 				minor = (minor_t)atol(optarg);
171*7c478bd9Sstevel@tonic-gate 			}
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		case 'f':
175*7c478bd9Sstevel@tonic-gate 			if (fflag|gflag|rflag|majflag|minflag)
176*7c478bd9Sstevel@tonic-gate 				errflag++;
177*7c478bd9Sstevel@tonic-gate 			else {
178*7c478bd9Sstevel@tonic-gate 				fflag++;
179*7c478bd9Sstevel@tonic-gate 				filenamep = optarg;
180*7c478bd9Sstevel@tonic-gate 			}
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 		case 'r':
184*7c478bd9Sstevel@tonic-gate 			if (fflag|gflag|rflag)
185*7c478bd9Sstevel@tonic-gate 				errflag++;
186*7c478bd9Sstevel@tonic-gate 			else
187*7c478bd9Sstevel@tonic-gate 				rflag++;
188*7c478bd9Sstevel@tonic-gate 			break;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 		case 'g':
191*7c478bd9Sstevel@tonic-gate 			if (fflag|gflag|rflag)
192*7c478bd9Sstevel@tonic-gate 				errflag++;
193*7c478bd9Sstevel@tonic-gate 			else
194*7c478bd9Sstevel@tonic-gate 				gflag++;
195*7c478bd9Sstevel@tonic-gate 			break;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		default:
198*7c478bd9Sstevel@tonic-gate 			errflag++;
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 		} /* switch */
201*7c478bd9Sstevel@tonic-gate 		if (errflag) {
202*7c478bd9Sstevel@tonic-gate 			usage();
203*7c478bd9Sstevel@tonic-gate 			exit(1);
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	} /* while */
206*7c478bd9Sstevel@tonic-gate 	if (((gflag || rflag) && (!majflag || !minflag)) || (optind != argc)) {
207*7c478bd9Sstevel@tonic-gate 		usage();
208*7c478bd9Sstevel@tonic-gate 		exit(1);
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 	if (fflag)
211*7c478bd9Sstevel@tonic-gate 		exitcode = set_info(filenamep);
212*7c478bd9Sstevel@tonic-gate 	else if (rflag)
213*7c478bd9Sstevel@tonic-gate 		exitcode = rem_info(major, minor);
214*7c478bd9Sstevel@tonic-gate 	else if (gflag)
215*7c478bd9Sstevel@tonic-gate 		exitcode = get_info(major, minor);
216*7c478bd9Sstevel@tonic-gate 	else {
217*7c478bd9Sstevel@tonic-gate 		usage();
218*7c478bd9Sstevel@tonic-gate 		exit(1);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	return (exitcode);
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate /*
225*7c478bd9Sstevel@tonic-gate  * usage():
226*7c478bd9Sstevel@tonic-gate  *	print out usage statement.
227*7c478bd9Sstevel@tonic-gate  */
228*7c478bd9Sstevel@tonic-gate static void
229*7c478bd9Sstevel@tonic-gate usage()
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,	gettext("%s: USAGE:\n\t%s -f filename\n"
232*7c478bd9Sstevel@tonic-gate 	    "\t%s -r -M major -m minor\n"
233*7c478bd9Sstevel@tonic-gate 	    "\t%s -g -M major -m minor\n"), Cmdp, Cmdp, Cmdp, Cmdp);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * set_info():
238*7c478bd9Sstevel@tonic-gate  *	set autopush configuration information.
239*7c478bd9Sstevel@tonic-gate  *	namep: autopush configuration filename
240*7c478bd9Sstevel@tonic-gate  */
241*7c478bd9Sstevel@tonic-gate static int
242*7c478bd9Sstevel@tonic-gate set_info(char *namep)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	int		line;		/* line number of file */
245*7c478bd9Sstevel@tonic-gate 	FILE		*fp;		/* file pointer of config file */
246*7c478bd9Sstevel@tonic-gate 	char		buf[256];	/* input buffer */
247*7c478bd9Sstevel@tonic-gate 	struct strapush push;		/* configuration information */
248*7c478bd9Sstevel@tonic-gate 	int		sadfd;		/* file descriptor to SAD driver */
249*7c478bd9Sstevel@tonic-gate 	int		retcode = 0;	/* return code */
250*7c478bd9Sstevel@tonic-gate 	int		parsecode;	/* return value from parse function */
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if ((sadfd = open(ADMINDEV, O_RDWR)) < 0) {
253*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, ADMINDEV);
254*7c478bd9Sstevel@tonic-gate 		perror("");
255*7c478bd9Sstevel@tonic-gate 		return (1);
256*7c478bd9Sstevel@tonic-gate 	}
257*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(namep, "r")) == NULL) {
258*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, namep);
259*7c478bd9Sstevel@tonic-gate 		perror("");
260*7c478bd9Sstevel@tonic-gate 		return (1);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 	line = 0;
263*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
264*7c478bd9Sstevel@tonic-gate 		line++;
265*7c478bd9Sstevel@tonic-gate 		if ((buf[0] == COMMENT) || is_white_space(buf))
266*7c478bd9Sstevel@tonic-gate 			continue;
267*7c478bd9Sstevel@tonic-gate 		(void) memset(&push, 0, sizeof (struct strapush));
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		parsecode = parse_line(buf, line, namep, &push);
270*7c478bd9Sstevel@tonic-gate 		if (parsecode != 0) {
271*7c478bd9Sstevel@tonic-gate 			retcode = parsecode;
272*7c478bd9Sstevel@tonic-gate 			continue;
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 		if (push.sap_minor == (minor_t)-1)
276*7c478bd9Sstevel@tonic-gate 			push.sap_cmd = SAP_ALL;
277*7c478bd9Sstevel@tonic-gate 		else if (push.sap_lastminor == 0)
278*7c478bd9Sstevel@tonic-gate 			push.sap_cmd = SAP_ONE;
279*7c478bd9Sstevel@tonic-gate 		else
280*7c478bd9Sstevel@tonic-gate 			push.sap_cmd = SAP_RANGE;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		if (ioctl(sadfd, SAD_SAP, &push) < 0) {
283*7c478bd9Sstevel@tonic-gate 			int error = errno;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 			retcode = 1;
286*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
287*7c478bd9Sstevel@tonic-gate 			    gettext("%s: ERROR: File %s: could not configure "
288*7c478bd9Sstevel@tonic-gate 			    "autopush for line %d\n"), Cmdp, namep, line);
289*7c478bd9Sstevel@tonic-gate 			switch (error) {
290*7c478bd9Sstevel@tonic-gate 			case EPERM:
291*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
292*7c478bd9Sstevel@tonic-gate 				    "You don't have permission to set autopush "
293*7c478bd9Sstevel@tonic-gate 				    "information\n"), Cmdp);
294*7c478bd9Sstevel@tonic-gate 				break;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 			case EINVAL:
297*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
298*7c478bd9Sstevel@tonic-gate 				    "Invalid major device number or invalid "
299*7c478bd9Sstevel@tonic-gate 				    "module name or too many modules\n"), Cmdp);
300*7c478bd9Sstevel@tonic-gate 				break;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 			case ENOSTR:
303*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
304*7c478bd9Sstevel@tonic-gate 				    "Major device is not a STREAMS "
305*7c478bd9Sstevel@tonic-gate 				    "driver\n"), Cmdp);
306*7c478bd9Sstevel@tonic-gate 				break;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 			case EEXIST:
309*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
310*7c478bd9Sstevel@tonic-gate 				    "Major/minor already configured\n"), Cmdp);
311*7c478bd9Sstevel@tonic-gate 				break;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 			case ENOSR:
314*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: Ran "
315*7c478bd9Sstevel@tonic-gate 				    "out of autopush structures\n"), Cmdp);
316*7c478bd9Sstevel@tonic-gate 				break;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 			case ERANGE:
319*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
320*7c478bd9Sstevel@tonic-gate 				    "lastminor must be greater than minor\n"),
321*7c478bd9Sstevel@tonic-gate 				    Cmdp);
322*7c478bd9Sstevel@tonic-gate 				break;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 			default:
325*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "),
326*7c478bd9Sstevel@tonic-gate 				    Cmdp);
327*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s\n", strerror(error));
328*7c478bd9Sstevel@tonic-gate 				break;
329*7c478bd9Sstevel@tonic-gate 			} /* switch */
330*7c478bd9Sstevel@tonic-gate 		} /* if */
331*7c478bd9Sstevel@tonic-gate 	} /* while */
332*7c478bd9Sstevel@tonic-gate 	return (retcode);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate /*
336*7c478bd9Sstevel@tonic-gate  * rem_info():
337*7c478bd9Sstevel@tonic-gate  *	remove autopush configuration information.
338*7c478bd9Sstevel@tonic-gate  */
339*7c478bd9Sstevel@tonic-gate static int
340*7c478bd9Sstevel@tonic-gate rem_info(major_t maj, minor_t min)
341*7c478bd9Sstevel@tonic-gate {
342*7c478bd9Sstevel@tonic-gate 	struct strapush push;		/* configuration information */
343*7c478bd9Sstevel@tonic-gate 	int		sadfd;		/* file descriptor to SAD driver */
344*7c478bd9Sstevel@tonic-gate 	int		retcode = 0;	/* return code */
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if ((sadfd = open(ADMINDEV, O_RDWR)) < 0) {
347*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, ADMINDEV);
348*7c478bd9Sstevel@tonic-gate 		perror("");
349*7c478bd9Sstevel@tonic-gate 		return (1);
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 	push.sap_cmd = SAP_CLEAR;
352*7c478bd9Sstevel@tonic-gate 	push.sap_minor = min;
353*7c478bd9Sstevel@tonic-gate 	push.sap_major = maj;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	if (ioctl(sadfd, SAD_SAP, &push) < 0) {
356*7c478bd9Sstevel@tonic-gate 		int error = errno;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 		retcode = 1;
359*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: ERROR: Could not remove "
360*7c478bd9Sstevel@tonic-gate 		    "autopush information\n"), Cmdp);
361*7c478bd9Sstevel@tonic-gate 		switch (error) {
362*7c478bd9Sstevel@tonic-gate 		case EPERM:
363*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: You don't "
364*7c478bd9Sstevel@tonic-gate 			    "have permission to remove autopush "
365*7c478bd9Sstevel@tonic-gate 			    "information\n"), Cmdp);
366*7c478bd9Sstevel@tonic-gate 			break;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 		case EINVAL:
369*7c478bd9Sstevel@tonic-gate 			if ((min != 0) && (ioctl(sadfd, SAD_GAP, &push) == 0) &&
370*7c478bd9Sstevel@tonic-gate 			    (push.sap_cmd == SAP_ALL))
371*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
372*7c478bd9Sstevel@tonic-gate 				    "When removing an entry for ALL minors, "
373*7c478bd9Sstevel@tonic-gate 				    "minor must be set to 0\n"), Cmdp);
374*7c478bd9Sstevel@tonic-gate 			else
375*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
376*7c478bd9Sstevel@tonic-gate 				    "Invalid major device number\n"), Cmdp);
377*7c478bd9Sstevel@tonic-gate 			break;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 		case ENODEV:
380*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major/minor "
381*7c478bd9Sstevel@tonic-gate 			    "not configured for autopush\n"), Cmdp);
382*7c478bd9Sstevel@tonic-gate 			break;
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 		case ERANGE:
385*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: minor must "
386*7c478bd9Sstevel@tonic-gate 			    "be set to begining of range when clearing\n"),
387*7c478bd9Sstevel@tonic-gate 			    Cmdp);
388*7c478bd9Sstevel@tonic-gate 			break;
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 		default:
391*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: "), Cmdp);
392*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", strerror(error));
393*7c478bd9Sstevel@tonic-gate 			break;
394*7c478bd9Sstevel@tonic-gate 		} /* switch */
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate 	return (retcode);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate /*
400*7c478bd9Sstevel@tonic-gate  * get_info():
401*7c478bd9Sstevel@tonic-gate  *	get autopush configuration information.
402*7c478bd9Sstevel@tonic-gate  */
403*7c478bd9Sstevel@tonic-gate static int
404*7c478bd9Sstevel@tonic-gate get_info(major_t maj, minor_t min)
405*7c478bd9Sstevel@tonic-gate {
406*7c478bd9Sstevel@tonic-gate 	struct strapush push;	/* configuration information */
407*7c478bd9Sstevel@tonic-gate 	int		i;	/* counter */
408*7c478bd9Sstevel@tonic-gate 	int		sadfd;	/* file descriptor to SAD driver */
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	if ((sadfd = open(USERDEV, O_RDWR)) < 0) {
411*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Openerr, Cmdp, USERDEV);
412*7c478bd9Sstevel@tonic-gate 		perror("");
413*7c478bd9Sstevel@tonic-gate 		return (1);
414*7c478bd9Sstevel@tonic-gate 	}
415*7c478bd9Sstevel@tonic-gate 	push.sap_major = maj;
416*7c478bd9Sstevel@tonic-gate 	push.sap_minor = min;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	if (ioctl(sadfd, SAD_GAP, &push) < 0) {
419*7c478bd9Sstevel@tonic-gate 		int error = errno;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: ERROR: Could not get "
422*7c478bd9Sstevel@tonic-gate 		    "autopush information\n"), Cmdp);
423*7c478bd9Sstevel@tonic-gate 		switch (error) {
424*7c478bd9Sstevel@tonic-gate 		case EINVAL:
425*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Invalid "
426*7c478bd9Sstevel@tonic-gate 			    "major device number\n"), Cmdp);
427*7c478bd9Sstevel@tonic-gate 			break;
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 		case ENOSTR:
430*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major "
431*7c478bd9Sstevel@tonic-gate 			    "device is not a STREAMS driver\n"), Cmdp);
432*7c478bd9Sstevel@tonic-gate 			break;
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 		case ENODEV:
435*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: Major/minor "
436*7c478bd9Sstevel@tonic-gate 			    "not configured for autopush\n"), Cmdp);
437*7c478bd9Sstevel@tonic-gate 			break;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 		default:
440*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: ERROR: "), Cmdp);
441*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", strerror(error));
442*7c478bd9Sstevel@tonic-gate 			break;
443*7c478bd9Sstevel@tonic-gate 		} /* switch */
444*7c478bd9Sstevel@tonic-gate 		return (1);
445*7c478bd9Sstevel@tonic-gate 	}
446*7c478bd9Sstevel@tonic-gate 	(void) printf(OHEADER);
447*7c478bd9Sstevel@tonic-gate 	switch (push.sap_cmd) {
448*7c478bd9Sstevel@tonic-gate 	case SAP_ONE:
449*7c478bd9Sstevel@tonic-gate 		(void) printf(OFORMAT1_ONE, push.sap_major, push.sap_minor);
450*7c478bd9Sstevel@tonic-gate 		break;
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	case SAP_RANGE:
453*7c478bd9Sstevel@tonic-gate 		(void) printf(OFORMAT1_RANGE, push.sap_major, push.sap_minor,
454*7c478bd9Sstevel@tonic-gate 		    push.sap_lastminor);
455*7c478bd9Sstevel@tonic-gate 		break;
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	case SAP_ALL:
458*7c478bd9Sstevel@tonic-gate 		(void) printf(OFORMAT1_ALL, push.sap_major);
459*7c478bd9Sstevel@tonic-gate 		break;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	default:
462*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
463*7c478bd9Sstevel@tonic-gate 		    gettext("%s: ERROR: Unknown configuration type\n"), Cmdp);
464*7c478bd9Sstevel@tonic-gate 		return (1);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < push.sap_npush; i++) {
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", push.sap_list[i]);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		if (push.sap_anchor == (i + 1))
472*7c478bd9Sstevel@tonic-gate 			(void) printf(" %s", AP_ANCHOR);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 		if (i < push.sap_npush - 1)
475*7c478bd9Sstevel@tonic-gate 			(void) printf(" ");
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	}
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
480*7c478bd9Sstevel@tonic-gate 	return (0);
481*7c478bd9Sstevel@tonic-gate }
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate /*
484*7c478bd9Sstevel@tonic-gate  * is_white_space():
485*7c478bd9Sstevel@tonic-gate  *	Return 1 if buffer is all white space.
486*7c478bd9Sstevel@tonic-gate  *	Return 0 otherwise.
487*7c478bd9Sstevel@tonic-gate  */
488*7c478bd9Sstevel@tonic-gate static int
489*7c478bd9Sstevel@tonic-gate is_white_space(char *bufp)
490*7c478bd9Sstevel@tonic-gate {
491*7c478bd9Sstevel@tonic-gate 	while (*bufp) {
492*7c478bd9Sstevel@tonic-gate 		if (!isspace(*bufp))
493*7c478bd9Sstevel@tonic-gate 			return (0);
494*7c478bd9Sstevel@tonic-gate 		bufp++;
495*7c478bd9Sstevel@tonic-gate 	}
496*7c478bd9Sstevel@tonic-gate 	return (1);
497*7c478bd9Sstevel@tonic-gate }
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate /*
500*7c478bd9Sstevel@tonic-gate  * parse_line():
501*7c478bd9Sstevel@tonic-gate  *	Parse input line from file and report any errors found.  Fill
502*7c478bd9Sstevel@tonic-gate  *	strapush structure along the way.  Returns 1 if the line has
503*7c478bd9Sstevel@tonic-gate  *	errors and 0 if the line is well-formed.  Another hidden
504*7c478bd9Sstevel@tonic-gate  *	dependency on MAXAPUSH. `linep' is the input buffer, `lineno'
505*7c478bd9Sstevel@tonic-gate  *	is the current line number, and `namep' is the filename.
506*7c478bd9Sstevel@tonic-gate  */
507*7c478bd9Sstevel@tonic-gate static int
508*7c478bd9Sstevel@tonic-gate parse_line(char *linep, int lineno, char *namep, struct strapush *pushp)
509*7c478bd9Sstevel@tonic-gate {
510*7c478bd9Sstevel@tonic-gate 	char		*wp;		/* word pointer */
511*7c478bd9Sstevel@tonic-gate 	char		*cp;		/* character pointer */
512*7c478bd9Sstevel@tonic-gate 	int		midx;		/* module index */
513*7c478bd9Sstevel@tonic-gate 	int		npush;		/* number of modules to push */
514*7c478bd9Sstevel@tonic-gate 	char		c;
515*7c478bd9Sstevel@tonic-gate 	major_t		major_num;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	pushp->sap_anchor = 0;		/* by default, no anchor */
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	/*
520*7c478bd9Sstevel@tonic-gate 	 * Find the major device number.
521*7c478bd9Sstevel@tonic-gate 	 */
522*7c478bd9Sstevel@tonic-gate 	for (wp = linep; isspace(*wp); wp++)
523*7c478bd9Sstevel@tonic-gate 		;
524*7c478bd9Sstevel@tonic-gate 	for (cp = wp; !isspace(*cp); cp++)
525*7c478bd9Sstevel@tonic-gate 		;
526*7c478bd9Sstevel@tonic-gate 	if (!isspace(*cp)) {
527*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
528*7c478bd9Sstevel@tonic-gate 		return (1);
529*7c478bd9Sstevel@tonic-gate 	}
530*7c478bd9Sstevel@tonic-gate 	c = *cp;
531*7c478bd9Sstevel@tonic-gate 	*cp = '\0';
532*7c478bd9Sstevel@tonic-gate 	if (modctl(MODGETMAJBIND, wp, strlen(wp) + 1, &major_num) != 0) {
533*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
534*7c478bd9Sstevel@tonic-gate 		return (1);
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 	*cp = c;
537*7c478bd9Sstevel@tonic-gate 	pushp->sap_major = major_num;
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	/*
540*7c478bd9Sstevel@tonic-gate 	 * Find the minor device number.  Must handle negative values here.
541*7c478bd9Sstevel@tonic-gate 	 */
542*7c478bd9Sstevel@tonic-gate 	for (wp = cp; isspace(*wp); wp++)
543*7c478bd9Sstevel@tonic-gate 		;
544*7c478bd9Sstevel@tonic-gate 	for (cp = wp; (isdigit(*cp) || (*cp == MINUS)); cp++)
545*7c478bd9Sstevel@tonic-gate 		;
546*7c478bd9Sstevel@tonic-gate 	if (!isspace(*cp)) {
547*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
548*7c478bd9Sstevel@tonic-gate 		return (1);
549*7c478bd9Sstevel@tonic-gate 	}
550*7c478bd9Sstevel@tonic-gate 	pushp->sap_minor = (minor_t)atol(wp);
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	/*
553*7c478bd9Sstevel@tonic-gate 	 * Find the lastminor.
554*7c478bd9Sstevel@tonic-gate 	 */
555*7c478bd9Sstevel@tonic-gate 	for (wp = cp; isspace(*wp); wp++)
556*7c478bd9Sstevel@tonic-gate 		;
557*7c478bd9Sstevel@tonic-gate 	for (cp = wp; isdigit(*cp); cp++)
558*7c478bd9Sstevel@tonic-gate 		;
559*7c478bd9Sstevel@tonic-gate 	if (!isspace(*cp)) {
560*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Badline, Cmdp, namep, lineno);
561*7c478bd9Sstevel@tonic-gate 		return (1);
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 	pushp->sap_lastminor = (minor_t)atol(wp);
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	/*
566*7c478bd9Sstevel@tonic-gate 	 * Read the list of module names.
567*7c478bd9Sstevel@tonic-gate 	 */
568*7c478bd9Sstevel@tonic-gate 	npush = 0;
569*7c478bd9Sstevel@tonic-gate 	while ((npush < MAXAPUSH) && (*cp)) {
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 		while (isspace(*cp))
572*7c478bd9Sstevel@tonic-gate 			cp++;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 		if (strncasecmp(cp, AP_ANCHOR, sizeof (AP_ANCHOR) - 1) == 0) {
575*7c478bd9Sstevel@tonic-gate 			if (pushp->sap_anchor != 0) {
576*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
577*7c478bd9Sstevel@tonic-gate 				    gettext("%s: ERROR: File %s: more than "
578*7c478bd9Sstevel@tonic-gate 				    "one anchor in line, line %d ignored\n"),
579*7c478bd9Sstevel@tonic-gate 				    Cmdp, namep, lineno);
580*7c478bd9Sstevel@tonic-gate 				return (1);
581*7c478bd9Sstevel@tonic-gate 			}
582*7c478bd9Sstevel@tonic-gate 			if (npush == 0)
583*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
584*7c478bd9Sstevel@tonic-gate 				    gettext("%s: WARNING: File %s: anchor at "
585*7c478bd9Sstevel@tonic-gate 				    "beginning of stream on line %d ignored\n"),
586*7c478bd9Sstevel@tonic-gate 				    Cmdp, namep, lineno);
587*7c478bd9Sstevel@tonic-gate 			pushp->sap_anchor = npush;
588*7c478bd9Sstevel@tonic-gate 			cp += sizeof (AP_ANCHOR) - 1;
589*7c478bd9Sstevel@tonic-gate 			continue;
590*7c478bd9Sstevel@tonic-gate 		}
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 		for (midx = 0; !isspace(*cp) && *cp; midx++) {
593*7c478bd9Sstevel@tonic-gate 			if (midx == FMNAMESZ) {
594*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: ERROR: "
595*7c478bd9Sstevel@tonic-gate 				    "File %s: module name too long, line %d "
596*7c478bd9Sstevel@tonic-gate 				    "ignored\n"), Cmdp, namep, lineno);
597*7c478bd9Sstevel@tonic-gate 				return (1);
598*7c478bd9Sstevel@tonic-gate 			}
599*7c478bd9Sstevel@tonic-gate 			pushp->sap_list[npush][midx] = *cp++;
600*7c478bd9Sstevel@tonic-gate 		}
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate 		if (midx > 0) {
603*7c478bd9Sstevel@tonic-gate 			pushp->sap_list[npush][midx] = '\0';
604*7c478bd9Sstevel@tonic-gate 			npush++;
605*7c478bd9Sstevel@tonic-gate 		}
606*7c478bd9Sstevel@tonic-gate 	}
607*7c478bd9Sstevel@tonic-gate 	pushp->sap_npush = npush;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	/*
610*7c478bd9Sstevel@tonic-gate 	 * We have everything we want from the line.
611*7c478bd9Sstevel@tonic-gate 	 * Now make sure there is no extra garbage on the line.
612*7c478bd9Sstevel@tonic-gate 	 */
613*7c478bd9Sstevel@tonic-gate 	while (isspace(*cp))
614*7c478bd9Sstevel@tonic-gate 		cp++;
615*7c478bd9Sstevel@tonic-gate 	if (*cp) {
616*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
617*7c478bd9Sstevel@tonic-gate 		    gettext("%s: ERROR: File %s: too many modules, line %d "
618*7c478bd9Sstevel@tonic-gate 		    "ignored\n"), Cmdp, namep, lineno);
619*7c478bd9Sstevel@tonic-gate 		return (1);
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 	return (0);
622*7c478bd9Sstevel@tonic-gate }
623