Home | History | Annotate | Line # | Download | only in drvctl
drvctl.c revision 1.11
      1 /* $NetBSD: drvctl.c,v 1.11 2011/08/07 12:00:11 jmcneill 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 #define OPTS "QRSa:dlnpr"
     41 
     42 #define	OPEN_MODE(mode)							\
     43 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
     44 					  : O_RDONLY)
     45 
     46 static void usage(void);
     47 static void extract_property(prop_dictionary_t, const char *);
     48 
     49 static void
     50 usage(void)
     51 {
     52 
     53 	fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
     54 	    "       %s -d device\n"
     55 	    "       %s [-n] -l [device]\n"
     56 	    "       %s -p device [prop]\n"
     57 	    "       %s -Q device\n"
     58 	    "       %s -R device\n"
     59 	    "       %s -S device\n",
     60 	    getprogname(), getprogname(), getprogname(), getprogname(),
     61 	    getprogname(), getprogname(), getprogname());
     62 	exit(1);
     63 }
     64 
     65 int
     66 main(int argc, char **argv)
     67 {
     68 	bool nflag = false;
     69 	int c, mode;
     70 	char *attr = 0;
     71 	extern char *optarg;
     72 	extern int optind;
     73 	int fd, res;
     74 	size_t children;
     75 	struct devpmargs paa = {.devname = "", .flags = 0};
     76 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
     77 				  .l_children = 0};
     78 	struct devdetachargs daa;
     79 	struct devrescanargs raa;
     80 	int *locs, i;
     81 	prop_dictionary_t command_dict, args_dict, results_dict,
     82 			  data_dict;
     83 	prop_string_t string;
     84 	prop_number_t number;
     85 	char *xml;
     86 
     87 	mode = 0;
     88 	while ((c = getopt(argc, argv, OPTS)) != -1) {
     89 		switch (c) {
     90 		case 'Q':
     91 		case 'R':
     92 		case 'S':
     93 		case 'd':
     94 		case 'l':
     95 		case 'p':
     96 		case 'r':
     97 			mode = c;
     98 			break;
     99 		case 'a':
    100 			attr = optarg;
    101 			break;
    102 		case 'n':
    103 			nflag = true;
    104 			break;
    105 		case '?':
    106 		default:
    107 			usage();
    108 		}
    109 	}
    110 
    111 	argc -= optind;
    112 	argv += optind;
    113 
    114 	if ((argc < 1 && mode != 'l') || mode == 0)
    115 		usage();
    116 
    117 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
    118 	if (fd < 0)
    119 		err(2, "open %s", DRVCTLDEV);
    120 
    121 	switch (mode) {
    122 	case 'Q':
    123 		paa.flags = DEVPM_F_SUBTREE;
    124 		/*FALLTHROUGH*/
    125 	case 'R':
    126 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    127 
    128 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
    129 			err(3, "DRVRESUMEDEV");
    130 		break;
    131 	case 'S':
    132 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
    133 
    134 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
    135 			err(3, "DRVSUSPENDDEV");
    136 		break;
    137 	case 'd':
    138 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
    139 
    140 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
    141 			err(3, "DRVDETACHDEV");
    142 		break;
    143 	case 'l':
    144 		if (argc == 0)
    145 			*laa.l_devname = '\0';
    146 		else
    147 			strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
    148 
    149 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    150 			err(3, "DRVLISTDEV");
    151 
    152 		children = laa.l_children;
    153 
    154 		laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
    155 		if (laa.l_childname == NULL)
    156 			err(5, "DRVLISTDEV");
    157 		if (ioctl(fd, DRVLISTDEV, &laa) == -1)
    158 			err(3, "DRVLISTDEV");
    159 		if (laa.l_children > children)
    160 			err(6, "DRVLISTDEV: number of children grew");
    161 
    162 		for (i = 0; i < (int)laa.l_children; i++) {
    163 			if (!nflag) {
    164 				printf("%s ",
    165 				    (argc == 0) ? "root" : laa.l_devname);
    166 			}
    167 			printf("%s\n", laa.l_childname[i]);
    168 		}
    169 		break;
    170 	case 'r':
    171 		memset(&raa, 0, sizeof(raa));
    172 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
    173 		if (attr)
    174 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
    175 		if (argc > 1) {
    176 			locs = malloc((argc - 1) * sizeof(int));
    177 			if (!locs)
    178 				err(5, "malloc int[%d]", argc - 1);
    179 			for (i = 0; i < argc - 1; i++)
    180 				locs[i] = atoi(argv[i + 1]);
    181 			raa.numlocators = argc - 1;
    182 			raa.locators = locs;
    183 		}
    184 
    185 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
    186 			err(3, "DRVRESCANBUS");
    187 		break;
    188 	case 'p':
    189 
    190 		command_dict = prop_dictionary_create();
    191 		args_dict = prop_dictionary_create();
    192 
    193 		string = prop_string_create_cstring_nocopy("get-properties");
    194 		prop_dictionary_set(command_dict, "drvctl-command", string);
    195 		prop_object_release(string);
    196 
    197 		string = prop_string_create_cstring(argv[0]);
    198 		prop_dictionary_set(args_dict, "device-name", string);
    199 		prop_object_release(string);
    200 
    201 		prop_dictionary_set(command_dict, "drvctl-arguments",
    202 				    args_dict);
    203 		prop_object_release(args_dict);
    204 
    205 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    206 						     DRVCTLCOMMAND,
    207 						     &results_dict);
    208 		prop_object_release(command_dict);
    209 		if (res)
    210 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
    211 
    212 		number = prop_dictionary_get(results_dict, "drvctl-error");
    213 		if (prop_number_integer_value(number) != 0) {
    214 			errx(3, "get-properties: %s",
    215 			    strerror((int)prop_number_integer_value(number)));
    216 		}
    217 
    218 		data_dict = prop_dictionary_get(results_dict,
    219 						"drvctl-result-data");
    220 		if (data_dict == NULL) {
    221 			errx(3, "get-properties: failed to return result data");
    222 		}
    223 
    224 		if (argc == 1) {
    225 			xml = prop_dictionary_externalize(data_dict);
    226 			printf("Properties for device `%s':\n%s",
    227 			       argv[0], xml);
    228 			free(xml);
    229 		} else {
    230 			for (i = 1; i < argc; i++)
    231 				extract_property(data_dict, argv[i]);
    232 		}
    233 
    234 		prop_object_release(results_dict);
    235 		break;
    236 	default:
    237 		errx(4, "unknown command");
    238 	}
    239 
    240 	return (0);
    241 }
    242 
    243 static void
    244 extract_property(prop_dictionary_t dict, const char *prop)
    245 {
    246 	char *s, *p, *cur, *ep = NULL, *xml;
    247 	prop_object_t obj;
    248 
    249 	s = strdup(prop);
    250 	p = strtok_r(s, "/", &ep);
    251 	while (p) {
    252 		cur = p;
    253 		p = strtok_r(NULL, "/", &ep);
    254 		if (p) {
    255 			if (prop_dictionary_get_dict(dict, cur, &dict) == false)
    256 				exit(EXIT_FAILURE);
    257 		} else {
    258 			obj = prop_dictionary_get(dict, cur);
    259 			if (obj == NULL)
    260 				exit(EXIT_FAILURE);
    261 			switch (prop_object_type(obj)) {
    262 			case PROP_TYPE_BOOL:
    263 				printf("%s\n",
    264 				    prop_bool_true(obj) ? "true" : "false");
    265 				break;
    266 			case PROP_TYPE_NUMBER:
    267 				printf("%" PRId64 "\n",
    268 				    prop_number_integer_value(obj));
    269 				break;
    270 			case PROP_TYPE_STRING:
    271 				printf("%s\n",
    272 				    prop_string_cstring_nocopy(obj));
    273 				break;
    274 			case PROP_TYPE_DICTIONARY:
    275 				xml = prop_dictionary_externalize(obj);
    276 				printf("%s", xml);
    277 				free(xml);
    278 				break;
    279 			default:
    280 				fprintf(stderr, "unhandled type %d\n",
    281 				    prop_object_type(obj));
    282 				exit(EXIT_FAILURE);
    283 			}
    284 		}
    285 	}
    286 
    287 	free(s);
    288 }
    289