xref: /netbsd-src/sbin/drvctl/drvctl.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /* $NetBSD: drvctl.c,v 1.21 2020/06/11 13:49:57 thorpej 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 				exit(EXIT_FAILURE);
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 				exit(EXIT_FAILURE);
240 			break;
241 		default:
242 			errx(EXIT_FAILURE, "Select neither dict nor array with"
243 			" `%s'", cur);
244 		}
245 	}
246 
247 	if (obj != NULL && cur != NULL)
248 		display_object(obj, nflag);
249 
250 	free(s);
251 }
252 
253 static void
254 display_object(prop_object_t obj, bool nflag)
255 {
256 	char *xml;
257 	prop_object_t next_obj;
258 	prop_object_iterator_t iter;
259 
260 	if (obj == NULL)
261 		exit(EXIT_FAILURE);
262 	switch (prop_object_type(obj)) {
263 	case PROP_TYPE_BOOL:
264 		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
265 		break;
266 	case PROP_TYPE_NUMBER:
267 		printf("%" PRId64 "\n", prop_number_signed_value(obj));
268 		break;
269 	case PROP_TYPE_STRING:
270 		printf("%s\n", prop_string_value(obj));
271 		break;
272 	case PROP_TYPE_DICTIONARY:
273 		xml = prop_dictionary_externalize(obj);
274 		printf("%s", xml);
275 		free(xml);
276 		break;
277 	case PROP_TYPE_ARRAY:
278 		iter = prop_array_iterator(obj);
279 		if (!nflag)
280 			printf("Array:\n");
281 		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
282 			display_object(next_obj, nflag);
283 		break;
284 	default:
285 		errx(EXIT_FAILURE, "Unhandled type %d", prop_object_type(obj));
286 	}
287 }
288 
289 static void
290 list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
291 {
292 	struct devlistargs laa = {
293 	    .l_devname = "", .l_childname = NULL, .l_children = 0
294 	};
295 	size_t children;
296 	int i, n;
297 
298 	if (dvname == NULL) {
299 		if (depth > 0)
300 			return;
301 		*laa.l_devname = '\0';
302 	} else {
303 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
304 	}
305 
306 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
307 		err(EXIT_FAILURE, "DRVLISTDEV");
308 
309 	children = laa.l_children;
310 
311 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
312 	if (laa.l_childname == NULL)
313 		err(EXIT_FAILURE, "DRVLISTDEV");
314 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
315 		err(EXIT_FAILURE, "DRVLISTDEV");
316 	if (laa.l_children > children)
317 		err(EXIT_FAILURE, "DRVLISTDEV: number of children grew");
318 
319 	for (i = 0; i < (int)laa.l_children; i++) {
320 		for (n = 0; n < depth; n++)
321 			printf("  ");
322 		if (!nflag) {
323 			printf("%s ",
324 			    (dvname == NULL) ? "root" : laa.l_devname);
325 		}
326 		printf("%s\n", laa.l_childname[i]);
327 		if (tflag) {
328 			list_children(fd, laa.l_childname[i], nflag,
329 			    tflag, depth + 1);
330 		}
331 	}
332 
333 	free(laa.l_childname);
334 }
335