xref: /freebsd/usr.bin/systat/devs.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
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 	/*
197 	 * Break the (pipe delimited) input string out into separate
198 	 * strings.
199 	 */
200 	tmpstr = tmpstr1 = strdup(args);
201 	for (tempstr = tstr, num_args  = 0;
202 	     (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100);
203 	     num_args++)
204 		if (**tempstr != '\0')
205 			if (++tempstr >= &tstr[100])
206 				break;
207 	free(tmpstr);
208 
209 	if (num_args > 99) {
210 		warnx("dsmatchselect: too many match arguments");
211 		return(0);
212 	}
213 
214 	/*
215 	 * If we've gone through the matching code before, clean out
216 	 * previously used memory.
217 	 */
218 	if (num_matches > 0) {
219 		free(matches);
220 		matches = NULL;
221 		num_matches = 0;
222 	}
223 
224 	for (i = 0; i < num_args; i++) {
225 		if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
226 			warnx("%s", devstat_errbuf);
227 			return(0);
228 		}
229 	}
230 	if (num_args > 0) {
231 
232 		last_type = DS_MATCHTYPE_PATTERN;
233 
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);
238 		if (retval == -1)
239 			err(1, "device selection error");
240 		else if (retval == 1)
241 			return(2);
242 	}
243 	return(1);
244 }
245 
246 static int
247 dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
248 	 struct statinfo *s1)
249 {
250 	char *cp, *tmpstr, *tmpstr1, *buffer;
251 	int i;
252 	int retval = 0;
253 
254 	/*
255 	 * If we've gone through this code before, free previously
256 	 * allocated resources.
257 	 */
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;
264 	}
265 
266 	/* do an initial malloc */
267 	specified_devices = (char **)malloc(sizeof(char *));
268 
269 	tmpstr = tmpstr1 = strdup(args);
270 	cp = strchr(tmpstr1, '\n');
271 	if (cp)
272 		*cp = '\0';
273 	for (;;) {
274 		for (cp = tmpstr1; *cp && isspace(*cp); cp++)
275 			;
276 		tmpstr1 = cp;
277 		for (; *cp && !isspace(*cp); cp++)
278 			;
279 		if (*cp)
280 			*cp++ = '\0';
281 		if (cp - args == 0)
282 			break;
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) {
287 
288 				num_devices_specified++;
289 
290 				specified_devices =(char **)realloc(
291 						specified_devices,
292 						sizeof(char *) *
293 						num_devices_specified);
294 				specified_devices[num_devices_specified -1]=
295 					strdup(tmpstr1);
296 				free(buffer);
297 
298 				break;
299 			}
300 			else
301 				free(buffer);
302 		}
303 		if (i >= num_devices)
304 			error("%s: unknown drive", args);
305 		args = cp;
306 	}
307 	free(tmpstr);
308 
309 	if (num_devices_specified > 0) {
310 		last_type = DS_MATCHTYPE_SPEC;
311 
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);
317 		if (retval == -1)
318 			err(1, "%s", devstat_errbuf);
319 		else if (retval == 1)
320 			return(2);
321 	}
322 	return(1);
323 }
324