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: releng/10.3/usr.sbin/devctl/devctl.c 295131 2016-02-01 23:07:31Z jhb $"); 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 40struct 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 63static int devctl_table_handler(struct devctl_command **start, 64 struct devctl_command **end, int ac, char **av); 65 66SET_DECLARE(DEVCTL_DATASET(top), struct devctl_command); 67 68DEVCTL_TABLE(top, set); 69 70static void 71usage(void) 72{ 73 fprintf(stderr, "%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 set driver [-f] device driver"); 79 exit(1); 80} 81 82static int 83devctl_table_handler(struct devctl_command **start, 84 struct devctl_command **end, int ac, char **av) 85{ 86 struct devctl_command **cmd; 87 88 if (ac < 2) { 89 warnx("The %s command requires a sub-command.", av[0]); 90 return (EINVAL); 91 } 92 for (cmd = start; cmd < end; cmd++) { 93 if (strcmp((*cmd)->name, av[1]) == 0) 94 return ((*cmd)->handler(ac - 1, av + 1)); 95 } 96 97 warnx("%s is not a valid sub-command of %s.", av[1], av[0]); 98 return (ENOENT); 99} 100 101static int 102help(int ac __unused, char **av __unused) 103{ 104 105 usage(); 106 return (0); 107} 108DEVCTL_COMMAND(top, help, help); 109 110static int 111attach(int ac, char **av) 112{ 113 114 if (ac != 2) 115 usage(); 116 if (devctl_attach(av[1]) < 0) 117 err(1, "Failed to attach %s", av[1]); 118 return (0); 119} 120DEVCTL_COMMAND(top, attach, attach); 121 122static void 123detach_usage(void) 124{ 125 126 fprintf(stderr, "usage: devctl detach [-f] device\n"); 127 exit(1); 128} 129 130static int 131detach(int ac, char **av) 132{ 133 bool force; 134 int ch; 135 136 force = false; 137 while ((ch = getopt(ac, av, "f")) != -1) 138 switch (ch) { 139 case 'f': 140 force = true; 141 break; 142 default: 143 detach_usage(); 144 } 145 ac -= optind; 146 av += optind; 147 148 if (ac != 1) 149 detach_usage(); 150 if (devctl_detach(av[0], force) < 0) 151 err(1, "Failed to detach %s", av[0]); 152 return (0); 153} 154DEVCTL_COMMAND(top, detach, detach); 155 156static void 157disable_usage(void) 158{ 159 160 fprintf(stderr, "usage: devctl disable [-f] device\n"); 161 exit(1); 162} 163 164static int 165disable(int ac, char **av) 166{ 167 bool force; 168 int ch; 169 170 force = false; 171 while ((ch = getopt(ac, av, "f")) != -1) 172 switch (ch) { 173 case 'f': 174 force = true; 175 break; 176 default: 177 disable_usage(); 178 } 179 ac -= optind; 180 av += optind; 181 182 if (ac != 1) 183 disable_usage(); 184 if (devctl_disable(av[0], force) < 0) 185 err(1, "Failed to disable %s", av[0]); 186 return (0); 187} 188DEVCTL_COMMAND(top, disable, disable); 189 190static int 191enable(int ac, char **av) 192{ 193 194 if (ac != 2) 195 usage(); 196 if (devctl_enable(av[1]) < 0) 197 err(1, "Failed to enable %s", av[1]); 198 return (0); 199} 200DEVCTL_COMMAND(top, enable, enable); 201 202static void 203set_driver_usage(void) 204{ 205 206 fprintf(stderr, "usage: devctl set driver [-f] device driver\n"); 207 exit(1); 208} 209 210static int 211set_driver(int ac, char **av) 212{ 213 bool force; 214 int ch; 215 216 force = false; 217 while ((ch = getopt(ac, av, "f")) != -1) 218 switch (ch) { 219 case 'f': 220 force = true; 221 break; 222 default: 223 set_driver_usage(); 224 } 225 ac -= optind; 226 av += optind; 227 228 if (ac != 2) 229 set_driver_usage(); 230 if (devctl_set_driver(av[0], av[1], force) < 0) 231 err(1, "Failed to set %s driver to %s", av[0], av[1]); 232 return (0); 233} 234DEVCTL_COMMAND(set, driver, set_driver); 235 236int 237main(int ac, char *av[]) 238{ 239 struct devctl_command **cmd; 240 241 if (ac == 1) 242 usage(); 243 ac--; 244 av++; 245 246 SET_FOREACH(cmd, DEVCTL_DATASET(top)) { 247 if (strcmp((*cmd)->name, av[0]) == 0) { 248 if ((*cmd)->handler(ac, av) != 0) 249 return (1); 250 else 251 return (0); 252 } 253 } 254 warnx("Unknown command %s.", av[0]); 255 return (1); 256} 257