xref: /titanic_50/usr/src/cmd/streams/strcmd/strconf.c (revision de3d2ce46fc25c7b67ccbae4afe5f15e5357568f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5	*/
32 
33 /*
34  * Streams Command strconf:	display the configuration of the
35  *				stream associated with stdin.
36  *
37  * USAGE:	strconf
38  *    or:	strconf -m module
39  *    or:	strconf -t
40  *
41  * strconf with no options lists the modules on the stream.
42  * -m module	echos "yes" and returns 0 if the module is on the stream.
43  *		echos "no" and returns 2 if not.
44  * -t		lists only the topmost module.  returns 0 if there is a
45  *		module, 2 if not.
46  *
47  * RETURNS:
48  *	0	SUCCESS		it works
49  *	1	ERR_USAGE	bad invocation
50  *	2	ERR_MODULE	module not there
51  *	3	ERR_STDIN	an ioctl on the stdin stream failed
52  *	4	ERR_MEM		couldn't allocate memory
53  */
54 
55 #include	<stdio.h>
56 #include	<sys/stropts.h>
57 #include	<string.h>
58 #include	<stdlib.h>
59 #include	<unistd.h>
60 
61 #define	OPTLIST		"m:t"
62 #define	USAGE		"USAGE: %s [ -m module | -t ]\n"
63 
64 #define	SUCCESS		0
65 #define	FAILURE		1
66 
67 #define	ERR_USAGE	1	/* bad invocation			*/
68 #define	ERR_MODULE	2	/* module not there			*/
69 #define	ERR_STDIN	3	/* an ioctl on the stdin stream failed	*/
70 #define	ERR_MEM		4	/* couldn't allocate memory		*/
71 
72 #define	NMODULES	16	/* "reasonable" # of modules on a stream  */
73 				/* 	(there can be more)		  */
74 #define	MAXMODULES	2048	/* max # of modules			  */
75 
76 static char	*Cmd_namep;		/* how was it invoked?	*/
77 static int	more_modules(struct str_list *, int);
78 
79 int
80 main(int argc, char **argv)
81 {
82 	char		*modp;		/* ptr to module name		*/
83 	int		i;		/* loop var & junk (what else?)	*/
84 	boolean_t	mod_present;	/* B_TRUE if -m module		*/
85 	boolean_t	topmost;	/* B_TRUE if -t			*/
86 	struct str_mlist mlist[NMODULES]; /* modlist for strlist	*/
87 	struct str_list	strlist;	/* mods on stream		*/
88 
89 	/*
90 	 *	init
91 	 */
92 	Cmd_namep = argv[0];
93 	mod_present = topmost = B_FALSE;
94 	strlist.sl_nmods = NMODULES;
95 	strlist.sl_modlist = mlist;
96 
97 	/*
98 	 *	parse args
99 	 */
100 	if (argc > 1) {
101 		while ((i = getopt(argc, argv, OPTLIST)) != -1) {
102 			switch (i) {
103 			case 'm':	/* module present ? */
104 				modp = optarg;
105 				mod_present = B_TRUE;
106 				break;
107 
108 			case 't':	/* list topmost	*/
109 				topmost = B_TRUE;
110 				break;
111 
112 			default:
113 				(void) fprintf(stderr, USAGE, Cmd_namep);
114 				return (ERR_USAGE);
115 			}
116 		}
117 
118 		if (optind < argc) {
119 			(void) fprintf(stderr, USAGE, Cmd_namep);
120 			return (ERR_USAGE);
121 		}
122 	}
123 
124 	if (topmost && mod_present) {
125 		(void) fprintf(stderr,
126 		    "%s: [-t] and [-m] options cannot be used together\n",
127 		    Cmd_namep);
128 		(void) fprintf(stderr, USAGE, Cmd_namep);
129 		return (ERR_USAGE);
130 	}
131 
132 	/*
133 	 * get number of modules on stream
134 	 * allocate more room if needed
135 	 */
136 	if ((i = ioctl(STDIN_FILENO, I_LIST, NULL)) < 0) {
137 		perror("I_LIST");
138 		(void) fprintf(stderr,
139 			"%s: I_LIST ioctl failed\n", Cmd_namep);
140 		return (ERR_STDIN);
141 	}
142 	if (i > strlist.sl_nmods)
143 		if (more_modules(&strlist, i) != SUCCESS)
144 			return (ERR_MEM);
145 
146 	/*
147 	 *	get list of modules on stream
148 	 */
149 	strlist.sl_nmods = i;
150 	if (ioctl(STDIN_FILENO, I_LIST, &strlist) < 0) {
151 		perror("I_LIST");
152 		(void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
153 		return (ERR_STDIN);
154 	}
155 
156 	/*
157 	 *	list topmost module
158 	 */
159 	if (topmost) {
160 		if (strlist.sl_nmods >= 2) {
161 			(void) puts(strlist.sl_modlist[0].l_name);
162 			return (SUCCESS);
163 		}
164 		return (ERR_MODULE);
165 	}
166 
167 	/*
168 	 *	check if module is present
169 	 */
170 	if (mod_present) {
171 		for (i = 0; i < strlist.sl_nmods; i++) {
172 			if (strncmp(modp, strlist.sl_modlist[i].l_name,
173 			    FMNAMESZ) == 0) {
174 				(void) puts("yes");
175 				return (SUCCESS);
176 			}
177 		}
178 		(void) puts("no");
179 		return (ERR_MODULE);
180 	}
181 
182 	/*
183 	 *	print names of all modules and topmost driver on stream
184 	 */
185 	for (i = 0; i < strlist.sl_nmods; i++)
186 		(void) puts(strlist.sl_modlist[i].l_name);
187 	return (SUCCESS);
188 }
189 
190 /*
191  * more_modules(listp, n)	allocate space for 'n' modules in 'listp'
192  *
193  * returns:	SUCCESS or FAILURE
194  */
195 
196 static int
197 more_modules(struct str_list *listp, int n)
198 {
199 	int			i;
200 	struct str_mlist	*modp;
201 
202 	if (n > MAXMODULES) {
203 		(void) fprintf(stderr,
204 		    "%s: too many modules (%d) -- max is %d\n",
205 		    Cmd_namep, n, MAXMODULES);
206 		return (FAILURE);
207 	}
208 
209 	if ((modp = calloc(n, sizeof (struct str_mlist))) == NULL) {
210 		perror("calloc");
211 		(void) fprintf(stderr,
212 		    "%s: failed to allocate space for module list\n",
213 		    Cmd_namep);
214 		return (FAILURE);
215 	}
216 
217 	for (i = 0; i < listp->sl_nmods; ++i)
218 		(void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
219 		    FMNAMESZ);
220 	listp->sl_nmods = n;
221 	listp->sl_modlist = modp;
222 	return (SUCCESS);
223 }
224