]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/devctl/devctl.c
MFV r301238:
[FreeBSD/FreeBSD.git] / usr.sbin / devctl / devctl.c
1 /*-
2  * Copyright (c) 2014 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/linker_set.h>
31 #include <devctl.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <unistd.h>
39
40 struct devctl_command {
41         const char *name;
42         int (*handler)(int ac, char **av);
43 };
44
45 #define DEVCTL_DATASET(name)    devctl_ ## name ## _table
46
47 #define DEVCTL_COMMAND(set, name, function)                             \
48         static struct devctl_command function ## _devctl_command =      \
49         { #name, function };                                            \
50         DATA_SET(DEVCTL_DATASET(set), function ## _devctl_command)
51
52 #define DEVCTL_TABLE(set, name)                                         \
53         SET_DECLARE(DEVCTL_DATASET(name), struct devctl_command);       \
54                                                                         \
55         static int                                                      \
56         devctl_ ## name ## _table_handler(int ac, char **av)            \
57         {                                                               \
58                 return (devctl_table_handler(SET_BEGIN(DEVCTL_DATASET(name)), \
59                     SET_LIMIT(DEVCTL_DATASET(name)), ac, av));          \
60         }                                                               \
61         DEVCTL_COMMAND(set, name, devctl_ ## name ## _table_handler)
62
63 static int      devctl_table_handler(struct devctl_command **start,
64     struct devctl_command **end, int ac, char **av);
65
66 SET_DECLARE(DEVCTL_DATASET(top), struct devctl_command);
67
68 DEVCTL_TABLE(top, set);
69
70 static void
71 usage(void)
72 {
73         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
74             "usage: devctl attach device",
75             "       devctl detach [-f] device",
76             "       devctl disable [-f] device",
77             "       devctl enable device",
78             "       devctl suspend device",
79             "       devctl resume device",
80             "       devctl set driver [-f] device driver",
81             "       devctl rescan device",
82             "       devctl delete [-f] device");
83         exit(1);
84 }
85
86 static int
87 devctl_table_handler(struct devctl_command **start,
88     struct devctl_command **end, int ac, char **av)
89 {
90         struct devctl_command **cmd;
91
92         if (ac < 2) {
93                 warnx("The %s command requires a sub-command.", av[0]);
94                 return (EINVAL);
95         }
96         for (cmd = start; cmd < end; cmd++) {
97                 if (strcmp((*cmd)->name, av[1]) == 0)
98                         return ((*cmd)->handler(ac - 1, av + 1));
99         }
100
101         warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
102         return (ENOENT);
103 }
104
105 static int
106 help(int ac __unused, char **av __unused)
107 {
108
109         usage();
110         return (0);
111 }
112 DEVCTL_COMMAND(top, help, help);
113
114 static int
115 attach(int ac, char **av)
116 {
117
118         if (ac != 2)
119                 usage();
120         if (devctl_attach(av[1]) < 0)
121                 err(1, "Failed to attach %s", av[1]);
122         return (0);
123 }
124 DEVCTL_COMMAND(top, attach, attach);
125
126 static void
127 detach_usage(void)
128 {
129
130         fprintf(stderr, "usage: devctl detach [-f] device\n");
131         exit(1);
132 }
133
134 static int
135 detach(int ac, char **av)
136 {
137         bool force;
138         int ch;
139
140         force = false;
141         while ((ch = getopt(ac, av, "f")) != -1)
142                 switch (ch) {
143                 case 'f':
144                         force = true;
145                         break;
146                 default:
147                         detach_usage();
148                 }
149         ac -= optind;
150         av += optind;
151
152         if (ac != 1)
153                 detach_usage();
154         if (devctl_detach(av[0], force) < 0)
155                 err(1, "Failed to detach %s", av[0]);
156         return (0);
157 }
158 DEVCTL_COMMAND(top, detach, detach);
159
160 static void
161 disable_usage(void)
162 {
163
164         fprintf(stderr, "usage: devctl disable [-f] device\n");
165         exit(1);
166 }
167
168 static int
169 disable(int ac, char **av)
170 {
171         bool force;
172         int ch;
173
174         force = false;
175         while ((ch = getopt(ac, av, "f")) != -1)
176                 switch (ch) {
177                 case 'f':
178                         force = true;
179                         break;
180                 default:
181                         disable_usage();
182                 }
183         ac -= optind;
184         av += optind;
185
186         if (ac != 1)
187                 disable_usage();
188         if (devctl_disable(av[0], force) < 0)
189                 err(1, "Failed to disable %s", av[0]);
190         return (0);
191 }
192 DEVCTL_COMMAND(top, disable, disable);
193
194 static int
195 enable(int ac, char **av)
196 {
197
198         if (ac != 2)
199                 usage();
200         if (devctl_enable(av[1]) < 0)
201                 err(1, "Failed to enable %s", av[1]);
202         return (0);
203 }
204 DEVCTL_COMMAND(top, enable, enable);
205
206 static int
207 suspend(int ac, char **av)
208 {
209
210         if (ac != 2)
211                 usage();
212         if (devctl_suspend(av[1]) < 0)
213                 err(1, "Failed to suspend %s", av[1]);
214         return (0);
215 }
216 DEVCTL_COMMAND(top, suspend, suspend);
217
218 static int
219 resume(int ac, char **av)
220 {
221
222         if (ac != 2)
223                 usage();
224         if (devctl_resume(av[1]) < 0)
225                 err(1, "Failed to resume %s", av[1]);
226         return (0);
227 }
228 DEVCTL_COMMAND(top, resume, resume);
229
230 static void
231 set_driver_usage(void)
232 {
233
234         fprintf(stderr, "usage: devctl set driver [-f] device driver\n");
235         exit(1);
236 }
237
238 static int
239 set_driver(int ac, char **av)
240 {
241         bool force;
242         int ch;
243
244         force = false;
245         while ((ch = getopt(ac, av, "f")) != -1)
246                 switch (ch) {
247                 case 'f':
248                         force = true;
249                         break;
250                 default:
251                         set_driver_usage();
252                 }
253         ac -= optind;
254         av += optind;
255
256         if (ac != 2)
257                 set_driver_usage();
258         if (devctl_set_driver(av[0], av[1], force) < 0)
259                 err(1, "Failed to set %s driver to %s", av[0], av[1]);
260         return (0);
261 }
262 DEVCTL_COMMAND(set, driver, set_driver);
263
264 static int
265 rescan(int ac, char **av)
266 {
267
268         if (ac != 2)
269                 usage();
270         if (devctl_rescan(av[1]) < 0)
271                 err(1, "Failed to rescan %s", av[1]);
272         return (0);
273 }
274 DEVCTL_COMMAND(top, rescan, rescan);
275
276 static void
277 delete_usage(void)
278 {
279
280         fprintf(stderr, "usage: devctl delete [-f] device\n");
281         exit(1);
282 }
283
284 static int
285 delete(int ac, char **av)
286 {
287         bool force;
288         int ch;
289
290         force = false;
291         while ((ch = getopt(ac, av, "f")) != -1)
292                 switch (ch) {
293                 case 'f':
294                         force = true;
295                         break;
296                 default:
297                         delete_usage();
298                 }
299         ac -= optind;
300         av += optind;
301
302         if (ac != 1)
303                 delete_usage();
304         if (devctl_delete(av[0], force) < 0)
305                 err(1, "Failed to delete %s", av[0]);
306         return (0);
307 }
308 DEVCTL_COMMAND(top, delete, delete);
309
310 int
311 main(int ac, char *av[])
312 {
313         struct devctl_command **cmd;
314
315         if (ac == 1)
316                 usage();
317         ac--;
318         av++;
319
320         SET_FOREACH(cmd, DEVCTL_DATASET(top)) {
321                 if (strcmp((*cmd)->name, av[0]) == 0) {
322                         if ((*cmd)->handler(ac, av) != 0)
323                                 return (1);
324                         else
325                                 return (0);
326                 }
327         }
328         warnx("Unknown command %s.", av[0]);
329         return (1);
330 }