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 /*
32 * Streams Command strconf: display the configuration of the
33 * stream associated with stdin.
34 *
35 * USAGE: strconf
36 * or: strconf -m module
37 * or: strconf -t
38 *
39 * strconf with no options lists the modules on the stream.
40 * -m module echos "yes" and returns 0 if the module is on the stream.
41 * echos "no" and returns 2 if not.
42 * -t lists only the topmost module. returns 0 if there is a
43 * module, 2 if not.
44 *
45 * RETURNS:
46 * 0 SUCCESS it works
47 * 1 ERR_USAGE bad invocation
48 * 2 ERR_MODULE module not there
49 * 3 ERR_STDIN an ioctl on the stdin stream failed
50 * 4 ERR_MEM couldn't allocate memory
51 */
52
53 #include <stdio.h>
54 #include <sys/stropts.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58
59 #define OPTLIST "m:t"
60 #define USAGE "USAGE: %s [ -m module | -t ]\n"
61
62 #define SUCCESS 0
63 #define FAILURE 1
64
65 #define ERR_USAGE 1 /* bad invocation */
66 #define ERR_MODULE 2 /* module not there */
67 #define ERR_STDIN 3 /* an ioctl on the stdin stream failed */
68 #define ERR_MEM 4 /* couldn't allocate memory */
69
70 #define NMODULES 16 /* "reasonable" # of modules on a stream */
71 /* (there can be more) */
72 #define MAXMODULES 2048 /* max # of modules */
73
74 static char *Cmd_namep; /* how was it invoked? */
75 static int more_modules(struct str_list *, int);
76
77 int
main(int argc,char ** argv)78 main(int argc, char **argv)
79 {
80 char *modp; /* ptr to module name */
81 int i; /* loop var & junk (what else?) */
82 boolean_t mod_present; /* B_TRUE if -m module */
83 boolean_t topmost; /* B_TRUE if -t */
84 struct str_mlist mlist[NMODULES]; /* modlist for strlist */
85 struct str_list strlist; /* mods on stream */
86
87 /*
88 * init
89 */
90 Cmd_namep = argv[0];
91 mod_present = topmost = B_FALSE;
92 strlist.sl_nmods = NMODULES;
93 strlist.sl_modlist = mlist;
94
95 /*
96 * parse args
97 */
98 if (argc > 1) {
99 while ((i = getopt(argc, argv, OPTLIST)) != -1) {
100 switch (i) {
101 case 'm': /* module present ? */
102 modp = optarg;
103 mod_present = B_TRUE;
104 break;
105
106 case 't': /* list topmost */
107 topmost = B_TRUE;
108 break;
109
110 default:
111 (void) fprintf(stderr, USAGE, Cmd_namep);
112 return (ERR_USAGE);
113 }
114 }
115
116 if (optind < argc) {
117 (void) fprintf(stderr, USAGE, Cmd_namep);
118 return (ERR_USAGE);
119 }
120 }
121
122 if (topmost && mod_present) {
123 (void) fprintf(stderr,
124 "%s: [-t] and [-m] options cannot be used together\n",
125 Cmd_namep);
126 (void) fprintf(stderr, USAGE, Cmd_namep);
127 return (ERR_USAGE);
128 }
129
130 /*
131 * get number of modules on stream
132 * allocate more room if needed
133 */
134 if ((i = ioctl(STDIN_FILENO, I_LIST, NULL)) < 0) {
135 perror("I_LIST");
136 (void) fprintf(stderr,
137 "%s: I_LIST ioctl failed\n", Cmd_namep);
138 return (ERR_STDIN);
139 }
140 if (i > strlist.sl_nmods)
141 if (more_modules(&strlist, i) != SUCCESS)
142 return (ERR_MEM);
143
144 /*
145 * get list of modules on stream
146 */
147 strlist.sl_nmods = i;
148 if (ioctl(STDIN_FILENO, I_LIST, &strlist) < 0) {
149 perror("I_LIST");
150 (void) fprintf(stderr, "%s: I_LIST ioctl failed\n", Cmd_namep);
151 return (ERR_STDIN);
152 }
153
154 /*
155 * list topmost module
156 */
157 if (topmost) {
158 if (strlist.sl_nmods >= 2) {
159 (void) puts(strlist.sl_modlist[0].l_name);
160 return (SUCCESS);
161 }
162 return (ERR_MODULE);
163 }
164
165 /*
166 * check if module is present
167 */
168 if (mod_present) {
169 for (i = 0; i < strlist.sl_nmods; i++) {
170 if (strncmp(modp, strlist.sl_modlist[i].l_name,
171 FMNAMESZ) == 0) {
172 (void) puts("yes");
173 return (SUCCESS);
174 }
175 }
176 (void) puts("no");
177 return (ERR_MODULE);
178 }
179
180 /*
181 * print names of all modules and topmost driver on stream
182 */
183 for (i = 0; i < strlist.sl_nmods; i++)
184 (void) puts(strlist.sl_modlist[i].l_name);
185 return (SUCCESS);
186 }
187
188 /*
189 * more_modules(listp, n) allocate space for 'n' modules in 'listp'
190 *
191 * returns: SUCCESS or FAILURE
192 */
193
194 static int
more_modules(struct str_list * listp,int n)195 more_modules(struct str_list *listp, int n)
196 {
197 int i;
198 struct str_mlist *modp;
199
200 if (n > MAXMODULES) {
201 (void) fprintf(stderr,
202 "%s: too many modules (%d) -- max is %d\n",
203 Cmd_namep, n, MAXMODULES);
204 return (FAILURE);
205 }
206
207 if ((modp = calloc(n, sizeof (struct str_mlist))) == NULL) {
208 perror("calloc");
209 (void) fprintf(stderr,
210 "%s: failed to allocate space for module list\n",
211 Cmd_namep);
212 return (FAILURE);
213 }
214
215 for (i = 0; i < listp->sl_nmods; ++i)
216 (void) strncpy(modp[i].l_name, listp->sl_modlist[i].l_name,
217 FMNAMESZ);
218 listp->sl_nmods = n;
219 listp->sl_modlist = modp;
220 return (SUCCESS);
221 }
222