| @@ -1,221 +1,226 @@ | | | @@ -1,221 +1,226 @@ |
1 | /* $NetBSD: drvctl.c,v 1.6 2008/01/27 01:38:33 dyoung Exp $ */ | | 1 | /* $NetBSD: drvctl.c,v 1.6.10.1 2009/05/03 22:49:51 snj 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 <stdio.h> | | 29 | #include <stdio.h> |
30 | #include <stdlib.h> | | 30 | #include <stdlib.h> |
31 | #include <unistd.h> | | 31 | #include <unistd.h> |
32 | #include <err.h> | | 32 | #include <err.h> |
33 | #include <fcntl.h> | | 33 | #include <fcntl.h> |
34 | #include <string.h> | | 34 | #include <string.h> |
35 | #include <sys/ioctl.h> | | 35 | #include <sys/ioctl.h> |
36 | #include <sys/drvctlio.h> | | 36 | #include <sys/drvctlio.h> |
37 | | | 37 | |
38 | #define OPTS "QRSa:dlpr" | | 38 | #define OPTS "QRSa:dlpr" |
39 | | | 39 | |
40 | #define OPEN_MODE(mode) \ | | 40 | #define OPEN_MODE(mode) \ |
41 | (((mode) == 'd' || (mode) == 'r') ? O_RDWR \ | | 41 | (((mode) == 'd' || (mode) == 'r') ? O_RDWR \ |
42 | : O_RDONLY) | | 42 | : O_RDONLY) |
43 | | | 43 | |
44 | static void usage(void); | | 44 | static void usage(void); |
45 | | | 45 | |
46 | static void | | 46 | static void |
47 | usage(void) | | 47 | usage(void) |
48 | { | | 48 | { |
49 | | | 49 | |
50 | fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n" | | 50 | fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n" |
51 | " %s -d device\n" | | 51 | " %s -d device\n" |
52 | " %s -l device\n" | | 52 | " %s -l [device]\n" |
53 | " %s -p device\n" | | 53 | " %s -p device\n" |
54 | " %s -Q device\n" | | 54 | " %s -Q device\n" |
55 | " %s -R device\n" | | 55 | " %s -R device\n" |
56 | " %s -S device\n", | | 56 | " %s -S device\n", |
57 | getprogname(), getprogname(), getprogname(), getprogname(), | | 57 | getprogname(), getprogname(), getprogname(), getprogname(), |
58 | getprogname(), getprogname(), getprogname()); | | 58 | getprogname(), getprogname(), getprogname()); |
59 | exit(1); | | 59 | exit(1); |
60 | } | | 60 | } |
61 | | | 61 | |
62 | int | | 62 | int |
63 | main(int argc, char **argv) | | 63 | main(int argc, char **argv) |
64 | { | | 64 | { |
65 | int c, mode; | | 65 | int c, mode; |
66 | char *attr = 0; | | 66 | char *attr = 0; |
67 | extern char *optarg; | | 67 | extern char *optarg; |
68 | extern int optind; | | 68 | extern int optind; |
69 | int fd, res; | | 69 | int fd, res; |
70 | size_t children; | | 70 | size_t children; |
71 | struct devpmargs paa = {.devname = "", .flags = 0}; | | 71 | struct devpmargs paa = {.devname = "", .flags = 0}; |
72 | struct devlistargs laa = {.l_devname = "", .l_childname = NULL, | | 72 | struct devlistargs laa = {.l_devname = "", .l_childname = NULL, |
73 | .l_children = 0}; | | 73 | .l_children = 0}; |
74 | struct devdetachargs daa; | | 74 | struct devdetachargs daa; |
75 | struct devrescanargs raa; | | 75 | struct devrescanargs raa; |
76 | int *locs, i; | | 76 | int *locs, i; |
77 | prop_dictionary_t command_dict, args_dict, results_dict, | | 77 | prop_dictionary_t command_dict, args_dict, results_dict, |
78 | data_dict; | | 78 | data_dict; |
79 | prop_string_t string; | | 79 | prop_string_t string; |
80 | prop_number_t number; | | 80 | prop_number_t number; |
81 | char *xml; | | 81 | char *xml; |
82 | | | 82 | |
83 | mode = 0; | | 83 | mode = 0; |
84 | while ((c = getopt(argc, argv, OPTS)) != -1) { | | 84 | while ((c = getopt(argc, argv, OPTS)) != -1) { |
85 | switch (c) { | | 85 | switch (c) { |
86 | case 'Q': | | 86 | case 'Q': |
87 | case 'R': | | 87 | case 'R': |
88 | case 'S': | | 88 | case 'S': |
89 | case 'd': | | 89 | case 'd': |
90 | case 'l': | | 90 | case 'l': |
91 | case 'p': | | 91 | case 'p': |
92 | case 'r': | | 92 | case 'r': |
93 | mode = c; | | 93 | mode = c; |
94 | break; | | 94 | break; |
95 | case 'a': | | 95 | case 'a': |
96 | attr = optarg; | | 96 | attr = optarg; |
97 | break; | | 97 | break; |
98 | case '?': | | 98 | case '?': |
99 | default: | | 99 | default: |
100 | usage(); | | 100 | usage(); |
101 | } | | 101 | } |
102 | } | | 102 | } |
103 | | | 103 | |
104 | argc -= optind; | | 104 | argc -= optind; |
105 | argv += optind; | | 105 | argv += optind; |
106 | | | 106 | |
107 | if (argc < 1 || mode == 0) | | 107 | if ((argc < 1 && mode != 'l') || mode == 0) |
108 | usage(); | | 108 | usage(); |
109 | | | 109 | |
110 | fd = open(DRVCTLDEV, OPEN_MODE(mode), 0); | | 110 | fd = open(DRVCTLDEV, OPEN_MODE(mode), 0); |
111 | if (fd < 0) | | 111 | if (fd < 0) |
112 | err(2, "open %s", DRVCTLDEV); | | 112 | err(2, "open %s", DRVCTLDEV); |
113 | | | 113 | |
114 | switch (mode) { | | 114 | switch (mode) { |
115 | case 'Q': | | 115 | case 'Q': |
116 | paa.flags = DEVPM_F_SUBTREE; | | 116 | paa.flags = DEVPM_F_SUBTREE; |
117 | /*FALLTHROUGH*/ | | 117 | /*FALLTHROUGH*/ |
118 | case 'R': | | 118 | case 'R': |
119 | strlcpy(paa.devname, argv[0], sizeof(paa.devname)); | | 119 | strlcpy(paa.devname, argv[0], sizeof(paa.devname)); |
120 | | | 120 | |
121 | if (ioctl(fd, DRVRESUMEDEV, &paa) == -1) | | 121 | if (ioctl(fd, DRVRESUMEDEV, &paa) == -1) |
122 | err(3, "DRVRESUMEDEV"); | | 122 | err(3, "DRVRESUMEDEV"); |
123 | break; | | 123 | break; |
124 | case 'S': | | 124 | case 'S': |
125 | strlcpy(paa.devname, argv[0], sizeof(paa.devname)); | | 125 | strlcpy(paa.devname, argv[0], sizeof(paa.devname)); |
126 | | | 126 | |
127 | if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1) | | 127 | if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1) |
128 | err(3, "DRVSUSPENDDEV"); | | 128 | err(3, "DRVSUSPENDDEV"); |
129 | break; | | 129 | break; |
130 | case 'd': | | 130 | case 'd': |
131 | strlcpy(daa.devname, argv[0], sizeof(daa.devname)); | | 131 | strlcpy(daa.devname, argv[0], sizeof(daa.devname)); |
132 | | | 132 | |
133 | if (ioctl(fd, DRVDETACHDEV, &daa) == -1) | | 133 | if (ioctl(fd, DRVDETACHDEV, &daa) == -1) |
134 | err(3, "DRVDETACHDEV"); | | 134 | err(3, "DRVDETACHDEV"); |
135 | break; | | 135 | break; |
136 | case 'l': | | 136 | case 'l': |
137 | strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname)); | | 137 | if (argc == 0) |
| | | 138 | *laa.l_devname = '\0'; |
| | | 139 | else |
| | | 140 | strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname)); |
138 | | | 141 | |
139 | if (ioctl(fd, DRVLISTDEV, &laa) == -1) | | 142 | if (ioctl(fd, DRVLISTDEV, &laa) == -1) |
140 | err(3, "DRVLISTDEV"); | | 143 | err(3, "DRVLISTDEV"); |
141 | | | 144 | |
142 | children = laa.l_children; | | 145 | children = laa.l_children; |
143 | | | 146 | |
144 | laa.l_childname = malloc(children * sizeof(laa.l_childname[0])); | | 147 | laa.l_childname = malloc(children * sizeof(laa.l_childname[0])); |
145 | if (laa.l_childname == NULL) | | 148 | if (laa.l_childname == NULL) |
146 | err(5, "DRVLISTDEV"); | | 149 | err(5, "DRVLISTDEV"); |
147 | if (ioctl(fd, DRVLISTDEV, &laa) == -1) | | 150 | if (ioctl(fd, DRVLISTDEV, &laa) == -1) |
148 | err(3, "DRVLISTDEV"); | | 151 | err(3, "DRVLISTDEV"); |
149 | if (laa.l_children > children) | | 152 | if (laa.l_children > children) |
150 | err(6, "DRVLISTDEV: number of children grew"); | | 153 | err(6, "DRVLISTDEV: number of children grew"); |
151 | | | 154 | |
152 | for (i = 0; i < laa.l_children; i++) | | 155 | for (i = 0; i < laa.l_children; i++) { |
153 | printf("%s %s\n", laa.l_devname, laa.l_childname[i]); | | 156 | printf("%s%s%s\n", laa.l_devname, (argc ? " " : ""), |
| | | 157 | laa.l_childname[i]); |
| | | 158 | } |
154 | break; | | 159 | break; |
155 | case 'r': | | 160 | case 'r': |
156 | memset(&raa, 0, sizeof(raa)); | | 161 | memset(&raa, 0, sizeof(raa)); |
157 | strlcpy(raa.busname, argv[0], sizeof(raa.busname)); | | 162 | strlcpy(raa.busname, argv[0], sizeof(raa.busname)); |
158 | if (attr) | | 163 | if (attr) |
159 | strlcpy(raa.ifattr, attr, sizeof(raa.ifattr)); | | 164 | strlcpy(raa.ifattr, attr, sizeof(raa.ifattr)); |
160 | if (argc > 1) { | | 165 | if (argc > 1) { |
161 | locs = malloc((argc - 1) * sizeof(int)); | | 166 | locs = malloc((argc - 1) * sizeof(int)); |
162 | if (!locs) | | 167 | if (!locs) |
163 | err(5, "malloc int[%d]", argc - 1); | | 168 | err(5, "malloc int[%d]", argc - 1); |
164 | for (i = 0; i < argc - 1; i++) | | 169 | for (i = 0; i < argc - 1; i++) |
165 | locs[i] = atoi(argv[i + 1]); | | 170 | locs[i] = atoi(argv[i + 1]); |
166 | raa.numlocators = argc - 1; | | 171 | raa.numlocators = argc - 1; |
167 | raa.locators = locs; | | 172 | raa.locators = locs; |
168 | } | | 173 | } |
169 | | | 174 | |
170 | if (ioctl(fd, DRVRESCANBUS, &raa) == -1) | | 175 | if (ioctl(fd, DRVRESCANBUS, &raa) == -1) |
171 | err(3, "DRVRESCANBUS"); | | 176 | err(3, "DRVRESCANBUS"); |
172 | break; | | 177 | break; |
173 | case 'p': | | 178 | case 'p': |
174 | | | 179 | |
175 | command_dict = prop_dictionary_create(); | | 180 | command_dict = prop_dictionary_create(); |
176 | args_dict = prop_dictionary_create(); | | 181 | args_dict = prop_dictionary_create(); |
177 | | | 182 | |
178 | string = prop_string_create_cstring_nocopy("get-properties"); | | 183 | string = prop_string_create_cstring_nocopy("get-properties"); |
179 | prop_dictionary_set(command_dict, "drvctl-command", string); | | 184 | prop_dictionary_set(command_dict, "drvctl-command", string); |
180 | prop_object_release(string); | | 185 | prop_object_release(string); |
181 | | | 186 | |
182 | string = prop_string_create_cstring(argv[0]); | | 187 | string = prop_string_create_cstring(argv[0]); |
183 | prop_dictionary_set(args_dict, "device-name", string); | | 188 | prop_dictionary_set(args_dict, "device-name", string); |
184 | prop_object_release(string); | | 189 | prop_object_release(string); |
185 | | | 190 | |
186 | prop_dictionary_set(command_dict, "drvctl-arguments", | | 191 | prop_dictionary_set(command_dict, "drvctl-arguments", |
187 | args_dict); | | 192 | args_dict); |
188 | prop_object_release(args_dict); | | 193 | prop_object_release(args_dict); |
189 | | | 194 | |
190 | res = prop_dictionary_sendrecv_ioctl(command_dict, fd, | | 195 | res = prop_dictionary_sendrecv_ioctl(command_dict, fd, |
191 | DRVCTLCOMMAND, | | 196 | DRVCTLCOMMAND, |
192 | &results_dict); | | 197 | &results_dict); |
193 | prop_object_release(command_dict); | | 198 | prop_object_release(command_dict); |
194 | if (res) | | 199 | if (res) |
195 | errx(3, "DRVCTLCOMMAND: %s", strerror(res)); | | 200 | errx(3, "DRVCTLCOMMAND: %s", strerror(res)); |
196 | | | 201 | |
197 | number = prop_dictionary_get(results_dict, "drvctl-error"); | | 202 | number = prop_dictionary_get(results_dict, "drvctl-error"); |
198 | if (prop_number_integer_value(number) != 0) { | | 203 | if (prop_number_integer_value(number) != 0) { |
199 | errx(3, "get-properties: %s", | | 204 | errx(3, "get-properties: %s", |
200 | strerror((int)prop_number_integer_value(number))); | | 205 | strerror((int)prop_number_integer_value(number))); |
201 | } | | 206 | } |
202 | | | 207 | |
203 | data_dict = prop_dictionary_get(results_dict, | | 208 | data_dict = prop_dictionary_get(results_dict, |
204 | "drvctl-result-data"); | | 209 | "drvctl-result-data"); |
205 | if (data_dict == NULL) { | | 210 | if (data_dict == NULL) { |
206 | errx(3, "get-properties: failed to return result data"); | | 211 | errx(3, "get-properties: failed to return result data"); |
207 | } | | 212 | } |
208 | | | 213 | |
209 | xml = prop_dictionary_externalize(data_dict); | | 214 | xml = prop_dictionary_externalize(data_dict); |
210 | prop_object_release(results_dict); | | 215 | prop_object_release(results_dict); |
211 | | | 216 | |
212 | printf("Properties for device `%s':\n%s", | | 217 | printf("Properties for device `%s':\n%s", |
213 | argv[0], xml); | | 218 | argv[0], xml); |
214 | free(xml); | | 219 | free(xml); |
215 | break; | | 220 | break; |
216 | default: | | 221 | default: |
217 | errx(4, "unknown command"); | | 222 | errx(4, "unknown command"); |
218 | } | | 223 | } |
219 | | | 224 | |
220 | return (0); | | 225 | return (0); |
221 | } | | 226 | } |