Tue Jan 17 08:22:09 2012 UTC ()
Add -n to usage.


(wiz)
diff -r1.15 -r1.16 src/sbin/drvctl/drvctl.c

cvs diff -r1.15 -r1.16 src/sbin/drvctl/drvctl.c (switch to unified diff)

--- src/sbin/drvctl/drvctl.c 2012/01/16 19:43:50 1.15
+++ src/sbin/drvctl/drvctl.c 2012/01/17 08:22:09 1.16
@@ -1,328 +1,328 @@ @@ -1,328 +1,328 @@
1/* $NetBSD: drvctl.c,v 1.15 2012/01/16 19:43:50 pgoyette Exp $ */ 1/* $NetBSD: drvctl.c,v 1.16 2012/01/17 08:22:09 wiz Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2004 4 * Copyright (c) 2004
5 * Matthias Drochner. All rights reserved. 5 * Matthias Drochner. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer. 11 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 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 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#include <inttypes.h> 29#include <inttypes.h>
30#include <stdbool.h> 30#include <stdbool.h>
31#include <stdio.h> 31#include <stdio.h>
32#include <stdlib.h> 32#include <stdlib.h>
33#include <unistd.h> 33#include <unistd.h>
34#include <err.h> 34#include <err.h>
35#include <fcntl.h> 35#include <fcntl.h>
36#include <string.h> 36#include <string.h>
37#include <sys/ioctl.h> 37#include <sys/ioctl.h>
38#include <sys/drvctlio.h> 38#include <sys/drvctlio.h>
39 39
40#define OPTS "QRSa:dlnprt" 40#define OPTS "QRSa:dlnprt"
41 41
42#define OPEN_MODE(mode) \ 42#define OPEN_MODE(mode) \
43 (((mode) == 'd' || (mode) == 'r') ? O_RDWR \ 43 (((mode) == 'd' || (mode) == 'r') ? O_RDWR \
44 : O_RDONLY) 44 : O_RDONLY)
45 45
46__dead static void usage(void); 46__dead static void usage(void);
47static void extract_property(prop_dictionary_t, const char *, bool); 47static void extract_property(prop_dictionary_t, const char *, bool);
48static void display_object(prop_object_t, bool); 48static void display_object(prop_object_t, bool);
49static void list_children(int, char *, bool, bool, int); 49static void list_children(int, char *, bool, bool, int);
50 50
51static void 51static void
52usage(void) 52usage(void)
53{ 53{
54 54
55 fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n" 55 fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
56 " %s -d device\n" 56 " %s -d device\n"
57 " %s [-nt] -l [device]\n" 57 " %s [-nt] -l [device]\n"
58 " %s -p device [prop]\n" 58 " %s [-n] -p device [prop]\n"
59 " %s -Q device\n" 59 " %s -Q device\n"
60 " %s -R device\n" 60 " %s -R device\n"
61 " %s -S device\n", 61 " %s -S device\n",
62 getprogname(), getprogname(), getprogname(), getprogname(), 62 getprogname(), getprogname(), getprogname(), getprogname(),
63 getprogname(), getprogname(), getprogname()); 63 getprogname(), getprogname(), getprogname());
64 exit(1); 64 exit(1);
65} 65}
66 66
67int 67int
68main(int argc, char **argv) 68main(int argc, char **argv)
69{ 69{
70 bool nflag = false, tflag = false; 70 bool nflag = false, tflag = false;
71 int c, mode; 71 int c, mode;
72 char *attr = 0; 72 char *attr = 0;
73 extern char *optarg; 73 extern char *optarg;
74 extern int optind; 74 extern int optind;
75 int fd, res; 75 int fd, res;
76 struct devpmargs paa = {.devname = "", .flags = 0}; 76 struct devpmargs paa = {.devname = "", .flags = 0};
77 struct devdetachargs daa; 77 struct devdetachargs daa;
78 struct devrescanargs raa; 78 struct devrescanargs raa;
79 int *locs, i; 79 int *locs, i;
80 prop_dictionary_t command_dict, args_dict, results_dict, 80 prop_dictionary_t command_dict, args_dict, results_dict,
81 data_dict; 81 data_dict;
82 prop_string_t string; 82 prop_string_t string;
83 prop_number_t number; 83 prop_number_t number;
84 char *xml; 84 char *xml;
85 85
86 mode = 0; 86 mode = 0;
87 while ((c = getopt(argc, argv, OPTS)) != -1) { 87 while ((c = getopt(argc, argv, OPTS)) != -1) {
88 switch (c) { 88 switch (c) {
89 case 'Q': 89 case 'Q':
90 case 'R': 90 case 'R':
91 case 'S': 91 case 'S':
92 case 'd': 92 case 'd':
93 case 'l': 93 case 'l':
94 case 'p': 94 case 'p':
95 case 'r': 95 case 'r':
96 mode = c; 96 mode = c;
97 break; 97 break;
98 case 'a': 98 case 'a':
99 attr = optarg; 99 attr = optarg;
100 break; 100 break;
101 case 'n': 101 case 'n':
102 nflag = true; 102 nflag = true;
103 break; 103 break;
104 case 't': 104 case 't':
105 tflag = nflag = true; 105 tflag = nflag = true;
106 break; 106 break;
107 case '?': 107 case '?':
108 default: 108 default:
109 usage(); 109 usage();
110 } 110 }
111 } 111 }
112 112
113 argc -= optind; 113 argc -= optind;
114 argv += optind; 114 argv += optind;
115 115
116 if ((argc < 1 && mode != 'l') || mode == 0) 116 if ((argc < 1 && mode != 'l') || mode == 0)
117 usage(); 117 usage();
118 118
119 fd = open(DRVCTLDEV, OPEN_MODE(mode), 0); 119 fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
120 if (fd < 0) 120 if (fd < 0)
121 err(2, "open %s", DRVCTLDEV); 121 err(2, "open %s", DRVCTLDEV);
122 122
123 switch (mode) { 123 switch (mode) {
124 case 'Q': 124 case 'Q':
125 paa.flags = DEVPM_F_SUBTREE; 125 paa.flags = DEVPM_F_SUBTREE;
126 /*FALLTHROUGH*/ 126 /*FALLTHROUGH*/
127 case 'R': 127 case 'R':
128 strlcpy(paa.devname, argv[0], sizeof(paa.devname)); 128 strlcpy(paa.devname, argv[0], sizeof(paa.devname));
129 129
130 if (ioctl(fd, DRVRESUMEDEV, &paa) == -1) 130 if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
131 err(3, "DRVRESUMEDEV"); 131 err(3, "DRVRESUMEDEV");
132 break; 132 break;
133 case 'S': 133 case 'S':
134 strlcpy(paa.devname, argv[0], sizeof(paa.devname)); 134 strlcpy(paa.devname, argv[0], sizeof(paa.devname));
135 135
136 if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1) 136 if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
137 err(3, "DRVSUSPENDDEV"); 137 err(3, "DRVSUSPENDDEV");
138 break; 138 break;
139 case 'd': 139 case 'd':
140 strlcpy(daa.devname, argv[0], sizeof(daa.devname)); 140 strlcpy(daa.devname, argv[0], sizeof(daa.devname));
141 141
142 if (ioctl(fd, DRVDETACHDEV, &daa) == -1) 142 if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
143 err(3, "DRVDETACHDEV"); 143 err(3, "DRVDETACHDEV");
144 break; 144 break;
145 case 'l': 145 case 'l':
146 list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0); 146 list_children(fd, argc ? argv[0] : NULL, nflag, tflag, 0);
147 break; 147 break;
148 case 'r': 148 case 'r':
149 memset(&raa, 0, sizeof(raa)); 149 memset(&raa, 0, sizeof(raa));
150 strlcpy(raa.busname, argv[0], sizeof(raa.busname)); 150 strlcpy(raa.busname, argv[0], sizeof(raa.busname));
151 if (attr) 151 if (attr)
152 strlcpy(raa.ifattr, attr, sizeof(raa.ifattr)); 152 strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
153 if (argc > 1) { 153 if (argc > 1) {
154 locs = malloc((argc - 1) * sizeof(int)); 154 locs = malloc((argc - 1) * sizeof(int));
155 if (!locs) 155 if (!locs)
156 err(5, "malloc int[%d]", argc - 1); 156 err(5, "malloc int[%d]", argc - 1);
157 for (i = 0; i < argc - 1; i++) 157 for (i = 0; i < argc - 1; i++)
158 locs[i] = atoi(argv[i + 1]); 158 locs[i] = atoi(argv[i + 1]);
159 raa.numlocators = argc - 1; 159 raa.numlocators = argc - 1;
160 raa.locators = locs; 160 raa.locators = locs;
161 } 161 }
162 162
163 if (ioctl(fd, DRVRESCANBUS, &raa) == -1) 163 if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
164 err(3, "DRVRESCANBUS"); 164 err(3, "DRVRESCANBUS");
165 break; 165 break;
166 case 'p': 166 case 'p':
167 167
168 command_dict = prop_dictionary_create(); 168 command_dict = prop_dictionary_create();
169 args_dict = prop_dictionary_create(); 169 args_dict = prop_dictionary_create();
170 170
171 string = prop_string_create_cstring_nocopy("get-properties"); 171 string = prop_string_create_cstring_nocopy("get-properties");
172 prop_dictionary_set(command_dict, "drvctl-command", string); 172 prop_dictionary_set(command_dict, "drvctl-command", string);
173 prop_object_release(string); 173 prop_object_release(string);
174 174
175 string = prop_string_create_cstring(argv[0]); 175 string = prop_string_create_cstring(argv[0]);
176 prop_dictionary_set(args_dict, "device-name", string); 176 prop_dictionary_set(args_dict, "device-name", string);
177 prop_object_release(string); 177 prop_object_release(string);
178 178
179 prop_dictionary_set(command_dict, "drvctl-arguments", 179 prop_dictionary_set(command_dict, "drvctl-arguments",
180 args_dict); 180 args_dict);
181 prop_object_release(args_dict); 181 prop_object_release(args_dict);
182 182
183 res = prop_dictionary_sendrecv_ioctl(command_dict, fd, 183 res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
184 DRVCTLCOMMAND, 184 DRVCTLCOMMAND,
185 &results_dict); 185 &results_dict);
186 prop_object_release(command_dict); 186 prop_object_release(command_dict);
187 if (res) 187 if (res)
188 errx(3, "DRVCTLCOMMAND: %s", strerror(res)); 188 errx(3, "DRVCTLCOMMAND: %s", strerror(res));
189 189
190 number = prop_dictionary_get(results_dict, "drvctl-error"); 190 number = prop_dictionary_get(results_dict, "drvctl-error");
191 if (prop_number_integer_value(number) != 0) { 191 if (prop_number_integer_value(number) != 0) {
192 errx(3, "get-properties: %s", 192 errx(3, "get-properties: %s",
193 strerror((int)prop_number_integer_value(number))); 193 strerror((int)prop_number_integer_value(number)));
194 } 194 }
195 195
196 data_dict = prop_dictionary_get(results_dict, 196 data_dict = prop_dictionary_get(results_dict,
197 "drvctl-result-data"); 197 "drvctl-result-data");
198 if (data_dict == NULL) { 198 if (data_dict == NULL) {
199 errx(3, "get-properties: failed to return result data"); 199 errx(3, "get-properties: failed to return result data");
200 } 200 }
201 201
202 if (argc == 1) { 202 if (argc == 1) {
203 xml = prop_dictionary_externalize(data_dict); 203 xml = prop_dictionary_externalize(data_dict);
204 if (!nflag) { 204 if (!nflag) {
205 printf("Properties for device `%s':\n", 205 printf("Properties for device `%s':\n",
206 argv[0]); 206 argv[0]);
207 } 207 }
208 printf("%s", xml); 208 printf("%s", xml);
209 free(xml); 209 free(xml);
210 } else { 210 } else {
211 for (i = 1; i < argc; i++) 211 for (i = 1; i < argc; i++)
212 extract_property(data_dict, argv[i], nflag); 212 extract_property(data_dict, argv[i], nflag);
213 } 213 }
214 214
215 prop_object_release(results_dict); 215 prop_object_release(results_dict);
216 break; 216 break;
217 default: 217 default:
218 errx(4, "unknown command"); 218 errx(4, "unknown command");
219 } 219 }
220 220
221 return (0); 221 return (0);
222} 222}
223 223
224static void 224static void
225extract_property(prop_dictionary_t dict, const char *prop, bool nflag) 225extract_property(prop_dictionary_t dict, const char *prop, bool nflag)
226{ 226{
227 char *s, *p, *cur, *ep = NULL; 227 char *s, *p, *cur, *ep = NULL;
228 prop_object_t obj; 228 prop_object_t obj;
229 229
230 s = strdup(prop); 230 s = strdup(prop);
231 p = strtok_r(s, "/", &ep); 231 p = strtok_r(s, "/", &ep);
232 while (p) { 232 while (p) {
233 cur = p; 233 cur = p;
234 p = strtok_r(NULL, "/", &ep); 234 p = strtok_r(NULL, "/", &ep);
235 if (p) { 235 if (p) {
236 if (prop_dictionary_get_dict(dict, cur, &dict) == false) 236 if (prop_dictionary_get_dict(dict, cur, &dict) == false)
237 exit(EXIT_FAILURE); 237 exit(EXIT_FAILURE);
238 } else { 238 } else {
239 obj = prop_dictionary_get(dict, cur); 239 obj = prop_dictionary_get(dict, cur);
240 display_object(obj, nflag); 240 display_object(obj, nflag);
241 } 241 }
242 } 242 }
243 243
244 free(s); 244 free(s);
245} 245}
246 246
247static void 247static void
248display_object(prop_object_t obj, bool nflag) 248display_object(prop_object_t obj, bool nflag)
249{ 249{
250 char *xml; 250 char *xml;
251 prop_object_t next_obj; 251 prop_object_t next_obj;
252 prop_object_iterator_t iter; 252 prop_object_iterator_t iter;
253 253
254 if (obj == NULL) 254 if (obj == NULL)
255 exit(EXIT_FAILURE); 255 exit(EXIT_FAILURE);
256 switch (prop_object_type(obj)) { 256 switch (prop_object_type(obj)) {
257 case PROP_TYPE_BOOL: 257 case PROP_TYPE_BOOL:
258 printf("%s\n", prop_bool_true(obj) ? "true" : "false"); 258 printf("%s\n", prop_bool_true(obj) ? "true" : "false");
259 break; 259 break;
260 case PROP_TYPE_NUMBER: 260 case PROP_TYPE_NUMBER:
261 printf("%" PRId64 "\n", prop_number_integer_value(obj)); 261 printf("%" PRId64 "\n", prop_number_integer_value(obj));
262 break; 262 break;
263 case PROP_TYPE_STRING: 263 case PROP_TYPE_STRING:
264 printf("%s\n", prop_string_cstring_nocopy(obj)); 264 printf("%s\n", prop_string_cstring_nocopy(obj));
265 break; 265 break;
266 case PROP_TYPE_DICTIONARY: 266 case PROP_TYPE_DICTIONARY:
267 xml = prop_dictionary_externalize(obj); 267 xml = prop_dictionary_externalize(obj);
268 printf("%s", xml); 268 printf("%s", xml);
269 free(xml); 269 free(xml);
270 break; 270 break;
271 case PROP_TYPE_ARRAY: 271 case PROP_TYPE_ARRAY:
272 iter = prop_array_iterator(obj); 272 iter = prop_array_iterator(obj);
273 if (!nflag) 273 if (!nflag)
274 printf("Array:\n"); 274 printf("Array:\n");
275 while ((next_obj = prop_object_iterator_next(iter)) != NULL) 275 while ((next_obj = prop_object_iterator_next(iter)) != NULL)
276 display_object(next_obj, nflag); 276 display_object(next_obj, nflag);
277 break; 277 break;
278 default: 278 default:
279 fprintf(stderr, "unhandled type %d\n", prop_object_type(obj)); 279 fprintf(stderr, "unhandled type %d\n", prop_object_type(obj));
280 exit(EXIT_FAILURE); 280 exit(EXIT_FAILURE);
281 } 281 }
282} 282}
283 283
284static void 284static void
285list_children(int fd, char *dvname, bool nflag, bool tflag, int depth) 285list_children(int fd, char *dvname, bool nflag, bool tflag, int depth)
286{ 286{
287 struct devlistargs laa = {.l_devname = "", .l_childname = NULL, 287 struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
288 .l_children = 0}; 288 .l_children = 0};
289 size_t children; 289 size_t children;
290 int i, n; 290 int i, n;
291 291
292 if (dvname == NULL) { 292 if (dvname == NULL) {
293 if (depth > 0) 293 if (depth > 0)
294 return; 294 return;
295 *laa.l_devname = '\0'; 295 *laa.l_devname = '\0';
296 } else { 296 } else {
297 strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname)); 297 strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname));
298 } 298 }
299 299
300 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 300 if (ioctl(fd, DRVLISTDEV, &laa) == -1)
301 err(3, "DRVLISTDEV"); 301 err(3, "DRVLISTDEV");
302 302
303 children = laa.l_children; 303 children = laa.l_children;
304 304
305 laa.l_childname = malloc(children * sizeof(laa.l_childname[0])); 305 laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
306 if (laa.l_childname == NULL) 306 if (laa.l_childname == NULL)
307 err(5, "DRVLISTDEV"); 307 err(5, "DRVLISTDEV");
308 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 308 if (ioctl(fd, DRVLISTDEV, &laa) == -1)
309 err(3, "DRVLISTDEV"); 309 err(3, "DRVLISTDEV");
310 if (laa.l_children > children) 310 if (laa.l_children > children)
311 err(6, "DRVLISTDEV: number of children grew"); 311 err(6, "DRVLISTDEV: number of children grew");
312 312
313 for (i = 0; i < (int)laa.l_children; i++) { 313 for (i = 0; i < (int)laa.l_children; i++) {
314 for (n = 0; n < depth; n++) 314 for (n = 0; n < depth; n++)
315 printf(" "); 315 printf(" ");
316 if (!nflag) { 316 if (!nflag) {
317 printf("%s ", 317 printf("%s ",
318 (dvname == NULL) ? "root" : laa.l_devname); 318 (dvname == NULL) ? "root" : laa.l_devname);
319 } 319 }
320 printf("%s\n", laa.l_childname[i]); 320 printf("%s\n", laa.l_childname[i]);
321 if (tflag) { 321 if (tflag) {
322 list_children(fd, laa.l_childname[i], nflag, 322 list_children(fd, laa.l_childname[i], nflag,
323 tflag, depth + 1); 323 tflag, depth + 1);
324 } 324 }
325 } 325 }
326 326
327 free(laa.l_childname); 327 free(laa.l_childname);
328} 328}