]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/systat/devs.c
Add myself to committers-ports.dot and calendar.freebsd
[FreeBSD/FreeBSD.git] / usr.bin / systat / devs.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Kenneth D. Merry.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 /*-
31  * Copyright (c) 1980, 1992, 1993
32  *      The Regents of the University of California.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
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.
45  *
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
56  * SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60
61 __FBSDID("$FreeBSD$");
62
63 #ifdef lint
64 static const char sccsid[] = "@(#)disks.c       8.1 (Berkeley) 6/6/93";
65 #endif
66
67 #include <sys/types.h>
68 #include <sys/devicestat.h>
69 #include <sys/resource.h>
70
71 #include <ctype.h>
72 #include <devstat.h>
73 #include <err.h>
74 #include <stdlib.h>
75 #include <string.h>
76
77 #include "systat.h"
78 #include "extern.h"
79 #include "devs.h"
80
81 typedef enum {
82         DS_MATCHTYPE_NONE,
83         DS_MATCHTYPE_SPEC,
84         DS_MATCHTYPE_PATTERN
85 } last_match_type;
86
87 last_match_type last_type;
88 struct device_selection *dev_select;
89 long generation;
90 int num_devices, num_selected;
91 int num_selections;
92 long select_generation;
93 struct devstat_match *matches = NULL;
94 int num_matches = 0;
95 char **specified_devices;
96 int num_devices_specified = 0;
97
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);
102
103 int
104 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused,
105        struct statinfo *s3 __unused)
106 {
107
108         /*
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.
112          */
113         if (devstat_checkversion(NULL) < 0)
114                 errx(1, "%s", devstat_errbuf);
115
116         generation = 0;
117         num_devices = 0;
118         num_selected = 0;
119         num_selections = 0;
120         select_generation = 0;
121         last_type = DS_MATCHTYPE_NONE;
122
123         if (devstat_getdevs(NULL, s1) == -1)
124                 errx(1, "%s", devstat_errbuf);
125
126         num_devices = s1->dinfo->numdevs;
127         generation = s1->dinfo->generation;
128
129         dev_select = NULL;
130
131         /*
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.
135          */
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);
140
141         return(1);
142 }
143
144 int
145 dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1)
146 {
147         int retval;
148
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);
167                 if (retval == -1) {
168                         warnx("%s", devstat_errbuf);
169                         return(0);
170                 } else if (retval == 1)
171                         return(2);
172         }
173         if (prefix(cmd, "drives")) {
174                 int i;
175                 move(CMDLINE, 0);
176                 clrtoeol();
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);
180                 }
181                 return(1);
182         }
183         return(0);
184 }
185
186 static int
187 dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
188               struct statinfo *s1)
189 {
190         char **tempstr, *tmpstr, *tmpstr1;
191         char *tstr[100];
192         int num_args = 0;
193         int i;
194         int retval = 0;
195
196         if (!args) {
197                 warnx("dsmatchselect: no arguments");
198                 return(1);
199         }
200
201         /*
202          * Break the (pipe delimited) input string out into separate
203          * strings.
204          */
205         tmpstr = tmpstr1 = strdup(args);
206         for (tempstr = tstr, num_args  = 0;
207              (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100);
208              num_args++)
209                 if (**tempstr != '\0')
210                         if (++tempstr >= &tstr[100])
211                                 break;
212         free(tmpstr);
213
214         if (num_args > 99) {
215                 warnx("dsmatchselect: too many match arguments");
216                 return(0);
217         }
218
219         /*
220          * If we've gone through the matching code before, clean out
221          * previously used memory.
222          */
223         if (num_matches > 0) {
224                 free(matches);
225                 matches = NULL;
226                 num_matches = 0;
227         }
228
229         for (i = 0; i < num_args; i++) {
230                 if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
231                         warnx("%s", devstat_errbuf);
232                         return(0);
233                 }
234         }
235         if (num_args > 0) {
236
237                 last_type = DS_MATCHTYPE_PATTERN;
238
239                 retval = devstat_selectdevs(&dev_select, &num_selected,
240                     &num_selections, &select_generation, generation,
241                     s1->dinfo->devices, num_devices, matches, num_matches,
242                     NULL, 0, select_mode, maxshowdevs, 0);
243                 if (retval == -1)
244                         err(1, "device selection error");
245                 else if (retval == 1)
246                         return(2);
247         }
248         return(1);
249 }
250
251 static int
252 dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
253          struct statinfo *s1)
254 {
255         char *cp, *tmpstr, *tmpstr1, *buffer;
256         int i;
257         int retval = 0;
258
259         if (!args) {
260                 warnx("dsselect: no argument");
261                 return(1);
262         }
263
264         /*
265          * If we've gone through this code before, free previously
266          * allocated resources.
267          */
268         if (num_devices_specified > 0) {
269                 for (i = 0; i < num_devices_specified; i++)
270                         free(specified_devices[i]);
271                 free(specified_devices);
272                 specified_devices = NULL;
273                 num_devices_specified = 0;
274         }
275
276         /* do an initial malloc */
277         specified_devices = (char **)malloc(sizeof(char *));
278
279         tmpstr = tmpstr1 = strdup(args);
280         cp = strchr(tmpstr1, '\n');
281         if (cp)
282                 *cp = '\0';
283         for (;;) {
284                 for (cp = tmpstr1; *cp && isspace(*cp); cp++)
285                         ;
286                 tmpstr1 = cp;
287                 for (; *cp && !isspace(*cp); cp++)
288                         ;
289                 if (*cp)
290                         *cp++ = '\0';
291                 if (cp - tmpstr1 == 0)
292                         break;
293                 for (i = 0; i < num_devices; i++) {
294                         asprintf(&buffer, "%s%d", dev_select[i].device_name,
295                                 dev_select[i].unit_number);
296                         if (strcmp(buffer, tmpstr1) == 0) {
297
298                                 num_devices_specified++;
299
300                                 specified_devices =(char **)realloc(
301                                                 specified_devices,
302                                                 sizeof(char *) *
303                                                 num_devices_specified);
304                                 specified_devices[num_devices_specified -1]=
305                                         strdup(tmpstr1);
306                                 free(buffer);
307
308                                 break;
309                         }
310                         else
311                                 free(buffer);
312                 }
313                 if (i >= num_devices)
314                         error("%s: unknown drive", args);
315                 tmpstr1 = cp;
316         }
317         free(tmpstr);
318
319         if (num_devices_specified > 0) {
320                 last_type = DS_MATCHTYPE_SPEC;
321
322                 retval = devstat_selectdevs(&dev_select, &num_selected,
323                     &num_selections, &select_generation, generation,
324                     s1->dinfo->devices, num_devices, NULL, 0,
325                     specified_devices, num_devices_specified,
326                     select_mode, maxshowdevs, 0);
327                 if (retval == -1)
328                         err(1, "%s", devstat_errbuf);
329                 else if (retval == 1)
330                         return(2);
331         }
332         return(1);
333 }