xref: /onnv-gate/usr/src/cmd/streams/strcmd/strconf.c (revision 515:484fe491001b)
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  */
22*515Smeem 
23*515Smeem /*
24*515Smeem  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*515Smeem  * Use is subject to license terms.
26*515Smeem  */
27*515Smeem 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
31*515Smeem #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5	*/
320Sstevel@tonic-gate 
33*515Smeem /*
340Sstevel@tonic-gate  * Streams Command strconf:	display the configuration of the
350Sstevel@tonic-gate  *				stream associated with stdin.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * USAGE:	strconf
380Sstevel@tonic-gate  *    or:	strconf -m module
390Sstevel@tonic-gate  *    or:	strconf -t
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * strconf with no options lists the modules on the stream.
420Sstevel@tonic-gate  * -m module	echos "yes" and returns 0 if the module is on the stream.
430Sstevel@tonic-gate  *		echos "no" and returns 2 if not.
440Sstevel@tonic-gate  * -t		lists only the topmost module.  returns 0 if there is a
450Sstevel@tonic-gate  *		module, 2 if not.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * RETURNS:
480Sstevel@tonic-gate  *	0	SUCCESS		it works
490Sstevel@tonic-gate  *	1	ERR_USAGE	bad invocation
500Sstevel@tonic-gate  *	2	ERR_MODULE	module not there
510Sstevel@tonic-gate  *	3	ERR_STDIN	an ioctl on the stdin stream failed
520Sstevel@tonic-gate  *	4	ERR_MEM		couldn't allocate memory
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include	<stdio.h>
560Sstevel@tonic-gate #include	<sys/stropts.h>
57*515Smeem #include	<string.h>
58*515Smeem #include	<stdlib.h>
59*515Smeem #include	<unistd.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	OPTLIST		"m:t"
62*515Smeem #define	USAGE		"USAGE: %s [ -m module | -t ]\n"
630Sstevel@tonic-gate 
64*515Smeem #define	SUCCESS		0
65*515Smeem #define	FAILURE		1
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	ERR_USAGE	1	/* bad invocation			*/
680Sstevel@tonic-gate #define	ERR_MODULE	2	/* module not there			*/
690Sstevel@tonic-gate #define	ERR_STDIN	3	/* an ioctl on the stdin stream failed	*/
700Sstevel@tonic-gate #define	ERR_MEM		4	/* couldn't allocate memory		*/
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	NMODULES	16	/* "reasonable" # of modules on a stream  */
730Sstevel@tonic-gate 				/* 	(there can be more)		  */
740Sstevel@tonic-gate #define	MAXMODULES	2048	/* max # of modules			  */
750Sstevel@tonic-gate 
76*515Smeem static char	*Cmd_namep;		/* how was it invoked?	*/
77*515Smeem static int	more_modules(struct str_list *, int);
780Sstevel@tonic-gate 
79*515Smeem int
main(int argc,char ** argv)80*515Smeem main(int argc, char **argv)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	char		*modp;		/* ptr to module name		*/
83*515Smeem 	int		i;		/* loop var & junk (what else?)	*/
84*515Smeem 	boolean_t	mod_present;	/* B_TRUE if -m module		*/
85*515Smeem 	boolean_t	topmost;	/* B_TRUE if -t			*/
86*515Smeem 	struct str_mlist mlist[NMODULES]; /* modlist for strlist	*/
870Sstevel@tonic-gate 	struct str_list	strlist;	/* mods on stream		*/
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 *	init
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	Cmd_namep = argv[0];
93*515Smeem 	mod_present = topmost = B_FALSE;
940Sstevel@tonic-gate 	strlist.sl_nmods = NMODULES;
950Sstevel@tonic-gate 	strlist.sl_modlist = mlist;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/*
980Sstevel@tonic-gate 	 *	parse args
990Sstevel@tonic-gate 	 */
100*515Smeem 	if (argc > 1) {
101*515Smeem 		while ((i = getopt(argc, argv, OPTLIST)) != -1) {
102*515Smeem 			switch (i) {
103*515Smeem 			case 'm':	/* module present ? */
104*515Smeem 				modp = optarg;
105*515Smeem 				mod_present = B_TRUE;
106*515Smeem 				break;
1070Sstevel@tonic-gate 
108*515Smeem 			case 't':	/* list topmost	*/
109*515Smeem 				topmost = B_TRUE;
110*515Smeem 				break;
1110Sstevel@tonic-gate 
112*515Smeem 			default:
113*515Smeem 				(void) fprintf(stderr, USAGE, Cmd_namep);
114*515Smeem 				return (ERR_USAGE);
1150Sstevel@tonic-gate 			}
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 
118*515Smeem 		if (optind < argc) {
1190Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE, Cmd_namep);
120*515Smeem 			return (ERR_USAGE);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (topmost && mod_present) {
1250Sstevel@tonic-gate 		(void) fprintf(stderr,
126*515Smeem 		    "%s: [-t] and [-m] options cannot be used together\n",
127*515Smeem 		    Cmd_namep);
1280Sstevel@tonic-gate 		(void) fprintf(stderr, USAGE, Cmd_namep);
129*515Smeem 		return (ERR_USAGE);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * get number of modules on stream
1340Sstevel@tonic-gate 	 * allocate more room if needed
1350Sstevel@tonic-gate 	 */
136*515Smeem 	if ((i = ioctl(STDIN_FILENO, I_LIST, NULL)) < 0) {
1370Sstevel@tonic-gate 		perror("I_LIST");
1380Sstevel@tonic-gate 		(void) fprintf(stderr,
1390Sstevel@tonic-gate 			"%s: I_LIST ioctl failed\n", Cmd_namep);
140*515Smeem 		return (ERR_STDIN);
1410Sstevel@tonic-gate 	}
142*515Smeem 	if (i > strlist.sl_nmods)
143*515Smeem 		if (more_modules(&strlist, i) != SUCCESS)
144*515Smeem 			return (ERR_MEM);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 *	get list of modules on stream
1480Sstevel@tonic-gate 	 */
1490Sstevel@tonic-gate 	strlist.sl_nmods = i;
150*515Smeem 	if (ioctl(STDIN_FILENO, I_LIST, &strlist) < 0) {
1510Sstevel@tonic-gate 		perror("I_LIST");
152*515Smeem 		(void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
153*515Smeem 		return (ERR_STDIN);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 *	list topmost module
1580Sstevel@tonic-gate 	 */
159*515Smeem 	if (topmost) {
160*515Smeem 		if (strlist.sl_nmods >= 2) {
161*515Smeem 			(void) puts(strlist.sl_modlist[0].l_name);
162*515Smeem 			return (SUCCESS);
1630Sstevel@tonic-gate 		}
164*515Smeem 		return (ERR_MODULE);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 *	check if module is present
1690Sstevel@tonic-gate 	 */
170*515Smeem 	if (mod_present) {
171*515Smeem 		for (i = 0; i < strlist.sl_nmods; i++) {
172*515Smeem 			if (strncmp(modp, strlist.sl_modlist[i].l_name,
173*515Smeem 			    FMNAMESZ) == 0) {
174*515Smeem 				(void) puts("yes");
175*515Smeem 				return (SUCCESS);
1760Sstevel@tonic-gate 			}
1770Sstevel@tonic-gate 		}
178*515Smeem 		(void) puts("no");
179*515Smeem 		return (ERR_MODULE);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 *	print names of all modules and topmost driver on stream
1840Sstevel@tonic-gate 	 */
185*515Smeem 	for (i = 0; i < strlist.sl_nmods; i++)
186*515Smeem 		(void) puts(strlist.sl_modlist[i].l_name);
187*515Smeem 	return (SUCCESS);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * more_modules(listp, n)	allocate space for 'n' modules in 'listp'
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * returns:	SUCCESS or FAILURE
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static int
more_modules(struct str_list * listp,int n)197*515Smeem more_modules(struct str_list *listp, int n)
1980Sstevel@tonic-gate {
199*515Smeem 	int			i;
200*515Smeem 	struct str_mlist	*modp;
2010Sstevel@tonic-gate 
202*515Smeem 	if (n > MAXMODULES) {
2030Sstevel@tonic-gate 		(void) fprintf(stderr,
204*515Smeem 		    "%s: too many modules (%d) -- max is %d\n",
205*515Smeem 		    Cmd_namep, n, MAXMODULES);
206*515Smeem 		return (FAILURE);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
209*515Smeem 	if ((modp = calloc(n, sizeof (struct str_mlist))) == NULL) {
2100Sstevel@tonic-gate 		perror("calloc");
2110Sstevel@tonic-gate 		(void) fprintf(stderr,
212*515Smeem 		    "%s: failed to allocate space for module list\n",
213*515Smeem 		    Cmd_namep);
214*515Smeem 		return (FAILURE);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
217*515Smeem 	for (i = 0; i < listp->sl_nmods; ++i)
2180Sstevel@tonic-gate 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
219*515Smeem 		    FMNAMESZ);
2200Sstevel@tonic-gate 	listp->sl_nmods = n;
2210Sstevel@tonic-gate 	listp->sl_modlist = modp;
222*515Smeem 	return (SUCCESS);
2230Sstevel@tonic-gate }
224