Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.6.10.3
      1  1.6.10.3       riz /* $NetBSD: drvctl.c,v 1.6.10.3 2011/08/12 20:55:55 riz Exp $ */
      2       1.1  drochner 
      3       1.1  drochner /*
      4       1.1  drochner  * Copyright (c) 2004
      5       1.1  drochner  * 	Matthias Drochner.  All rights reserved.
      6       1.1  drochner  *
      7       1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8       1.1  drochner  * modification, are permitted provided that the following conditions
      9       1.1  drochner  * are met:
     10       1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11       1.1  drochner  *    notice, this list of conditions, and the following disclaimer.
     12       1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15       1.1  drochner  *
     16       1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1  drochner  * SUCH DAMAGE.
     27       1.1  drochner  */
     28       1.1  drochner 
     29  1.6.10.3       riz #include <inttypes.h>
     30  1.6.10.2    bouyer #include <stdbool.h>
     31       1.1  drochner #include <stdio.h>
     32       1.1  drochner #include <stdlib.h>
     33       1.1  drochner #include <unistd.h>
     34       1.1  drochner #include <err.h>
     35       1.1  drochner #include <fcntl.h>
     36       1.1  drochner #include <string.h>
     37       1.1  drochner #include <sys/ioctl.h>
     38       1.1  drochner #include <sys/drvctlio.h>
     39       1.1  drochner 
     40  1.6.10.3       riz #define OPTS "QRSa:dlnprt"
     41       1.5   thorpej 
     42       1.5   thorpej #define	OPEN_MODE(mode)							\
     43       1.5   thorpej 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     44       1.5   thorpej 					  : O_RDONLY)
     45       1.1  drochner 
     46       1.1  drochner static void usage(void);
     47  1.6.10.3       riz static void extract_property(prop_dictionary_t, const char *);
     48  1.6.10.3       riz static void list_children(int, char *, bool, bool, int);
     49       1.1  drochner 
     50       1.1  drochner static void
     51       1.3   xtraeme usage(void)
     52       1.1  drochner {
     53       1.1  drochner 
     54       1.2       wiz 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     55       1.5   thorpej 	    "       %s -d device\n"
     56  1.6.10.3       riz 	    "       %s [-nt] -l [device]\n"
     57  1.6.10.3       riz 	    "       %s -p device [prop]\n"
     58       1.6    dyoung 	    "       %s -Q device\n"
     59       1.6    dyoung 	    "       %s -R device\n"
     60       1.6    dyoung 	    "       %s -S device\n",
     61       1.6    dyoung 	    getprogname(), getprogname(), getprogname(), getprogname(),
     62       1.5   thorpej 	    getprogname(), getprogname(), getprogname());
     63       1.1  drochner 	exit(1);
     64       1.1  drochner }
     65       1.1  drochner 
     66       1.1  drochner int
     67       1.1  drochner main(int argc, char **argv)
     68       1.1  drochner {
     69  1.6.10.3       riz 	bool nflag = false, tflag = false;
     70       1.1  drochner 	int c, mode;
     71       1.1  drochner 	char *attr = 0;
     72       1.1  drochner 	extern char *optarg;
     73       1.1  drochner 	extern int optind;
     74       1.1  drochner 	int fd, res;
     75       1.6    dyoung 	struct devpmargs paa = {.devname = "", .flags = 0};
     76       1.1  drochner 	struct devdetachargs daa;
     77       1.1  drochner 	struct devrescanargs raa;
     78       1.1  drochner 	int *locs, i;
     79       1.6    dyoung 	prop_dictionary_t command_dict, args_dict, results_dict,
     80       1.6    dyoung 			  data_dict;
     81       1.6    dyoung 	prop_string_t string;
     82       1.6    dyoung 	prop_number_t number;
     83       1.6    dyoung 	char *xml;
     84       1.1  drochner 
     85       1.4     lukem 	mode = 0;
     86       1.1  drochner 	while ((c = getopt(argc, argv, OPTS)) != -1) {
     87       1.1  drochner 		switch (c) {
     88       1.6    dyoung 		case 'Q':
     89       1.6    dyoung 		case 'R':
     90       1.6    dyoung 		case 'S':
     91       1.1  drochner 		case 'd':
     92       1.6    dyoung 		case 'l':
     93       1.6    dyoung 		case 'p':
     94       1.1  drochner 		case 'r':
     95       1.1  drochner 			mode = c;
     96       1.1  drochner 			break;
     97       1.1  drochner 		case 'a':
     98       1.1  drochner 			attr = optarg;
     99       1.1  drochner 			break;
    100  1.6.10.2    bouyer 		case 'n':
    101  1.6.10.2    bouyer 			nflag = true;
    102  1.6.10.2    bouyer 			break;
    103  1.6.10.3       riz 		case 't':
    104  1.6.10.3       riz 			tflag = nflag = true;
    105  1.6.10.3       riz 			break;
    106       1.1  drochner 		case '?':
    107       1.1  drochner 		default:
    108       1.1  drochner 			usage();
    109       1.1  drochner 		}
    110       1.1  drochner 	}
    111       1.1  drochner 
    112       1.1  drochner 	argc -= optind;
    113       1.1  drochner 	argv += optind;
    114       1.1  drochner 
    115  1.6.10.1       snj 	if ((argc < 1 && mode != 'l') || mode == 0)
    116       1.1  drochner 		usage();
    117       1.1  drochner 
    118       1.5   thorpej 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    119       1.1  drochner 	if (fd < 0)
    120       1.1  drochner 		err(2, "open %s", DRVCTLDEV);
    121       1.1  drochner 
    122       1.6    dyoung 	switch (mode) {
    123       1.6    dyoung 	case 'Q':
    124       1.6    dyoung 		paa.flags = DEVPM_F_SUBTREE;
    125       1.6    dyoung 		/*FALLTHROUGH*/
    126       1.6    dyoung 	case 'R':
    127       1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    128       1.6    dyoung 
    129       1.6    dyoung 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    130       1.6    dyoung 			err(3, "DRVRESUMEDEV");
    131       1.6    dyoung 		break;
    132       1.6    dyoung 	case 'S':
    133       1.6    dyoung 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    134       1.6    dyoung 
    135       1.6    dyoung 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    136       1.6    dyoung 			err(3, "DRVSUSPENDDEV");
    137       1.6    dyoung 		break;
    138       1.6    dyoung 	case 'd':
    139       1.1  drochner 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    140       1.1  drochner 
    141       1.6    dyoung 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    142       1.1  drochner 			err(3, "DRVDETACHDEV");
    143       1.6    dyoung 		break;
    144       1.6    dyoung 	case 'l':
    145  1.6.10.3       riz 		list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
    146       1.6    dyoung 		break;
    147       1.6    dyoung 	case 'r':
    148       1.1  drochner 		memset(&raa, 0, sizeof(raa));
    149       1.1  drochner 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    150       1.1  drochner 		if (attr)
    151       1.1  drochner 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    152       1.1  drochner 		if (argc > 1) {
    153       1.1  drochner 			locs = malloc((argc - 1) * sizeof(int));
    154       1.1  drochner 			if (!locs)
    155       1.1  drochner 				err(5, "malloc int[%d]", argc - 1);
    156       1.1  drochner 			for (i = 0; i < argc - 1; i++)
    157       1.1  drochner 				locs[i] = atoi(argv[i + 1]);
    158       1.1  drochner 			raa.numlocators = argc - 1;
    159       1.1  drochner 			raa.locators = locs;
    160       1.1  drochner 		}
    161       1.1  drochner 
    162       1.6    dyoung 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    163       1.1  drochner 			err(3, "DRVRESCANBUS");
    164       1.6    dyoung 		break;
    165       1.6    dyoung 	case 'p':
    166       1.5   thorpej 
    167       1.5   thorpej 		command_dict = prop_dictionary_create();
    168       1.5   thorpej 		args_dict = prop_dictionary_create();
    169       1.5   thorpej 
    170       1.5   thorpej 		string = prop_string_create_cstring_nocopy("get-properties");
    171       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-command", string);
    172       1.5   thorpej 		prop_object_release(string);
    173       1.5   thorpej 
    174       1.5   thorpej 		string = prop_string_create_cstring(argv[0]);
    175       1.5   thorpej 		prop_dictionary_set(args_dict, "device-name", string);
    176       1.5   thorpej 		prop_object_release(string);
    177       1.5   thorpej 
    178       1.5   thorpej 		prop_dictionary_set(command_dict, "drvctl-arguments",
    179       1.5   thorpej 				    args_dict);
    180       1.5   thorpej 		prop_object_release(args_dict);
    181       1.5   thorpej 
    182       1.5   thorpej 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    183       1.5   thorpej 						     DRVCTLCOMMAND,
    184       1.5   thorpej 						     &results_dict);
    185       1.5   thorpej 		prop_object_release(command_dict);
    186       1.5   thorpej 		if (res)
    187       1.5   thorpej 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    188       1.5   thorpej 
    189       1.5   thorpej 		number = prop_dictionary_get(results_dict, "drvctl-error");
    190       1.5   thorpej 		if (prop_number_integer_value(number) != 0) {
    191       1.5   thorpej 			errx(3, "get-properties: %s",
    192       1.5   thorpej 			    strerror((int)prop_number_integer_value(number)));
    193       1.5   thorpej 		}
    194       1.5   thorpej 
    195       1.5   thorpej 		data_dict = prop_dictionary_get(results_dict,
    196       1.5   thorpej 						"drvctl-result-data");
    197       1.5   thorpej 		if (data_dict == NULL) {
    198       1.5   thorpej 			errx(3, "get-properties: failed to return result data");
    199       1.5   thorpej 		}
    200       1.5   thorpej 
    201  1.6.10.3       riz 		if (argc == 1) {
    202  1.6.10.3       riz 			xml = prop_dictionary_externalize(data_dict);
    203  1.6.10.3       riz 			printf("Properties for device `%s':\n%s",
    204  1.6.10.3       riz 			       argv[0], xml);
    205  1.6.10.3       riz 			free(xml);
    206  1.6.10.3       riz 		} else {
    207  1.6.10.3       riz 			for (i = 1; i < argc; i++)
    208  1.6.10.3       riz 				extract_property(data_dict, argv[i]);
    209  1.6.10.3       riz 		}
    210       1.5   thorpej 
    211  1.6.10.3       riz 		prop_object_release(results_dict);
    212       1.6    dyoung 		break;
    213       1.6    dyoung 	default:
    214       1.1  drochner 		errx(4, "unknown command");
    215       1.6    dyoung 	}
    216       1.1  drochner 
    217       1.1  drochner 	return (0);
    218       1.1  drochner }
    219  1.6.10.3       riz 
    220  1.6.10.3       riz static void
    221  1.6.10.3       riz extract_property(prop_dictionary_t dict, const char *prop)
    222  1.6.10.3       riz {
    223  1.6.10.3       riz 	char *s, *p, *cur, *ep = NULL, *xml;
    224  1.6.10.3       riz 	prop_object_t obj;
    225  1.6.10.3       riz 
    226  1.6.10.3       riz 	s = strdup(prop);
    227  1.6.10.3       riz 	p = strtok_r(s, "/", &ep);
    228  1.6.10.3       riz 	while (p) {
    229  1.6.10.3       riz 		cur = p;
    230  1.6.10.3       riz 		p = strtok_r(NULL, "/", &ep);
    231  1.6.10.3       riz 		if (p) {
    232  1.6.10.3       riz 			if (prop_dictionary_get_dict(dict, cur, &dict) == false)
    233  1.6.10.3       riz 				exit(EXIT_FAILURE);
    234  1.6.10.3       riz 		} else {
    235  1.6.10.3       riz 			obj = prop_dictionary_get(dict, cur);
    236  1.6.10.3       riz 			if (obj == NULL)
    237  1.6.10.3       riz 				exit(EXIT_FAILURE);
    238  1.6.10.3       riz 			switch (prop_object_type(obj)) {
    239  1.6.10.3       riz 			case PROP_TYPE_BOOL:
    240  1.6.10.3       riz 				printf("%s\n",
    241  1.6.10.3       riz 				    prop_bool_true(obj) ? "true" : "false");
    242  1.6.10.3       riz 				break;
    243  1.6.10.3       riz 			case PROP_TYPE_NUMBER:
    244  1.6.10.3       riz 				printf("%" PRId64 "\n",
    245  1.6.10.3       riz 				    prop_number_integer_value(obj));
    246  1.6.10.3       riz 				break;
    247  1.6.10.3       riz 			case PROP_TYPE_STRING:
    248  1.6.10.3       riz 				printf("%s\n",
    249  1.6.10.3       riz 				    prop_string_cstring_nocopy(obj));
    250  1.6.10.3       riz 				break;
    251  1.6.10.3       riz 			case PROP_TYPE_DICTIONARY:
    252  1.6.10.3       riz 				xml = prop_dictionary_externalize(obj);
    253  1.6.10.3       riz 				printf("%s", xml);
    254  1.6.10.3       riz 				free(xml);
    255  1.6.10.3       riz 				break;
    256  1.6.10.3       riz 			default:
    257  1.6.10.3       riz 				fprintf(stderr, "unhandled type %d\n",
    258  1.6.10.3       riz 				    prop_object_type(obj));
    259  1.6.10.3       riz 				exit(EXIT_FAILURE);
    260  1.6.10.3       riz 			}
    261  1.6.10.3       riz 		}
    262  1.6.10.3       riz 	}
    263  1.6.10.3       riz 
    264  1.6.10.3       riz 	free(s);
    265  1.6.10.3       riz }
    266  1.6.10.3       riz 
    267  1.6.10.3       riz static void
    268  1.6.10.3       riz list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
    269  1.6.10.3       riz {
    270  1.6.10.3       riz 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
    271  1.6.10.3       riz 				  .l_children = 0};
    272  1.6.10.3       riz 	size_t children;
    273  1.6.10.3       riz 	int i, n;
    274  1.6.10.3       riz 
    275  1.6.10.3       riz 	if (dvname == NULL) {
    276  1.6.10.3       riz 		if (depth > 0)
    277  1.6.10.3       riz 			return;
    278  1.6.10.3       riz 		*laa.l_devname = '\0';
    279  1.6.10.3       riz 	} else {
    280  1.6.10.3       riz 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
    281  1.6.10.3       riz 	}
    282  1.6.10.3       riz 
    283  1.6.10.3       riz 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    284  1.6.10.3       riz 		err(3, "DRVLISTDEV");
    285  1.6.10.3       riz 
    286  1.6.10.3       riz 	children = laa.l_children;
    287  1.6.10.3       riz 
    288  1.6.10.3       riz 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    289  1.6.10.3       riz 	if (laa.l_childname == NULL)
    290  1.6.10.3       riz 		err(5, "DRVLISTDEV");
    291  1.6.10.3       riz 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    292  1.6.10.3       riz 		err(3, "DRVLISTDEV");
    293  1.6.10.3       riz 	if (laa.l_children > children)
    294  1.6.10.3       riz 		err(6, "DRVLISTDEV: number of children grew");
    295  1.6.10.3       riz 
    296  1.6.10.3       riz 	for (i = 0; i < (int)laa.l_children; i++) {
    297  1.6.10.3       riz 		for (n = 0; n < depth; n++)
    298  1.6.10.3       riz 			printf("  ");
    299  1.6.10.3       riz 		if (!nflag) {
    300  1.6.10.3       riz 			printf("%s ",
    301  1.6.10.3       riz 			    (dvname == NULL) ? "root" : laa.l_devname);
    302  1.6.10.3       riz 		}
    303  1.6.10.3       riz 		printf("%s\n", laa.l_childname[i]);
    304  1.6.10.3       riz 		if (tflag) {
    305  1.6.10.3       riz 			list_children(fd, laa.l_childname[i], nflag,
    306  1.6.10.3       riz 			    tflag, depth + 1);
    307  1.6.10.3       riz 		}
    308  1.6.10.3       riz 	}
    309  1.6.10.3       riz 
    310  1.6.10.3       riz 	free(laa.l_childname);
    311  1.6.10.3       riz }
    312