^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * command structure borrowed from udev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (git://git.kernel.org/pub/scm/linux/hotplug/udev.git)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * 2005-2007 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <syslog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "usbip_network.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "usbip.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int usbip_help(int argc, char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int usbip_version(int argc, char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const char usbip_version_string[] = PACKAGE_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static const char usbip_usage_string[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) "usbip [--debug] [--log] [--tcp-port PORT] [version]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) " [help] <command> <args>\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static void usbip_usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) printf("usage: %s", usbip_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct command {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int (*fn)(int argc, char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) const char *help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void (*usage)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static const struct command cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .name = "help",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .fn = usbip_help,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .help = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .usage = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .name = "version",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .fn = usbip_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .help = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .usage = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .name = "attach",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .fn = usbip_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .help = "Attach a remote USB device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .usage = usbip_attach_usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .name = "detach",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .fn = usbip_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .help = "Detach a remote USB device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .usage = usbip_detach_usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .name = "list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .fn = usbip_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .help = "List exportable or local USB devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .usage = usbip_list_usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .name = "bind",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .fn = usbip_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .help = "Bind device to " USBIP_HOST_DRV_NAME ".ko",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .usage = usbip_bind_usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .name = "unbind",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .fn = usbip_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .help = "Unbind device from " USBIP_HOST_DRV_NAME ".ko",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .usage = usbip_unbind_usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .name = "port",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .fn = usbip_port_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .help = "Show imported USB devices",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .usage = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) { NULL, NULL, NULL, NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int usbip_help(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) const struct command *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (argc > 1 && argv++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) for (i = 0; cmds[i].name != NULL; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!strcmp(cmds[i].name, argv[0]) && cmds[i].usage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cmds[i].usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) usbip_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) for (cmd = cmds; cmd->name != NULL; cmd++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (cmd->help != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) printf(" %-10s %s\n", cmd->name, cmd->help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int usbip_version(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (void) argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (void) argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) printf(PROGNAME " (%s)\n", usbip_version_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int run_command(const struct command *cmd, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dbg("running command: `%s'", cmd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return cmd->fn(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static const struct option opts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) { "debug", no_argument, NULL, 'd' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) { "log", no_argument, NULL, 'l' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) { "tcp-port", required_argument, NULL, 't' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) { NULL, 0, NULL, 0 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int i, rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) usbip_use_stderr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) opterr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) opt = getopt_long(argc, argv, "+dlt:", opts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (opt == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) usbip_use_debug = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) usbip_use_syslog = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) openlog("", LOG_PID, LOG_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) usbip_setup_port_number(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) printf("usbip: invalid option\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Terminate after printing error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* FALLTHRU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) usbip_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) cmd = argv[optind];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) for (i = 0; cmds[i].name != NULL; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!strcmp(cmds[i].name, cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) argc -= optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) argv += optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) optind = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) rc = run_command(&cmds[i], argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* invalid command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) usbip_help(0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }