int usbip_bind(int argc, char *argv[]) { static const struct option opts[] = { { "busid", required_argument, NULL, 'b' }, { NULL, 0, NULL, 0 } }; int opt; int ret = -1; for (;;) { opt = getopt_long(argc, argv, "b:", opts, NULL); if (opt == -1) break; switch (opt) { case 'b': ret = use_device_by_usbip(optarg); goto out; default: goto err_out; } } err_out: usbip_bind_usage(); out: return ret; }
static int export_to(char *host, char *busid) { int ret; if( host == NULL ) { printf( "no host given\n\n"); show_help(); return -1; } if( busid == NULL ) { /* XXX print device list and ask for busnumber, if none is * given */ printf( "no busid given, use --busid switch\n\n"); show_help(); return -1; } ret = use_device_by_usbip(busid); if( ret != 0 ) { printf( "could not bind driver to usbip\n"); return -1; } printf( "DEBUG: exporting device '%s' to '%s'\n", busid, host ); ret = export_busid_to_host(host, busid); /* usbip_export.[ch] */ if( ret != 0 ) { printf( "could not export device to host\n" ); printf( " host: %s, device: %s\n", host, busid ); use_device_by_other(busid); return -1; } return 0; }
static int allusbip(void) { DIR *dir; dir = opendir("/sys/bus/usb/devices/"); if (!dir) g_error("opendir: %s", strerror(errno)); for (;;) { struct dirent *dirent; char *busid; dirent = readdir(dir); if (!dirent) break; busid = dirent->d_name; if (!is_usb_device(busid)) continue; { char name[PATH_MAX]; int conf, ninf = 0; int i; int be_local = 0; conf = read_bConfigurationValue(busid); ninf = read_bNumInterfaces(busid); getdevicename(busid, name, sizeof(name)); for (i = 0; i < ninf; i++) { char driver[PATH_MAX]; getdriver(busid, conf, i, driver, sizeof(driver)); #if 0 if (strncmp(driver, "usbhid", 6) == 0 || strncmp(driver, "usb-storage", 11) == 0) { be_local = 1; break; } #endif } if (be_local == 0) use_device_by_usbip(busid); } } closedir(dir); return 0; }
int main(int argc, char **argv) { char *busid = NULL; char *remote_host = NULL; enum { cmd_unknown = 0, cmd_use_by_usbip, cmd_use_by_other, cmd_list, cmd_list2, cmd_allusbip, cmd_export_to, cmd_unexport, cmd_help, } cmd = cmd_unknown; if (geteuid() != 0) g_warning("running non-root?"); for (;;) { int c; int index = 0; c = getopt_long(argc, argv, "u:o:hlLae:x:b:", longopts, &index); if (c == -1) break; switch (c) { case 'u': cmd = cmd_use_by_usbip; busid = optarg; break; case 'o' : cmd = cmd_use_by_other; busid = optarg; break; case 'l' : cmd = cmd_list; break; case 'L' : cmd = cmd_list2; break; case 'a' : cmd = cmd_allusbip; break; case 'b': busid = optarg; break; case 'e': cmd = cmd_export_to; remote_host = optarg; break; case 'x': cmd = cmd_unexport; remote_host = optarg; break; case 'h': /* fallthrough */ case '?': cmd = cmd_help; break; default: g_error("getopt"); } //if (cmd) // break; } switch (cmd) { case cmd_use_by_usbip: use_device_by_usbip(busid); break; case cmd_use_by_other: use_device_by_other(busid); break; case cmd_list: show_devices(); break; case cmd_list2: show_devices2(); break; #if 0 case cmd_allusbip: allusbip(); break; case cmd_export_to: export_to(remote_host, busid); break; case cmd_unexport: unexport_from(remote_host, busid); break; #endif case cmd_help: /* fallthrough */ case cmd_unknown: show_help(); break; default: g_error("NOT REACHED"); } return 0; }