xref: /dragonfly/usr.bin/systat/devs.c (revision 99dd49c5)
1 /*
2  * Copyright (c) 1998 Kenneth D. Merry.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.bin/systat/devs.c,v 1.4 1999/08/28 01:05:59 peter Exp $
29  * $DragonFly: src/usr.bin/systat/devs.c,v 1.8 2008/11/10 04:59:45 swildner Exp $
30  */
31 /*
32  * Some code and ideas taken from the old disks.c.
33  * static char sccsid[] = "@(#)disks.c	8.1 (Berkeley) 6/6/93";
34  */
35 /*-
36  * Copyright (c) 1980, 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *	This product includes software developed by the University of
50  *	California, Berkeley and its contributors.
51  * 4. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  */
67 
68 #include <sys/types.h>
69 #include <sys/devicestat.h>
70 
71 #include <string.h>
72 #include <devstat.h>
73 #include <stdlib.h>
74 #include <ctype.h>
75 #include <err.h>
76 #include "systat.h"
77 #include "extern.h"
78 #include "devs.h"
79 
80 typedef enum {
81 	DS_MATCHTYPE_NONE,
82 	DS_MATCHTYPE_SPEC,
83 	DS_MATCHTYPE_PATTERN
84 } last_match_type;
85 
86 last_match_type last_type;
87 struct device_selection *dev_select;
88 long generation;
89 int num_devices, num_selected;
90 int num_selections;
91 long select_generation;
92 struct devstat_match *matches = NULL;
93 int num_matches = 0;
94 char **specified_devices;
95 int num_devices_specified = 0;
96 
97 static int dsmatchselect(char *args, devstat_select_mode select_mode,
98 			 int maxshowdevs, struct statinfo *s1);
99 static int dsselect(char *args, devstat_select_mode select_mode,
100 		    int maxshowdevs, struct statinfo *s1);
101 
102 int
103 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused,
104        struct statinfo *s3 __unused)
105 {
106 
107 	/*
108 	 * Make sure that the userland devstat version matches the kernel
109 	 * devstat version.  If not, exit and print a message informing
110 	 * the user of his mistake.
111 	 */
112 	if (checkversion() < 0)
113 		errx(1, "%s", devstat_errbuf);
114 
115 	generation = 0;
116 	num_devices = 0;
117 	num_selected = 0;
118 	num_selections = 0;
119 	select_generation = 0;
120 	last_type = DS_MATCHTYPE_NONE;
121 
122 	if (getdevs(s1) == -1)
123 		errx(1, "%s", devstat_errbuf);
124 
125 	num_devices = s1->dinfo->numdevs;
126 	generation = s1->dinfo->generation;
127 
128 	dev_select = NULL;
129 
130 	/*
131 	 * At this point, selectdevs will almost surely indicate that the
132 	 * device list has changed, so we don't look for return values of 0
133 	 * or 1.  If we get back -1, though, there is an error.
134 	 */
135 	if (selectdevs(&dev_select, &num_selected, &num_selections,
136 		       &select_generation, generation, s1->dinfo->devices,
137 		       num_devices, NULL, 0, NULL, 0, DS_SELECT_ADD,
138 		       maxshowdevs, 0) == -1)
139 		errx(1, "%s", devstat_errbuf);
140 
141 	return(1);
142 }
143 
144 int
145 dscmd(const char *cmd, char *args, int maxshowdevs, struct statinfo *s1)
146 {
147 	int retval;
148 
149 	if (prefix(cmd, "display") || prefix(cmd, "add"))
150 		return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1));
151 	if (prefix(cmd, "ignore") || prefix(cmd, "delete"))
152 		return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1));
153 	if (prefix(cmd, "show") || prefix(cmd, "only"))
154 		return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
155 	if (prefix(cmd, "type") || prefix(cmd, "match"))
156 		return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
157 	if (prefix(cmd, "refresh")) {
158 		retval = selectdevs(&dev_select, &num_selected, &num_selections,
159 				    &select_generation, generation,
160 				    s1->dinfo->devices, num_devices,
161 				    (last_type == DS_MATCHTYPE_PATTERN) ?
162 					matches : NULL,
163 				    (last_type == DS_MATCHTYPE_PATTERN) ?
164 					num_matches : 0,
165 				    (last_type == DS_MATCHTYPE_SPEC) ?
166 					specified_devices : NULL,
167 				    (last_type == DS_MATCHTYPE_SPEC) ?
168 					num_devices_specified : 0,
169 				    (last_type == DS_MATCHTYPE_NONE) ?
170 					DS_SELECT_ADD : DS_SELECT_ADDONLY,
171 				    maxshowdevs, 0);
172 		if (retval == -1) {
173 			warnx("%s", devstat_errbuf);
174 			return(0);
175 		} else if (retval == 1)
176 			return(2);
177 	}
178 	if (prefix(cmd, "drives")) {
179 		int i;
180 		move(CMDLINE, 0);
181 		clrtoeol();
182 		for (i = 0; i < num_devices; i++) {
183 			printw("%s%d ", s1->dinfo->devices[i].device_name,
184 			       s1->dinfo->devices[i].unit_number);
185 		}
186 		return(1);
187 	}
188 	return(0);
189 }
190 
191 static int
192 dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
193               struct statinfo *s1)
194 {
195 	char **tempstr;
196 	char *tstr[100];
197 	int num_args = 0;
198 	int i;
199 	int retval = 0;
200 
201 	/*
202 	 * Break the (pipe delimited) input string out into separate
203 	 * strings.
204 	 */
205 	for (tempstr = tstr, num_args  = 0;
206 	     (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100);
207 	     num_args++)
208 		if (**tempstr != '\0')
209 			if (++tempstr >= &tstr[100])
210 				break;
211 
212 	if (num_args > 99) {
213 		warnx("dsmatchselect: too many match arguments");
214 		return(0);
215 	}
216 
217 	/*
218 	 * If we've gone through the matching code before, clean out
219 	 * previously used memory.
220 	 */
221 	if (num_matches > 0) {
222 		free(matches);
223 		matches = NULL;
224 		num_matches = 0;
225 	}
226 
227 	for (i = 0; i < num_args; i++) {
228 		if (buildmatch(tstr[i], &matches, &num_matches) != 0) {
229 			warnx("%s", devstat_errbuf);
230 			return(0);
231 		}
232 	}
233 	if (num_args > 0) {
234 
235 		last_type = DS_MATCHTYPE_PATTERN;
236 
237 		retval = selectdevs(&dev_select, &num_selected, &num_selections,
238 				    &select_generation, generation,
239 				    s1->dinfo->devices, num_devices, matches,
240 				    num_matches, NULL, 0, select_mode,
241 				    maxshowdevs, 0);
242 		if (retval == -1)
243 			err(1, "device selection error");
244 		else if (retval == 1)
245 			return(2);
246 	}
247 	return(1);
248 }
249 
250 static int
251 dsselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
252 	 struct statinfo *s1)
253 {
254 	char *cp;
255 	int i;
256 	int retval = 0;
257 
258 	/*
259 	 * If we've gone through this code before, free previously
260 	 * allocated resources.
261 	 */
262 	if (num_devices_specified > 0) {
263 		for (i = 0; i < num_devices_specified; i++)
264 			free(specified_devices[i]);
265 		free(specified_devices);
266 		specified_devices = NULL;
267 		num_devices_specified = 0;
268 	}
269 
270 	/* do an initial malloc */
271 	specified_devices = (char **)malloc(sizeof(char *));
272 
273 	cp = strchr(args, '\n');
274 	if (cp)
275 		*cp = '\0';
276 	for (;;) {
277 		for (cp = args; *cp && isspace(*cp); cp++)
278 			;
279 		args = cp;
280 		for (; *cp && !isspace(*cp); cp++)
281 			;
282 		if (*cp)
283 			*cp++ = '\0';
284 		if (cp - args == 0)
285 			break;
286 		for (i = 0; i < num_devices; i++) {
287 			char tmpstr[80];
288 
289 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
290 				dev_select[i].unit_number);
291 			if (strcmp(args, tmpstr) == 0) {
292 
293 				num_devices_specified++;
294 
295 				specified_devices =(char **)realloc(
296 						specified_devices,
297 						sizeof(char *) *
298 						num_devices_specified);
299 				specified_devices[num_devices_specified -1]=
300 					strdup(args);
301 
302 				break;
303 			}
304 		}
305 		if (i >= num_devices)
306 			error("%s: unknown drive", args);
307 		args = cp;
308 	}
309 
310 	if (num_devices_specified > 0) {
311 		last_type = DS_MATCHTYPE_SPEC;
312 
313 		retval = selectdevs(&dev_select, &num_selected, &num_selections,
314 				    &select_generation, generation,
315 				    s1->dinfo->devices, num_devices, NULL, 0,
316 				    specified_devices, num_devices_specified,
317 				    select_mode, maxshowdevs, 0);
318 		if (retval == -1)
319 			err(1, "%s", devstat_errbuf);
320 		else if (retval == 1)
321 			return(2);
322 	}
323 	return(1);
324 }
325