xref: /netbsd-src/sbin/drvctl/drvctl.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /* $NetBSD: drvctl.c,v 1.16 2012/01/17 08:22:09 wiz 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 [prop]\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 
230 	s = strdup(prop);
231 	p = strtok_r(s, "/", &ep);
232 	while (p) {
233 		cur = p;
234 		p = strtok_r(NULL, "/", &ep);
235 		if (p) {
236 			if (prop_dictionary_get_dict(dict, cur, &dict) == false)
237 				exit(EXIT_FAILURE);
238 		} else {
239 			obj = prop_dictionary_get(dict, cur);
240 			display_object(obj, nflag);
241 		}
242 	}
243 
244 	free(s);
245 }
246 
247 static void
248 display_object(prop_object_t obj, bool nflag)
249 {
250 	char *xml;
251 	prop_object_t next_obj;
252 	prop_object_iterator_t iter;
253 
254 	if (obj == NULL)
255 		exit(EXIT_FAILURE);
256 	switch (prop_object_type(obj)) {
257 	case PROP_TYPE_BOOL:
258 		printf("%s\n", prop_bool_true(obj) ? "true" : "false");
259 		break;
260 	case PROP_TYPE_NUMBER:
261 		printf("%" PRId64 "\n", prop_number_integer_value(obj));
262 		break;
263 	case PROP_TYPE_STRING:
264 		printf("%s\n", prop_string_cstring_nocopy(obj));
265 		break;
266 	case PROP_TYPE_DICTIONARY:
267 		xml = prop_dictionary_externalize(obj);
268 		printf("%s", xml);
269 		free(xml);
270 		break;
271 	case PROP_TYPE_ARRAY:
272 		iter = prop_array_iterator(obj);
273 		if (!nflag)
274 			printf("Array:\n");
275 		while ((next_obj = prop_object_iterator_next(iter)) != NULL)
276 			display_object(next_obj, nflag);
277 		break;
278 	default:
279 		fprintf(stderr, "unhandled type %d\n", prop_object_type(obj));
280 		exit(EXIT_FAILURE);
281 	}
282 }
283 
284 static void
285 list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
286 {
287 	struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
288 				  .l_children = 0};
289 	size_t children;
290 	int i, n;
291 
292 	if (dvname == NULL) {
293 		if (depth > 0)
294 			return;
295 		*laa.l_devname = '\0';
296 	} else {
297 		strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
298 	}
299 
300 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
301 		err(3, "DRVLISTDEV");
302 
303 	children = laa.l_children;
304 
305 	laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
306 	if (laa.l_childname == NULL)
307 		err(5, "DRVLISTDEV");
308 	if (ioctl(fd, DRVLISTDEV, &laa) == -1)
309 		err(3, "DRVLISTDEV");
310 	if (laa.l_children > children)
311 		err(6, "DRVLISTDEV: number of children grew");
312 
313 	for (i = 0; i < (int)laa.l_children; i++) {
314 		for (n = 0; n < depth; n++)
315 			printf("  ");
316 		if (!nflag) {
317 			printf("%s ",
318 			    (dvname == NULL) ? "root" : laa.l_devname);
319 		}
320 		printf("%s\n", laa.l_childname[i]);
321 		if (tflag) {
322 			list_children(fd, laa.l_childname[i], nflag,
323 			    tflag, depth + 1);
324 		}
325 	}
326 
327 	free(laa.l_childname);
328 }
329