xref: /freebsd/usr.bin/systat/devs.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48d2fbde5SJustin T. Gibbs  * Copyright (c) 1998 Kenneth D. Merry.
545518845SMichael Reifenberger  *               2015 Yoshihiro Ota
68d2fbde5SJustin T. Gibbs  * All rights reserved.
78d2fbde5SJustin T. Gibbs  *
88d2fbde5SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
98d2fbde5SJustin T. Gibbs  * modification, are permitted provided that the following conditions
108d2fbde5SJustin T. Gibbs  * are met:
118d2fbde5SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
128d2fbde5SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
138d2fbde5SJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
148d2fbde5SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
158d2fbde5SJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
168d2fbde5SJustin T. Gibbs  * 3. The name of the author may not be used to endorse or promote products
178d2fbde5SJustin T. Gibbs  *    derived from this software without specific prior written permission.
188d2fbde5SJustin T. Gibbs  *
198d2fbde5SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
208d2fbde5SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218d2fbde5SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228d2fbde5SJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
238d2fbde5SJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248d2fbde5SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258d2fbde5SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268d2fbde5SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278d2fbde5SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288d2fbde5SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298d2fbde5SJustin T. Gibbs  * SUCH DAMAGE.
308d2fbde5SJustin T. Gibbs  */
318d2fbde5SJustin T. Gibbs /*-
328d2fbde5SJustin T. Gibbs  * Copyright (c) 1980, 1992, 1993
338d2fbde5SJustin T. Gibbs  *	The Regents of the University of California.  All rights reserved.
348d2fbde5SJustin T. Gibbs  *
358d2fbde5SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
368d2fbde5SJustin T. Gibbs  * modification, are permitted provided that the following conditions
378d2fbde5SJustin T. Gibbs  * are met:
388d2fbde5SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
398d2fbde5SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
408d2fbde5SJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
418d2fbde5SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
428d2fbde5SJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
43fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
448d2fbde5SJustin T. Gibbs  *    may be used to endorse or promote products derived from this software
458d2fbde5SJustin T. Gibbs  *    without specific prior written permission.
468d2fbde5SJustin T. Gibbs  *
478d2fbde5SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
488d2fbde5SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
498d2fbde5SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
508d2fbde5SJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
518d2fbde5SJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
528d2fbde5SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
538d2fbde5SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
548d2fbde5SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
558d2fbde5SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
568d2fbde5SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
578d2fbde5SJustin T. Gibbs  * SUCH DAMAGE.
588d2fbde5SJustin T. Gibbs  */
598d2fbde5SJustin T. Gibbs 
609ff712b0SMark Murray 
619ff712b0SMark Murray 
628d2fbde5SJustin T. Gibbs #include <sys/types.h>
638d2fbde5SJustin T. Gibbs #include <sys/devicestat.h>
643bd41074SPoul-Henning Kamp #include <sys/resource.h>
658d2fbde5SJustin T. Gibbs 
66d22889b8SDavid E. O'Brien #include <ctype.h>
67d22889b8SDavid E. O'Brien #include <err.h>
689ff712b0SMark Murray #include <stdlib.h>
699ff712b0SMark Murray #include <string.h>
709ff712b0SMark Murray 
718d2fbde5SJustin T. Gibbs #include "systat.h"
728d2fbde5SJustin T. Gibbs #include "extern.h"
73d22889b8SDavid E. O'Brien #include "devs.h"
748d2fbde5SJustin T. Gibbs 
758d2fbde5SJustin T. Gibbs typedef enum {
768d2fbde5SJustin T. Gibbs 	DS_MATCHTYPE_NONE,
778d2fbde5SJustin T. Gibbs 	DS_MATCHTYPE_SPEC,
788d2fbde5SJustin T. Gibbs 	DS_MATCHTYPE_PATTERN
798d2fbde5SJustin T. Gibbs } last_match_type;
808d2fbde5SJustin T. Gibbs 
8145518845SMichael Reifenberger struct statinfo cur_dev, last_dev, run_dev;
8245518845SMichael Reifenberger 
83*6d88f9feSMateusz Guzik static last_match_type last_type;
848d2fbde5SJustin T. Gibbs struct device_selection *dev_select;
85bcc6a3daSKenneth D. Merry long generation;
86bcc6a3daSKenneth D. Merry int num_devices, num_selected;
87bcc6a3daSKenneth D. Merry int num_selections;
88bcc6a3daSKenneth D. Merry long select_generation;
89*6d88f9feSMateusz Guzik static struct devstat_match *matches = NULL;
90*6d88f9feSMateusz Guzik static int num_matches = 0;
91*6d88f9feSMateusz Guzik static char **specified_devices;
92*6d88f9feSMateusz Guzik static int num_devices_specified = 0;
938d2fbde5SJustin T. Gibbs 
949ff712b0SMark Murray static int dsmatchselect(const char *args, devstat_select_mode select_mode,
958d2fbde5SJustin T. Gibbs 			 int maxshowdevs, struct statinfo *s1);
969ff712b0SMark Murray static int dsselect(const char *args, devstat_select_mode select_mode,
978d2fbde5SJustin T. Gibbs 		    int maxshowdevs, struct statinfo *s1);
988d2fbde5SJustin T. Gibbs 
998d2fbde5SJustin T. Gibbs int
dsinit(int maxshowdevs)10045518845SMichael Reifenberger dsinit(int maxshowdevs)
1018d2fbde5SJustin T. Gibbs {
1028d2fbde5SJustin T. Gibbs 	/*
1038d2fbde5SJustin T. Gibbs 	 * Make sure that the userland devstat version matches the kernel
1048d2fbde5SJustin T. Gibbs 	 * devstat version.  If not, exit and print a message informing
1058d2fbde5SJustin T. Gibbs 	 * the user of his mistake.
1068d2fbde5SJustin T. Gibbs 	 */
1078f29de8bSKenneth D. Merry 	if (devstat_checkversion(NULL) < 0)
1088d2fbde5SJustin T. Gibbs 		errx(1, "%s", devstat_errbuf);
1098d2fbde5SJustin T. Gibbs 
11045518845SMichael Reifenberger 	if( cur_dev.dinfo ) // init was alreay ran
11145518845SMichael Reifenberger 		return(1);
11245518845SMichael Reifenberger 
11345518845SMichael Reifenberger 	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
11445518845SMichael Reifenberger 		warnx("%s", devstat_errbuf);
11545518845SMichael Reifenberger 		return(0);
11645518845SMichael Reifenberger 	}
11745518845SMichael Reifenberger 
11845518845SMichael Reifenberger 	cur_dev.dinfo = calloc(1, sizeof(struct devinfo));
11945518845SMichael Reifenberger 	last_dev.dinfo = calloc(1, sizeof(struct devinfo));
12045518845SMichael Reifenberger 	run_dev.dinfo = calloc(1, sizeof(struct devinfo));
12145518845SMichael Reifenberger 
1228d2fbde5SJustin T. Gibbs 	generation = 0;
1238d2fbde5SJustin T. Gibbs 	num_devices = 0;
1248d2fbde5SJustin T. Gibbs 	num_selected = 0;
1258d2fbde5SJustin T. Gibbs 	num_selections = 0;
1268d2fbde5SJustin T. Gibbs 	select_generation = 0;
1278d2fbde5SJustin T. Gibbs 	last_type = DS_MATCHTYPE_NONE;
1288d2fbde5SJustin T. Gibbs 
12945518845SMichael Reifenberger 	if (devstat_getdevs(NULL, &cur_dev) == -1)
1308d2fbde5SJustin T. Gibbs 		errx(1, "%s", devstat_errbuf);
1318d2fbde5SJustin T. Gibbs 
13245518845SMichael Reifenberger 	num_devices = cur_dev.dinfo->numdevs;
13345518845SMichael Reifenberger 	generation = cur_dev.dinfo->generation;
1348d2fbde5SJustin T. Gibbs 
1358d2fbde5SJustin T. Gibbs 	dev_select = NULL;
1368d2fbde5SJustin T. Gibbs 
1378d2fbde5SJustin T. Gibbs 	/*
1388d2fbde5SJustin T. Gibbs 	 * At this point, selectdevs will almost surely indicate that the
1398d2fbde5SJustin T. Gibbs 	 * device list has changed, so we don't look for return values of 0
1408d2fbde5SJustin T. Gibbs 	 * or 1.  If we get back -1, though, there is an error.
1418d2fbde5SJustin T. Gibbs 	 */
1428f29de8bSKenneth D. Merry 	if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
14345518845SMichael Reifenberger 	    &select_generation, generation, cur_dev.dinfo->devices, num_devices,
1448f29de8bSKenneth D. Merry 	    NULL, 0, NULL, 0, DS_SELECT_ADD, maxshowdevs, 0) == -1)
145f46a0535SPoul-Henning Kamp 		errx(1, "%d %s", __LINE__, devstat_errbuf);
1468d2fbde5SJustin T. Gibbs 
1478d2fbde5SJustin T. Gibbs 	return(1);
1488d2fbde5SJustin T. Gibbs }
1498d2fbde5SJustin T. Gibbs 
15045518845SMichael Reifenberger 
15145518845SMichael Reifenberger void
dsgetinfo(struct statinfo * dev)15245518845SMichael Reifenberger dsgetinfo(struct statinfo* dev)
15345518845SMichael Reifenberger {
15445518845SMichael Reifenberger 	switch (devstat_getdevs(NULL, dev)) {
15545518845SMichael Reifenberger 	case -1:
15645518845SMichael Reifenberger 		errx(1, "%s", devstat_errbuf);
15745518845SMichael Reifenberger 		break;
15845518845SMichael Reifenberger 	case 1:
15945518845SMichael Reifenberger 		num_devices = dev->dinfo->numdevs;
16045518845SMichael Reifenberger 		generation = dev->dinfo->generation;
16145518845SMichael Reifenberger 		cmdkre("refresh", NULL);
16245518845SMichael Reifenberger 		break;
16345518845SMichael Reifenberger 	default:
16445518845SMichael Reifenberger 		break;
16545518845SMichael Reifenberger 	}
16645518845SMichael Reifenberger }
16745518845SMichael Reifenberger 
1688d2fbde5SJustin T. Gibbs int
dscmd(const char * cmd,const char * args,int maxshowdevs,struct statinfo * s1)1699ff712b0SMark Murray dscmd(const char *cmd, const char *args, int maxshowdevs, struct statinfo *s1)
1708d2fbde5SJustin T. Gibbs {
1718d2fbde5SJustin T. Gibbs 	int retval;
1728d2fbde5SJustin T. Gibbs 
1738d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "display") || prefix(cmd, "add"))
1748d2fbde5SJustin T. Gibbs 		return(dsselect(args, DS_SELECT_ADDONLY, maxshowdevs, s1));
1758d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "ignore") || prefix(cmd, "delete"))
1768d2fbde5SJustin T. Gibbs 		return(dsselect(args, DS_SELECT_REMOVE, maxshowdevs, s1));
1778d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "show") || prefix(cmd, "only"))
1788d2fbde5SJustin T. Gibbs 		return(dsselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
1798d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "type") || prefix(cmd, "match"))
1808d2fbde5SJustin T. Gibbs 		return(dsmatchselect(args, DS_SELECT_ONLY, maxshowdevs, s1));
1818d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "refresh")) {
1828f29de8bSKenneth D. Merry 		retval = devstat_selectdevs(&dev_select, &num_selected,
1838f29de8bSKenneth D. Merry 		    &num_selections, &select_generation, generation,
1848d2fbde5SJustin T. Gibbs 		    s1->dinfo->devices, num_devices,
1858f29de8bSKenneth D. Merry 		    (last_type ==DS_MATCHTYPE_PATTERN) ?  matches : NULL,
1868f29de8bSKenneth D. Merry 		    (last_type ==DS_MATCHTYPE_PATTERN) ?  num_matches : 0,
1878f29de8bSKenneth D. Merry 		    (last_type == DS_MATCHTYPE_SPEC) ?specified_devices : NULL,
1888f29de8bSKenneth D. Merry 		    (last_type == DS_MATCHTYPE_SPEC) ?num_devices_specified : 0,
1898f29de8bSKenneth D. Merry 		    (last_type == DS_MATCHTYPE_NONE) ?  DS_SELECT_ADD :
1908f29de8bSKenneth D. Merry 		    DS_SELECT_ADDONLY, maxshowdevs, 0);
1918d2fbde5SJustin T. Gibbs 		if (retval == -1) {
1928d2fbde5SJustin T. Gibbs 			warnx("%s", devstat_errbuf);
1938d2fbde5SJustin T. Gibbs 			return(0);
1948d2fbde5SJustin T. Gibbs 		} else if (retval == 1)
1958d2fbde5SJustin T. Gibbs 			return(2);
1968d2fbde5SJustin T. Gibbs 	}
1978d2fbde5SJustin T. Gibbs 	if (prefix(cmd, "drives")) {
19893b9f504SXin LI 		int i;
1998d2fbde5SJustin T. Gibbs 		move(CMDLINE, 0);
2008d2fbde5SJustin T. Gibbs 		clrtoeol();
2018d2fbde5SJustin T. Gibbs 		for (i = 0; i < num_devices; i++) {
2028d2fbde5SJustin T. Gibbs 			printw("%s%d ", s1->dinfo->devices[i].device_name,
2038d2fbde5SJustin T. Gibbs 			       s1->dinfo->devices[i].unit_number);
2048d2fbde5SJustin T. Gibbs 		}
2058d2fbde5SJustin T. Gibbs 		return(1);
2068d2fbde5SJustin T. Gibbs 	}
2078d2fbde5SJustin T. Gibbs 	return(0);
2088d2fbde5SJustin T. Gibbs }
2098d2fbde5SJustin T. Gibbs 
2108d2fbde5SJustin T. Gibbs static int
dsmatchselect(const char * args,devstat_select_mode select_mode,int maxshowdevs,struct statinfo * s1)2119ff712b0SMark Murray dsmatchselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
2128d2fbde5SJustin T. Gibbs 	      struct statinfo *s1)
2138d2fbde5SJustin T. Gibbs {
2149ff712b0SMark Murray 	char **tempstr, *tmpstr, *tmpstr1;
2158d2fbde5SJustin T. Gibbs 	char *tstr[100];
2168d2fbde5SJustin T. Gibbs 	int num_args = 0;
2179ff712b0SMark Murray 	int i;
2188d2fbde5SJustin T. Gibbs 	int retval = 0;
2198d2fbde5SJustin T. Gibbs 
22013fe9f7fSOleksandr Tymoshenko 	if (!args) {
22113fe9f7fSOleksandr Tymoshenko 		warnx("dsmatchselect: no arguments");
222e457729fSOleksandr Tymoshenko 		return(1);
22313fe9f7fSOleksandr Tymoshenko 	}
22413fe9f7fSOleksandr Tymoshenko 
2258d2fbde5SJustin T. Gibbs 	/*
2268d2fbde5SJustin T. Gibbs 	 * Break the (pipe delimited) input string out into separate
2278d2fbde5SJustin T. Gibbs 	 * strings.
2288d2fbde5SJustin T. Gibbs 	 */
2299ff712b0SMark Murray 	tmpstr = tmpstr1 = strdup(args);
2308d2fbde5SJustin T. Gibbs 	for (tempstr = tstr, num_args  = 0;
2319ff712b0SMark Murray 	     (*tempstr = strsep(&tmpstr1, "|")) != NULL && (num_args < 100);
2328d2fbde5SJustin T. Gibbs 	     num_args++)
2338d2fbde5SJustin T. Gibbs 		if (**tempstr != '\0')
2348d2fbde5SJustin T. Gibbs 			if (++tempstr >= &tstr[100])
2358d2fbde5SJustin T. Gibbs 				break;
2369ff712b0SMark Murray 	free(tmpstr);
2378d2fbde5SJustin T. Gibbs 
2388d2fbde5SJustin T. Gibbs 	if (num_args > 99) {
2398d2fbde5SJustin T. Gibbs 		warnx("dsmatchselect: too many match arguments");
2408d2fbde5SJustin T. Gibbs 		return(0);
2418d2fbde5SJustin T. Gibbs 	}
2428d2fbde5SJustin T. Gibbs 
2438d2fbde5SJustin T. Gibbs 	/*
2448d2fbde5SJustin T. Gibbs 	 * If we've gone through the matching code before, clean out
2458d2fbde5SJustin T. Gibbs 	 * previously used memory.
2468d2fbde5SJustin T. Gibbs 	 */
2478d2fbde5SJustin T. Gibbs 	if (num_matches > 0) {
2488d2fbde5SJustin T. Gibbs 		free(matches);
2498d2fbde5SJustin T. Gibbs 		matches = NULL;
2508d2fbde5SJustin T. Gibbs 		num_matches = 0;
2518d2fbde5SJustin T. Gibbs 	}
2528d2fbde5SJustin T. Gibbs 
2538d2fbde5SJustin T. Gibbs 	for (i = 0; i < num_args; i++) {
2548f29de8bSKenneth D. Merry 		if (devstat_buildmatch(tstr[i], &matches, &num_matches) != 0) {
2558d2fbde5SJustin T. Gibbs 			warnx("%s", devstat_errbuf);
2568d2fbde5SJustin T. Gibbs 			return(0);
2578d2fbde5SJustin T. Gibbs 		}
2588d2fbde5SJustin T. Gibbs 	}
2598d2fbde5SJustin T. Gibbs 	if (num_args > 0) {
2608d2fbde5SJustin T. Gibbs 
2618d2fbde5SJustin T. Gibbs 		last_type = DS_MATCHTYPE_PATTERN;
2628d2fbde5SJustin T. Gibbs 
2638f29de8bSKenneth D. Merry 		retval = devstat_selectdevs(&dev_select, &num_selected,
2648f29de8bSKenneth D. Merry 		    &num_selections, &select_generation, generation,
2658f29de8bSKenneth D. Merry 		    s1->dinfo->devices, num_devices, matches, num_matches,
2668f29de8bSKenneth D. Merry 		    NULL, 0, select_mode, maxshowdevs, 0);
2678d2fbde5SJustin T. Gibbs 		if (retval == -1)
2688d2fbde5SJustin T. Gibbs 			err(1, "device selection error");
2698d2fbde5SJustin T. Gibbs 		else if (retval == 1)
2708d2fbde5SJustin T. Gibbs 			return(2);
2718d2fbde5SJustin T. Gibbs 	}
2728d2fbde5SJustin T. Gibbs 	return(1);
2738d2fbde5SJustin T. Gibbs }
2748d2fbde5SJustin T. Gibbs 
2758d2fbde5SJustin T. Gibbs static int
dsselect(const char * args,devstat_select_mode select_mode,int maxshowdevs,struct statinfo * s1)2769ff712b0SMark Murray dsselect(const char *args, devstat_select_mode select_mode, int maxshowdevs,
2778d2fbde5SJustin T. Gibbs 	 struct statinfo *s1)
2788d2fbde5SJustin T. Gibbs {
2799ff712b0SMark Murray 	char *cp, *tmpstr, *tmpstr1, *buffer;
2809ff712b0SMark Murray 	int i;
2818d2fbde5SJustin T. Gibbs 	int retval = 0;
2828d2fbde5SJustin T. Gibbs 
28313fe9f7fSOleksandr Tymoshenko 	if (!args) {
28413fe9f7fSOleksandr Tymoshenko 		warnx("dsselect: no argument");
28513fe9f7fSOleksandr Tymoshenko 		return(1);
28613fe9f7fSOleksandr Tymoshenko 	}
28713fe9f7fSOleksandr Tymoshenko 
2888d2fbde5SJustin T. Gibbs 	/*
2898d2fbde5SJustin T. Gibbs 	 * If we've gone through this code before, free previously
2908d2fbde5SJustin T. Gibbs 	 * allocated resources.
2918d2fbde5SJustin T. Gibbs 	 */
2928d2fbde5SJustin T. Gibbs 	if (num_devices_specified > 0) {
2938d2fbde5SJustin T. Gibbs 		for (i = 0; i < num_devices_specified; i++)
2948d2fbde5SJustin T. Gibbs 			free(specified_devices[i]);
2958d2fbde5SJustin T. Gibbs 		free(specified_devices);
2968d2fbde5SJustin T. Gibbs 		specified_devices = NULL;
2978d2fbde5SJustin T. Gibbs 		num_devices_specified = 0;
2988d2fbde5SJustin T. Gibbs 	}
2998d2fbde5SJustin T. Gibbs 
3008d2fbde5SJustin T. Gibbs 	/* do an initial malloc */
3018d2fbde5SJustin T. Gibbs 	specified_devices = (char **)malloc(sizeof(char *));
3028d2fbde5SJustin T. Gibbs 
3039ff712b0SMark Murray 	tmpstr = tmpstr1 = strdup(args);
304b3608ae1SEd Schouten 	cp = strchr(tmpstr1, '\n');
3058d2fbde5SJustin T. Gibbs 	if (cp)
3068d2fbde5SJustin T. Gibbs 		*cp = '\0';
3078d2fbde5SJustin T. Gibbs 	for (;;) {
3089ff712b0SMark Murray 		for (cp = tmpstr1; *cp && isspace(*cp); cp++)
3098d2fbde5SJustin T. Gibbs 			;
3109ff712b0SMark Murray 		tmpstr1 = cp;
3118d2fbde5SJustin T. Gibbs 		for (; *cp && !isspace(*cp); cp++)
3128d2fbde5SJustin T. Gibbs 			;
3138d2fbde5SJustin T. Gibbs 		if (*cp)
3148d2fbde5SJustin T. Gibbs 			*cp++ = '\0';
315d65e72a8SOleksandr Tymoshenko 		if (cp - tmpstr1 == 0)
3168d2fbde5SJustin T. Gibbs 			break;
3178d2fbde5SJustin T. Gibbs 		for (i = 0; i < num_devices; i++) {
3189ff712b0SMark Murray 			asprintf(&buffer, "%s%d", dev_select[i].device_name,
3198d2fbde5SJustin T. Gibbs 				dev_select[i].unit_number);
3209ff712b0SMark Murray 			if (strcmp(buffer, tmpstr1) == 0) {
3218d2fbde5SJustin T. Gibbs 
3228d2fbde5SJustin T. Gibbs 				num_devices_specified++;
3238d2fbde5SJustin T. Gibbs 
3248d2fbde5SJustin T. Gibbs 				specified_devices =(char **)realloc(
3258d2fbde5SJustin T. Gibbs 						specified_devices,
3268d2fbde5SJustin T. Gibbs 						sizeof(char *) *
3278d2fbde5SJustin T. Gibbs 						num_devices_specified);
3288d2fbde5SJustin T. Gibbs 				specified_devices[num_devices_specified -1]=
3299ff712b0SMark Murray 					strdup(tmpstr1);
3309ff712b0SMark Murray 				free(buffer);
3318d2fbde5SJustin T. Gibbs 
3328d2fbde5SJustin T. Gibbs 				break;
3338d2fbde5SJustin T. Gibbs 			}
3349ff712b0SMark Murray 			else
3359ff712b0SMark Murray 				free(buffer);
3368d2fbde5SJustin T. Gibbs 		}
3378d2fbde5SJustin T. Gibbs 		if (i >= num_devices)
3388d2fbde5SJustin T. Gibbs 			error("%s: unknown drive", args);
339d65e72a8SOleksandr Tymoshenko 		tmpstr1 = cp;
3408d2fbde5SJustin T. Gibbs 	}
3419ff712b0SMark Murray 	free(tmpstr);
3428d2fbde5SJustin T. Gibbs 
3438d2fbde5SJustin T. Gibbs 	if (num_devices_specified > 0) {
3448d2fbde5SJustin T. Gibbs 		last_type = DS_MATCHTYPE_SPEC;
3458d2fbde5SJustin T. Gibbs 
3468f29de8bSKenneth D. Merry 		retval = devstat_selectdevs(&dev_select, &num_selected,
3478f29de8bSKenneth D. Merry 		    &num_selections, &select_generation, generation,
3488d2fbde5SJustin T. Gibbs 		    s1->dinfo->devices, num_devices, NULL, 0,
3498d2fbde5SJustin T. Gibbs 		    specified_devices, num_devices_specified,
3508d2fbde5SJustin T. Gibbs 		    select_mode, maxshowdevs, 0);
3518d2fbde5SJustin T. Gibbs 		if (retval == -1)
3528d2fbde5SJustin T. Gibbs 			err(1, "%s", devstat_errbuf);
3538d2fbde5SJustin T. Gibbs 		else if (retval == 1)
3548d2fbde5SJustin T. Gibbs 			return(2);
3558d2fbde5SJustin T. Gibbs 	}
3568d2fbde5SJustin T. Gibbs 	return(1);
3578d2fbde5SJustin T. Gibbs }
35845518845SMichael Reifenberger 
35945518845SMichael Reifenberger 
36045518845SMichael Reifenberger void
dslabel(int maxdrives,int diskcol,int diskrow)36145518845SMichael Reifenberger dslabel(int maxdrives, int diskcol, int diskrow)
36245518845SMichael Reifenberger {
36345518845SMichael Reifenberger 	int i, j;
36445518845SMichael Reifenberger 
36545518845SMichael Reifenberger 	mvprintw(diskrow, diskcol, "Disks");
36645518845SMichael Reifenberger 	mvprintw(diskrow + 1, diskcol, "KB/t");
36745518845SMichael Reifenberger 	mvprintw(diskrow + 2, diskcol, "tps");
36845518845SMichael Reifenberger 	mvprintw(diskrow + 3, diskcol, "MB/s");
36945518845SMichael Reifenberger 	mvprintw(diskrow + 4, diskcol, "%%busy");
37045518845SMichael Reifenberger 	/*
37145518845SMichael Reifenberger 	 * For now, we don't support a fourth disk statistic.  So there's
37245518845SMichael Reifenberger 	 * no point in providing a label for it.  If someone can think of a
37345518845SMichael Reifenberger 	 * fourth useful disk statistic, there is room to add it.
37445518845SMichael Reifenberger 	 */
37545518845SMichael Reifenberger 	/* mvprintw(diskrow + 4, diskcol, " msps"); */
37645518845SMichael Reifenberger 	j = 0;
37745518845SMichael Reifenberger 	for (i = 0; i < num_devices && j < maxdrives; i++)
37845518845SMichael Reifenberger 		if (dev_select[i].selected) {
37945518845SMichael Reifenberger 			char tmpstr[80];
38045518845SMichael Reifenberger 			sprintf(tmpstr, "%s%d", dev_select[i].device_name,
38145518845SMichael Reifenberger 				dev_select[i].unit_number);
38245518845SMichael Reifenberger 			mvprintw(diskrow, diskcol + 5 + 6 * j,
38345518845SMichael Reifenberger 				" %5.5s", tmpstr);
38445518845SMichael Reifenberger 			j++;
38545518845SMichael Reifenberger 		}
38645518845SMichael Reifenberger }
38745518845SMichael Reifenberger 
38845518845SMichael Reifenberger static void
dsshow2(int diskcol,int diskrow,int dn,int lc,struct statinfo * now,struct statinfo * then)38945518845SMichael Reifenberger dsshow2(int diskcol, int diskrow, int dn, int lc, struct statinfo *now, struct statinfo *then)
39045518845SMichael Reifenberger {
39145518845SMichael Reifenberger 	long double transfers_per_second;
39245518845SMichael Reifenberger 	long double kb_per_transfer, mb_per_second;
39345518845SMichael Reifenberger 	long double elapsed_time, device_busy;
39445518845SMichael Reifenberger 	int di;
39545518845SMichael Reifenberger 
39645518845SMichael Reifenberger 	di = dev_select[dn].position;
39745518845SMichael Reifenberger 
39845518845SMichael Reifenberger 	if (then != NULL) {
39945518845SMichael Reifenberger 		/* Calculate relative to previous sample */
40045518845SMichael Reifenberger 		elapsed_time = now->snap_time - then->snap_time;
40145518845SMichael Reifenberger 	} else {
40245518845SMichael Reifenberger 		/* Calculate relative to device creation */
40345518845SMichael Reifenberger 		elapsed_time = now->snap_time - devstat_compute_etime(
40445518845SMichael Reifenberger 		    &now->dinfo->devices[di].creation_time, NULL);
40545518845SMichael Reifenberger 	}
40645518845SMichael Reifenberger 
40745518845SMichael Reifenberger 	if (devstat_compute_statistics(&now->dinfo->devices[di], then ?
40845518845SMichael Reifenberger 	    &then->dinfo->devices[di] : NULL, elapsed_time,
40945518845SMichael Reifenberger 	    DSM_KB_PER_TRANSFER, &kb_per_transfer,
41045518845SMichael Reifenberger 	    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
41145518845SMichael Reifenberger 	    DSM_MB_PER_SECOND, &mb_per_second,
41245518845SMichael Reifenberger 	    DSM_BUSY_PCT, &device_busy,
41345518845SMichael Reifenberger 	    DSM_NONE) != 0)
41445518845SMichael Reifenberger 		errx(1, "%s", devstat_errbuf);
41545518845SMichael Reifenberger 
41645518845SMichael Reifenberger 	lc = diskcol + lc * 6;
41745518845SMichael Reifenberger 	putlongdouble(kb_per_transfer, diskrow + 1, lc, 5, 2, 0);
41845518845SMichael Reifenberger 	putlongdouble(transfers_per_second, diskrow + 2, lc, 5, 0, 0);
41945518845SMichael Reifenberger 	putlongdouble(mb_per_second, diskrow + 3, lc, 5, 2, 0);
42045518845SMichael Reifenberger 	putlongdouble(device_busy, diskrow + 4, lc, 5, 0, 0);
42145518845SMichael Reifenberger }
42245518845SMichael Reifenberger 
42345518845SMichael Reifenberger void
dsshow(int maxdrives,int diskcol,int diskrow,struct statinfo * now,struct statinfo * then)42445518845SMichael Reifenberger dsshow(int maxdrives, int diskcol, int diskrow, struct statinfo *now, struct statinfo *then)
42545518845SMichael Reifenberger {
42645518845SMichael Reifenberger 	int i, lc;
42745518845SMichael Reifenberger 
42845518845SMichael Reifenberger 	for (i = 0, lc = 0; i < num_devices && lc < maxdrives; i++)
42945518845SMichael Reifenberger 		if (dev_select[i].selected)
43066483838SMichael Reifenberger 			dsshow2(diskcol, diskrow, i, ++lc, now, then);
43145518845SMichael Reifenberger }
432