2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "../config.h"
29 #include <sys/types.h>
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
43 #include "usbip_host_driver.h"
44 #include "usbip_common.h"
45 #include "usbip_network.h"
48 #define PROGNAME "usbipd"
51 #define MAIN_LOOP_TIMEOUT 10
53 #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
55 static const char usbip_version_string[] = PACKAGE_STRING;
57 static const char usbipd_help_string[] =
58 "usage: usbipd [options]\n"
60 " Run as a daemon process.\n"
63 " Print debugging information.\n"
65 " -PFILE, --pid FILE\n"
66 " Write process id to FILE.\n"
67 " If no FILE specified, use " DEFAULT_PID_FILE "\n"
69 " -tPORT, --tcp-port PORT\n"
70 " Listen on TCP/IP port PORT.\n"
78 static void usbipd_help(void)
80 printf("%s\n", usbipd_help_string);
83 static int recv_request_import(int sockfd)
85 struct op_import_request req;
86 struct op_common reply;
87 struct usbip_exported_device *edev;
88 struct usbip_usb_device pdu_udev;
93 memset(&req, 0, sizeof(req));
94 memset(&reply, 0, sizeof(reply));
96 rc = usbip_net_recv(sockfd, &req, sizeof(req));
98 dbg("usbip_net_recv failed: import request");
101 PACK_OP_IMPORT_REQUEST(0, &req);
103 dlist_for_each_data(host_driver->edev_list, edev,
104 struct usbip_exported_device) {
105 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
106 info("found requested device: %s", req.busid);
113 /* should set TCP_NODELAY for usbip */
114 usbip_net_set_nodelay(sockfd);
116 /* export device needs a TCP/IP socket descriptor */
117 rc = usbip_host_export_device(edev, sockfd);
121 info("requested device not found: %s", req.busid);
125 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
126 (!error ? ST_OK : ST_NA));
128 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
133 dbg("import request busid %s: failed", req.busid);
137 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
138 usbip_net_pack_usb_device(1, &pdu_udev);
140 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
142 dbg("usbip_net_send failed: devinfo");
146 dbg("import request busid %s: complete", req.busid);
151 static int send_reply_devlist(int connfd)
153 struct usbip_exported_device *edev;
154 struct usbip_usb_device pdu_udev;
155 struct usbip_usb_interface pdu_uinf;
156 struct op_devlist_reply reply;
161 /* number of exported devices */
162 dlist_for_each_data(host_driver->edev_list, edev,
163 struct usbip_exported_device) {
166 info("exportable devices: %d", reply.ndev);
168 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
170 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
173 PACK_OP_DEVLIST_REPLY(1, &reply);
175 rc = usbip_net_send(connfd, &reply, sizeof(reply));
177 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
181 dlist_for_each_data(host_driver->edev_list, edev,
182 struct usbip_exported_device) {
183 dump_usb_device(&edev->udev);
184 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
185 usbip_net_pack_usb_device(1, &pdu_udev);
187 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
189 dbg("usbip_net_send failed: pdu_udev");
193 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
194 dump_usb_interface(&edev->uinf[i]);
195 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
196 usbip_net_pack_usb_interface(1, &pdu_uinf);
198 rc = usbip_net_send(connfd, &pdu_uinf,
201 dbg("usbip_net_send failed: pdu_uinf");
210 static int recv_request_devlist(int connfd)
212 struct op_devlist_request req;
215 memset(&req, 0, sizeof(req));
217 rc = usbip_net_recv(connfd, &req, sizeof(req));
219 dbg("usbip_net_recv failed: devlist request");
223 rc = send_reply_devlist(connfd);
225 dbg("send_reply_devlist failed");
232 static int recv_pdu(int connfd)
234 uint16_t code = OP_UNSPEC;
237 ret = usbip_net_recv_op_common(connfd, &code);
239 dbg("could not receive opcode: %#0x", code);
243 ret = usbip_host_refresh_device_list();
245 dbg("could not refresh device list: %d", ret);
249 info("received request: %#0x(%d)", code, connfd);
252 ret = recv_request_devlist(connfd);
255 ret = recv_request_import(connfd);
260 err("received an unknown opcode: %#0x", code);
265 info("request %#0x(%d): complete", code, connfd);
267 info("request %#0x(%d): failed", code, connfd);
273 static int tcpd_auth(int connfd)
275 struct request_info request;
278 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
280 rc = hosts_access(&request);
288 static int do_accept(int listenfd)
291 struct sockaddr_storage ss;
292 socklen_t len = sizeof(ss);
293 char host[NI_MAXHOST], port[NI_MAXSERV];
296 memset(&ss, 0, sizeof(ss));
298 connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
300 err("failed to accept connection");
304 rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
305 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
307 err("getnameinfo: %s", gai_strerror(rc));
310 rc = tcpd_auth(connfd);
312 info("denied access from %s", host);
317 info("connection from %s:%s", host, port);
322 int process_request(int listenfd)
327 connfd = do_accept(listenfd);
340 static void addrinfo_to_text(struct addrinfo *ai, char buf[],
341 const size_t buf_size)
343 char hbuf[NI_MAXHOST];
344 char sbuf[NI_MAXSERV];
349 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
350 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
352 err("getnameinfo: %s", gai_strerror(rc));
354 snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
357 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
360 int ret, nsockfd = 0;
361 const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
362 char ai_buf[ai_buf_size];
364 for (ai = ai_head; ai && nsockfd < MAXSOCKFD; ai = ai->ai_next) {
366 addrinfo_to_text(ai, ai_buf, ai_buf_size);
367 dbg("opening %s", ai_buf);
368 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
370 err("socket: %s: %d (%s)",
371 ai_buf, errno, strerror(errno));
375 usbip_net_set_reuseaddr(sock);
376 usbip_net_set_nodelay(sock);
378 if (sock >= FD_SETSIZE) {
379 err("FD_SETSIZE: %s: sock=%d, max=%d",
380 ai_buf, sock, FD_SETSIZE);
385 ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
387 err("bind: %s: %d (%s)",
388 ai_buf, errno, strerror(errno));
393 ret = listen(sock, SOMAXCONN);
395 err("listen: %s: %d (%s)",
396 ai_buf, errno, strerror(errno));
401 info("listening on %s", ai_buf);
402 sockfdlist[nsockfd++] = sock;
408 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
413 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
415 struct addrinfo hints, *ai_head;
418 memset(&hints, 0, sizeof(hints));
419 hints.ai_family = ai_family;
420 hints.ai_socktype = SOCK_STREAM;
421 hints.ai_flags = AI_PASSIVE;
423 rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head);
425 err("failed to get a network address %s: %s", usbip_port_string,
433 static void signal_handler(int i)
435 dbg("received '%s' signal", strsignal(i));
438 static void set_signal(void)
440 struct sigaction act;
442 memset(&act, 0, sizeof(act));
443 act.sa_handler = signal_handler;
444 sigemptyset(&act.sa_mask);
445 sigaction(SIGTERM, &act, NULL);
446 sigaction(SIGINT, &act, NULL);
447 act.sa_handler = SIG_IGN;
448 sigaction(SIGCLD, &act, NULL);
451 static const char *pid_file;
453 static void write_pid_file()
456 dbg("creating pid file %s", pid_file);
457 FILE *fp = fopen(pid_file, "w");
459 err("pid_file: %s: %d (%s)",
460 pid_file, errno, strerror(errno));
463 fprintf(fp, "%d\n", getpid());
468 static void remove_pid_file()
471 dbg("removing pid file %s", pid_file);
476 static int do_standalone_mode(int daemonize)
478 struct addrinfo *ai_head;
479 int sockfdlist[MAXSOCKFD];
483 struct timespec timeout;
486 if (usbip_host_driver_open()) {
487 err("please load " USBIP_CORE_MOD_NAME ".ko and "
488 USBIP_HOST_DRV_NAME ".ko!");
493 if (daemon(0, 0) < 0) {
494 err("daemonizing failed: %s", strerror(errno));
495 usbip_host_driver_close();
499 usbip_use_syslog = 1;
504 ai_head = do_getaddrinfo(NULL, PF_UNSPEC);
506 usbip_host_driver_close();
510 info("starting " PROGNAME " (%s)", usbip_version_string);
512 nsockfd = listen_all_addrinfo(ai_head, sockfdlist);
514 err("failed to open a listening socket");
515 freeaddrinfo(ai_head);
516 usbip_host_driver_close();
519 fds = calloc(nsockfd, sizeof(struct pollfd));
520 for (i = 0; i < nsockfd; i++) {
521 fds[i].fd = sockfdlist[i];
522 fds[i].events = POLLIN;
524 timeout.tv_sec = MAIN_LOOP_TIMEOUT;
527 sigfillset(&sigmask);
528 sigdelset(&sigmask, SIGTERM);
529 sigdelset(&sigmask, SIGINT);
535 r = ppoll(fds, nsockfd, &timeout, &sigmask);
537 dbg("%s", strerror(errno));
540 for (i = 0; i < nsockfd; i++) {
541 if (fds[i].revents & POLLIN) {
542 dbg("read event on fd[%d]=%d",
544 process_request(sockfdlist[i]);
548 dbg("heartbeat timeout on ppoll()");
552 info("shutting down " PROGNAME);
554 freeaddrinfo(ai_head);
555 usbip_host_driver_close();
560 int main(int argc, char *argv[])
562 static const struct option longopts[] = {
563 { "daemon", no_argument, NULL, 'D' },
564 { "debug", no_argument, NULL, 'd' },
565 { "pid", optional_argument, NULL, 'P' },
566 { "tcp-port", required_argument, NULL, 't' },
567 { "help", no_argument, NULL, 'h' },
568 { "version", no_argument, NULL, 'v' },
573 cmd_standalone_mode = 1,
582 usbip_use_stderr = 1;
583 usbip_use_syslog = 0;
586 err("not running as root?");
588 cmd = cmd_standalone_mode;
590 opt = getopt_long(argc, argv, "DdP::t:hv", longopts, NULL);
606 pid_file = optarg ? optarg : DEFAULT_PID_FILE;
609 usbip_setup_port_number(optarg);
622 case cmd_standalone_mode:
623 rc = do_standalone_mode(daemonize);
627 printf(PROGNAME " (%s)\n", usbip_version_string);
640 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);