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