devs.c revision 87715
134925Sdufault/* 234925Sdufault * Copyright (c) 1998 Kenneth D. Merry. 334030Sdufault * All rights reserved. 434030Sdufault * 534030Sdufault * Redistribution and use in source and binary forms, with or without 634030Sdufault * modification, are permitted provided that the following conditions 734030Sdufault * are met: 834030Sdufault * 1. Redistributions of source code must retain the above copyright 934030Sdufault * notice, this list of conditions and the following disclaimer. 1034030Sdufault * 2. Redistributions in binary form must reproduce the above copyright 1134030Sdufault * notice, this list of conditions and the following disclaimer in the 1234030Sdufault * documentation and/or other materials provided with the distribution. 1334030Sdufault * 3. The name of the author may not be used to endorse or promote products 1434030Sdufault * derived from this software without specific prior written permission. 1534030Sdufault * 1634030Sdufault * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1734030Sdufault * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1834030Sdufault * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1934030Sdufault * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2034030Sdufault * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2134030Sdufault * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2234030Sdufault * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2334030Sdufault * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2434030Sdufault * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2534030Sdufault * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2634030Sdufault * SUCH DAMAGE. 2734030Sdufault */ 2834030Sdufault/*- 2934030Sdufault * Copyright (c) 1980, 1992, 1993 3034030Sdufault * The Regents of the University of California. All rights reserved. 3134030Sdufault * 3234030Sdufault * Redistribution and use in source and binary forms, with or without 3334030Sdufault * modification, are permitted provided that the following conditions 3455140Sbde * are met: 3534030Sdufault * 1. Redistributions of source code must retain the above copyright 3634030Sdufault * notice, this list of conditions and the following disclaimer. 3734030Sdufault * 2. Redistributions in binary form must reproduce the above copyright 3834030Sdufault * notice, this list of conditions and the following disclaimer in the 3934925Sdufault * documentation and/or other materials provided with the distribution. 4034925Sdufault * 3. All advertising materials mentioning features or use of this software 4134030Sdufault * must display the following acknowledgement: 4234925Sdufault * This product includes software developed by the University of 4345739Speter * California, Berkeley and its contributors. 4434925Sdufault * 4. Neither the name of the University nor the names of its contributors 4534030Sdufault * may be used to endorse or promote products derived from this software 4634925Sdufault * without specific prior written permission. 4734925Sdufault * 4883366Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4934030Sdufault * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5034925Sdufault * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5183366Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5234925Sdufault * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5383366Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5434925Sdufault * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5534030Sdufault * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5634030Sdufault * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5734925Sdufault * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5834030Sdufault * SUCH DAMAGE. 59111119Simp */ 6034925Sdufault 6134925Sdufault#include <sys/cdefs.h> 6292727Salfred 6334925Sdufault__FBSDID("$FreeBSD: head/usr.bin/systat/devs.c 87715 2001-12-12 00:13:37Z markm $"); 6492727Salfred 65103574Salfred#ifdef lint 66106998Salfredstatic const char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93"; 6734925Sdufault#endif 6834925Sdufault 6934925Sdufault#include <sys/types.h> 7034030Sdufault#include <sys/devicestat.h> 7134030Sdufault#include <sys/dkstat.h> 7234030Sdufault 7334030Sdufault#include <ctype.h> 7434030Sdufault#include <devstat.h> 7534030Sdufault#include <err.h> 7634030Sdufault#include <stdlib.h> 7734030Sdufault#include <string.h> 7834030Sdufault 7934030Sdufault#include "systat.h" 8034030Sdufault#include "extern.h" 8134030Sdufault#include "devs.h" 8234030Sdufault 8334030Sdufaulttypedef enum { 8434030Sdufault DS_MATCHTYPE_NONE, 8534030Sdufault DS_MATCHTYPE_SPEC, 8634030Sdufault DS_MATCHTYPE_PATTERN 8734030Sdufault} last_match_type; 8834030Sdufault 8934030Sdufaultlast_match_type last_type; 9034030Sdufaultstruct device_selection *dev_select; 9134030Sdufaultlong generation; 9234925Sdufaultint num_devices, num_selected; 9334030Sdufaultint num_selections; 9434925Sdufaultlong select_generation; 9534925Sdufaultstruct devstat_match *matches = NULL; 9634030Sdufaultint num_matches = 0; 97160257Sdavidxuchar **specified_devices; 9883366Sjulianint num_devices_specified = 0; 99160257Sdavidxu 10083366Sjulianstatic int dsmatchselect(const char *args, devstat_select_mode select_mode, 10134030Sdufault int maxshowdevs, struct statinfo *s1); 102160257Sdavidxustatic int dsselect(const char *args, devstat_select_mode select_mode, 10383366Sjulian int maxshowdevs, struct statinfo *s1); 104160257Sdavidxu 10534030Sdufaultint 106160257Sdavidxudsinit(int maxshowdevs, struct statinfo *s1, struct statinfo *s2 __unused, 10734030Sdufault struct statinfo *s3 __unused) 108160257Sdavidxu{ 109160257Sdavidxu 11034030Sdufault /* 111160257Sdavidxu * Make sure that the userland devstat version matches the kernel 11283366Sjulian * devstat version. If not, exit and print a message informing 11334030Sdufault * the user of his mistake. 11434925Sdufault */ 11534030Sdufault if (devstat_checkversion(NULL) < 0) 11634925Sdufault errx(1, "%s", devstat_errbuf); 117 118 generation = 0; 119 num_devices = 0; 120 num_selected = 0; 121 num_selections = 0; 122 select_generation = 0; 123 last_type = DS_MATCHTYPE_NONE; 124 125 if (devstat_getdevs(NULL, s1) == -1) 126 errx(1, "%s", devstat_errbuf); 127 128 num_devices = s1->dinfo->numdevs; 129 generation = s1->dinfo->generation; 130 131 dev_select = NULL; 132 133 /* 134 * At this point, selectdevs will almost surely indicate that the 135 * device list has changed, so we don't look for return values of 0 136 * or 1. If we get back -1, though, there is an error. 137 */ 138 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections, 139 &select_generation, generation, s1->dinfo->devices, num_devices, 140 NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1) 141 errx(1, "%s", devstat_errbuf); 142 143 return(1); 144} 145 146int 147dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1) 148{ 149 int retval; 150 151 if (prefix(cmd, "display") || prefix(cmd, "add")) 152 return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1)); 153 if (prefix(cmd, "ignore") || prefix(cmd, "delete")) 154 return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1)); 155 if (prefix(cmd, "show") || prefix(cmd, "only")) 156 return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 157 if (prefix(cmd, "type") || prefix(cmd, "match")) 158 return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1)); 159 if (prefix(cmd, "refresh")) { 160 retval = devstat_selectdevs(&dev_select, &num_selected, 161 &num_selections, &select_generation, generation, 162 s1->dinfo->devices, num_devices, 163 (last_type ==DS_MATCHTYPE_PATTERN) ? matches : NULL, 164 (last_type ==DS_MATCHTYPE_PATTERN) ? num_matches : 0, 165 (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL, 166 (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0, 167 (last_type == DS_MATCHTYPE_NONE) ? DS_SELECT_ADD : 168 DS_SELECT_ADDONLY, maxshowdevs, 0); 169 if (retval == -1) { 170 warnx("%s", devstat_errbuf); 171 return(0); 172 } else if (retval == 1) 173 return(2); 174 } 175 if (prefix(cmd, "drives")) { 176 register int i; 177 move(CMDLINE, 0); 178 clrtoeol(); 179 for (i = 0; i < num_devices; i++) { 180 printw("%s%d ", s1->dinfo->devices[i].device_name, 181 s1->dinfo->devices[i].unit_number); 182 } 183 return(1); 184 } 185 return(0); 186} 187 188static int 189dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, 190 struct statinfo *s1) 191{ 192 char **tempstr, *tmpstr, *tmpstr1; 193 char *tstr[100]; 194 int num_args = 0; 195 int i; 196 int retval = 0; 197 198 /* 199 * Break the (pipe delimited) input string out into separate 200 * strings. 201 */ 202 tmpstr = tmpstr1 = strdup(args); 203 for (tempstr = tstr, num_args = 0; 204 (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100); 205 num_args++) 206 if (**tempstr != '\0') 207 if (++tempstr >= &tstr[100]) 208 break; 209 free(tmpstr); 210 211 if (num_args > 99) { 212 warnx("dsmatchselect: too many match arguments"); 213 return(0); 214 } 215 216 /* 217 * If we've gone through the matching code before, clean out 218 * previously used memory. 219 */ 220 if (num_matches > 0) { 221 free(matches); 222 matches = NULL; 223 num_matches = 0; 224 } 225 226 for (i = 0; i < num_args; i++) { 227 if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) { 228 warnx("%s", devstat_errbuf); 229 return(0); 230 } 231 } 232 if (num_args > 0) { 233 234 last_type = DS_MATCHTYPE_PATTERN; 235 236 retval = devstat_selectdevs(&dev_select, &num_selected, 237 &num_selections, &select_generation, generation, 238 s1->dinfo->devices, num_devices, matches, num_matches, 239 NULL, 0, select_mode, maxshowdevs, 0); 240 if (retval == -1) 241 err(1, "device selection error"); 242 else if (retval == 1) 243 return(2); 244 } 245 return(1); 246} 247 248static int 249dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs, 250 struct statinfo *s1) 251{ 252 char *cp, *tmpstr, *tmpstr1, *buffer; 253 int i; 254 int retval = 0; 255 256 /* 257 * If we've gone through this code before, free previously 258 * allocated resources. 259 */ 260 if (num_devices_specified > 0) { 261 for (i = 0; i < num_devices_specified; i++) 262 free(specified_devices[i]); 263 free(specified_devices); 264 specified_devices = NULL; 265 num_devices_specified = 0; 266 } 267 268 /* do an initial malloc */ 269 specified_devices = (char **)malloc(sizeof(char *)); 270 271 tmpstr = tmpstr1 = strdup(args); 272 cp = index(tmpstr1, '\n'); 273 if (cp) 274 *cp = '\0'; 275 for (;;) { 276 for (cp = tmpstr1; *cp && isspace(*cp); cp++) 277 ; 278 tmpstr1 = cp; 279 for (; *cp && !isspace(*cp); cp++) 280 ; 281 if (*cp) 282 *cp++ = '\0'; 283 if (cp - args == 0) 284 break; 285 for (i = 0; i < num_devices; i++) { 286 asprintf(&buffer, "%s%d", dev_select[i].device_name, 287 dev_select[i].unit_number); 288 if (strcmp(buffer, tmpstr1) == 0) { 289 290 num_devices_specified++; 291 292 specified_devices =(char **)realloc( 293 specified_devices, 294 sizeof(char *) * 295 num_devices_specified); 296 specified_devices[num_devices_specified -1]= 297 strdup(tmpstr1); 298 free(buffer); 299 300 break; 301 } 302 else 303 free(buffer); 304 } 305 if (i >= num_devices) 306 error("%s: unknown drive", args); 307 args = cp; 308 } 309 free(tmpstr); 310 311 if (num_devices_specified > 0) { 312 last_type = DS_MATCHTYPE_SPEC; 313 314 retval = devstat_selectdevs(&dev_select, &num_selected, 315 &num_selections, &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