2 * Copyright (c) 1998 Kenneth D. Merry.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
32 * Some code and ideas taken from the old disks.c.
33 * static char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93";
36 * Copyright (c) 1980, 1992, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/types.h>
65 #include <sys/devicestat.h>
83 last_match_type last_type
;
84 struct device_selection
*dev_select
;
86 int num_devices
, num_selected
;
88 long select_generation
;
89 struct devstat_match
*matches
= NULL
;
91 char **specified_devices
;
92 int num_devices_specified
= 0;
94 static void dsinit_ignores(int maxshowdevs
, struct statinfo
*s1
);
95 static int dsmatchselect(char *args
, devstat_select_mode select_mode
,
96 int maxshowdevs
, struct statinfo
*s1
);
97 static int dsselect(char *args
, devstat_select_mode select_mode
,
98 int maxshowdevs
, struct statinfo
*s1
);
101 dsinit(int maxshowdevs
, struct statinfo
*s1
, struct statinfo
*s2 __unused
,
102 struct statinfo
*s3 __unused
)
106 * Make sure that the userland devstat version matches the kernel
107 * devstat version. If not, exit and print a message informing
108 * the user of his mistake.
110 if (checkversion() < 0)
111 errx(1, "%s", devstat_errbuf
);
117 select_generation
= 0;
118 last_type
= DS_MATCHTYPE_NONE
;
120 if (getdevs(s1
) == -1)
121 errx(1, "%s", devstat_errbuf
);
123 num_devices
= s1
->dinfo
->numdevs
;
124 generation
= s1
->dinfo
->generation
;
129 * At this point, selectdevs will almost surely indicate that the
130 * device list has changed, so we don't look for return values of 0
131 * or 1. If we get back -1, though, there is an error.
133 if (selectdevs(&dev_select
, &num_selected
, &num_selections
,
134 &select_generation
, generation
, s1
->dinfo
->devices
,
135 num_devices
, NULL
, 0, NULL
, 0, DS_SELECT_ADD
,
136 maxshowdevs
, 0) == -1) {
137 errx(1, "%s", devstat_errbuf
);
139 dsinit_ignores(maxshowdevs
, s1
);
145 * Clean up the device list a bit by removing devices people usually
146 * aren't interested in.
150 dsinit_ignores(int maxshowdevs
, struct statinfo
*s1
)
154 snprintf(tmp
, sizeof(tmp
), "md*");
155 dscmd("ignore", tmp
, maxshowdevs
, s1
);
156 snprintf(tmp
, sizeof(tmp
), "pass*");
157 dscmd("ignore", tmp
, maxshowdevs
, s1
);
158 snprintf(tmp
, sizeof(tmp
), "sg*");
159 dscmd("ignore", tmp
, maxshowdevs
, s1
);
163 dscmd(const char *cmd
, char *args
, int maxshowdevs
, struct statinfo
*s1
)
167 if (prefix(cmd
, "display") || prefix(cmd
, "add"))
168 return(dsselect(args
, DS_SELECT_ADDONLY
, maxshowdevs
, s1
));
169 if (prefix(cmd
, "ignore") || prefix(cmd
, "delete"))
170 return(dsselect(args
, DS_SELECT_REMOVE
, maxshowdevs
, s1
));
171 if (prefix(cmd
, "show") || prefix(cmd
, "only"))
172 return(dsselect(args
, DS_SELECT_ONLY
, maxshowdevs
, s1
));
173 if (prefix(cmd
, "type") || prefix(cmd
, "match"))
174 return(dsmatchselect(args
, DS_SELECT_ONLY
, maxshowdevs
, s1
));
175 if (prefix(cmd
, "refresh")) {
176 retval
= selectdevs(&dev_select
, &num_selected
, &num_selections
,
177 &select_generation
, generation
,
178 s1
->dinfo
->devices
, num_devices
,
179 (last_type
== DS_MATCHTYPE_PATTERN
) ?
181 (last_type
== DS_MATCHTYPE_PATTERN
) ?
183 (last_type
== DS_MATCHTYPE_SPEC
) ?
184 specified_devices
: NULL
,
185 (last_type
== DS_MATCHTYPE_SPEC
) ?
186 num_devices_specified
: 0,
187 (last_type
== DS_MATCHTYPE_NONE
) ?
188 DS_SELECT_ADD
: DS_SELECT_ADDONLY
,
192 * refresh resets the list, ignore some non-useful devices
194 dsinit_ignores(maxshowdevs
, s1
);
197 warnx("%s", devstat_errbuf
);
199 } else if (retval
== 1) {
203 if (prefix(cmd
, "drives")) {
207 for (i
= 0; i
< num_devices
; i
++) {
208 printw("%s%d ", s1
->dinfo
->devices
[i
].device_name
,
209 s1
->dinfo
->devices
[i
].unit_number
);
217 dsmatchselect(char *args
, devstat_select_mode select_mode
, int maxshowdevs
,
227 * Break the (pipe delimited) input string out into separate
230 for (tempstr
= tstr
, num_args
= 0;
231 (*tempstr
= strsep(&args
, "|")) != NULL
&& (num_args
< 100);
233 if (**tempstr
!= '\0')
234 if (++tempstr
>= &tstr
[100])
238 warnx("dsmatchselect: too many match arguments");
243 * If we've gone through the matching code before, clean out
244 * previously used memory.
246 if (num_matches
> 0) {
252 for (i
= 0; i
< num_args
; i
++) {
253 if (buildmatch(tstr
[i
], &matches
, &num_matches
) != 0) {
254 warnx("%s", devstat_errbuf
);
260 last_type
= DS_MATCHTYPE_PATTERN
;
262 retval
= selectdevs(&dev_select
, &num_selected
, &num_selections
,
263 &select_generation
, generation
,
264 s1
->dinfo
->devices
, num_devices
, matches
,
265 num_matches
, NULL
, 0, select_mode
,
268 err(1, "device selection error");
269 else if (retval
== 1)
276 dsselect(char *args
, devstat_select_mode select_mode
, int maxshowdevs
,
285 * If we've gone through this code before, free previously
286 * allocated resources.
288 if (num_devices_specified
> 0) {
289 for (i
= 0; i
< num_devices_specified
; i
++)
290 free(specified_devices
[i
]);
291 free(specified_devices
);
292 specified_devices
= NULL
;
293 num_devices_specified
= 0;
296 /* do an initial malloc */
297 specified_devices
= (char **)malloc(sizeof(char *));
299 cp
= strchr(args
, '\n');
303 for (cp
= args
; *cp
&& isspace(*cp
); cp
++)
306 for (; *cp
&& !isspace(*cp
); cp
++)
319 for (i
= 0; i
< num_devices
; i
++) {
322 sprintf(tmpstr
, "%s%d", dev_select
[i
].device_name
,
323 dev_select
[i
].unit_number
);
324 if (strcmp(args
, tmpstr
) == 0 ||
326 strcmp(args
, dev_select
[i
].device_name
) == 0)) {
327 num_devices_specified
++;
329 specified_devices
=(char **)realloc(
332 num_devices_specified
);
333 specified_devices
[num_devices_specified
- 1] =
338 /* don't complain if device not known */
339 if (i
>= num_devices
)
340 error("%s: unknown drive", args
);
345 if (num_devices_specified
> 0) {
346 last_type
= DS_MATCHTYPE_SPEC
;
348 retval
= selectdevs(&dev_select
, &num_selected
, &num_selections
,
349 &select_generation
, generation
,
350 s1
->dinfo
->devices
, num_devices
, NULL
, 0,
351 specified_devices
, num_devices_specified
,
352 select_mode
, maxshowdevs
, 0);
354 err(1, "%s", devstat_errbuf
);
355 else if (retval
== 1)