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