xref: /illumos-gate/usr/src/cmd/streams/strcmd/strconf.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Streams Command strconf:	display the configuration of the
337c478bd9Sstevel@tonic-gate  *				stream associated with stdin.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * USAGE:	strconf
367c478bd9Sstevel@tonic-gate  *    or:	strconf -m module
377c478bd9Sstevel@tonic-gate  *    or:	strconf -t
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * strconf with no options lists the modules on the stream.
407c478bd9Sstevel@tonic-gate  * -m module	echos "yes" and returns 0 if the module is on the stream.
417c478bd9Sstevel@tonic-gate  *		echos "no" and returns 2 if not.
427c478bd9Sstevel@tonic-gate  * -t		lists only the topmost module.  returns 0 if there is a
437c478bd9Sstevel@tonic-gate  *		module, 2 if not.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * RETURNS:
467c478bd9Sstevel@tonic-gate  *	0	SUCCESS		it works
477c478bd9Sstevel@tonic-gate  *	1	ERR_USAGE	bad invocation
487c478bd9Sstevel@tonic-gate  *	2	ERR_MODULE	module not there
497c478bd9Sstevel@tonic-gate  *	3	ERR_STDIN	an ioctl on the stdin stream failed
507c478bd9Sstevel@tonic-gate  *	4	ERR_MEM		couldn't allocate memory
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include	<stdio.h>
547c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
55*4b908718Smeem #include	<string.h>
56*4b908718Smeem #include	<stdlib.h>
57*4b908718Smeem #include	<unistd.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	OPTLIST		"m:t"
607c478bd9Sstevel@tonic-gate #define	USAGE		"USAGE: %s [ -m module | -t ]\n"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	SUCCESS		0
637c478bd9Sstevel@tonic-gate #define	FAILURE		1
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	ERR_USAGE	1	/* bad invocation			*/
667c478bd9Sstevel@tonic-gate #define	ERR_MODULE	2	/* module not there			*/
677c478bd9Sstevel@tonic-gate #define	ERR_STDIN	3	/* an ioctl on the stdin stream failed	*/
687c478bd9Sstevel@tonic-gate #define	ERR_MEM		4	/* couldn't allocate memory		*/
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	NMODULES	16	/* "reasonable" # of modules on a stream  */
717c478bd9Sstevel@tonic-gate 				/* 	(there can be more)		  */
727c478bd9Sstevel@tonic-gate #define	MAXMODULES	2048	/* max # of modules			  */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static char	*Cmd_namep;		/* how was it invoked?	*/
75*4b908718Smeem static int	more_modules(struct str_list *, int);
767c478bd9Sstevel@tonic-gate 
77*4b908718Smeem int
main(int argc,char ** argv)78*4b908718Smeem main(int argc, char **argv)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	char		*modp;		/* ptr to module name		*/
81*4b908718Smeem 	int		i;		/* loop var & junk (what else?)	*/
82*4b908718Smeem 	boolean_t	mod_present;	/* B_TRUE if -m module		*/
83*4b908718Smeem 	boolean_t	topmost;	/* B_TRUE if -t			*/
84*4b908718Smeem 	struct str_mlist mlist[NMODULES]; /* modlist for strlist	*/
857c478bd9Sstevel@tonic-gate 	struct str_list	strlist;	/* mods on stream		*/
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 *	init
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	Cmd_namep = argv[0];
91*4b908718Smeem 	mod_present = topmost = B_FALSE;
927c478bd9Sstevel@tonic-gate 	strlist.sl_nmods = NMODULES;
937c478bd9Sstevel@tonic-gate 	strlist.sl_modlist = mlist;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
967c478bd9Sstevel@tonic-gate 	 *	parse args
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	if (argc > 1) {
997c478bd9Sstevel@tonic-gate 		while ((i = getopt(argc, argv, OPTLIST)) != -1) {
1007c478bd9Sstevel@tonic-gate 			switch (i) {
1017c478bd9Sstevel@tonic-gate 			case 'm':	/* module present ? */
1027c478bd9Sstevel@tonic-gate 				modp = optarg;
103*4b908718Smeem 				mod_present = B_TRUE;
1047c478bd9Sstevel@tonic-gate 				break;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 			case 't':	/* list topmost	*/
107*4b908718Smeem 				topmost = B_TRUE;
1087c478bd9Sstevel@tonic-gate 				break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 			default:
1117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, USAGE, Cmd_namep);
1127c478bd9Sstevel@tonic-gate 				return (ERR_USAGE);
1137c478bd9Sstevel@tonic-gate 			}
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if (optind < argc) {
1177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE, Cmd_namep);
1187c478bd9Sstevel@tonic-gate 			return (ERR_USAGE);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (topmost && mod_present) {
1237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
124*4b908718Smeem 		    "%s: [-t] and [-m] options cannot be used together\n",
125*4b908718Smeem 		    Cmd_namep);
1267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, USAGE, Cmd_namep);
1277c478bd9Sstevel@tonic-gate 		return (ERR_USAGE);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * get number of modules on stream
1327c478bd9Sstevel@tonic-gate 	 * allocate more room if needed
1337c478bd9Sstevel@tonic-gate 	 */
134*4b908718Smeem 	if ((i = ioctl(STDIN_FILENO, I_LIST, NULL)) < 0) {
1357c478bd9Sstevel@tonic-gate 		perror("I_LIST");
1367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1377c478bd9Sstevel@tonic-gate 			"%s: I_LIST ioctl failed\n", Cmd_namep);
1387c478bd9Sstevel@tonic-gate 		return (ERR_STDIN);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	if (i > strlist.sl_nmods)
1417c478bd9Sstevel@tonic-gate 		if (more_modules(&strlist, i) != SUCCESS)
1427c478bd9Sstevel@tonic-gate 			return (ERR_MEM);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 *	get list of modules on stream
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	strlist.sl_nmods = i;
148*4b908718Smeem 	if (ioctl(STDIN_FILENO, I_LIST, &strlist) < 0) {
1497c478bd9Sstevel@tonic-gate 		perror("I_LIST");
1507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
1517c478bd9Sstevel@tonic-gate 		return (ERR_STDIN);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 *	list topmost module
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if (topmost) {
1587c478bd9Sstevel@tonic-gate 		if (strlist.sl_nmods >= 2) {
159*4b908718Smeem 			(void) puts(strlist.sl_modlist[0].l_name);
1607c478bd9Sstevel@tonic-gate 			return (SUCCESS);
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 		return (ERR_MODULE);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 *	check if module is present
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	if (mod_present) {
1697c478bd9Sstevel@tonic-gate 		for (i = 0; i < strlist.sl_nmods; i++) {
1707c478bd9Sstevel@tonic-gate 			if (strncmp(modp, strlist.sl_modlist[i].l_name,
171*4b908718Smeem 			    FMNAMESZ) == 0) {
172*4b908718Smeem 				(void) puts("yes");
1737c478bd9Sstevel@tonic-gate 				return (SUCCESS);
1747c478bd9Sstevel@tonic-gate 			}
1757c478bd9Sstevel@tonic-gate 		}
176*4b908718Smeem 		(void) puts("no");
1777c478bd9Sstevel@tonic-gate 		return (ERR_MODULE);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 *	print names of all modules and topmost driver on stream
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlist.sl_nmods; i++)
184*4b908718Smeem 		(void) puts(strlist.sl_modlist[i].l_name);
1857c478bd9Sstevel@tonic-gate 	return (SUCCESS);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * more_modules(listp, n)	allocate space for 'n' modules in 'listp'
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * returns:	SUCCESS or FAILURE
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static int
more_modules(struct str_list * listp,int n)195*4b908718Smeem more_modules(struct str_list *listp, int n)
1967c478bd9Sstevel@tonic-gate {
197*4b908718Smeem 	int			i;
198*4b908718Smeem 	struct str_mlist	*modp;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (n > MAXMODULES) {
2017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2027c478bd9Sstevel@tonic-gate 		    "%s: too many modules (%d) -- max is %d\n",
2037c478bd9Sstevel@tonic-gate 		    Cmd_namep, n, MAXMODULES);
2047c478bd9Sstevel@tonic-gate 		return (FAILURE);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
207*4b908718Smeem 	if ((modp = calloc(n, sizeof (struct str_mlist))) == NULL) {
2087c478bd9Sstevel@tonic-gate 		perror("calloc");
2097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2107c478bd9Sstevel@tonic-gate 		    "%s: failed to allocate space for module list\n",
2117c478bd9Sstevel@tonic-gate 		    Cmd_namep);
2127c478bd9Sstevel@tonic-gate 		return (FAILURE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	for (i = 0; i < listp->sl_nmods; ++i)
2167c478bd9Sstevel@tonic-gate 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
2177c478bd9Sstevel@tonic-gate 		    FMNAMESZ);
2187c478bd9Sstevel@tonic-gate 	listp->sl_nmods = n;
2197c478bd9Sstevel@tonic-gate 	listp->sl_modlist = modp;
2207c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2217c478bd9Sstevel@tonic-gate }
222