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