usbip.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * command structure borrowed from udev
  3. * (git://git.kernel.org/pub/scm/linux/hotplug/udev.git)
  4. *
  5. * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  6. * 2005-2007 Takahiro Hirofuchi
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <getopt.h>
  24. #include <syslog.h>
  25. #include "usbip_common.h"
  26. #include "usbip_network.h"
  27. #include "usbip.h"
  28. static int usbip_help(int argc, char *argv[]);
  29. static int usbip_version(int argc, char *argv[]);
  30. static const char usbip_version_string[] = PACKAGE_STRING;
  31. static const char usbip_usage_string[] =
  32. "usbip [--debug] [--log] [--tcp-port PORT] [version]\n"
  33. " [help] <command> <args>\n";
  34. static void usbip_usage(void)
  35. {
  36. printf("usage: %s", usbip_usage_string);
  37. }
  38. struct command {
  39. const char *name;
  40. int (*fn)(int argc, char *argv[]);
  41. const char *help;
  42. void (*usage)(void);
  43. };
  44. static const struct command cmds[] = {
  45. {
  46. .name = "help",
  47. .fn = usbip_help,
  48. .help = NULL,
  49. .usage = NULL
  50. },
  51. {
  52. .name = "version",
  53. .fn = usbip_version,
  54. .help = NULL,
  55. .usage = NULL
  56. },
  57. {
  58. .name = "attach",
  59. .fn = usbip_attach,
  60. .help = "Attach a remote USB device",
  61. .usage = usbip_attach_usage
  62. },
  63. {
  64. .name = "detach",
  65. .fn = usbip_detach,
  66. .help = "Detach a remote USB device",
  67. .usage = usbip_detach_usage
  68. },
  69. {
  70. .name = "list",
  71. .fn = usbip_list,
  72. .help = "List exportable or local USB devices",
  73. .usage = usbip_list_usage
  74. },
  75. {
  76. .name = "bind",
  77. .fn = usbip_bind,
  78. .help = "Bind device to " USBIP_HOST_DRV_NAME ".ko",
  79. .usage = usbip_bind_usage
  80. },
  81. {
  82. .name = "unbind",
  83. .fn = usbip_unbind,
  84. .help = "Unbind device from " USBIP_HOST_DRV_NAME ".ko",
  85. .usage = usbip_unbind_usage
  86. },
  87. {
  88. .name = "port",
  89. .fn = usbip_port_show,
  90. .help = "Show imported USB devices",
  91. .usage = NULL
  92. },
  93. { NULL, NULL, NULL, NULL }
  94. };
  95. static int usbip_help(int argc, char *argv[])
  96. {
  97. const struct command *cmd;
  98. int i;
  99. int ret = 0;
  100. if (argc > 1 && argv++) {
  101. for (i = 0; cmds[i].name != NULL; i++)
  102. if (!strcmp(cmds[i].name, argv[0]) && cmds[i].usage) {
  103. cmds[i].usage();
  104. goto done;
  105. }
  106. ret = -1;
  107. }
  108. usbip_usage();
  109. printf("\n");
  110. for (cmd = cmds; cmd->name != NULL; cmd++)
  111. if (cmd->help != NULL)
  112. printf(" %-10s %s\n", cmd->name, cmd->help);
  113. printf("\n");
  114. done:
  115. return ret;
  116. }
  117. static int usbip_version(int argc, char *argv[])
  118. {
  119. (void) argc;
  120. (void) argv;
  121. printf(PROGNAME " (%s)\n", usbip_version_string);
  122. return 0;
  123. }
  124. static int run_command(const struct command *cmd, int argc, char *argv[])
  125. {
  126. dbg("running command: `%s'", cmd->name);
  127. return cmd->fn(argc, argv);
  128. }
  129. int main(int argc, char *argv[])
  130. {
  131. static const struct option opts[] = {
  132. { "debug", no_argument, NULL, 'd' },
  133. { "log", no_argument, NULL, 'l' },
  134. { "tcp-port", required_argument, NULL, 't' },
  135. { NULL, 0, NULL, 0 }
  136. };
  137. char *cmd;
  138. int opt;
  139. int i, rc = -1;
  140. usbip_use_stderr = 1;
  141. opterr = 0;
  142. for (;;) {
  143. opt = getopt_long(argc, argv, "+dlt:", opts, NULL);
  144. if (opt == -1)
  145. break;
  146. switch (opt) {
  147. case 'd':
  148. usbip_use_debug = 1;
  149. break;
  150. case 'l':
  151. usbip_use_syslog = 1;
  152. openlog("", LOG_PID, LOG_USER);
  153. break;
  154. case 't':
  155. usbip_setup_port_number(optarg);
  156. break;
  157. case '?':
  158. printf("usbip: invalid option\n");
  159. default:
  160. usbip_usage();
  161. goto out;
  162. }
  163. }
  164. cmd = argv[optind];
  165. if (cmd) {
  166. for (i = 0; cmds[i].name != NULL; i++)
  167. if (!strcmp(cmds[i].name, cmd)) {
  168. argc -= optind;
  169. argv += optind;
  170. optind = 0;
  171. rc = run_command(&cmds[i], argc, argv);
  172. goto out;
  173. }
  174. }
  175. /* invalid command */
  176. usbip_help(0, NULL);
  177. out:
  178. return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
  179. }