17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*4b908718Smeem 
23*4b908718Smeem /*
24*4b908718Smeem  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*4b908718Smeem  * Use is subject to license terms.
26*4b908718Smeem  */
27*4b908718Smeem 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
31*4b908718Smeem #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5	*/
327c478bd9Sstevel@tonic-gate 
33*4b908718Smeem /*
347c478bd9Sstevel@tonic-gate  * Streams Command strconf:	display the configuration of the
357c478bd9Sstevel@tonic-gate  *				stream associated with stdin.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * USAGE:	strconf
387c478bd9Sstevel@tonic-gate  *    or:	strconf -m module
397c478bd9Sstevel@tonic-gate  *    or:	strconf -t
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * strconf with no options lists the modules on the stream.
427c478bd9Sstevel@tonic-gate  * -m module	echos "yes" and returns 0 if the module is on the stream.
437c478bd9Sstevel@tonic-gate  *		echos "no" and returns 2 if not.
447c478bd9Sstevel@tonic-gate  * -t		lists only the topmost module.  returns 0 if there is a
457c478bd9Sstevel@tonic-gate  *		module, 2 if not.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * RETURNS:
487c478bd9Sstevel@tonic-gate  *	0	SUCCESS		it works
497c478bd9Sstevel@tonic-gate  *	1	ERR_USAGE	bad invocation
507c478bd9Sstevel@tonic-gate  *	2	ERR_MODULE	module not there
517c478bd9Sstevel@tonic-gate  *	3	ERR_STDIN	an ioctl on the stdin stream failed
527c478bd9Sstevel@tonic-gate  *	4	ERR_MEM		couldn't allocate memory
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include	<stdio.h>
567c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
57*4b908718Smeem #include	<string.h>
58*4b908718Smeem #include	<stdlib.h>
59*4b908718Smeem #include	<unistd.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	OPTLIST		"m:t"
62*4b908718Smeem #define	USAGE		"USAGE: %s [ -m module | -t ]\n"
637c478bd9Sstevel@tonic-gate 
64*4b908718Smeem #define	SUCCESS		0
65*4b908718Smeem #define	FAILURE		1
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	ERR_USAGE	1	/* bad invocation			*/
687c478bd9Sstevel@tonic-gate #define	ERR_MODULE	2	/* module not there			*/
697c478bd9Sstevel@tonic-gate #define	ERR_STDIN	3	/* an ioctl on the stdin stream failed	*/
707c478bd9Sstevel@tonic-gate #define	ERR_MEM		4	/* couldn't allocate memory		*/
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	NMODULES	16	/* "reasonable" # of modules on a stream  */
737c478bd9Sstevel@tonic-gate 				/* 	(there can be more)		  */
747c478bd9Sstevel@tonic-gate #define	MAXMODULES	2048	/* max # of modules			  */
757c478bd9Sstevel@tonic-gate 
76*4b908718Smeem static char	*Cmd_namep;		/* how was it invoked?	*/
77*4b908718Smeem static int	more_modules(struct str_list *, int);
787c478bd9Sstevel@tonic-gate 
79*4b908718Smeem int
main(int argc,char ** argv)80*4b908718Smeem main(int argc, char **argv)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	char		*modp;		/* ptr to module name		*/
83*4b908718Smeem 	int		i;		/* loop var & junk (what else?)	*/
84*4b908718Smeem 	boolean_t	mod_present;	/* B_TRUE if -m module		*/
85*4b908718Smeem 	boolean_t	topmost;	/* B_TRUE if -t			*/
86*4b908718Smeem 	struct str_mlist mlist[NMODULES]; /* modlist for strlist	*/
877c478bd9Sstevel@tonic-gate 	struct str_list	strlist;	/* mods on stream		*/
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 *	init
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	Cmd_namep = argv[0];
93*4b908718Smeem 	mod_present = topmost = B_FALSE;
947c478bd9Sstevel@tonic-gate 	strlist.sl_nmods = NMODULES;
957c478bd9Sstevel@tonic-gate 	strlist.sl_modlist = mlist;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 *	parse args
997c478bd9Sstevel@tonic-gate 	 */
100*4b908718Smeem 	if (argc > 1) {
101*4b908718Smeem 		while ((i = getopt(argc, argv, OPTLIST)) != -1) {
102*4b908718Smeem 			switch (i) {
103*4b908718Smeem 			case 'm':	/* module present ? */
104*4b908718Smeem 				modp = optarg;
105*4b908718Smeem 				mod_present = B_TRUE;
106*4b908718Smeem 				break;
107*4b908718Smeem 
108*4b908718Smeem 			case 't':	/* list topmost	*/
109*4b908718Smeem 				topmost = B_TRUE;
110*4b908718Smeem 				break;
111*4b908718Smeem 
112*4b908718Smeem 			default:
113*4b908718Smeem 				(void) fprintf(stderr, USAGE, Cmd_namep);
114*4b908718Smeem 				return (ERR_USAGE);
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 
118*4b908718Smeem 		if (optind < argc) {
1197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE, Cmd_namep);
120*4b908718Smeem 			return (ERR_USAGE);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (topmost && mod_present) {
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
126*4b908718Smeem 		    "%s: [-t] and [-m] options cannot be used together\n",
127*4b908718Smeem 		    Cmd_namep);
1287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, USAGE, Cmd_namep);
129*4b908718Smeem 		return (ERR_USAGE);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * get number of modules on stream
1347c478bd9Sstevel@tonic-gate 	 * allocate more room if needed
1357c478bd9Sstevel@tonic-gate 	 */
136*4b908718Smeem 	if ((i = ioctl(STDIN_FILENO, I_LIST, NULL)) < 0) {
1377c478bd9Sstevel@tonic-gate 		perror("I_LIST");
1387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1397c478bd9Sstevel@tonic-gate 			"%s: I_LIST ioctl failed\n", Cmd_namep);
140*4b908718Smeem 		return (ERR_STDIN);
1417c478bd9Sstevel@tonic-gate 	}
142*4b908718Smeem 	if (i > strlist.sl_nmods)
143*4b908718Smeem 		if (more_modules(&strlist, i) != SUCCESS)
144*4b908718Smeem 			return (ERR_MEM);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 *	get list of modules on stream
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	strlist.sl_nmods = i;
150*4b908718Smeem 	if (ioctl(STDIN_FILENO, I_LIST, &strlist) < 0) {
1517c478bd9Sstevel@tonic-gate 		perror("I_LIST");
152*4b908718Smeem 		(void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
153*4b908718Smeem 		return (ERR_STDIN);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 *	list topmost module
1587c478bd9Sstevel@tonic-gate 	 */
159*4b908718Smeem 	if (topmost) {
160*4b908718Smeem 		if (strlist.sl_nmods >= 2) {
161*4b908718Smeem 			(void) puts(strlist.sl_modlist[0].l_name);
162*4b908718Smeem 			return (SUCCESS);
1637c478bd9Sstevel@tonic-gate 		}
164*4b908718Smeem 		return (ERR_MODULE);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 *	check if module is present
1697c478bd9Sstevel@tonic-gate 	 */
170*4b908718Smeem 	if (mod_present) {
171*4b908718Smeem 		for (i = 0; i < strlist.sl_nmods; i++) {
172*4b908718Smeem 			if (strncmp(modp, strlist.sl_modlist[i].l_name,
173*4b908718Smeem 			    FMNAMESZ) == 0) {
174*4b908718Smeem 				(void) puts("yes");
175*4b908718Smeem 				return (SUCCESS);
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 		}
178*4b908718Smeem 		(void) puts("no");
179*4b908718Smeem 		return (ERR_MODULE);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 *	print names of all modules and topmost driver on stream
1847c478bd9Sstevel@tonic-gate 	 */
185*4b908718Smeem 	for (i = 0; i < strlist.sl_nmods; i++)
186*4b908718Smeem 		(void) puts(strlist.sl_modlist[i].l_name);
187*4b908718Smeem 	return (SUCCESS);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * more_modules(listp, n)	allocate space for 'n' modules in 'listp'
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  * returns:	SUCCESS or FAILURE
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate static int
more_modules(struct str_list * listp,int n)197*4b908718Smeem more_modules(struct str_list *listp, int n)
1987c478bd9Sstevel@tonic-gate {
199*4b908718Smeem 	int			i;
200*4b908718Smeem 	struct str_mlist	*modp;
2017c478bd9Sstevel@tonic-gate 
202*4b908718Smeem 	if (n > MAXMODULES) {
2037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
204*4b908718Smeem 		    "%s: too many modules (%d) -- max is %d\n",
205*4b908718Smeem 		    Cmd_namep, n, MAXMODULES);
206*4b908718Smeem 		return (FAILURE);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
209*4b908718Smeem 	if ((modp = calloc(n, sizeof (struct str_mlist))) == NULL) {
2107c478bd9Sstevel@tonic-gate 		perror("calloc");
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
212*4b908718Smeem 		    "%s: failed to allocate space for module list\n",
213*4b908718Smeem 		    Cmd_namep);
214*4b908718Smeem 		return (FAILURE);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
217*4b908718Smeem 	for (i = 0; i < listp->sl_nmods; ++i)
2187c478bd9Sstevel@tonic-gate 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
219*4b908718Smeem 		    FMNAMESZ);
2207c478bd9Sstevel@tonic-gate 	listp->sl_nmods = n;
2217c478bd9Sstevel@tonic-gate 	listp->sl_modlist = modp;
222*4b908718Smeem 	return (SUCCESS);
2237c478bd9Sstevel@tonic-gate }
224