drvctl.c revision 1.10 1 1.10 dyoung /* $NetBSD: drvctl.c,v 1.10 2009/04/20 21:41:50 dyoung Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 2004
5 1.1 drochner * Matthias Drochner. All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Redistribution and use in source and binary forms, with or without
8 1.1 drochner * modification, are permitted provided that the following conditions
9 1.1 drochner * are met:
10 1.1 drochner * 1. Redistributions of source code must retain the above copyright
11 1.1 drochner * notice, this list of conditions, and the following disclaimer.
12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer in the
14 1.1 drochner * documentation and/or other materials provided with the distribution.
15 1.1 drochner *
16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 drochner * SUCH DAMAGE.
27 1.1 drochner */
28 1.1 drochner
29 1.9 dyoung #include <stdbool.h>
30 1.1 drochner #include <stdio.h>
31 1.1 drochner #include <stdlib.h>
32 1.1 drochner #include <unistd.h>
33 1.1 drochner #include <err.h>
34 1.1 drochner #include <fcntl.h>
35 1.1 drochner #include <string.h>
36 1.1 drochner #include <sys/ioctl.h>
37 1.1 drochner #include <sys/drvctlio.h>
38 1.1 drochner
39 1.9 dyoung #define OPTS "QRSa:dlnpr"
40 1.5 thorpej
41 1.5 thorpej #define OPEN_MODE(mode) \
42 1.5 thorpej (((mode) == 'd' || (mode) == 'r') ? O_RDWR \
43 1.5 thorpej : O_RDONLY)
44 1.1 drochner
45 1.1 drochner static void usage(void);
46 1.1 drochner
47 1.1 drochner static void
48 1.3 xtraeme usage(void)
49 1.1 drochner {
50 1.1 drochner
51 1.2 wiz fprintf(stderr, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
52 1.5 thorpej " %s -d device\n"
53 1.10 dyoung " %s [-n] -l [device]\n"
54 1.6 dyoung " %s -p device\n"
55 1.6 dyoung " %s -Q device\n"
56 1.6 dyoung " %s -R device\n"
57 1.6 dyoung " %s -S device\n",
58 1.6 dyoung getprogname(), getprogname(), getprogname(), getprogname(),
59 1.5 thorpej getprogname(), getprogname(), getprogname());
60 1.1 drochner exit(1);
61 1.1 drochner }
62 1.1 drochner
63 1.1 drochner int
64 1.1 drochner main(int argc, char **argv)
65 1.1 drochner {
66 1.9 dyoung bool nflag = false;
67 1.1 drochner int c, mode;
68 1.1 drochner char *attr = 0;
69 1.1 drochner extern char *optarg;
70 1.1 drochner extern int optind;
71 1.1 drochner int fd, res;
72 1.6 dyoung size_t children;
73 1.6 dyoung struct devpmargs paa = {.devname = "", .flags = 0};
74 1.6 dyoung struct devlistargs laa = {.l_devname = "", .l_childname = NULL,
75 1.6 dyoung .l_children = 0};
76 1.1 drochner struct devdetachargs daa;
77 1.1 drochner struct devrescanargs raa;
78 1.1 drochner int *locs, i;
79 1.6 dyoung prop_dictionary_t command_dict, args_dict, results_dict,
80 1.6 dyoung data_dict;
81 1.6 dyoung prop_string_t string;
82 1.6 dyoung prop_number_t number;
83 1.6 dyoung char *xml;
84 1.1 drochner
85 1.4 lukem mode = 0;
86 1.1 drochner while ((c = getopt(argc, argv, OPTS)) != -1) {
87 1.1 drochner switch (c) {
88 1.6 dyoung case 'Q':
89 1.6 dyoung case 'R':
90 1.6 dyoung case 'S':
91 1.1 drochner case 'd':
92 1.6 dyoung case 'l':
93 1.6 dyoung case 'p':
94 1.1 drochner case 'r':
95 1.1 drochner mode = c;
96 1.1 drochner break;
97 1.1 drochner case 'a':
98 1.1 drochner attr = optarg;
99 1.1 drochner break;
100 1.9 dyoung case 'n':
101 1.9 dyoung nflag = true;
102 1.9 dyoung break;
103 1.1 drochner case '?':
104 1.1 drochner default:
105 1.1 drochner usage();
106 1.1 drochner }
107 1.1 drochner }
108 1.1 drochner
109 1.1 drochner argc -= optind;
110 1.1 drochner argv += optind;
111 1.1 drochner
112 1.8 joerg if ((argc < 1 && mode != 'l') || mode == 0)
113 1.1 drochner usage();
114 1.1 drochner
115 1.5 thorpej fd = open(DRVCTLDEV, OPEN_MODE(mode), 0);
116 1.1 drochner if (fd < 0)
117 1.1 drochner err(2, "open %s", DRVCTLDEV);
118 1.1 drochner
119 1.6 dyoung switch (mode) {
120 1.6 dyoung case 'Q':
121 1.6 dyoung paa.flags = DEVPM_F_SUBTREE;
122 1.6 dyoung /*FALLTHROUGH*/
123 1.6 dyoung case 'R':
124 1.6 dyoung strlcpy(paa.devname, argv[0], sizeof(paa.devname));
125 1.6 dyoung
126 1.6 dyoung if (ioctl(fd, DRVRESUMEDEV, &paa) == -1)
127 1.6 dyoung err(3, "DRVRESUMEDEV");
128 1.6 dyoung break;
129 1.6 dyoung case 'S':
130 1.6 dyoung strlcpy(paa.devname, argv[0], sizeof(paa.devname));
131 1.6 dyoung
132 1.6 dyoung if (ioctl(fd, DRVSUSPENDDEV, &paa) == -1)
133 1.6 dyoung err(3, "DRVSUSPENDDEV");
134 1.6 dyoung break;
135 1.6 dyoung case 'd':
136 1.1 drochner strlcpy(daa.devname, argv[0], sizeof(daa.devname));
137 1.1 drochner
138 1.6 dyoung if (ioctl(fd, DRVDETACHDEV, &daa) == -1)
139 1.1 drochner err(3, "DRVDETACHDEV");
140 1.6 dyoung break;
141 1.6 dyoung case 'l':
142 1.8 joerg if (argc == 0)
143 1.8 joerg *laa.l_devname = '\0';
144 1.8 joerg else
145 1.8 joerg strlcpy(laa.l_devname, argv[0], sizeof(laa.l_devname));
146 1.6 dyoung
147 1.6 dyoung if (ioctl(fd, DRVLISTDEV, &laa) == -1)
148 1.6 dyoung err(3, "DRVLISTDEV");
149 1.6 dyoung
150 1.6 dyoung children = laa.l_children;
151 1.6 dyoung
152 1.6 dyoung laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
153 1.6 dyoung if (laa.l_childname == NULL)
154 1.6 dyoung err(5, "DRVLISTDEV");
155 1.6 dyoung if (ioctl(fd, DRVLISTDEV, &laa) == -1)
156 1.6 dyoung err(3, "DRVLISTDEV");
157 1.6 dyoung if (laa.l_children > children)
158 1.6 dyoung err(6, "DRVLISTDEV: number of children grew");
159 1.6 dyoung
160 1.8 joerg for (i = 0; i < (int)laa.l_children; i++) {
161 1.9 dyoung if (!nflag) {
162 1.9 dyoung printf("%s ",
163 1.9 dyoung (argc == 0) ? "root" : laa.l_devname);
164 1.9 dyoung }
165 1.9 dyoung printf("%s\n", laa.l_childname[i]);
166 1.8 joerg }
167 1.6 dyoung break;
168 1.6 dyoung case 'r':
169 1.1 drochner memset(&raa, 0, sizeof(raa));
170 1.1 drochner strlcpy(raa.busname, argv[0], sizeof(raa.busname));
171 1.1 drochner if (attr)
172 1.1 drochner strlcpy(raa.ifattr, attr, sizeof(raa.ifattr));
173 1.1 drochner if (argc > 1) {
174 1.1 drochner locs = malloc((argc - 1) * sizeof(int));
175 1.1 drochner if (!locs)
176 1.1 drochner err(5, "malloc int[%d]", argc - 1);
177 1.1 drochner for (i = 0; i < argc - 1; i++)
178 1.1 drochner locs[i] = atoi(argv[i + 1]);
179 1.1 drochner raa.numlocators = argc - 1;
180 1.1 drochner raa.locators = locs;
181 1.1 drochner }
182 1.1 drochner
183 1.6 dyoung if (ioctl(fd, DRVRESCANBUS, &raa) == -1)
184 1.1 drochner err(3, "DRVRESCANBUS");
185 1.6 dyoung break;
186 1.6 dyoung case 'p':
187 1.5 thorpej
188 1.5 thorpej command_dict = prop_dictionary_create();
189 1.5 thorpej args_dict = prop_dictionary_create();
190 1.5 thorpej
191 1.5 thorpej string = prop_string_create_cstring_nocopy("get-properties");
192 1.5 thorpej prop_dictionary_set(command_dict, "drvctl-command", string);
193 1.5 thorpej prop_object_release(string);
194 1.5 thorpej
195 1.5 thorpej string = prop_string_create_cstring(argv[0]);
196 1.5 thorpej prop_dictionary_set(args_dict, "device-name", string);
197 1.5 thorpej prop_object_release(string);
198 1.5 thorpej
199 1.5 thorpej prop_dictionary_set(command_dict, "drvctl-arguments",
200 1.5 thorpej args_dict);
201 1.5 thorpej prop_object_release(args_dict);
202 1.5 thorpej
203 1.5 thorpej res = prop_dictionary_sendrecv_ioctl(command_dict, fd,
204 1.5 thorpej DRVCTLCOMMAND,
205 1.5 thorpej &results_dict);
206 1.5 thorpej prop_object_release(command_dict);
207 1.5 thorpej if (res)
208 1.5 thorpej errx(3, "DRVCTLCOMMAND: %s", strerror(res));
209 1.5 thorpej
210 1.5 thorpej number = prop_dictionary_get(results_dict, "drvctl-error");
211 1.5 thorpej if (prop_number_integer_value(number) != 0) {
212 1.5 thorpej errx(3, "get-properties: %s",
213 1.5 thorpej strerror((int)prop_number_integer_value(number)));
214 1.5 thorpej }
215 1.5 thorpej
216 1.5 thorpej data_dict = prop_dictionary_get(results_dict,
217 1.5 thorpej "drvctl-result-data");
218 1.5 thorpej if (data_dict == NULL) {
219 1.5 thorpej errx(3, "get-properties: failed to return result data");
220 1.5 thorpej }
221 1.5 thorpej
222 1.5 thorpej xml = prop_dictionary_externalize(data_dict);
223 1.5 thorpej prop_object_release(results_dict);
224 1.5 thorpej
225 1.5 thorpej printf("Properties for device `%s':\n%s",
226 1.5 thorpej argv[0], xml);
227 1.5 thorpej free(xml);
228 1.6 dyoung break;
229 1.6 dyoung default:
230 1.1 drochner errx(4, "unknown command");
231 1.6 dyoung }
232 1.1 drochner
233 1.1 drochner return (0);
234 1.1 drochner }
235