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.5 2004/09/03 20:38:01 dillon 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. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * 4. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68 #include <sys/types.h> 69 #include <sys/devicestat.h> 70 #include <sys/dkstat.h> 71 72 #include <string.h> 73 #include <devstat.h> 74 #include <stdlib.h> 75 #include <ctype.h> 76 #include <err.h> 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(char *args, devstat_select_mode select_mode, 99 int maxshowdevs, struct statinfo *s1); 100 static int dsselect(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, 105 struct statinfo *s3) 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 (checkversion() < 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 (getdevs(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 (selectdevs(&dev_select, &num_selected, &num_selections, 137 &select_generation, generation, s1->dinfo->devices, 138 num_devices, NULL, 0, NULL, 0, DS_SELECT_ADD, 139 maxshowdevs, 0) == -1) 140 errx(1, "%s", devstat_errbuf); 141 142 return(1); 143 } 144 145 int 146 dscmd(char *cmd, char *args, int maxshowdevs, struct statinfo *s1) 147 { 148 int retval; 149 150 if (prefix(cmd, "display") || prefix(cmd, "add")) 151 return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1)); 152 if (prefix(cmd, "ignore") || prefix(cmd, "delete")) 153 return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1)); 154 if (prefix(cmd, "show") || prefix(cmd, "only")) 155 return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 156 if (prefix(cmd, "type") || prefix(cmd, "match")) 157 return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 158 if (prefix(cmd, "refresh")) { 159 retval = selectdevs(&dev_select, &num_selected, &num_selections, 160 &select_generation, generation, 161 s1->dinfo->devices, num_devices, 162 (last_type == DS_MATCHTYPE_PATTERN) ? 163 matches : NULL, 164 (last_type == DS_MATCHTYPE_PATTERN) ? 165 num_matches : 0, 166 (last_type == DS_MATCHTYPE_SPEC) ? 167 specified_devices : NULL, 168 (last_type == DS_MATCHTYPE_SPEC) ? 169 num_devices_specified : 0, 170 (last_type == DS_MATCHTYPE_NONE) ? 171 DS_SELECT_ADD : DS_SELECT_ADDONLY, 172 maxshowdevs, 0); 173 if (retval == -1) { 174 warnx("%s", devstat_errbuf); 175 return(0); 176 } else if (retval == 1) 177 return(2); 178 } 179 if (prefix(cmd, "drives")) { 180 register int i; 181 move(CMDLINE, 0); 182 clrtoeol(); 183 for (i = 0; i < num_devices; i++) { 184 printw("%s%d ", s1->dinfo->devices[i].device_name, 185 s1->dinfo->devices[i].unit_number); 186 } 187 return(1); 188 } 189 return(0); 190 } 191 192 static int 193 dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs, 194 struct statinfo *s1) 195 { 196 char **tempstr; 197 char *tstr[100]; 198 int num_args = 0; 199 register int i; 200 int retval = 0; 201 202 /* 203 * Break the (pipe delimited) input string out into separate 204 * strings. 205 */ 206 for (tempstr = tstr, num_args = 0; 207 (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100); 208 num_args++) 209 if (**tempstr != '\0') 210 if (++tempstr >= &tstr[100]) 211 break; 212 213 if (num_args > 99) { 214 warnx("dsmatchselect: too many match arguments"); 215 return(0); 216 } 217 218 /* 219 * If we've gone through the matching code before, clean out 220 * previously used memory. 221 */ 222 if (num_matches > 0) { 223 free(matches); 224 matches = NULL; 225 num_matches = 0; 226 } 227 228 for (i = 0; i < num_args; i++) { 229 if (buildmatch(tstr[i], &matches, &num_matches) != 0) { 230 warnx("%s", devstat_errbuf); 231 return(0); 232 } 233 } 234 if (num_args > 0) { 235 236 last_type = DS_MATCHTYPE_PATTERN; 237 238 retval = selectdevs(&dev_select, &num_selected, &num_selections, 239 &select_generation, generation, 240 s1->dinfo->devices, num_devices, matches, 241 num_matches, NULL, 0, select_mode, 242 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(char *args, devstat_select_mode select_mode, int maxshowdevs, 253 struct statinfo *s1) 254 { 255 register char *cp; 256 register int i; 257 int retval = 0; 258 259 /* 260 * If we've gone through this code before, free previously 261 * allocated resources. 262 */ 263 if (num_devices_specified > 0) { 264 for (i = 0; i < num_devices_specified; i++) 265 free(specified_devices[i]); 266 free(specified_devices); 267 specified_devices = NULL; 268 num_devices_specified = 0; 269 } 270 271 /* do an initial malloc */ 272 specified_devices = (char **)malloc(sizeof(char *)); 273 274 cp = strchr(args, '\n'); 275 if (cp) 276 *cp = '\0'; 277 for (;;) { 278 for (cp = args; *cp && isspace(*cp); cp++) 279 ; 280 args = cp; 281 for (; *cp && !isspace(*cp); cp++) 282 ; 283 if (*cp) 284 *cp++ = '\0'; 285 if (cp - args == 0) 286 break; 287 for (i = 0; i < num_devices; i++) { 288 char tmpstr[80]; 289 290 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 291 dev_select[i].unit_number); 292 if (strcmp(args, tmpstr) == 0) { 293 294 num_devices_specified++; 295 296 specified_devices =(char **)realloc( 297 specified_devices, 298 sizeof(char *) * 299 num_devices_specified); 300 specified_devices[num_devices_specified -1]= 301 strdup(args); 302 303 break; 304 } 305 } 306 if (i >= num_devices) 307 error("%s: unknown drive", args); 308 args = cp; 309 } 310 311 if (num_devices_specified > 0) { 312 last_type = DS_MATCHTYPE_SPEC; 313 314 retval = selectdevs(&dev_select, &num_selected, &num_selections, 315 &select_generation, generation, 316 s1->dinfo->devices, num_devices, NULL, 0, 317 specified_devices, num_devices_specified, 318 select_mode, maxshowdevs, 0); 319 if (retval == -1) 320 err(1, "%s", devstat_errbuf); 321 else if (retval == 1) 322 return(2); 323 } 324 return(1); 325 } 326