1 /* $NetBSD: drvctl.c,v 1.13 2011/08/29 14:34:59 joerg 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 printf("Properties for device `%s':\n%s", 204 argv[0], xml); 205 free(xml); 206 } else { 207 for (i = 1; i < argc; i++) 208 extract_property(data_dict, argv[i]); 209 } 210 211 prop_object_release(results_dict); 212 break; 213 default: 214 errx(4, "unknown command"); 215 } 216 217 return (0); 218 } 219 220 static void 221 extract_property(prop_dictionary_t dict, const char *prop) 222 { 223 char *s, *p, *cur, *ep = NULL, *xml; 224 prop_object_t obj; 225 226 s = strdup(prop); 227 p = strtok_r(s, "/", &ep); 228 while (p) { 229 cur = p; 230 p = strtok_r(NULL, "/", &ep); 231 if (p) { 232 if (prop_dictionary_get_dict(dict, cur, &dict) == false) 233 exit(EXIT_FAILURE); 234 } else { 235 obj = prop_dictionary_get(dict, cur); 236 if (obj == NULL) 237 exit(EXIT_FAILURE); 238 switch (prop_object_type(obj)) { 239 case PROP_TYPE_BOOL: 240 printf("%s\n", 241 prop_bool_true(obj) ? "true" : "false"); 242 break; 243 case PROP_TYPE_NUMBER: 244 printf("%" PRId64 "\n", 245 prop_number_integer_value(obj)); 246 break; 247 case PROP_TYPE_STRING: 248 printf("%s\n", 249 prop_string_cstring_nocopy(obj)); 250 break; 251 case PROP_TYPE_DICTIONARY: 252 xml = prop_dictionary_externalize(obj); 253 printf("%s", xml); 254 free(xml); 255 break; 256 default: 257 fprintf(stderr, "unhandled type %d\n", 258 prop_object_type(obj)); 259 exit(EXIT_FAILURE); 260 } 261 } 262 } 263 264 free(s); 265 } 266 267 static void 268 list_children(int fd, char *dvname, bool nflag, bool tflag, int depth) 269 { 270 struct devlistargs laa = {.l_devname = "", .l_childname = NULL, 271 .l_children = 0}; 272 size_t children; 273 int i, n; 274 275 if (dvname == NULL) { 276 if (depth > 0) 277 return; 278 *laa.l_devname = '\0'; 279 } else { 280 strlcpy(laa.l_devname, dvname, sizeof(laa.l_devname)); 281 } 282 283 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 284 err(3, "DRVLISTDEV"); 285 286 children = laa.l_children; 287 288 laa.l_childname = malloc(children * sizeof(laa.l_childname[0])); 289 if (laa.l_childname == NULL) 290 err(5, "DRVLISTDEV"); 291 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 292 err(3, "DRVLISTDEV"); 293 if (laa.l_children > children) 294 err(6, "DRVLISTDEV: number of children grew"); 295 296 for (i = 0; i < (int)laa.l_children; i++) { 297 for (n = 0; n < depth; n++) 298 printf(" "); 299 if (!nflag) { 300 printf("%s ", 301 (dvname == NULL) ? "root" : laa.l_devname); 302 } 303 printf("%s\n", laa.l_childname[i]); 304 if (tflag) { 305 list_children(fd, laa.l_childname[i], nflag, 306 tflag, depth + 1); 307 } 308 } 309 310 free(laa.l_childname); 311 } 312