1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * Copyright (C) 2011 matt mooney <mfm@muteddi 2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com> 4 * 2005-2007 Takahiro Hirofuchi 3 * 2005-2007 Takahiro Hirofuchi 5 * Copyright (C) 2015-2016 Samsung Electronics 4 * Copyright (C) 2015-2016 Samsung Electronics 6 * Igor Kotrasinski <i.kotrasins 5 * Igor Kotrasinski <i.kotrasinsk@samsung.com> 7 * Krzysztof Opasiak <k.opasiak@ 6 * Krzysztof Opasiak <k.opasiak@samsung.com> >> 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/>. 8 */ 20 */ 9 21 10 #ifdef HAVE_CONFIG_H 22 #ifdef HAVE_CONFIG_H 11 #include "../config.h" 23 #include "../config.h" 12 #endif 24 #endif 13 25 14 #define _GNU_SOURCE 26 #define _GNU_SOURCE 15 #include <errno.h> 27 #include <errno.h> 16 #include <unistd.h> 28 #include <unistd.h> 17 #include <netdb.h> 29 #include <netdb.h> 18 #include <string.h> 30 #include <string.h> 19 #include <stdlib.h> 31 #include <stdlib.h> 20 #include <sys/types.h> 32 #include <sys/types.h> 21 #include <sys/stat.h> 33 #include <sys/stat.h> 22 #include <arpa/inet.h> 34 #include <arpa/inet.h> 23 #include <sys/socket.h> 35 #include <sys/socket.h> 24 #include <netinet/in.h> 36 #include <netinet/in.h> 25 37 26 #ifdef HAVE_LIBWRAP 38 #ifdef HAVE_LIBWRAP 27 #include <tcpd.h> 39 #include <tcpd.h> 28 #endif 40 #endif 29 41 30 #include <getopt.h> 42 #include <getopt.h> 31 #include <signal.h> 43 #include <signal.h> 32 #include <poll.h> 44 #include <poll.h> 33 45 34 #include "usbip_host_driver.h" 46 #include "usbip_host_driver.h" 35 #include "usbip_host_common.h" 47 #include "usbip_host_common.h" 36 #include "usbip_device_driver.h" 48 #include "usbip_device_driver.h" 37 #include "usbip_common.h" 49 #include "usbip_common.h" 38 #include "usbip_network.h" 50 #include "usbip_network.h" 39 #include "list.h" 51 #include "list.h" 40 52 41 #undef PROGNAME 53 #undef PROGNAME 42 #define PROGNAME "usbipd" 54 #define PROGNAME "usbipd" 43 #define MAXSOCKFD 20 55 #define MAXSOCKFD 20 44 56 45 #define MAIN_LOOP_TIMEOUT 10 57 #define MAIN_LOOP_TIMEOUT 10 46 58 47 #define DEFAULT_PID_FILE "/var/run/" PROGNAME 59 #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid" 48 60 49 static const char usbip_version_string[] = PAC 61 static const char usbip_version_string[] = PACKAGE_STRING; 50 62 51 static const char usbipd_help_string[] = 63 static const char usbipd_help_string[] = 52 "usage: usbipd [options]\n" 64 "usage: usbipd [options]\n" 53 "\n" 65 "\n" 54 " -4, --ipv4\n" 66 " -4, --ipv4\n" 55 " Bind to IPv4. Default 67 " Bind to IPv4. Default is both.\n" 56 "\n" 68 "\n" 57 " -6, --ipv6\n" 69 " -6, --ipv6\n" 58 " Bind to IPv6. Default 70 " Bind to IPv6. Default is both.\n" 59 "\n" 71 "\n" 60 " -e, --device\n" 72 " -e, --device\n" 61 " Run in device mode.\n" 73 " Run in device mode.\n" 62 " Rather than drive an a 74 " Rather than drive an attached device, create\n" 63 " a virtual UDC to bind 75 " a virtual UDC to bind gadgets to.\n" 64 "\n" 76 "\n" 65 " -D, --daemon\n" 77 " -D, --daemon\n" 66 " Run as a daemon proces 78 " Run as a daemon process.\n" 67 "\n" 79 "\n" 68 " -d, --debug\n" 80 " -d, --debug\n" 69 " Print debugging inform 81 " Print debugging information.\n" 70 "\n" 82 "\n" 71 " -PFILE, --pid FILE\n" 83 " -PFILE, --pid FILE\n" 72 " Write process id to FI 84 " Write process id to FILE.\n" 73 " If no FILE specified, 85 " If no FILE specified, use " DEFAULT_PID_FILE "\n" 74 "\n" 86 "\n" 75 " -tPORT, --tcp-port PORT\n" 87 " -tPORT, --tcp-port PORT\n" 76 " Listen on TCP/IP port 88 " Listen on TCP/IP port PORT.\n" 77 "\n" 89 "\n" 78 " -h, --help\n" 90 " -h, --help\n" 79 " Print this help.\n" 91 " Print this help.\n" 80 "\n" 92 "\n" 81 " -v, --version\n" 93 " -v, --version\n" 82 " Show version.\n"; 94 " Show version.\n"; 83 95 84 static struct usbip_host_driver *driver; 96 static struct usbip_host_driver *driver; 85 97 86 static void usbipd_help(void) 98 static void usbipd_help(void) 87 { 99 { 88 printf("%s\n", usbipd_help_string); 100 printf("%s\n", usbipd_help_string); 89 } 101 } 90 102 91 static int recv_request_import(int sockfd) 103 static int recv_request_import(int sockfd) 92 { 104 { 93 struct op_import_request req; 105 struct op_import_request req; 94 struct usbip_exported_device *edev; 106 struct usbip_exported_device *edev; 95 struct usbip_usb_device pdu_udev; 107 struct usbip_usb_device pdu_udev; 96 struct list_head *i; 108 struct list_head *i; 97 int found = 0; 109 int found = 0; 98 int status = ST_OK; !! 110 int error = 0; 99 int rc; 111 int rc; 100 112 101 memset(&req, 0, sizeof(req)); 113 memset(&req, 0, sizeof(req)); 102 114 103 rc = usbip_net_recv(sockfd, &req, size 115 rc = usbip_net_recv(sockfd, &req, sizeof(req)); 104 if (rc < 0) { 116 if (rc < 0) { 105 dbg("usbip_net_recv failed: im 117 dbg("usbip_net_recv failed: import request"); 106 return -1; 118 return -1; 107 } 119 } 108 PACK_OP_IMPORT_REQUEST(0, &req); 120 PACK_OP_IMPORT_REQUEST(0, &req); 109 121 110 list_for_each(i, &driver->edev_list) { 122 list_for_each(i, &driver->edev_list) { 111 edev = list_entry(i, struct us 123 edev = list_entry(i, struct usbip_exported_device, node); 112 if (!strncmp(req.busid, edev-> 124 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) { 113 info("found requested 125 info("found requested device: %s", req.busid); 114 found = 1; 126 found = 1; 115 break; 127 break; 116 } 128 } 117 } 129 } 118 130 119 if (found) { 131 if (found) { 120 /* should set TCP_NODELAY for 132 /* should set TCP_NODELAY for usbip */ 121 usbip_net_set_nodelay(sockfd); 133 usbip_net_set_nodelay(sockfd); 122 134 123 /* export device needs a TCP/I 135 /* export device needs a TCP/IP socket descriptor */ 124 status = usbip_export_device(e !! 136 rc = usbip_export_device(edev, sockfd); 125 if (status < 0) !! 137 if (rc < 0) 126 status = ST_NA; !! 138 error = 1; 127 } else { 139 } else { 128 info("requested device not fou 140 info("requested device not found: %s", req.busid); 129 status = ST_NODEV; !! 141 error = 1; 130 } 142 } 131 143 132 rc = usbip_net_send_op_common(sockfd, !! 144 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, >> 145 (!error ? ST_OK : ST_NA)); 133 if (rc < 0) { 146 if (rc < 0) { 134 dbg("usbip_net_send_op_common 147 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT); 135 return -1; 148 return -1; 136 } 149 } 137 150 138 if (status) { !! 151 if (error) { 139 dbg("import request busid %s: 152 dbg("import request busid %s: failed", req.busid); 140 return -1; 153 return -1; 141 } 154 } 142 155 143 memcpy(&pdu_udev, &edev->udev, sizeof( 156 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev)); 144 usbip_net_pack_usb_device(1, &pdu_udev 157 usbip_net_pack_usb_device(1, &pdu_udev); 145 158 146 rc = usbip_net_send(sockfd, &pdu_udev, 159 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev)); 147 if (rc < 0) { 160 if (rc < 0) { 148 dbg("usbip_net_send failed: de 161 dbg("usbip_net_send failed: devinfo"); 149 return -1; 162 return -1; 150 } 163 } 151 164 152 dbg("import request busid %s: complete 165 dbg("import request busid %s: complete", req.busid); 153 166 154 return 0; 167 return 0; 155 } 168 } 156 169 157 static int send_reply_devlist(int connfd) 170 static int send_reply_devlist(int connfd) 158 { 171 { 159 struct usbip_exported_device *edev; 172 struct usbip_exported_device *edev; 160 struct usbip_usb_device pdu_udev; 173 struct usbip_usb_device pdu_udev; 161 struct usbip_usb_interface pdu_uinf; 174 struct usbip_usb_interface pdu_uinf; 162 struct op_devlist_reply reply; 175 struct op_devlist_reply reply; 163 struct list_head *j; 176 struct list_head *j; 164 int rc, i; 177 int rc, i; 165 178 166 /* << 167 * Exclude devices that are already ex << 168 * the exportable device list to avoid << 169 * - import requests for devices << 170 * fail the request. << 171 * - revealing devices that are i << 172 * another client. << 173 */ << 174 << 175 reply.ndev = 0; 179 reply.ndev = 0; 176 /* number of exported devices */ 180 /* number of exported devices */ 177 list_for_each(j, &driver->edev_list) { 181 list_for_each(j, &driver->edev_list) { 178 edev = list_entry(j, struct us !! 182 reply.ndev += 1; 179 if (edev->status != SDEV_ST_US << 180 reply.ndev += 1; << 181 } 183 } 182 info("exportable devices: %d", reply.n 184 info("exportable devices: %d", reply.ndev); 183 185 184 rc = usbip_net_send_op_common(connfd, 186 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK); 185 if (rc < 0) { 187 if (rc < 0) { 186 dbg("usbip_net_send_op_common 188 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST); 187 return -1; 189 return -1; 188 } 190 } 189 PACK_OP_DEVLIST_REPLY(1, &reply); 191 PACK_OP_DEVLIST_REPLY(1, &reply); 190 192 191 rc = usbip_net_send(connfd, &reply, si 193 rc = usbip_net_send(connfd, &reply, sizeof(reply)); 192 if (rc < 0) { 194 if (rc < 0) { 193 dbg("usbip_net_send failed: %# 195 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST); 194 return -1; 196 return -1; 195 } 197 } 196 198 197 list_for_each(j, &driver->edev_list) { 199 list_for_each(j, &driver->edev_list) { 198 edev = list_entry(j, struct us 200 edev = list_entry(j, struct usbip_exported_device, node); 199 if (edev->status == SDEV_ST_US << 200 continue; << 201 << 202 dump_usb_device(&edev->udev); 201 dump_usb_device(&edev->udev); 203 memcpy(&pdu_udev, &edev->udev, 202 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev)); 204 usbip_net_pack_usb_device(1, & 203 usbip_net_pack_usb_device(1, &pdu_udev); 205 204 206 rc = usbip_net_send(connfd, &p 205 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev)); 207 if (rc < 0) { 206 if (rc < 0) { 208 dbg("usbip_net_send fa 207 dbg("usbip_net_send failed: pdu_udev"); 209 return -1; 208 return -1; 210 } 209 } 211 210 212 for (i = 0; i < edev->udev.bNu 211 for (i = 0; i < edev->udev.bNumInterfaces; i++) { 213 dump_usb_interface(&ed 212 dump_usb_interface(&edev->uinf[i]); 214 memcpy(&pdu_uinf, &ede 213 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf)); 215 usbip_net_pack_usb_int 214 usbip_net_pack_usb_interface(1, &pdu_uinf); 216 215 217 rc = usbip_net_send(co 216 rc = usbip_net_send(connfd, &pdu_uinf, 218 sizeof 217 sizeof(pdu_uinf)); 219 if (rc < 0) { 218 if (rc < 0) { 220 err("usbip_net 219 err("usbip_net_send failed: pdu_uinf"); 221 return -1; 220 return -1; 222 } 221 } 223 } 222 } 224 } 223 } 225 224 226 return 0; 225 return 0; 227 } 226 } 228 227 229 static int recv_request_devlist(int connfd) 228 static int recv_request_devlist(int connfd) 230 { 229 { 231 struct op_devlist_request req; 230 struct op_devlist_request req; 232 int rc; 231 int rc; 233 232 234 memset(&req, 0, sizeof(req)); 233 memset(&req, 0, sizeof(req)); 235 234 236 rc = usbip_net_recv(connfd, &req, size 235 rc = usbip_net_recv(connfd, &req, sizeof(req)); 237 if (rc < 0) { 236 if (rc < 0) { 238 dbg("usbip_net_recv failed: de 237 dbg("usbip_net_recv failed: devlist request"); 239 return -1; 238 return -1; 240 } 239 } 241 240 242 rc = send_reply_devlist(connfd); 241 rc = send_reply_devlist(connfd); 243 if (rc < 0) { 242 if (rc < 0) { 244 dbg("send_reply_devlist failed 243 dbg("send_reply_devlist failed"); 245 return -1; 244 return -1; 246 } 245 } 247 246 248 return 0; 247 return 0; 249 } 248 } 250 249 251 static int recv_pdu(int connfd) 250 static int recv_pdu(int connfd) 252 { 251 { 253 uint16_t code = OP_UNSPEC; 252 uint16_t code = OP_UNSPEC; 254 int ret; 253 int ret; 255 int status; << 256 254 257 ret = usbip_net_recv_op_common(connfd, !! 255 ret = usbip_net_recv_op_common(connfd, &code); 258 if (ret < 0) { 256 if (ret < 0) { 259 dbg("could not receive opcode: 257 dbg("could not receive opcode: %#0x", code); 260 return -1; 258 return -1; 261 } 259 } 262 260 263 ret = usbip_refresh_device_list(driver 261 ret = usbip_refresh_device_list(driver); 264 if (ret < 0) { 262 if (ret < 0) { 265 dbg("could not refresh device 263 dbg("could not refresh device list: %d", ret); 266 return -1; 264 return -1; 267 } 265 } 268 266 269 info("received request: %#0x(%d)", cod 267 info("received request: %#0x(%d)", code, connfd); 270 switch (code) { 268 switch (code) { 271 case OP_REQ_DEVLIST: 269 case OP_REQ_DEVLIST: 272 ret = recv_request_devlist(con 270 ret = recv_request_devlist(connfd); 273 break; 271 break; 274 case OP_REQ_IMPORT: 272 case OP_REQ_IMPORT: 275 ret = recv_request_import(conn 273 ret = recv_request_import(connfd); 276 break; 274 break; 277 case OP_REQ_DEVINFO: 275 case OP_REQ_DEVINFO: 278 case OP_REQ_CRYPKEY: 276 case OP_REQ_CRYPKEY: 279 default: 277 default: 280 err("received an unknown opcod 278 err("received an unknown opcode: %#0x", code); 281 ret = -1; 279 ret = -1; 282 } 280 } 283 281 284 if (ret == 0) 282 if (ret == 0) 285 info("request %#0x(%d): comple 283 info("request %#0x(%d): complete", code, connfd); 286 else 284 else 287 info("request %#0x(%d): failed 285 info("request %#0x(%d): failed", code, connfd); 288 286 289 return ret; 287 return ret; 290 } 288 } 291 289 292 #ifdef HAVE_LIBWRAP 290 #ifdef HAVE_LIBWRAP 293 static int tcpd_auth(int connfd) 291 static int tcpd_auth(int connfd) 294 { 292 { 295 struct request_info request; 293 struct request_info request; 296 int rc; 294 int rc; 297 295 298 request_init(&request, RQ_DAEMON, PROG 296 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0); 299 fromhost(&request); 297 fromhost(&request); 300 rc = hosts_access(&request); 298 rc = hosts_access(&request); 301 if (rc == 0) 299 if (rc == 0) 302 return -1; 300 return -1; 303 301 304 return 0; 302 return 0; 305 } 303 } 306 #endif 304 #endif 307 305 308 static int do_accept(int listenfd) 306 static int do_accept(int listenfd) 309 { 307 { 310 int connfd; 308 int connfd; 311 struct sockaddr_storage ss; 309 struct sockaddr_storage ss; 312 socklen_t len = sizeof(ss); 310 socklen_t len = sizeof(ss); 313 char host[NI_MAXHOST], port[NI_MAXSERV 311 char host[NI_MAXHOST], port[NI_MAXSERV]; 314 int rc; 312 int rc; 315 313 316 memset(&ss, 0, sizeof(ss)); 314 memset(&ss, 0, sizeof(ss)); 317 315 318 connfd = accept(listenfd, (struct sock 316 connfd = accept(listenfd, (struct sockaddr *)&ss, &len); 319 if (connfd < 0) { 317 if (connfd < 0) { 320 err("failed to accept connecti 318 err("failed to accept connection"); 321 return -1; 319 return -1; 322 } 320 } 323 321 324 rc = getnameinfo((struct sockaddr *)&s 322 rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host), 325 port, sizeof(port), N 323 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); 326 if (rc) 324 if (rc) 327 err("getnameinfo: %s", gai_str 325 err("getnameinfo: %s", gai_strerror(rc)); 328 326 329 #ifdef HAVE_LIBWRAP 327 #ifdef HAVE_LIBWRAP 330 rc = tcpd_auth(connfd); 328 rc = tcpd_auth(connfd); 331 if (rc < 0) { 329 if (rc < 0) { 332 info("denied access from %s", 330 info("denied access from %s", host); 333 close(connfd); 331 close(connfd); 334 return -1; 332 return -1; 335 } 333 } 336 #endif 334 #endif 337 info("connection from %s:%s", host, po 335 info("connection from %s:%s", host, port); 338 336 339 return connfd; 337 return connfd; 340 } 338 } 341 339 342 int process_request(int listenfd) 340 int process_request(int listenfd) 343 { 341 { 344 pid_t childpid; 342 pid_t childpid; 345 int connfd; 343 int connfd; 346 344 347 connfd = do_accept(listenfd); 345 connfd = do_accept(listenfd); 348 if (connfd < 0) 346 if (connfd < 0) 349 return -1; 347 return -1; 350 childpid = fork(); 348 childpid = fork(); 351 if (childpid == 0) { 349 if (childpid == 0) { 352 close(listenfd); 350 close(listenfd); 353 recv_pdu(connfd); 351 recv_pdu(connfd); 354 exit(0); 352 exit(0); 355 } 353 } 356 close(connfd); 354 close(connfd); 357 return 0; 355 return 0; 358 } 356 } 359 357 360 static void addrinfo_to_text(struct addrinfo * 358 static void addrinfo_to_text(struct addrinfo *ai, char buf[], 361 const size_t buf_ 359 const size_t buf_size) 362 { 360 { 363 char hbuf[NI_MAXHOST]; 361 char hbuf[NI_MAXHOST]; 364 char sbuf[NI_MAXSERV]; 362 char sbuf[NI_MAXSERV]; 365 int rc; 363 int rc; 366 364 367 buf[0] = '\0'; 365 buf[0] = '\0'; 368 366 369 rc = getnameinfo(ai->ai_addr, ai->ai_a 367 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), 370 sbuf, sizeof(sbuf), N 368 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); 371 if (rc) 369 if (rc) 372 err("getnameinfo: %s", gai_str 370 err("getnameinfo: %s", gai_strerror(rc)); 373 371 374 snprintf(buf, buf_size, "%s:%s", hbuf, 372 snprintf(buf, buf_size, "%s:%s", hbuf, sbuf); 375 } 373 } 376 374 377 static int listen_all_addrinfo(struct addrinfo 375 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[], 378 int maxsockfd) 376 int maxsockfd) 379 { 377 { 380 struct addrinfo *ai; 378 struct addrinfo *ai; 381 int ret, nsockfd = 0; 379 int ret, nsockfd = 0; 382 const size_t ai_buf_size = NI_MAXHOST 380 const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2; 383 char ai_buf[ai_buf_size]; 381 char ai_buf[ai_buf_size]; 384 382 385 for (ai = ai_head; ai && nsockfd < max 383 for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) { 386 int sock; 384 int sock; 387 385 388 addrinfo_to_text(ai, ai_buf, a 386 addrinfo_to_text(ai, ai_buf, ai_buf_size); 389 dbg("opening %s", ai_buf); 387 dbg("opening %s", ai_buf); 390 sock = socket(ai->ai_family, a 388 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 391 if (sock < 0) { 389 if (sock < 0) { 392 err("socket: %s: %d (% 390 err("socket: %s: %d (%s)", 393 ai_buf, errno, str 391 ai_buf, errno, strerror(errno)); 394 continue; 392 continue; 395 } 393 } 396 394 397 usbip_net_set_reuseaddr(sock); 395 usbip_net_set_reuseaddr(sock); 398 usbip_net_set_nodelay(sock); 396 usbip_net_set_nodelay(sock); 399 /* We use seperate sockets for 397 /* We use seperate sockets for IPv4 and IPv6 400 * (see do_standalone_mode()) 398 * (see do_standalone_mode()) */ 401 usbip_net_set_v6only(sock); 399 usbip_net_set_v6only(sock); 402 400 403 ret = bind(sock, ai->ai_addr, 401 ret = bind(sock, ai->ai_addr, ai->ai_addrlen); 404 if (ret < 0) { 402 if (ret < 0) { 405 err("bind: %s: %d (%s) 403 err("bind: %s: %d (%s)", 406 ai_buf, errno, str 404 ai_buf, errno, strerror(errno)); 407 close(sock); 405 close(sock); 408 continue; 406 continue; 409 } 407 } 410 408 411 ret = listen(sock, SOMAXCONN); 409 ret = listen(sock, SOMAXCONN); 412 if (ret < 0) { 410 if (ret < 0) { 413 err("listen: %s: %d (% 411 err("listen: %s: %d (%s)", 414 ai_buf, errno, str 412 ai_buf, errno, strerror(errno)); 415 close(sock); 413 close(sock); 416 continue; 414 continue; 417 } 415 } 418 416 419 info("listening on %s", ai_buf 417 info("listening on %s", ai_buf); 420 sockfdlist[nsockfd++] = sock; 418 sockfdlist[nsockfd++] = sock; 421 } 419 } 422 420 423 return nsockfd; 421 return nsockfd; 424 } 422 } 425 423 426 static struct addrinfo *do_getaddrinfo(char *h 424 static struct addrinfo *do_getaddrinfo(char *host, int ai_family) 427 { 425 { 428 struct addrinfo hints, *ai_head; 426 struct addrinfo hints, *ai_head; 429 int rc; 427 int rc; 430 428 431 memset(&hints, 0, sizeof(hints)); 429 memset(&hints, 0, sizeof(hints)); 432 hints.ai_family = ai_family; 430 hints.ai_family = ai_family; 433 hints.ai_socktype = SOCK_STREAM; 431 hints.ai_socktype = SOCK_STREAM; 434 hints.ai_flags = AI_PASSIVE; 432 hints.ai_flags = AI_PASSIVE; 435 433 436 rc = getaddrinfo(host, usbip_port_stri 434 rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head); 437 if (rc) { 435 if (rc) { 438 err("failed to get a network a 436 err("failed to get a network address %s: %s", usbip_port_string, 439 gai_strerror(rc)); 437 gai_strerror(rc)); 440 return NULL; 438 return NULL; 441 } 439 } 442 440 443 return ai_head; 441 return ai_head; 444 } 442 } 445 443 446 static void signal_handler(int i) 444 static void signal_handler(int i) 447 { 445 { 448 dbg("received '%s' signal", strsignal( 446 dbg("received '%s' signal", strsignal(i)); 449 } 447 } 450 448 451 static void set_signal(void) 449 static void set_signal(void) 452 { 450 { 453 struct sigaction act; 451 struct sigaction act; 454 452 455 memset(&act, 0, sizeof(act)); 453 memset(&act, 0, sizeof(act)); 456 act.sa_handler = signal_handler; 454 act.sa_handler = signal_handler; 457 sigemptyset(&act.sa_mask); 455 sigemptyset(&act.sa_mask); 458 sigaction(SIGTERM, &act, NULL); 456 sigaction(SIGTERM, &act, NULL); 459 sigaction(SIGINT, &act, NULL); 457 sigaction(SIGINT, &act, NULL); 460 act.sa_handler = SIG_IGN; 458 act.sa_handler = SIG_IGN; 461 sigaction(SIGCHLD, &act, NULL); !! 459 sigaction(SIGCLD, &act, NULL); 462 } 460 } 463 461 464 static const char *pid_file; 462 static const char *pid_file; 465 463 466 static void write_pid_file(void) 464 static void write_pid_file(void) 467 { 465 { 468 if (pid_file) { 466 if (pid_file) { 469 dbg("creating pid file %s", pi 467 dbg("creating pid file %s", pid_file); 470 FILE *fp; 468 FILE *fp; 471 469 472 fp = fopen(pid_file, "w"); 470 fp = fopen(pid_file, "w"); 473 if (!fp) { 471 if (!fp) { 474 err("pid_file: %s: %d 472 err("pid_file: %s: %d (%s)", 475 pid_file, errno, s 473 pid_file, errno, strerror(errno)); 476 return; 474 return; 477 } 475 } 478 fprintf(fp, "%d\n", getpid()); 476 fprintf(fp, "%d\n", getpid()); 479 fclose(fp); 477 fclose(fp); 480 } 478 } 481 } 479 } 482 480 483 static void remove_pid_file(void) 481 static void remove_pid_file(void) 484 { 482 { 485 if (pid_file) { 483 if (pid_file) { 486 dbg("removing pid file %s", pi 484 dbg("removing pid file %s", pid_file); 487 unlink(pid_file); 485 unlink(pid_file); 488 } 486 } 489 } 487 } 490 488 491 static int do_standalone_mode(int daemonize, i 489 static int do_standalone_mode(int daemonize, int ipv4, int ipv6) 492 { 490 { 493 struct addrinfo *ai_head; 491 struct addrinfo *ai_head; 494 int sockfdlist[MAXSOCKFD]; 492 int sockfdlist[MAXSOCKFD]; 495 int nsockfd, family; 493 int nsockfd, family; 496 int i, terminate; 494 int i, terminate; 497 struct pollfd *fds; 495 struct pollfd *fds; 498 struct timespec timeout; 496 struct timespec timeout; 499 sigset_t sigmask; 497 sigset_t sigmask; 500 498 501 if (usbip_driver_open(driver)) 499 if (usbip_driver_open(driver)) 502 return -1; 500 return -1; 503 501 504 if (daemonize) { 502 if (daemonize) { 505 if (daemon(0, 0) < 0) { 503 if (daemon(0, 0) < 0) { 506 err("daemonizing faile 504 err("daemonizing failed: %s", strerror(errno)); 507 usbip_driver_close(dri 505 usbip_driver_close(driver); 508 return -1; 506 return -1; 509 } 507 } 510 umask(0); 508 umask(0); 511 usbip_use_syslog = 1; 509 usbip_use_syslog = 1; 512 } 510 } 513 set_signal(); 511 set_signal(); 514 write_pid_file(); 512 write_pid_file(); 515 513 516 info("starting " PROGNAME " (%s)", usb 514 info("starting " PROGNAME " (%s)", usbip_version_string); 517 515 518 /* 516 /* 519 * To suppress warnings on systems wit 517 * To suppress warnings on systems with bindv6only disabled 520 * (default), we use seperate sockets 518 * (default), we use seperate sockets for IPv6 and IPv4 and set 521 * IPV6_V6ONLY on the IPv6 sockets. 519 * IPV6_V6ONLY on the IPv6 sockets. 522 */ 520 */ 523 if (ipv4 && ipv6) 521 if (ipv4 && ipv6) 524 family = AF_UNSPEC; 522 family = AF_UNSPEC; 525 else if (ipv4) 523 else if (ipv4) 526 family = AF_INET; 524 family = AF_INET; 527 else 525 else 528 family = AF_INET6; 526 family = AF_INET6; 529 527 530 ai_head = do_getaddrinfo(NULL, family) 528 ai_head = do_getaddrinfo(NULL, family); 531 if (!ai_head) { 529 if (!ai_head) { 532 usbip_driver_close(driver); 530 usbip_driver_close(driver); 533 return -1; 531 return -1; 534 } 532 } 535 nsockfd = listen_all_addrinfo(ai_head, 533 nsockfd = listen_all_addrinfo(ai_head, sockfdlist, 536 sizeof(sockfdlist) / sizeof(*s 534 sizeof(sockfdlist) / sizeof(*sockfdlist)); 537 freeaddrinfo(ai_head); 535 freeaddrinfo(ai_head); 538 if (nsockfd <= 0) { 536 if (nsockfd <= 0) { 539 err("failed to open a listenin 537 err("failed to open a listening socket"); 540 usbip_driver_close(driver); 538 usbip_driver_close(driver); 541 return -1; 539 return -1; 542 } 540 } 543 541 544 dbg("listening on %d address%s", nsock 542 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es"); 545 543 546 fds = calloc(nsockfd, sizeof(struct po 544 fds = calloc(nsockfd, sizeof(struct pollfd)); 547 for (i = 0; i < nsockfd; i++) { 545 for (i = 0; i < nsockfd; i++) { 548 fds[i].fd = sockfdlist[i]; 546 fds[i].fd = sockfdlist[i]; 549 fds[i].events = POLLIN; 547 fds[i].events = POLLIN; 550 } 548 } 551 timeout.tv_sec = MAIN_LOOP_TIMEOUT; 549 timeout.tv_sec = MAIN_LOOP_TIMEOUT; 552 timeout.tv_nsec = 0; 550 timeout.tv_nsec = 0; 553 551 554 sigfillset(&sigmask); 552 sigfillset(&sigmask); 555 sigdelset(&sigmask, SIGTERM); 553 sigdelset(&sigmask, SIGTERM); 556 sigdelset(&sigmask, SIGINT); 554 sigdelset(&sigmask, SIGINT); 557 555 558 terminate = 0; 556 terminate = 0; 559 while (!terminate) { 557 while (!terminate) { 560 int r; 558 int r; 561 559 562 r = ppoll(fds, nsockfd, &timeo 560 r = ppoll(fds, nsockfd, &timeout, &sigmask); 563 if (r < 0) { 561 if (r < 0) { 564 dbg("%s", strerror(err 562 dbg("%s", strerror(errno)); 565 terminate = 1; 563 terminate = 1; 566 } else if (r) { 564 } else if (r) { 567 for (i = 0; i < nsockf 565 for (i = 0; i < nsockfd; i++) { 568 if (fds[i].rev 566 if (fds[i].revents & POLLIN) { 569 dbg("r 567 dbg("read event on fd[%d]=%d", 570 i, 568 i, sockfdlist[i]); 571 proces 569 process_request(sockfdlist[i]); 572 } 570 } 573 } 571 } 574 } else { 572 } else { 575 dbg("heartbeat timeout 573 dbg("heartbeat timeout on ppoll()"); 576 } 574 } 577 } 575 } 578 576 579 info("shutting down " PROGNAME); 577 info("shutting down " PROGNAME); 580 free(fds); 578 free(fds); 581 usbip_driver_close(driver); 579 usbip_driver_close(driver); 582 580 583 return 0; 581 return 0; 584 } 582 } 585 583 586 int main(int argc, char *argv[]) 584 int main(int argc, char *argv[]) 587 { 585 { 588 static const struct option longopts[] 586 static const struct option longopts[] = { 589 { "ipv4", no_argument, 587 { "ipv4", no_argument, NULL, '4' }, 590 { "ipv6", no_argument, 588 { "ipv6", no_argument, NULL, '6' }, 591 { "daemon", no_argument, 589 { "daemon", no_argument, NULL, 'D' }, 592 { "daemon", no_argument, 590 { "daemon", no_argument, NULL, 'D' }, 593 { "debug", no_argument, 591 { "debug", no_argument, NULL, 'd' }, 594 { "device", no_argument, 592 { "device", no_argument, NULL, 'e' }, 595 { "pid", optional_argumen 593 { "pid", optional_argument, NULL, 'P' }, 596 { "tcp-port", required_argumen 594 { "tcp-port", required_argument, NULL, 't' }, 597 { "help", no_argument, 595 { "help", no_argument, NULL, 'h' }, 598 { "version", no_argument, 596 { "version", no_argument, NULL, 'v' }, 599 { NULL, 0, 597 { NULL, 0, NULL, 0 } 600 }; 598 }; 601 599 602 enum { 600 enum { 603 cmd_standalone_mode = 1, 601 cmd_standalone_mode = 1, 604 cmd_help, 602 cmd_help, 605 cmd_version 603 cmd_version 606 } cmd; 604 } cmd; 607 605 608 int daemonize = 0; 606 int daemonize = 0; 609 int ipv4 = 0, ipv6 = 0; 607 int ipv4 = 0, ipv6 = 0; 610 int opt, rc = -1; 608 int opt, rc = -1; 611 609 612 pid_file = NULL; 610 pid_file = NULL; 613 611 614 usbip_use_stderr = 1; 612 usbip_use_stderr = 1; 615 usbip_use_syslog = 0; 613 usbip_use_syslog = 0; 616 614 617 if (geteuid() != 0) 615 if (geteuid() != 0) 618 err("not running as root?"); 616 err("not running as root?"); 619 617 620 cmd = cmd_standalone_mode; 618 cmd = cmd_standalone_mode; 621 driver = &host_driver; 619 driver = &host_driver; 622 for (;;) { 620 for (;;) { 623 opt = getopt_long(argc, argv, 621 opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL); 624 622 625 if (opt == -1) 623 if (opt == -1) 626 break; 624 break; 627 625 628 switch (opt) { 626 switch (opt) { 629 case '4': 627 case '4': 630 ipv4 = 1; 628 ipv4 = 1; 631 break; 629 break; 632 case '6': 630 case '6': 633 ipv6 = 1; 631 ipv6 = 1; 634 break; 632 break; 635 case 'D': 633 case 'D': 636 daemonize = 1; 634 daemonize = 1; 637 break; 635 break; 638 case 'd': 636 case 'd': 639 usbip_use_debug = 1; 637 usbip_use_debug = 1; 640 break; 638 break; 641 case 'h': 639 case 'h': 642 cmd = cmd_help; 640 cmd = cmd_help; 643 break; 641 break; 644 case 'P': 642 case 'P': 645 pid_file = optarg ? op 643 pid_file = optarg ? optarg : DEFAULT_PID_FILE; 646 break; 644 break; 647 case 't': 645 case 't': 648 usbip_setup_port_numbe 646 usbip_setup_port_number(optarg); 649 break; 647 break; 650 case 'v': 648 case 'v': 651 cmd = cmd_version; 649 cmd = cmd_version; 652 break; 650 break; 653 case 'e': 651 case 'e': 654 driver = &device_drive 652 driver = &device_driver; 655 break; 653 break; 656 case '?': 654 case '?': 657 usbipd_help(); 655 usbipd_help(); 658 default: 656 default: 659 goto err_out; 657 goto err_out; 660 } 658 } 661 } 659 } 662 660 663 if (!ipv4 && !ipv6) 661 if (!ipv4 && !ipv6) 664 ipv4 = ipv6 = 1; 662 ipv4 = ipv6 = 1; 665 663 666 switch (cmd) { 664 switch (cmd) { 667 case cmd_standalone_mode: 665 case cmd_standalone_mode: 668 rc = do_standalone_mode(daemon 666 rc = do_standalone_mode(daemonize, ipv4, ipv6); 669 remove_pid_file(); 667 remove_pid_file(); 670 break; 668 break; 671 case cmd_version: 669 case cmd_version: 672 printf(PROGNAME " (%s)\n", usb 670 printf(PROGNAME " (%s)\n", usbip_version_string); 673 rc = 0; 671 rc = 0; 674 break; 672 break; 675 case cmd_help: 673 case cmd_help: 676 usbipd_help(); 674 usbipd_help(); 677 rc = 0; 675 rc = 0; 678 break; 676 break; 679 default: 677 default: 680 usbipd_help(); 678 usbipd_help(); 681 goto err_out; 679 goto err_out; 682 } 680 } 683 681 684 err_out: 682 err_out: 685 return (rc > -1 ? EXIT_SUCCESS : EXIT_ 683 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE); 686 } 684 } 687 685
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.