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