devs.c revision 83131
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: head/usr.bin/systat/devs.c 83131 2001-09-06 04:06:12Z ken $
29 */
30/*
31 * Some code and ideas taken from the old disks.c.
32 * static char sccsid[] = "@(#)disks.c	8.1 (Berkeley) 6/6/93";
33 */
34/*-
35 * Copyright (c) 1980, 1992, 1993
36 *	The Regents of the University of California.  All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the University of
49 *	California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 *    may be used to endorse or promote products derived from this software
52 *    without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66
67#include <sys/types.h>
68#include <sys/devicestat.h>
69#include <sys/dkstat.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
80typedef enum {
81	DS_MATCHTYPE_NONE,
82	DS_MATCHTYPE_SPEC,
83	DS_MATCHTYPE_PATTERN
84} last_match_type;
85
86last_match_type last_type;
87struct device_selection *dev_select;
88long generation;
89int num_devices, num_selected;
90int num_selections;
91long select_generation;
92struct devstat_match *matches = NULL;
93int num_matches = 0;
94char **specified_devices;
95int num_devices_specified = 0;
96
97static int dsmatchselect(char *args, devstat_select_mode select_mode,
98			 int maxshowdevs, struct statinfo *s1);
99static int dsselect(char *args, devstat_select_mode select_mode,
100		    int maxshowdevs, struct statinfo *s1);
101
102int
103dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2,
104       struct statinfo *s3)
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 (devstat_checkversion(NULL) < 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 (devstat_getdevs(NULL, 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 (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
136	    &select_generation, generation, s1->dinfo->devices, num_devices,
137	    NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1)
138		errx(1, "%s", devstat_errbuf);
139
140	return(1);
141}
142
143int
144dscmd(char *cmd, char *args, int maxshowdevs, struct statinfo *s1)
145{
146	int retval;
147
148	if (prefix(cmd, "display") || prefix(cmd, "add"))
149		return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1));
150	if (prefix(cmd, "ignore") || prefix(cmd, "delete"))
151		return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1));
152	if (prefix(cmd, "show") || prefix(cmd, "only"))
153		return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
154	if (prefix(cmd, "type") || prefix(cmd, "match"))
155		return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
156	if (prefix(cmd, "refresh")) {
157		retval = devstat_selectdevs(&dev_select, &num_selected,
158		    &num_selections, &select_generation, generation,
159		    s1->dinfo->devices, num_devices,
160		    (last_type ==DS_MATCHTYPE_PATTERN) ?  matches : NULL,
161		    (last_type ==DS_MATCHTYPE_PATTERN) ?  num_matches : 0,
162		    (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL,
163		    (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0,
164		    (last_type == DS_MATCHTYPE_NONE) ?  DS_SELECT_ADD :
165		    DS_SELECT_ADDONLY, maxshowdevs, 0);
166		if (retval == -1) {
167			warnx("%s", devstat_errbuf);
168			return(0);
169		} else if (retval == 1)
170			return(2);
171	}
172	if (prefix(cmd, "drives")) {
173		register int i;
174		move(CMDLINE, 0);
175		clrtoeol();
176		for (i = 0; i < num_devices; i++) {
177			printw("%s%d ", s1->dinfo->devices[i].device_name,
178			       s1->dinfo->devices[i].unit_number);
179		}
180		return(1);
181	}
182	return(0);
183}
184
185static int
186dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
187	      struct statinfo *s1)
188{
189	char **tempstr;
190	char *tstr[100];
191	int num_args = 0;
192	register int i;
193	int retval = 0;
194
195	/*
196	 * Break the (pipe delimited) input string out into separate
197	 * strings.
198	 */
199	for (tempstr = tstr, num_args  = 0;
200	     (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100);
201	     num_args++)
202		if (**tempstr != '\0')
203			if (++tempstr >= &tstr[100])
204				break;
205
206	if (num_args > 99) {
207		warnx("dsmatchselect: too many match arguments");
208		return(0);
209	}
210
211	/*
212	 * If we've gone through the matching code before, clean out
213	 * previously used memory.
214	 */
215	if (num_matches > 0) {
216		free(matches);
217		matches = NULL;
218		num_matches = 0;
219	}
220
221	for (i = 0; i < num_args; i++) {
222		if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
223			warnx("%s", devstat_errbuf);
224			return(0);
225		}
226	}
227	if (num_args > 0) {
228
229		last_type = DS_MATCHTYPE_PATTERN;
230
231		retval = devstat_selectdevs(&dev_select, &num_selected,
232		    &num_selections, &select_generation, generation,
233		    s1->dinfo->devices, num_devices, matches, num_matches,
234		    NULL, 0, select_mode, maxshowdevs, 0);
235		if (retval == -1)
236			err(1, "device selection error");
237		else if (retval == 1)
238			return(2);
239	}
240	return(1);
241}
242
243static int
244dsselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
245	 struct statinfo *s1)
246{
247	register char *cp;
248	register int i;
249	int retval = 0;
250	char *index();
251
252	/*
253	 * If we've gone through this code before, free previously
254	 * allocated resources.
255	 */
256	if (num_devices_specified > 0) {
257		for (i = 0; i < num_devices_specified; i++)
258			free(specified_devices[i]);
259		free(specified_devices);
260		specified_devices = NULL;
261		num_devices_specified = 0;
262	}
263
264	/* do an initial malloc */
265	specified_devices = (char **)malloc(sizeof(char *));
266
267	cp = index(args, '\n');
268	if (cp)
269		*cp = '\0';
270	for (;;) {
271		for (cp = args; *cp && isspace(*cp); cp++)
272			;
273		args = cp;
274		for (; *cp && !isspace(*cp); cp++)
275			;
276		if (*cp)
277			*cp++ = '\0';
278		if (cp - args == 0)
279			break;
280		for (i = 0; i < num_devices; i++) {
281			char tmpstr[80];
282
283			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
284				dev_select[i].unit_number);
285			if (strcmp(args, tmpstr) == 0) {
286
287				num_devices_specified++;
288
289				specified_devices =(char **)realloc(
290						specified_devices,
291						sizeof(char *) *
292						num_devices_specified);
293				specified_devices[num_devices_specified -1]=
294					strdup(args);
295
296				break;
297			}
298		}
299		if (i >= num_devices)
300			error("%s: unknown drive", args);
301		args = cp;
302	}
303
304	if (num_devices_specified > 0) {
305		last_type = DS_MATCHTYPE_SPEC;
306
307		retval = devstat_selectdevs(&dev_select, &num_selected,
308		    &num_selections, &select_generation, generation,
309		    s1->dinfo->devices, num_devices, NULL, 0,
310		    specified_devices, num_devices_specified,
311		    select_mode, maxshowdevs, 0);
312		if (retval == -1)
313			err(1, "%s", devstat_errbuf);
314		else if (retval == 1)
315			return(2);
316	}
317	return(1);
318}
319