static void do_standalone_mode(gboolean daemonize) { int ret; int lsock[MAXSOCK]; struct addrinfo *ai_head; int n; ret = usbip_names_init(USBIDS_FILE); if (ret) err("open usb.ids"); ret = usbip_stub_driver_open(); if (ret < 0) g_error("driver open failed"); if (daemonize) { if (daemon(0,0) < 0) g_error("daemonizing failed: %s", g_strerror(errno)); usbip_use_syslog = 1; } set_signal(); ai_head = my_getaddrinfo(NULL, PF_UNSPEC); if (!ai_head) return; n = listen_all_addrinfo(ai_head, lsock); if (n <= 0) g_error("no socket to listen to"); for (int i = 0; i < n; i++) { GIOChannel *gio; gio = g_io_channel_unix_new(lsock[i]); g_io_add_watch(gio, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), process_comming_request, NULL); } info("usbipd start (%s)", version); main_loop = g_main_loop_new(FALSE, FALSE); g_main_loop_run(main_loop); info("shutdown"); freeaddrinfo(ai_head); usbip_names_free(); usbip_stub_driver_close(); return; }
static int list_imported_devices(void) { int i; struct usbip_imported_device *idev; int ret; if (usbip_names_init(USBIDS_FILE)) err("failed to open %s", USBIDS_FILE); ret = usbip_vhci_driver_open(); if (ret < 0) { err("open vhci_driver"); goto err_names_free; } printf("Imported USB devices\n"); printf("====================\n"); for (i = 0; i < vhci_driver->nports; i++) { idev = &vhci_driver->idev[i]; if (usbip_vhci_imported_device_dump(idev) < 0) goto err_driver_close; } usbip_vhci_driver_close(); usbip_names_free(); return ret; err_driver_close: usbip_vhci_driver_close(); err_names_free: usbip_names_free(); return -1; }
int usbip_list(int argc, char *argv[]) { static const struct option opts[] = { { "parsable", no_argument, NULL, 'p' }, { "remote", required_argument, NULL, 'r' }, { "local", no_argument, NULL, 'l' }, { NULL, 0, NULL, 0 } }; bool parsable = false; int opt; int ret = -1; if (usbip_names_init(USBIDS_FILE)) err("failed to open %s", USBIDS_FILE); for (;;) { opt = getopt_long(argc, argv, "pr:l", opts, NULL); if (opt == -1) break; switch (opt) { case 'p': parsable = true; break; case 'r': ret = list_exported_devices(optarg); goto out; case 'l': ret = list_devices(parsable); goto out; default: goto err_out; } } err_out: usbip_list_usage(); out: usbip_names_free(); return ret; }
int main(int argc, char *argv[]) { int ret; enum { cmd_attach = 1, cmd_attachall, cmd_detach, cmd_port, cmd_list, cmd_help, cmd_version } cmd = 0; usbip_use_stderr = 1; if (geteuid() != 0) g_warning("running non-root?"); ret = usbip_names_init(USBIDS_FILE); if (ret) err("open usb.ids"); for (;;) { int c; int index = 0; c = getopt_long(argc, argv, "adplvhDSx", longopts, &index); if (c == -1) break; switch(c) { case 'a': if (!cmd) cmd = cmd_attach; else cmd = cmd_help; break; case 'd': if (!cmd) cmd = cmd_detach; else cmd = cmd_help; break; case 'p': if (!cmd) cmd = cmd_port; else cmd = cmd_help; break; case 'l': if (!cmd) cmd = cmd_list; else cmd = cmd_help; break; case 'v': if (!cmd) cmd = cmd_version; else cmd = cmd_help; break; case 'x': if(!cmd) cmd = cmd_attachall; else cmd = cmd_help; break; case 'h': cmd = cmd_help; break; case 'D': usbip_use_debug = 1; break; case 'S': usbip_use_syslog = 1; break; case '?': break; default: err("getopt"); } } switch(cmd) { case cmd_attach: if (optind == argc - 2) attach_device(argv[optind], argv[optind+1]); else show_help(); break; case cmd_detach: while (optind < argc) detach_port(argv[optind++]); break; case cmd_port: show_port_status(); break; case cmd_list: while (optind < argc) show_exported_devices(argv[optind++]); break; case cmd_attachall: while(optind < argc) attach_devices_all(argv[optind++]); break; case cmd_version: printf("%s\n", version); break; case cmd_help: show_help(); break; default: show_help(); } usbip_names_free(); return 0; }
int main(int argc, char *argv[]) { int ret; enum { cmd_attach = 1, cmd_attachall, cmd_detach, cmd_port, cmd_list, cmd_help, cmd_version } cmd = 0; usbip_use_stderr = 1; #ifdef __linux__ if (geteuid() != 0) notice("running non-root?"); #endif if (init_socket()) return EXIT_FAILURE; ret = usbip_names_init(USBIDS_FILE); if (ret) notice("failed to open %s", USBIDS_FILE); for (;;) { int c; int index = 0; c = getopt_long(argc, argv, "adplvhDSx", longopts, &index); if (c == -1) break; switch(c) { case 'a': if (!cmd) cmd = cmd_attach; else cmd = cmd_help; break; case 'd': if (!cmd) cmd = cmd_detach; else cmd = cmd_help; break; case 'p': if (!cmd) cmd = cmd_port; else cmd = cmd_help; break; case 'l': if (!cmd) cmd = cmd_list; else cmd = cmd_help; break; case 'v': if (!cmd) cmd = cmd_version; else cmd = cmd_help; break; #ifdef __linux__ case 'x': if(!cmd) cmd = cmd_attachall; else cmd = cmd_help; break; #endif case 'h': cmd = cmd_help; break; case 'D': usbip_use_debug = 1; break; case 'S': usbip_use_syslog = 1; break; case '?': break; default: err("getopt"); } } /* disable output buffering (needed to read the pipe when launched from another program) */ setbuf(stderr, NULL); ret = 0; switch(cmd) { case cmd_attach: if (optind == argc - 2) ret = attach_device(argv[optind], argv[optind+1]); else show_help(); break; case cmd_detach: while (optind < argc) ret = detach_port(argv[optind++]); break; case cmd_port: ret = show_port_status(); break; case cmd_list: while (optind < argc) ret = show_exported_devices(argv[optind++]); break; case cmd_attachall: while(optind < argc) ret = attach_devices_all(argv[optind++]); break; case cmd_version: printf("%s\n", version); break; case cmd_help: show_help(); break; default: show_help(); } usbip_names_free(); cleanup_socket(); exit((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); }
static int do_standalone_mode(int daemonize) { struct addrinfo *ai_head; int sockfdlist[MAXSOCKFD]; int nsockfd; int i, terminate; struct pollfd *fds; struct timespec timeout; sigset_t sigmask; if (usbip_names_init(USBIDS_FILE)) err("failed to open %s", USBIDS_FILE); if (usbip_host_driver_open()) { err("please load " USBIP_CORE_MOD_NAME ".ko and " USBIP_HOST_DRV_NAME ".ko!"); return -1; } if (daemonize) { if (daemon(0, 0) < 0) { err("daemonizing failed: %s", strerror(errno)); return -1; } umask(0); usbip_use_syslog = 1; } set_signal(); ai_head = do_getaddrinfo(NULL, PF_UNSPEC); if (!ai_head) return -1; info("starting " PROGNAME " (%s)", usbip_version_string); nsockfd = listen_all_addrinfo(ai_head, sockfdlist); if (nsockfd <= 0) { err("failed to open a listening socket"); return -1; } fds = calloc(nsockfd, sizeof(struct pollfd)); for (i = 0; i < nsockfd; i++) { fds[i].fd = sockfdlist[i]; fds[i].events = POLLIN; } timeout.tv_sec = MAIN_LOOP_TIMEOUT; timeout.tv_nsec = 0; sigfillset(&sigmask); sigdelset(&sigmask, SIGTERM); sigdelset(&sigmask, SIGINT); terminate = 0; while (!terminate) { int r; r = ppoll(fds, nsockfd, &timeout, &sigmask); if (r < 0) { dbg("%s", strerror(errno)); terminate = 1; } else if (r) { for (i = 0; i < nsockfd; i++) { if (fds[i].revents & POLLIN) { dbg("read event on fd[%d]=%d", i, sockfdlist[i]); process_request(sockfdlist[i]); } } } else dbg("heartbeat timeout on ppoll()"); } info("shutting down " PROGNAME); free(fds); freeaddrinfo(ai_head); usbip_host_driver_close(); usbip_names_free(); return 0; }