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 * $Id$ 29 */ 30 /* 31 * Some code and ideas taken from the old disks.c. 32 * static char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93"; 33 */ 34 /*- 35 * Copyright (c) 1980, 1992, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #include <sys/types.h> 68 #include <sys/devicestat.h> 69 #include <sys/dkstat.h> 70 71 #include <string.h> 72 #include <devstat.h> 73 #include "systat.h" 74 #include "extern.h" 75 76 typedef enum { 77 DS_MATCHTYPE_NONE, 78 DS_MATCHTYPE_SPEC, 79 DS_MATCHTYPE_PATTERN 80 } last_match_type; 81 82 last_match_type last_type; 83 struct device_selection *dev_select; 84 int generation, num_devices, num_selected; 85 int num_selections, select_generation; 86 struct devstat_match *matches = NULL; 87 int num_matches = 0; 88 char **specified_devices; 89 int num_devices_specified = 0; 90 91 static int dsmatchselect(char *args, devstat_select_mode select_mode, 92 int maxshowdevs, struct statinfo *s1); 93 static int dsselect(char *args, devstat_select_mode select_mode, 94 int maxshowdevs, struct statinfo *s1); 95 96 int 97 dsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2, 98 struct statinfo *s3) 99 { 100 101 /* 102 * Make sure that the userland devstat version matches the kernel 103 * devstat version. If not, exit and print a message informing 104 * the user of his mistake. 105 */ 106 if (checkversion() < 0) 107 errx(1, "%s", devstat_errbuf); 108 109 generation = 0; 110 num_devices = 0; 111 num_selected = 0; 112 num_selections = 0; 113 select_generation = 0; 114 last_type = DS_MATCHTYPE_NONE; 115 116 if (getdevs(s1) == -1) 117 errx(1, "%s", devstat_errbuf); 118 119 num_devices = s1->dinfo->numdevs; 120 generation = s1->dinfo->generation; 121 122 dev_select = NULL; 123 124 /* 125 * At this point, selectdevs will almost surely indicate that the 126 * device list has changed, so we don't look for return values of 0 127 * or 1. If we get back -1, though, there is an error. 128 */ 129 if (selectdevs(&dev_select, &num_selected, &num_selections, 130 &select_generation, generation, s1->dinfo->devices, 131 num_devices, NULL, 0, NULL, 0, DS_SELECT_ADD, 132 maxshowdevs, 0) == -1) 133 errx(1, "%s", devstat_errbuf); 134 135 return(1); 136 } 137 138 int 139 dscmd(char *cmd, char *args, int maxshowdevs, struct statinfo *s1) 140 { 141 int retval; 142 143 if (prefix(cmd, "display") || prefix(cmd, "add")) 144 return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1)); 145 if (prefix(cmd, "ignore") || prefix(cmd, "delete")) 146 return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1)); 147 if (prefix(cmd, "show") || prefix(cmd, "only")) 148 return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 149 if (prefix(cmd, "type") || prefix(cmd, "match")) 150 return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 151 if (prefix(cmd, "refresh")) { 152 retval = selectdevs(&dev_select, &num_selected, &num_selections, 153 &select_generation, generation, 154 s1->dinfo->devices, num_devices, 155 (last_type == DS_MATCHTYPE_PATTERN) ? 156 matches : NULL, 157 (last_type == DS_MATCHTYPE_PATTERN) ? 158 num_matches : 0, 159 (last_type == DS_MATCHTYPE_SPEC) ? 160 specified_devices : NULL, 161 (last_type == DS_MATCHTYPE_SPEC) ? 162 num_devices_specified : 0, 163 (last_type == DS_MATCHTYPE_NONE) ? 164 DS_SELECT_ADD : DS_SELECT_ADDONLY, 165 maxshowdevs, 0); 166 if (retval == -1) { 167 warnx("%s", devstat_errbuf); 168 return(0); 169 } else if (retval == 1) 170 return(2); 171 } 172 if (prefix(cmd, "drives")) { 173 register int i; 174 move(CMDLINE, 0); 175 clrtoeol(); 176 for (i = 0; i < num_devices; i++) { 177 printw("%s%d ", s1->dinfo->devices[i].device_name, 178 s1->dinfo->devices[i].unit_number); 179 } 180 return(1); 181 } 182 return(0); 183 } 184 185 static int 186 dsmatchselect(char *args, devstat_select_mode select_mode, int maxshowdevs, 187 struct statinfo *s1) 188 { 189 char **tempstr; 190 char *tstr[100]; 191 char *err_str; 192 int num_args = 0; 193 register int i; 194 int retval = 0; 195 196 /* 197 * Break the (pipe delimited) input string out into separate 198 * strings. 199 */ 200 for (tempstr = tstr, num_args = 0; 201 (*tempstr = strsep(&args, "|")) != NULL && (num_args < 100); 202 num_args++) 203 if (**tempstr != '\0') 204 if (++tempstr >= &tstr[100]) 205 break; 206 207 if (num_args > 99) { 208 warnx("dsmatchselect: too many match arguments"); 209 return(0); 210 } 211 212 /* 213 * If we've gone through the matching code before, clean out 214 * previously used memory. 215 */ 216 if (num_matches > 0) { 217 free(matches); 218 matches = NULL; 219 num_matches = 0; 220 } 221 222 for (i = 0; i < num_args; i++) { 223 if (buildmatch(tstr[i], &matches, &num_matches) != 0) { 224 warnx("%s", devstat_errbuf); 225 return(0); 226 } 227 } 228 if (num_args > 0) { 229 230 last_type = DS_MATCHTYPE_PATTERN; 231 232 retval = selectdevs(&dev_select, &num_selected, &num_selections, 233 &select_generation, generation, 234 s1->dinfo->devices, num_devices, matches, 235 num_matches, NULL, 0, select_mode, 236 maxshowdevs, 0); 237 if (retval == -1) 238 err(1, "device selection error"); 239 else if (retval == 1) 240 return(2); 241 } 242 return(1); 243 } 244 245 static int 246 dsselect(char *args, devstat_select_mode select_mode, int maxshowdevs, 247 struct statinfo *s1) 248 { 249 register char *cp; 250 register int i; 251 int retval = 0; 252 char *index(); 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 cp = index(args, '\n'); 270 if (cp) 271 *cp = '\0'; 272 for (;;) { 273 for (cp = args; *cp && isspace(*cp); cp++) 274 ; 275 args = cp; 276 for (; *cp && !isspace(*cp); cp++) 277 ; 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]; 284 285 sprintf(tmpstr, "%s%d", dev_select[i].device_name, 286 dev_select[i].unit_number); 287 if (strcmp(args, tmpstr) == 0) { 288 289 num_devices_specified++; 290 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); 297 298 break; 299 } 300 } 301 if (i >= num_devices) 302 error("%s: unknown drive", args); 303 args = cp; 304 } 305 306 if (num_devices_specified > 0) { 307 last_type = DS_MATCHTYPE_SPEC; 308 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); 318 } 319 return(1); 320 } 321