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"
75 static void usbipd_help(void)
77 printf("%s\n", usbipd_help_string);
80 static int recv_request_import(int sockfd)
82 struct op_import_request req;
83 struct op_common reply;
84 struct usbip_exported_device *edev;
85 struct usbip_usb_device pdu_udev;
90 memset(&req, 0, sizeof(req));
91 memset(&reply, 0, sizeof(reply));
93 rc = usbip_net_recv(sockfd, &req, sizeof(req));
95 dbg("usbip_net_recv failed: import request");
98 PACK_OP_IMPORT_REQUEST(0, &req);
100 dlist_for_each_data(host_driver->edev_list, edev,
101 struct usbip_exported_device) {
102 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
103 info("found requested device: %s", req.busid);
110 /* should set TCP_NODELAY for usbip */
111 usbip_net_set_nodelay(sockfd);
113 /* export device needs a TCP/IP socket descriptor */
114 rc = usbip_host_export_device(edev, sockfd);
118 info("requested device not found: %s", req.busid);
122 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
123 (!error ? ST_OK : ST_NA));
125 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
130 dbg("import request busid %s: failed", req.busid);
134 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
135 usbip_net_pack_usb_device(1, &pdu_udev);
137 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
139 dbg("usbip_net_send failed: devinfo");
143 dbg("import request busid %s: complete", req.busid);
148 static int send_reply_devlist(int connfd)
150 struct usbip_exported_device *edev;
151 struct usbip_usb_device pdu_udev;
152 struct usbip_usb_interface pdu_uinf;
153 struct op_devlist_reply reply;
158 /* number of exported devices */
159 dlist_for_each_data(host_driver->edev_list, edev,
160 struct usbip_exported_device) {
163 info("exportable devices: %d", reply.ndev);
165 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
167 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
170 PACK_OP_DEVLIST_REPLY(1, &reply);
172 rc = usbip_net_send(connfd, &reply, sizeof(reply));
174 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
178 dlist_for_each_data(host_driver->edev_list, edev,
179 struct usbip_exported_device) {
180 dump_usb_device(&edev->udev);
181 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
182 usbip_net_pack_usb_device(1, &pdu_udev);
184 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
186 dbg("usbip_net_send failed: pdu_udev");
190 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
191 dump_usb_interface(&edev->uinf[i]);
192 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
193 usbip_net_pack_usb_interface(1, &pdu_uinf);
195 rc = usbip_net_send(connfd, &pdu_uinf,
198 dbg("usbip_net_send failed: pdu_uinf");
207 static int recv_request_devlist(int connfd)
209 struct op_devlist_request req;
212 memset(&req, 0, sizeof(req));
214 rc = usbip_net_recv(connfd, &req, sizeof(req));
216 dbg("usbip_net_recv failed: devlist request");
220 rc = send_reply_devlist(connfd);
222 dbg("send_reply_devlist failed");
229 static int recv_pdu(int connfd)
231 uint16_t code = OP_UNSPEC;
234 ret = usbip_net_recv_op_common(connfd, &code);
236 dbg("could not receive opcode: %#0x", code);
240 ret = usbip_host_refresh_device_list();
242 dbg("could not refresh device list: %d", ret);
246 info("received request: %#0x(%d)", code, connfd);
249 ret = recv_request_devlist(connfd);
252 ret = recv_request_import(connfd);
257 err("received an unknown opcode: %#0x", code);
262 info("request %#0x(%d): complete", code, connfd);
264 info("request %#0x(%d): failed", code, connfd);
270 static int tcpd_auth(int connfd)
272 struct request_info request;
275 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
277 rc = hosts_access(&request);
285 static int do_accept(int listenfd)
288 struct sockaddr_storage ss;
289 socklen_t len = sizeof(ss);
290 char host[NI_MAXHOST], port[NI_MAXSERV];
293 memset(&ss, 0, sizeof(ss));
295 connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
297 err("failed to accept connection");
301 rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
302 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
304 err("getnameinfo: %s", gai_strerror(rc));
307 rc = tcpd_auth(connfd);
309 info("denied access from %s", host);
314 info("connection from %s:%s", host, port);
319 int process_request(int listenfd)
324 connfd = do_accept(listenfd);
337 static void addrinfo_to_text(struct addrinfo *ai, char buf[],
338 const size_t buf_size)
340 char hbuf[NI_MAXHOST];
341 char sbuf[NI_MAXSERV];
346 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
347 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
349 err("getnameinfo: %s", gai_strerror(rc));
351 snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
354 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
357 int ret, nsockfd = 0;
358 const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
359 char ai_buf[ai_buf_size];
361 for (ai = ai_head; ai && nsockfd < MAXSOCKFD; ai = ai->ai_next) {
363 addrinfo_to_text(ai, ai_buf, ai_buf_size);
364 dbg("opening %s", ai_buf);
365 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
367 err("socket: %s: %d (%s)",
368 ai_buf, errno, strerror(errno));
372 usbip_net_set_reuseaddr(sock);
373 usbip_net_set_nodelay(sock);
375 if (sock >= FD_SETSIZE) {
376 err("FD_SETSIZE: %s: sock=%d, max=%d",
377 ai_buf, sock, FD_SETSIZE);
382 ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
384 err("bind: %s: %d (%s)",
385 ai_buf, errno, strerror(errno));
390 ret = listen(sock, SOMAXCONN);
392 err("listen: %s: %d (%s)",
393 ai_buf, errno, strerror(errno));
398 info("listening on %s", ai_buf);
399 sockfdlist[nsockfd++] = sock;
405 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
410 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
412 struct addrinfo hints, *ai_head;
415 memset(&hints, 0, sizeof(hints));
416 hints.ai_family = ai_family;
417 hints.ai_socktype = SOCK_STREAM;
418 hints.ai_flags = AI_PASSIVE;
420 rc = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
422 err("failed to get a network address %s: %s", USBIP_PORT_STRING,
430 static void signal_handler(int i)
432 dbg("received '%s' signal", strsignal(i));
435 static void set_signal(void)
437 struct sigaction act;
439 memset(&act, 0, sizeof(act));
440 act.sa_handler = signal_handler;
441 sigemptyset(&act.sa_mask);
442 sigaction(SIGTERM, &act, NULL);
443 sigaction(SIGINT, &act, NULL);
444 act.sa_handler = SIG_IGN;
445 sigaction(SIGCLD, &act, NULL);
448 static const char *pid_file;
450 static void write_pid_file()
453 dbg("creating pid file %s", pid_file);
454 FILE *fp = fopen(pid_file, "w");
456 err("pid_file: %s: %d (%s)",
457 pid_file, errno, strerror(errno));
460 fprintf(fp, "%d\n", getpid());
465 static void remove_pid_file()
468 dbg("removing pid file %s", pid_file);
473 static int do_standalone_mode(int daemonize)
475 struct addrinfo *ai_head;
476 int sockfdlist[MAXSOCKFD];
480 struct timespec timeout;
483 if (usbip_host_driver_open()) {
484 err("please load " USBIP_CORE_MOD_NAME ".ko and "
485 USBIP_HOST_DRV_NAME ".ko!");
490 if (daemon(0, 0) < 0) {
491 err("daemonizing failed: %s", strerror(errno));
492 usbip_host_driver_close();
496 usbip_use_syslog = 1;
501 ai_head = do_getaddrinfo(NULL, PF_UNSPEC);
503 usbip_host_driver_close();
507 info("starting " PROGNAME " (%s)", usbip_version_string);
509 nsockfd = listen_all_addrinfo(ai_head, sockfdlist);
511 err("failed to open a listening socket");
512 freeaddrinfo(ai_head);
513 usbip_host_driver_close();
516 fds = calloc(nsockfd, sizeof(struct pollfd));
517 for (i = 0; i < nsockfd; i++) {
518 fds[i].fd = sockfdlist[i];
519 fds[i].events = POLLIN;
521 timeout.tv_sec = MAIN_LOOP_TIMEOUT;
524 sigfillset(&sigmask);
525 sigdelset(&sigmask, SIGTERM);
526 sigdelset(&sigmask, SIGINT);
532 r = ppoll(fds, nsockfd, &timeout, &sigmask);
534 dbg("%s", strerror(errno));
537 for (i = 0; i < nsockfd; i++) {
538 if (fds[i].revents & POLLIN) {
539 dbg("read event on fd[%d]=%d",
541 process_request(sockfdlist[i]);
545 dbg("heartbeat timeout on ppoll()");
549 info("shutting down " PROGNAME);
551 freeaddrinfo(ai_head);
552 usbip_host_driver_close();
557 int main(int argc, char *argv[])
559 static const struct option longopts[] = {
560 { "daemon", no_argument, NULL, 'D' },
561 { "debug", no_argument, NULL, 'd' },
562 { "pid", optional_argument, NULL, 'P' },
563 { "help", no_argument, NULL, 'h' },
564 { "version", no_argument, NULL, 'v' },
569 cmd_standalone_mode = 1,
578 usbip_use_stderr = 1;
579 usbip_use_syslog = 0;
582 err("not running as root?");
584 cmd = cmd_standalone_mode;
586 opt = getopt_long(argc, argv, "DdP::hv", longopts, NULL);
602 pid_file = optarg ? optarg : DEFAULT_PID_FILE;
615 case cmd_standalone_mode:
616 rc = do_standalone_mode(daemonize);
620 printf(PROGNAME " (%s)\n", usbip_version_string);
633 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);