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