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; }
/* * exports the device on @busid to @host. * * few function calls. But error checking makes this function long and ugly. */ int export_busid_to_host(char *host, char *busid) { int ret; int sockfd; uint16_t code = OP_REP_EXPORT; struct usbip_exported_device *edev; /* * open stub driver */ ret = usbip_stub_driver_open(); if( ret != 0 ) { err( "could not open stub_driver"); return -1; } /* * get the relevant device */ edev = busid_to_edev(busid); if( edev == NULL ) { err( "no device found matching busid" ); goto exit_failure; } /* * Open connection and tell server* we want to export a device * * * server here means 'remote host', 'OS-Server', ... * This is the machine thats runs the virtual host controller */ sockfd = tcp_connect(host, USBAID_PORT_STRING); if( sockfd < 0 ) { err("tcp connection failed"); goto exit_failure; } dbg("tcp connection established"); /* * mark device as exported */ ret = usbip_stub_export_device(edev, sockfd); if( ret < 0 ) { err( "exporting of the device failed" ); goto exit_failure; } dbg("devices marked as exported"); /* * now, tell server */ ret = usbip_send_op_common( sockfd, OP_REQ_EXPORT, 0 ); if( ret < 0 ) { err( "sending OP_REQ_EXPORT failed" ); goto exit_failure; } dbg("export request (OP_COMMON) sent"); ret = send_request_export( sockfd, &(edev->udev) ); if( ret < 0 ) { err( "sending export request failed" ); goto exit_failure; } dbg("export request (device) sent"); dbg("device exported" ); /* * We do not wait for a status notification. see * usbaid.c::handle_export_query() for details. * * For now, we simply assume that the export was successfull */ usbip_stub_driver_close(); return 0; exit_failure: close(sockfd); usbip_stub_driver_close(); return -1; }
int unexport_busid_from_host(char *host, char *busid) { int ret; int sockfd; uint16_t code = OP_REP_UNEXPORT; struct usbip_exported_device *edev; /* * open stub driver */ ret = usbip_stub_driver_open(); if( ret != 0 ) { err( "could not open stub_driver"); return -1; } /* * get the relevant device */ edev = busid_to_edev(busid); if( edev == NULL ) { err( "no device found matching busid" ); goto exit_failure; } /* * Open connection and tell server we want to unexport a device */ sockfd = tcp_connect(host, USBAID_PORT_STRING); if( sockfd < 0 ) { err("tcp connection failed"); goto exit_failure; } dbg("tcp connection established"); /* * now, tell server */ ret = usbip_send_op_common( sockfd, OP_REQ_UNEXPORT, 0 ); if( ret < 0 ) { err( "sending OP_REQ_UNEXPORT failed" ); goto exit_failure; } dbg("unexport request (OP_COMMON) sent"); ret = send_request_unexport( sockfd, &(edev->udev) ); if( ret < 0 ) { err( "sending unexport request failed" ); goto exit_failure; } dbg("unexport request (device) sent"); /* * mark device as no longer exported */ /*XXX There seems to be no action on the 'client' side neccessary. * The server needs to be told, that it should detach the device. * On the client side, the device is 'unexported' by releasing it from * the usbip driver. */ /* receive status message from server */ ret = usbip_recv_op_common(sockfd, &code); if( ret < 0 ) { err( "receiving op_common failed" ); goto exit_failure; } usbip_stub_driver_close(); return 0; exit_failure: usbip_stub_driver_close(); return -1; }