xref: /netbsd-src/sbin/drvctl/drvctl.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* $NetBSD: drvctl.c,v 1.18 2015/12/07 03:34:00 pgoyette 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:dlnprt"
41 
42 #define	OPEN_MODE(mode)							\
43 	(((mode) == 'd' || (mode) == 'r') ? O_RDWR			\
44 					  : O_RDONLY)
45 
46 __dead static void usage(void);
47 static void extract_property(prop_dictionary_t, const char *, bool);
48 static void display_object(prop_object_t, bool);
49 static void list_children(int, char *, bool, bool, int);
50 
51 static void
52 usage(void)
53 {
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",
62 	    getprogname(), getprogname(), getprogname(), getprogname(),
63 	    getprogname(), getprogname(), getprogname());
64 	exit(1);
65 }
66 
67 int
68 main(int argc, char **argv)
69 {
70 	bool nflag = false, tflag = false;
71 	int c, mode;
72 	char *attr = 0;
73 	extern char *optarg;
74 	extern int optind;
75 	int fd, res;
76 	struct devpmargs paa = {.devname = "", .flags = 0};
77 	struct devdetachargs daa;
78 	struct devrescanargs raa;
79 	int *locs, i;
80 	prop_dictionary_t command_dict, args_dict, results_dict,
81 			  data_dict;
82 	prop_string_t string;
83 	prop_number_t number;
84 	char *xml;
85 
86 	mode = 0;
87 	while ((c = getopt(argc, argv, OPTS)) != -1) {
88 		switch (c) {
89 		case 'Q':
90 		case 'R':
91 		case 'S':
92 		case 'd':
93 		case 'l':
94 		case 'p':
95 		case 'r':
96 			mode = c;
97 			break;
98 		case 'a':
99 			attr = optarg;
100 			break;
101 		case 'n':
102 			nflag = true;
103 			break;
104 		case 't':
105 			tflag = nflag = true;
106 			break;
107 		case '?':
108 		default:
109 			usage();
110 		}
111 	}
112 
113 	argc -= optind;
114 	argv += optind;
115 
116 	if ((argc < 1 && mode != 'l') || mode == 0)
117 		usage();
118 
119 	fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
120 	if (fd < 0)
121 		err(2, "open %s", DRVCTLDEV);
122 
123 	switch (mode) {
124 	case 'Q':
125 		paa.flags = DEVPM_F_SUBTREE;
126 		/*FALLTHROUGH*/
127 	case 'R':
128 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
129 
130 		if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
131 			err(3, "DRVRESUMEDEV");
132 		break;
133 	case 'S':
134 		strlcpy(paa.devname, argv[0], sizeof(paa.devname));
135 
136 		if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
137 			err(3, "DRVSUSPENDDEV");
138 		break;
139 	case 'd':
140 		strlcpy(daa.devname, argv[0], sizeof(daa.devname));
141 
142 		if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
143 			err(3, "DRVDETACHDEV");
144 		break;
145 	case 'l':
146 		list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
147 		break;
148 	case 'r':
149 		memset(&raa, 0, sizeof(raa));
150 		strlcpy(raa.busname, argv[0], sizeof(raa.busname));
151 		if (attr)
152 			strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
153 		if (argc > 1) {
154 			locs = malloc((argc - 1) * sizeof(int));
155 			if (!locs)
156 				err(5, "malloc int[%d]", argc - 1);
157 			for (i = 0; i < argc - 1; i++)
158 				locs[i] = atoi(argv[i + 1]);
159 			raa.numlocators = argc - 1;
160 			raa.locators = locs;
161 		}
162 
163 		if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
164 			err(3, "DRVRESCANBUS");
165 		break;
166 	case 'p':
167 
168 		command_dict = prop_dictionary_create();
169 		args_dict = prop_dictionary_create();
170 
171 		string = prop_string_create_cstring_nocopy("get-properties");
172 		prop_dictionary_set(command_dict, "drvctl-command", string);
173 		prop_object_release(string);
174 
175 		string = prop_string_create_cstring(argv[0]);
176 		prop_dictionary_set(args_dict, "device-name", string);
177 		prop_object_release(string);
178 
179 		prop_dictionary_set(command_dict, "drvctl-arguments",
180 				    args_dict);
181 		prop_object_release(args_dict);
182 
183 		res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
184 						     DRVCTLCOMMAND,
185 						     &results_dict);
186 		prop_object_release(command_dict);
187 		if (res)
188 			errx(3, "DRVCTLCOMMAND: %s", strerror(res));
189 
190 		number = prop_dictionary_get(results_dict, "drvctl-error");
191 		if (prop_number_integer_value(number) != 0) {
192 			errx(3, "get-properties: %s",
193 			    strerror((int)prop_number_integer_value(number)));
194 		}
195 
196 		data_dict = prop_dictionary_get(results_dict,
197 						"drvctl-result-data");
198 		if (data_dict == NULL) {
199 			errx(3, "get-properties: failed to return result data");
200 		}
201 
202 		if (argc == 1) {
203 			xml = prop_dictionary_externalize(data_dict);
204 			if (!nflag) {
205 				printf("Properties for device `%s':\n",
206 				    argv[0]);
207 			}
208 			printf("%s", xml);
209 			free(xml);
210 		} else {
211 			for (i = 1; i < argc; i++)
212 				extract_property(data_dict, argv[i], nflag);
213 		}
214 
215 		prop_object_release(results_dict);
216 		break;
217 	default:
218 		errx(4, "unknown command");
219 	}
220 
221 	return (0);
222 }
223 
224 static void
225 extract_property(prop_dictionary_t dict, const char *prop, bool nflag)
226 {
227 	char *s, *p, *cur, *ep = NULL;
228 	prop_object_t obj;
229 	unsigned long ind;
230 
231 	obj = dict;
232 	cur = NULL;
233 	s = strdup(prop);
234 	p = strtok_r(s, "/", &ep);
235 	while (p) {
236 		cur = p;
237 		p = strtok_r(NULL, "/", &ep);
238 
239 		switch (prop_object_type(obj)) {
240 		case PROP_TYPE_DICTIONARY:
241 			obj = prop_dictionary_get(obj, cur);
242 			if (obj == NULL)
243 				exit(EXIT_FAILURE);
244 			break;
245 		case PROP_TYPE_ARRAY:
246 			ind = strtoul(cur, NULL, 0);
247 			obj = prop_array_get(obj, ind);
248 			if (obj == NULL)
249 				exit(EXIT_FAILURE);
250 			break;
251 		default:
252 			fprintf(stderr, "select neither dict nor array with '%s'\n", cur);
253 			exit(EXIT_FAILURE);
254 		}
255 	}
256 
257 	if (obj != NULL && cur != NULL)
258 		display_object(obj, nflag);
259 
260 	free(s);
261 }
262 
263 static void
264 display_object(prop_object_t obj, bool nflag)
265 {
266 	char *xml;
267 	prop_object_t next_obj;
268 	prop_object_iterator_t iter;
269 
270 	if (obj == NULL)
271 		exit(EXIT_FAILURE);
272 	switch (prop_object_type(obj)) {
273 	case PROP_TYPE_BOOL:
274 		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
275 		break;
276 	case PROP_TYPE_NUMBER:
277 		printf("%" PRId64 "\n", prop_number_integer_value(obj));
278 		break;
279 	case PROP_TYPE_STRING:
280 		printf("%s\n", prop_string_cstring_nocopy(obj));
281 		break;
282 	case PROP_TYPE_DICTIONARY:
283 		xml = prop_dictionary_externalize(obj);
284 		printf("%s", xml);
285 		free(xml);
286 		break;
287 	case PROP_TYPE_ARRAY:
288 		iter = prop_array_iterator(obj);
289 		if (!nflag)
290 			printf("Array:\n");
291 		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
292 			display_object(next_obj, nflag);
293 		break;
294 	default:
295 		fprintf(stderr, "unhandled type %d\n", prop_object_type(obj));
296 		exit(EXIT_FAILURE);
297 	}
298 }
299 
300 static void
301 list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
302 {
303 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
304 				  .l_children = 0};
305 	size_t children;
306 	int i, n;
307 
308 	if (dvname == NULL) {
309 		if (depth > 0)
310 			return;
311 		*laa.l_devname = '\0';
312 	} else {
313 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
314 	}
315 
316 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
317 		err(3, "DRVLISTDEV");
318 
319 	children = laa.l_children;
320 
321 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
322 	if (laa.l_childname == NULL)
323 		err(5, "DRVLISTDEV");
324 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
325 		err(3, "DRVLISTDEV");
326 	if (laa.l_children > children)
327 		err(6, "DRVLISTDEV: number of children grew");
328 
329 	for (i = 0; i < (int)laa.l_children; i++) {
330 		for (n = 0; n < depth; n++)
331 			printf("  ");
332 		if (!nflag) {
333 			printf("%s ",
334 			    (dvname == NULL) ? "root" : laa.l_devname);
335 		}
336 		printf("%s\n", laa.l_childname[i]);
337 		if (tflag) {
338 			list_children(fd, laa.l_childname[i], nflag,
339 			    tflag, depth + 1);
340 		}
341 	}
342 
343 	free(laa.l_childname);
344 }
345