1 /* 2 * Copyright (c) 1998 Kenneth D. Merry. 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 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 $ 30 */ 31 /* 32 * Some code and ideas taken from the old disks.c. 33 * static char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93"; 34 */ 35 /*- 36 * Copyright (c) 1980, 1992, 1993 37 * The Regents of the University of California. All rights reserved. 38 * 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. 50 * 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. 62 */ 63 64 #include <sys/types.h> 65 #include <sys/devicestat.h> 66 67 #include <string.h> 68 #include <devstat.h> 69 #include <stdlib.h> 70 #include <unistd.h> 71 #include <ctype.h> 72 #include <err.h> 73 #include "systat.h" 74 #include "extern.h" 75 #include "devs.h" 76 77 typedef enum { 78 DS_MATCHTYPE_NONE, 79 DS_MATCHTYPE_SPEC, 80 DS_MATCHTYPE_PATTERN 81 } last_match_type; 82 83 last_match_type last_type; 84 struct device_selection *dev_select; 85 long generation; 86 int num_devices, num_selected; 87 int num_selections; 88 long select_generation; 89 struct devstat_match *matches = NULL; 90 int num_matches = 0; 91 char **specified_devices; 92 int num_devices_specified = 0; 93 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); 99 100 int 101 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused, 102 struct statinfo *s3 __unused) 103 { 104 105 /* 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. 109 */ 110 if (checkversion() < 0) 111 errx(1, "%s", devstat_errbuf); 112 113 generation = 0; 114 num_devices = 0; 115 num_selected = 0; 116 num_selections = 0; 117 select_generation = 0; 118 last_type = DS_MATCHTYPE_NONE; 119 120 if (getdevs(s1) == -1) 121 errx(1, "%s", devstat_errbuf); 122 123 num_devices = s1->dinfo->numdevs; 124 generation = s1->dinfo->generation; 125 126 dev_select = NULL; 127 128 /* 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. 132 */ 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); 138 } 139 dsinit_ignores(maxshowdevs, s1); 140 141 return(1); 142 } 143 144 /* 145 * Clean up the device list a bit by removing devices people usually 146 * aren't interested in. 147 */ 148 static 149 void 150 dsinit_ignores(int maxshowdevs, struct statinfo *s1) 151 { 152 char tmp[64]; 153 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); 160 } 161 162 int 163 dscmd(const char *cmd, char *args, int maxshowdevs, struct statinfo *s1) 164 { 165 int retval; 166 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) ? 180 matches : NULL, 181 (last_type == DS_MATCHTYPE_PATTERN) ? 182 num_matches : 0, 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, 189 maxshowdevs, 0); 190 191 /* 192 * refresh resets the list, ignore some non-useful devices 193 */ 194 dsinit_ignores(maxshowdevs, s1); 195 196 if (retval == -1) { 197 warnx("%s", devstat_errbuf); 198 return(0); 199 } else if (retval == 1) { 200 return(2); 201 } 202 } 203 if (prefix(cmd, "drives")) { 204 int i; 205 move(CMDLINE, 0); 206 clrtoeol(); 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); 210 } 211 return(1); 212 } 213 return(0); 214 } 215 216 static int 217 dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs, 218 struct statinfo *s1) 219 { 220 char **tempstr; 221 char *tstr[100]; 222 int num_args = 0; 223 int i; 224 int retval = 0; 225 226 /* 227 * Break the (pipe delimited) input string out into separate 228 * strings. 229 */ 230 for (tempstr = tstr, num_args = 0; 231 (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100); 232 num_args++) 233 if (**tempstr != '\0') 234 if (++tempstr >= &tstr[100]) 235 break; 236 237 if (num_args > 99) { 238 warnx("dsmatchselect: too many match arguments"); 239 return(0); 240 } 241 242 /* 243 * If we've gone through the matching code before, clean out 244 * previously used memory. 245 */ 246 if (num_matches > 0) { 247 free(matches); 248 matches = NULL; 249 num_matches = 0; 250 } 251 252 for (i = 0; i < num_args; i++) { 253 if (buildmatch(tstr[i], &matches, &num_matches) != 0) { 254 warnx("%s", devstat_errbuf); 255 return(0); 256 } 257 } 258 if (num_args > 0) { 259 260 last_type = DS_MATCHTYPE_PATTERN; 261 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, 266 maxshowdevs, 0); 267 if (retval == -1) 268 err(1, "device selection error"); 269 else if (retval == 1) 270 return(2); 271 } 272 return(1); 273 } 274 275 static int 276 dsselect(char *args, devstat_select_mode select_mode, int maxshowdevs, 277 struct statinfo *s1) 278 { 279 char *cp; 280 int i; 281 int retval = 0; 282 int iswild; 283 284 /* 285 * If we've gone through this code before, free previously 286 * allocated resources. 287 */ 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; 294 } 295 296 /* do an initial malloc */ 297 specified_devices = (char **)malloc(sizeof(char *)); 298 299 cp = strchr(args, '\n'); 300 if (cp) 301 *cp = '\0'; 302 for (;;) { 303 for (cp = args; *cp && isspace(*cp); cp++) 304 ; 305 args = cp; 306 for (; *cp && !isspace(*cp); cp++) 307 ; 308 if (*cp) 309 *cp++ = '\0'; 310 if (cp - args == 0) 311 break; 312 if (cp[-1] == '*') { 313 cp[-1] = 0; 314 iswild = 1; 315 } else { 316 iswild = 0; 317 } 318 319 for (i = 0; i < num_devices; i++) { 320 char tmpstr[80]; 321 322 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 323 dev_select[i].unit_number); 324 if (strcmp(args, tmpstr) == 0 || 325 (iswild && 326 strcmp(args, dev_select[i].device_name) == 0)) { 327 num_devices_specified++; 328 329 specified_devices =(char **)realloc( 330 specified_devices, 331 sizeof(char *) * 332 num_devices_specified); 333 specified_devices[num_devices_specified - 1] = 334 strdup(tmpstr); 335 } 336 } 337 #if 0 338 /* don't complain if device not known */ 339 if (i >= num_devices) 340 error("%s: unknown drive", args); 341 #endif 342 args = cp; 343 } 344 345 if (num_devices_specified > 0) { 346 last_type = DS_MATCHTYPE_SPEC; 347 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); 353 if (retval == -1) 354 err(1, "%s", devstat_errbuf); 355 else if (retval == 1) 356 return(2); 357 } 358 return(1); 359 } 360