2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 Kenneth D. Merry.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1980, 1992, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
64 static const char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93";
67 #include <sys/types.h>
68 #include <sys/devicestat.h>
69 #include <sys/resource.h>
87 last_match_type last_type;
88 struct device_selection *dev_select;
90 int num_devices, num_selected;
92 long select_generation;
93 struct devstat_match *matches = NULL;
95 char **specified_devices;
96 int num_devices_specified = 0;
98 static int dsmatchselect(const char *args, devstat_select_mode select_mode,
99 int maxshowdevs, struct statinfo *s1);
100 static int dsselect(const char *args, devstat_select_mode select_mode,
101 int maxshowdevs, struct statinfo *s1);
104 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused,
105 struct statinfo *s3 __unused)
109 * Make sure that the userland devstat version matches the kernel
110 * devstat version. If not, exit and print a message informing
111 * the user of his mistake.
113 if (devstat_checkversion(NULL) < 0)
114 errx(1, "%s", devstat_errbuf);
120 select_generation = 0;
121 last_type = DS_MATCHTYPE_NONE;
123 if (devstat_getdevs(NULL, s1) == -1)
124 errx(1, "%s", devstat_errbuf);
126 num_devices = s1->dinfo->numdevs;
127 generation = s1->dinfo->generation;
132 * At this point, selectdevs will almost surely indicate that the
133 * device list has changed, so we don't look for return values of 0
134 * or 1. If we get back -1, though, there is an error.
136 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
137 &select_generation, generation, s1->dinfo->devices, num_devices,
138 NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1)
139 errx(1, "%d %s", __LINE__, devstat_errbuf);
145 dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1)
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 = devstat_selectdevs(&dev_select, &num_selected,
159 &num_selections, &select_generation, generation,
160 s1->dinfo->devices, num_devices,
161 (last_type ==DS_MATCHTYPE_PATTERN) ? matches : NULL,
162 (last_type ==DS_MATCHTYPE_PATTERN) ? num_matches : 0,
163 (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL,
164 (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0,
165 (last_type == DS_MATCHTYPE_NONE) ? DS_SELECT_ADD :
166 DS_SELECT_ADDONLY, maxshowdevs, 0);
168 warnx("%s", devstat_errbuf);
170 } else if (retval == 1)
173 if (prefix(cmd, "drives")) {
177 for (i = 0; i < num_devices; i++) {
178 printw("%s%d ", s1->dinfo->devices[i].device_name,
179 s1->dinfo->devices[i].unit_number);
187 dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
190 char **tempstr, *tmpstr, *tmpstr1;
197 * Break the (pipe delimited) input string out into separate
200 tmpstr = tmpstr1 = strdup(args);
201 for (tempstr = tstr, num_args = 0;
202 (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100);
204 if (**tempstr != '\0')
205 if (++tempstr >= &tstr[100])
210 warnx("dsmatchselect: too many match arguments");
215 * If we've gone through the matching code before, clean out
216 * previously used memory.
218 if (num_matches > 0) {
224 for (i = 0; i < num_args; i++) {
225 if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
226 warnx("%s", devstat_errbuf);
232 last_type = DS_MATCHTYPE_PATTERN;
234 retval = devstat_selectdevs(&dev_select, &num_selected,
235 &num_selections, &select_generation, generation,
236 s1->dinfo->devices, num_devices, matches, num_matches,
237 NULL, 0, select_mode, maxshowdevs, 0);
239 err(1, "device selection error");
240 else if (retval == 1)
247 dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
250 char *cp, *tmpstr, *tmpstr1, *buffer;
255 * If we've gone through this code before, free previously
256 * allocated resources.
258 if (num_devices_specified > 0) {
259 for (i = 0; i < num_devices_specified; i++)
260 free(specified_devices[i]);
261 free(specified_devices);
262 specified_devices = NULL;
263 num_devices_specified = 0;
266 /* do an initial malloc */
267 specified_devices = (char **)malloc(sizeof(char *));
269 tmpstr = tmpstr1 = strdup(args);
270 cp = strchr(tmpstr1, '\n');
274 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
277 for (; *cp && !isspace(*cp); cp++)
283 for (i = 0; i < num_devices; i++) {
284 asprintf(&buffer, "%s%d", dev_select[i].device_name,
285 dev_select[i].unit_number);
286 if (strcmp(buffer, tmpstr1) == 0) {
288 num_devices_specified++;
290 specified_devices =(char **)realloc(
293 num_devices_specified);
294 specified_devices[num_devices_specified -1]=
303 if (i >= num_devices)
304 error("%s: unknown drive", args);
309 if (num_devices_specified > 0) {
310 last_type = DS_MATCHTYPE_SPEC;
312 retval = devstat_selectdevs(&dev_select, &num_selected,
313 &num_selections, &select_generation, generation,
314 s1->dinfo->devices, num_devices, NULL, 0,
315 specified_devices, num_devices_specified,
316 select_mode, maxshowdevs, 0);
318 err(1, "%s", devstat_errbuf);
319 else if (retval == 1)