ifnet: Add oqdrops statistics
[dragonfly.git] / usr.bin / systat / devs.c
blobf6982a5731f64e0437ec1ade8db35f1c5d942999
1 /*
2 * Copyright (c) 1998 Kenneth D. Merry.
3 * All rights reserved.
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.
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.
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";
35 /*-
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
41 * are met:
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
61 * SUCH DAMAGE.
64 #include <sys/types.h>
65 #include <sys/devicestat.h>
67 #include <string.h>
68 #include <devstat.h>
69 #include <stdlib.h>
70 #include <ctype.h>
71 #include <err.h>
72 #include "systat.h"
73 #include "extern.h"
74 #include "devs.h"
76 typedef enum {
77 DS_MATCHTYPE_NONE,
78 DS_MATCHTYPE_SPEC,
79 DS_MATCHTYPE_PATTERN
80 } last_match_type;
82 last_match_type last_type;
83 struct device_selection *dev_select;
84 long generation;
85 int num_devices, num_selected;
86 int num_selections;
87 long select_generation;
88 struct devstat_match *matches = NULL;
89 int num_matches = 0;
90 char **specified_devices;
91 int num_devices_specified = 0;
93 static int dsmatchselect(char *args, devstat_select_mode select_mode,
94 int maxshowdevs, struct statinfo *s1);
95 static int dsselect(char *args, devstat_select_mode select_mode,
96 int maxshowdevs, struct statinfo *s1);
98 int
99 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused,
100 struct statinfo *s3 __unused)
104 * Make sure that the userland devstat version matches the kernel
105 * devstat version. If not, exit and print a message informing
106 * the user of his mistake.
108 if (checkversion() < 0)
109 errx(1, "%s", devstat_errbuf);
111 generation = 0;
112 num_devices = 0;
113 num_selected = 0;
114 num_selections = 0;
115 select_generation = 0;
116 last_type = DS_MATCHTYPE_NONE;
118 if (getdevs(s1) == -1)
119 errx(1, "%s", devstat_errbuf);
121 num_devices = s1->dinfo->numdevs;
122 generation = s1->dinfo->generation;
124 dev_select = NULL;
127 * At this point, selectdevs will almost surely indicate that the
128 * device list has changed, so we don't look for return values of 0
129 * or 1. If we get back -1, though, there is an error.
131 if (selectdevs(&dev_select, &num_selected, &num_selections,
132 &select_generation, generation, s1->dinfo->devices,
133 num_devices, NULL, 0, NULL, 0, DS_SELECT_ADD,
134 maxshowdevs, 0) == -1)
135 errx(1, "%s", devstat_errbuf);
137 return(1);
141 dscmd(const char *cmd, char *args, int maxshowdevs, struct statinfo *s1)
143 int retval;
145 if (prefix(cmd, "display") || prefix(cmd, "add"))
146 return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1));
147 if (prefix(cmd, "ignore") || prefix(cmd, "delete"))
148 return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1));
149 if (prefix(cmd, "show") || prefix(cmd, "only"))
150 return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
151 if (prefix(cmd, "type") || prefix(cmd, "match"))
152 return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
153 if (prefix(cmd, "refresh")) {
154 retval = selectdevs(&dev_select, &num_selected, &num_selections,
155 &select_generation, generation,
156 s1->dinfo->devices, num_devices,
157 (last_type == DS_MATCHTYPE_PATTERN) ?
158 matches : NULL,
159 (last_type == DS_MATCHTYPE_PATTERN) ?
160 num_matches : 0,
161 (last_type == DS_MATCHTYPE_SPEC) ?
162 specified_devices : NULL,
163 (last_type == DS_MATCHTYPE_SPEC) ?
164 num_devices_specified : 0,
165 (last_type == DS_MATCHTYPE_NONE) ?
166 DS_SELECT_ADD : DS_SELECT_ADDONLY,
167 maxshowdevs, 0);
168 if (retval == -1) {
169 warnx("%s", devstat_errbuf);
170 return(0);
171 } else if (retval == 1)
172 return(2);
174 if (prefix(cmd, "drives")) {
175 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);
182 return(1);
184 return(0);
187 static int
188 dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
189 struct statinfo *s1)
191 char **tempstr;
192 char *tstr[100];
193 int num_args = 0;
194 int i;
195 int retval = 0;
198 * Break the (pipe delimited) input string out into separate
199 * strings.
201 for (tempstr = tstr, num_args = 0;
202 (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100);
203 num_args++)
204 if (**tempstr != '\0')
205 if (++tempstr >= &tstr[100])
206 break;
208 if (num_args > 99) {
209 warnx("dsmatchselect: too many match arguments");
210 return(0);
214 * If we've gone through the matching code before, clean out
215 * previously used memory.
217 if (num_matches > 0) {
218 free(matches);
219 matches = NULL;
220 num_matches = 0;
223 for (i = 0; i < num_args; i++) {
224 if (buildmatch(tstr[i], &matches, &num_matches) != 0) {
225 warnx("%s", devstat_errbuf);
226 return(0);
229 if (num_args > 0) {
231 last_type = DS_MATCHTYPE_PATTERN;
233 retval = selectdevs(&dev_select, &num_selected, &num_selections,
234 &select_generation, generation,
235 s1->dinfo->devices, num_devices, matches,
236 num_matches, NULL, 0, select_mode,
237 maxshowdevs, 0);
238 if (retval == -1)
239 err(1, "device selection error");
240 else if (retval == 1)
241 return(2);
243 return(1);
246 static int
247 dsselect(char *args, devstat_select_mode select_mode, int maxshowdevs,
248 struct statinfo *s1)
250 char *cp;
251 int i;
252 int retval = 0;
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 cp = strchr(args, '\n');
270 if (cp)
271 *cp = '\0';
272 for (;;) {
273 for (cp = args; *cp && isspace(*cp); cp++)
275 args = cp;
276 for (; *cp && !isspace(*cp); cp++)
278 if (*cp)
279 *cp++ = '\0';
280 if (cp - args == 0)
281 break;
282 for (i = 0; i < num_devices; i++) {
283 char tmpstr[80];
285 sprintf(tmpstr, "%s%d", dev_select[i].device_name,
286 dev_select[i].unit_number);
287 if (strcmp(args, tmpstr) == 0) {
289 num_devices_specified++;
291 specified_devices =(char **)realloc(
292 specified_devices,
293 sizeof(char *) *
294 num_devices_specified);
295 specified_devices[num_devices_specified -1]=
296 strdup(args);
298 break;
301 if (i >= num_devices)
302 error("%s: unknown drive", args);
303 args = cp;
306 if (num_devices_specified > 0) {
307 last_type = DS_MATCHTYPE_SPEC;
309 retval = selectdevs(&dev_select, &num_selected, &num_selections,
310 &select_generation, generation,
311 s1->dinfo->devices, num_devices, NULL, 0,
312 specified_devices, num_devices_specified,
313 select_mode, maxshowdevs, 0);
314 if (retval == -1)
315 err(1, "%s", devstat_errbuf);
316 else if (retval == 1)
317 return(2);
319 return(1);