Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.22
      1 /* $NetBSD: drvctl.c,v 1.22 2022/08/21 07:51:30 mlelstv Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004
      5  * 	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions, and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     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 
     29 #include <inttypes.h>
     30 #include <stdbool.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <unistd.h>
     34 #include <err.h>
     35 #include <fcntl.h>
     36 #include <string.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/drvctlio.h>
     39 
     40 
     41 #define	OPEN_MODE(mode)							\
     42 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     43 					  : O_RDONLY)
     44 
     45 __dead static void usage(void);
     46 static void extract_property(prop_dictionary_t, const char *, bool);
     47 static void display_object(prop_object_t, bool);
     48 static void list_children(int, char *, bool, bool, int);
     49 
     50 static void
     51 usage(void)
     52 {
     53 	const char *p = getprogname();
     54 
     55 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     56 	    "       %s -d device\n"
     57 	    "       %s [-nt] -l [device]\n"
     58 	    "       %s [-n] -p device [property]\n"
     59 	    "       %s -Q device\n"
     60 	    "       %s -R device\n"
     61 	    "       %s -S device\n", p, p, p, p, p, p, p);
     62 	exit(EXIT_FAILURE);
     63 }
     64 
     65 int
     66 main(int argc, char **argv)
     67 {
     68 	bool nflag = false, tflag = false;
     69 	int c, mode;
     70 	char *attr = 0;
     71 	extern char *optarg;
     72 	extern int optind;
     73 	int fd, res;
     74 	struct devpmargs paa = { .devname = "", .flags = 0 };
     75 	struct devdetachargs daa;
     76 	struct devrescanargs raa;
     77 	int *locs, i;
     78 	prop_dictionary_t command_dict, args_dict, results_dict, data_dict;
     79 	char *xml;
     80 	int drvctl_error;
     81 
     82 	mode = 0;
     83 	while ((c = getopt(argc, argv, "QRSa:dlnprt")) != -1) {
     84 		switch (c) {
     85 		case 'Q':
     86 		case 'R':
     87 		case 'S':
     88 		case 'd':
     89 		case 'l':
     90 		case 'p':
     91 		case 'r':
     92 			mode = c;
     93 			break;
     94 		case 'a':
     95 			attr = optarg;
     96 			break;
     97 		case 'n':
     98 			nflag = true;
     99 			break;
    100 		case 't':
    101 			tflag = nflag = true;
    102 			break;
    103 		case '?':
    104 		default:
    105 			usage();
    106 		}
    107 	}
    108 
    109 	argc -= optind;
    110 	argv += optind;
    111 
    112 	if ((argc < 1 && mode != 'l') || mode == 0)
    113 		usage();
    114 
    115 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    116 	if (fd == -1)
    117 		err(EXIT_FAILURE, "open %s", DRVCTLDEV);
    118 
    119 	switch (mode) {
    120 	case 'Q':
    121 		paa.flags = DEVPM_F_SUBTREE;
    122 		/*FALLTHROUGH*/
    123 	case 'R':
    124 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    125 
    126 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    127 			err(EXIT_FAILURE, "DRVRESUMEDEV");
    128 		break;
    129 	case 'S':
    130 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    131 
    132 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    133 			err(EXIT_FAILURE, "DRVSUSPENDDEV");
    134 		break;
    135 	case 'd':
    136 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    137 
    138 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    139 			err(EXIT_FAILURE, "DRVDETACHDEV");
    140 		break;
    141 	case 'l':
    142 		list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
    143 		break;
    144 	case 'r':
    145 		memset(&raa, 0, sizeof(raa));
    146 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    147 		if (attr)
    148 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    149 		if (argc > 1) {
    150 			locs = malloc((argc - 1) * sizeof(int));
    151 			if (!locs)
    152 				err(EXIT_FAILURE, "malloc int[%d]", argc - 1);
    153 			for (i = 0; i < argc - 1; i++)
    154 				locs[i] = atoi(argv[i + 1]);
    155 			raa.numlocators = argc - 1;
    156 			raa.locators = locs;
    157 		}
    158 
    159 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    160 			err(EXIT_FAILURE, "DRVRESCANBUS");
    161 		break;
    162 	case 'p':
    163 		command_dict = prop_dictionary_create();
    164 		args_dict = prop_dictionary_create();
    165 
    166 		prop_dictionary_set_string_nocopy(command_dict,
    167 		    "drvctl-command", "get-properties");
    168 		prop_dictionary_set_string(args_dict, "device-name",
    169 		    argv[0]);
    170 		prop_dictionary_set_and_rel(command_dict, "drvctl-arguments",
    171 		    args_dict);
    172 
    173 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    174 		    DRVCTLCOMMAND, &results_dict);
    175 		prop_object_release(command_dict);
    176 		if (res)
    177 			errc(EXIT_FAILURE, res, "DRVCTLCOMMAND");
    178 
    179 		if (prop_dictionary_get_int(results_dict, "drvctl-error",
    180 					    &drvctl_error) &&
    181 		    drvctl_error != 0) {
    182 			errc(EXIT_FAILURE, drvctl_error, "get-properties");
    183 		}
    184 
    185 		data_dict = prop_dictionary_get(results_dict,
    186 		    "drvctl-result-data");
    187 		if (data_dict == NULL) {
    188 			errx(EXIT_FAILURE,
    189 			    "get-properties: failed to return result data");
    190 		}
    191 
    192 		if (argc == 1) {
    193 			xml = prop_dictionary_externalize(data_dict);
    194 			if (!nflag) {
    195 				printf("Properties for device `%s':\n",
    196 				    argv[0]);
    197 			}
    198 			printf("%s", xml);
    199 			free(xml);
    200 		} else {
    201 			for (i = 1; i < argc; i++)
    202 				extract_property(data_dict, argv[i], nflag);
    203 		}
    204 
    205 		prop_object_release(results_dict);
    206 		break;
    207 	default:
    208 		errx(EXIT_FAILURE, "unknown command `%c'", mode);
    209 	}
    210 
    211 	return EXIT_SUCCESS;
    212 }
    213 
    214 static void
    215 extract_property(prop_dictionary_t dict, const char *prop, bool nflag)
    216 {
    217 	char *s, *p, *cur, *ep = NULL;
    218 	prop_object_t obj;
    219 	unsigned long ind;
    220 
    221 	obj = dict;
    222 	cur = NULL;
    223 	s = strdup(prop);
    224 	p = strtok_r(s, "/", &ep);
    225 	while (p) {
    226 		cur = p;
    227 		p = strtok_r(NULL, "/", &ep);
    228 
    229 		switch (prop_object_type(obj)) {
    230 		case PROP_TYPE_DICTIONARY:
    231 			obj = prop_dictionary_get(obj, cur);
    232 			if (obj == NULL)
    233 				p = NULL;
    234 			break;
    235 		case PROP_TYPE_ARRAY:
    236 			ind = strtoul(cur, NULL, 0);
    237 			obj = prop_array_get(obj, ind);
    238 			if (obj == NULL)
    239 				p = NULL;
    240 			break;
    241 		default:
    242 			fprintf(stderr, "Select neither dict nor array with"
    243 			    " `%s'\n", cur);
    244 			obj = NULL;
    245 			p = NULL;
    246 			break;
    247 		}
    248 	}
    249 
    250 	if (obj != NULL && cur != NULL)
    251 		display_object(obj, nflag);
    252 
    253 	free(s);
    254 }
    255 
    256 static void
    257 display_object(prop_object_t obj, bool nflag)
    258 {
    259 	char *xml;
    260 	prop_object_t next_obj;
    261 	prop_object_iterator_t iter;
    262 
    263 	if (obj == NULL)
    264 		exit(EXIT_FAILURE);
    265 	switch (prop_object_type(obj)) {
    266 	case PROP_TYPE_BOOL:
    267 		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
    268 		break;
    269 	case PROP_TYPE_NUMBER:
    270 		printf("%" PRId64 "\n", prop_number_signed_value(obj));
    271 		break;
    272 	case PROP_TYPE_STRING:
    273 		printf("%s\n", prop_string_value(obj));
    274 		break;
    275 	case PROP_TYPE_DICTIONARY:
    276 		xml = prop_dictionary_externalize(obj);
    277 		printf("%s", xml);
    278 		free(xml);
    279 		break;
    280 	case PROP_TYPE_ARRAY:
    281 		iter = prop_array_iterator(obj);
    282 		if (!nflag)
    283 			printf("Array:\n");
    284 		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
    285 			display_object(next_obj, nflag);
    286 		break;
    287 	default:
    288 		errx(EXIT_FAILURE, "Unhandled type %d", prop_object_type(obj));
    289 	}
    290 }
    291 
    292 static void
    293 list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
    294 {
    295 	struct devlistargs laa = {
    296 	    .l_devname = "", .l_childname = NULL, .l_children = 0
    297 	};
    298 	size_t children;
    299 	int i, n;
    300 
    301 	if (dvname == NULL) {
    302 		if (depth > 0)
    303 			return;
    304 		*laa.l_devname = '\0';
    305 	} else {
    306 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
    307 	}
    308 
    309 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    310 		err(EXIT_FAILURE, "DRVLISTDEV");
    311 
    312 	children = laa.l_children;
    313 
    314 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    315 	if (laa.l_childname == NULL)
    316 		err(EXIT_FAILURE, "DRVLISTDEV");
    317 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    318 		err(EXIT_FAILURE, "DRVLISTDEV");
    319 	if (laa.l_children > children)
    320 		err(EXIT_FAILURE, "DRVLISTDEV: number of children grew");
    321 
    322 	for (i = 0; i < (int)laa.l_children; i++) {
    323 		for (n = 0; n < depth; n++)
    324 			printf("  ");
    325 		if (!nflag) {
    326 			printf("%s ",
    327 			    (dvname == NULL) ? "root" : laa.l_devname);
    328 		}
    329 		printf("%s\n", laa.l_childname[i]);
    330 		if (tflag) {
    331 			list_children(fd, laa.l_childname[i], nflag,
    332 			    tflag, depth + 1);
    333 		}
    334 	}
    335 
    336 	free(laa.l_childname);
    337 }
    338