xref: /titanic_41/usr/src/cmd/streams/strcmd/strconf.c (revision 4b908718db419b27633010608bf691c20684c0e2)
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 
337c478bd9Sstevel@tonic-gate /*
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"
627c478bd9Sstevel@tonic-gate #define	USAGE		"USAGE: %s [ -m module | -t ]\n"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	SUCCESS		0
657c478bd9Sstevel@tonic-gate #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 
767c478bd9Sstevel@tonic-gate 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 	 */
1007c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1017c478bd9Sstevel@tonic-gate 		while ((i = getopt(argc, argv, OPTLIST)) != -1) {
1027c478bd9Sstevel@tonic-gate 			switch (i) {
1037c478bd9Sstevel@tonic-gate 			case 'm':	/* module present ? */
1047c478bd9Sstevel@tonic-gate 				modp = optarg;
105*4b908718Smeem 				mod_present = B_TRUE;
1067c478bd9Sstevel@tonic-gate 				break;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 			case 't':	/* list topmost	*/
109*4b908718Smeem 				topmost = B_TRUE;
1107c478bd9Sstevel@tonic-gate 				break;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 			default:
1137c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, USAGE, Cmd_namep);
1147c478bd9Sstevel@tonic-gate 				return (ERR_USAGE);
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		if (optind < argc) {
1197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE, Cmd_namep);
1207c478bd9Sstevel@tonic-gate 			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);
1297c478bd9Sstevel@tonic-gate 		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);
1407c478bd9Sstevel@tonic-gate 		return (ERR_STDIN);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	if (i > strlist.sl_nmods)
1437c478bd9Sstevel@tonic-gate 		if (more_modules(&strlist, i) != SUCCESS)
1447c478bd9Sstevel@tonic-gate 			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");
1527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
1537c478bd9Sstevel@tonic-gate 		return (ERR_STDIN);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 *	list topmost module
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if (topmost) {
1607c478bd9Sstevel@tonic-gate 		if (strlist.sl_nmods >= 2) {
161*4b908718Smeem 			(void) puts(strlist.sl_modlist[0].l_name);
1627c478bd9Sstevel@tonic-gate 			return (SUCCESS);
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		return (ERR_MODULE);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 *	check if module is present
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	if (mod_present) {
1717c478bd9Sstevel@tonic-gate 		for (i = 0; i < strlist.sl_nmods; i++) {
1727c478bd9Sstevel@tonic-gate 			if (strncmp(modp, strlist.sl_modlist[i].l_name,
173*4b908718Smeem 			    FMNAMESZ) == 0) {
174*4b908718Smeem 				(void) puts("yes");
1757c478bd9Sstevel@tonic-gate 				return (SUCCESS);
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 		}
178*4b908718Smeem 		(void) puts("no");
1797c478bd9Sstevel@tonic-gate 		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 	 */
1857c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlist.sl_nmods; i++)
186*4b908718Smeem 		(void) puts(strlist.sl_modlist[i].l_name);
1877c478bd9Sstevel@tonic-gate 	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 
2027c478bd9Sstevel@tonic-gate 	if (n > MAXMODULES) {
2037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2047c478bd9Sstevel@tonic-gate 		    "%s: too many modules (%d) -- max is %d\n",
2057c478bd9Sstevel@tonic-gate 		    Cmd_namep, n, MAXMODULES);
2067c478bd9Sstevel@tonic-gate 		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,
2127c478bd9Sstevel@tonic-gate 		    "%s: failed to allocate space for module list\n",
2137c478bd9Sstevel@tonic-gate 		    Cmd_namep);
2147c478bd9Sstevel@tonic-gate 		return (FAILURE);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	for (i = 0; i < listp->sl_nmods; ++i)
2187c478bd9Sstevel@tonic-gate 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
2197c478bd9Sstevel@tonic-gate 		    FMNAMESZ);
2207c478bd9Sstevel@tonic-gate 	listp->sl_nmods = n;
2217c478bd9Sstevel@tonic-gate 	listp->sl_modlist = modp;
2227c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2237c478bd9Sstevel@tonic-gate }
224