staging: usbip: userspace: remove gcc warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / userspace / src / usbipd.c
blobec9faac5ff8f29b09c12c3a0adbfed9ac06b4878
1 /*
3 * Copyright (C) 2005-2007 Takahiro Hirofuchi
4 */
6 #ifdef HAVE_CONFIG_H
7 #include "../config.h"
8 #endif
10 #include <unistd.h>
11 #include <netdb.h>
12 #include <strings.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <arpa/inet.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
20 #ifdef HAVE_LIBWRAP
21 #include <tcpd.h>
22 #endif
24 #define _GNU_SOURCE
25 #include <getopt.h>
26 #include <signal.h>
28 #include "usbip.h"
29 #include "usbip_network.h"
31 #include <glib.h>
33 static const char version[] = PACKAGE_STRING;
36 static int send_reply_devlist(int sockfd)
38 int ret;
39 struct usbip_exported_device *edev;
40 struct op_devlist_reply reply;
43 reply.ndev = 0;
45 /* how many devices are exported ? */
46 dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
47 reply.ndev += 1;
50 dbg("%d devices are exported", reply.ndev);
52 ret = usbip_send_op_common(sockfd, OP_REP_DEVLIST, ST_OK);
53 if (ret < 0) {
54 err("send op_common");
55 return ret;
58 PACK_OP_DEVLIST_REPLY(1, &reply);
60 ret = usbip_send(sockfd, (void *) &reply, sizeof(reply));
61 if (ret < 0) {
62 err("send op_devlist_reply");
63 return ret;
66 dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
67 struct usb_device pdu_udev;
69 dump_usb_device(&edev->udev);
70 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
71 pack_usb_device(1, &pdu_udev);
73 ret = usbip_send(sockfd, (void *) &pdu_udev, sizeof(pdu_udev));
74 if (ret < 0) {
75 err("send pdu_udev");
76 return ret;
79 for (int i=0; i < edev->udev.bNumInterfaces; i++) {
80 struct usb_interface pdu_uinf;
82 dump_usb_interface(&edev->uinf[i]);
83 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
84 pack_usb_interface(1, &pdu_uinf);
86 ret = usbip_send(sockfd, (void *) &pdu_uinf, sizeof(pdu_uinf));
87 if (ret < 0) {
88 err("send pdu_uinf");
89 return ret;
94 return 0;
98 static int recv_request_devlist(int sockfd)
100 int ret;
101 struct op_devlist_request req;
103 bzero(&req, sizeof(req));
105 ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
106 if (ret < 0) {
107 err("recv devlist request");
108 return -1;
111 ret = send_reply_devlist(sockfd);
112 if (ret < 0) {
113 err("send devlist reply");
114 return -1;
117 return 0;
121 static int recv_request_import(int sockfd)
123 int ret;
124 struct op_import_request req;
125 struct op_common reply;
126 struct usbip_exported_device *edev;
127 int found = 0;
128 int error = 0;
130 bzero(&req, sizeof(req));
131 bzero(&reply, sizeof(reply));
133 ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
134 if (ret < 0) {
135 err("recv import request");
136 return -1;
139 PACK_OP_IMPORT_REQUEST(0, &req);
141 dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
142 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
143 dbg("found requested device %s", req.busid);
144 found = 1;
145 break;
149 if (found) {
150 /* should set TCP_NODELAY for usbip */
151 usbip_set_nodelay(sockfd);
153 /* export_device needs a TCP/IP socket descriptor */
154 ret = usbip_stub_export_device(edev, sockfd);
155 if (ret < 0)
156 error = 1;
157 } else {
158 info("not found requested device %s", req.busid);
159 error = 1;
163 ret = usbip_send_op_common(sockfd, OP_REP_IMPORT, (!error ? ST_OK : ST_NA));
164 if (ret < 0) {
165 err("send import reply");
166 return -1;
169 if (!error) {
170 struct usb_device pdu_udev;
172 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
173 pack_usb_device(1, &pdu_udev);
175 ret = usbip_send(sockfd, (void *) &pdu_udev, sizeof(pdu_udev));
176 if (ret < 0) {
177 err("send devinfo");
178 return -1;
182 return 0;
187 static int recv_pdu(int sockfd)
189 int ret;
190 uint16_t code = OP_UNSPEC;
193 ret = usbip_recv_op_common(sockfd, &code);
194 if (ret < 0) {
195 err("recv op_common, %d", ret);
196 return ret;
200 ret = usbip_stub_refresh_device_list();
201 if (ret < 0)
202 return -1;
204 switch(code) {
205 case OP_REQ_DEVLIST:
206 ret = recv_request_devlist(sockfd);
207 break;
209 case OP_REQ_IMPORT:
210 ret = recv_request_import(sockfd);
211 break;
213 case OP_REQ_DEVINFO:
214 case OP_REQ_CRYPKEY:
216 default:
217 err("unknown op_code, %d", code);
218 ret = -1;
222 return ret;
228 static void log_addrinfo(struct addrinfo *ai)
230 int ret;
231 char hbuf[NI_MAXHOST];
232 char sbuf[NI_MAXSERV];
234 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
235 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
236 if (ret)
237 err("getnameinfo, %s", gai_strerror(ret));
239 info("listen at [%s]:%s", hbuf, sbuf);
242 static struct addrinfo *my_getaddrinfo(char *host, int ai_family)
244 int ret;
245 struct addrinfo hints, *ai_head;
247 bzero(&hints, sizeof(hints));
249 hints.ai_family = ai_family;
250 hints.ai_socktype = SOCK_STREAM;
251 hints.ai_flags = AI_PASSIVE;
253 ret = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
254 if (ret) {
255 err("%s: %s", USBIP_PORT_STRING, gai_strerror(ret));
256 return NULL;
259 return ai_head;
262 #define MAXSOCK 20
263 static int listen_all_addrinfo(struct addrinfo *ai_head, int lsock[])
265 struct addrinfo *ai;
266 int n = 0; /* number of sockets */
268 for (ai = ai_head; ai && n < MAXSOCK; ai = ai->ai_next) {
269 int ret;
271 lsock[n] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
272 if (lsock[n] < 0)
273 continue;
275 usbip_set_reuseaddr(lsock[n]);
276 usbip_set_nodelay(lsock[n]);
278 if (lsock[n] >= FD_SETSIZE) {
279 close(lsock[n]);
280 lsock[n] = -1;
281 continue;
284 ret = bind(lsock[n], ai->ai_addr, ai->ai_addrlen);
285 if (ret < 0) {
286 close(lsock[n]);
287 lsock[n] = -1;
288 continue;
291 ret = listen(lsock[n], SOMAXCONN);
292 if (ret < 0) {
293 close(lsock[n]);
294 lsock[n] = -1;
295 continue;
298 log_addrinfo(ai);
300 /* next if succeed */
301 n++;
304 if (n == 0) {
305 err("no socket to listen to");
306 return -1;
309 dbg("listen %d address%s", n, (n==1)?"":"es");
311 return n;
314 #ifdef HAVE_LIBWRAP
315 static int tcpd_auth(int csock)
317 int ret;
318 struct request_info request;
320 request_init(&request, RQ_DAEMON, "usbipd", RQ_FILE, csock, 0);
322 fromhost(&request);
324 ret = hosts_access(&request);
325 if (!ret)
326 return -1;
328 return 0;
330 #endif
332 static int my_accept(int lsock)
334 int csock;
335 struct sockaddr_storage ss;
336 socklen_t len = sizeof(ss);
337 char host[NI_MAXHOST], port[NI_MAXSERV];
338 int ret;
340 bzero(&ss, sizeof(ss));
342 csock = accept(lsock, (struct sockaddr *) &ss, &len);
343 if (csock < 0) {
344 err("accept");
345 return -1;
348 ret = getnameinfo((struct sockaddr *) &ss, len,
349 host, sizeof(host), port, sizeof(port),
350 (NI_NUMERICHOST | NI_NUMERICSERV));
351 if (ret)
352 err("getnameinfo, %s", gai_strerror(ret));
354 #ifdef HAVE_LIBWRAP
355 ret = tcpd_auth(csock);
356 if (ret < 0) {
357 info("deny access from %s", host);
358 close(csock);
359 return -1;
361 #endif
363 info("connected from %s:%s", host, port);
365 return csock;
369 GMainLoop *main_loop;
371 static void signal_handler(int i)
373 dbg("signal catched, code %d", i);
375 if (main_loop)
376 g_main_loop_quit(main_loop);
379 static void set_signal(void)
381 struct sigaction act;
383 bzero(&act, sizeof(act));
384 act.sa_handler = signal_handler;
385 sigemptyset(&act.sa_mask);
386 sigaction(SIGTERM, &act, NULL);
387 sigaction(SIGINT, &act, NULL);
391 gboolean process_comming_request(GIOChannel *gio, GIOCondition condition,
392 gpointer data __attribute__((unused)))
394 int ret;
396 if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
397 g_error("unknown condition");
400 if (condition & G_IO_IN) {
401 int lsock;
402 int csock;
404 lsock = g_io_channel_unix_get_fd(gio);
406 csock = my_accept(lsock);
407 if (csock < 0)
408 return TRUE;
410 ret = recv_pdu(csock);
411 if (ret < 0)
412 err("process recieved pdu");
414 close(csock);
417 return TRUE;
421 static void do_standalone_mode(gboolean daemonize)
423 int ret;
424 int lsock[MAXSOCK];
425 struct addrinfo *ai_head;
426 int n;
430 ret = usbip_names_init(USBIDS_FILE);
431 if (ret)
432 err("open usb.ids");
434 ret = usbip_stub_driver_open();
435 if (ret < 0)
436 g_error("driver open failed");
438 if (daemonize) {
439 if (daemon(0,0) < 0)
440 g_error("daemonizing failed: %s", g_strerror(errno));
442 usbip_use_syslog = 1;
445 set_signal();
447 ai_head = my_getaddrinfo(NULL, PF_UNSPEC);
448 if (!ai_head)
449 return;
451 n = listen_all_addrinfo(ai_head, lsock);
452 if (n <= 0)
453 g_error("no socket to listen to");
455 for (int i = 0; i < n; i++) {
456 GIOChannel *gio;
458 gio = g_io_channel_unix_new(lsock[i]);
459 g_io_add_watch(gio, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
460 process_comming_request, NULL);
464 info("usbipd start (%s)", version);
467 main_loop = g_main_loop_new(FALSE, FALSE);
468 g_main_loop_run(main_loop);
470 info("shutdown");
472 freeaddrinfo(ai_head);
473 usbip_names_free();
474 usbip_stub_driver_close();
476 return;
480 static const char help_message[] = "\
481 Usage: usbipd [options] \n\
482 -D, --daemon \n\
483 Run as a daemon process. \n\
485 -d, --debug \n\
486 Print debugging information. \n\
488 -v, --version \n\
489 Show version. \n\
491 -h, --help \n\
492 Print this help. \n";
494 static void show_help(void)
496 printf("%s", help_message);
499 static const struct option longopts[] = {
500 {"daemon", no_argument, NULL, 'D'},
501 {"debug", no_argument, NULL, 'd'},
502 {"version", no_argument, NULL, 'v'},
503 {"help", no_argument, NULL, 'h'},
504 {NULL, 0, NULL, 0}
507 int main(int argc, char *argv[])
509 gboolean daemonize = FALSE;
511 enum {
512 cmd_standalone_mode = 1,
513 cmd_help,
514 cmd_version
515 } cmd = cmd_standalone_mode;
518 usbip_use_stderr = 1;
519 usbip_use_syslog = 0;
521 if (geteuid() != 0)
522 g_warning("running non-root?");
524 for (;;) {
525 int c;
526 int index = 0;
528 c = getopt_long(argc, argv, "vhdD", longopts, &index);
530 if (c == -1)
531 break;
533 switch (c) {
534 case 'd':
535 usbip_use_debug = 1;
536 continue;
537 case 'v':
538 cmd = cmd_version;
539 break;
540 case 'h':
541 cmd = cmd_help;
542 break;
543 case 'D':
544 daemonize = TRUE;
545 break;
546 case '?':
547 show_help();
548 exit(EXIT_FAILURE);
549 default:
550 err("getopt");
554 switch (cmd) {
555 case cmd_standalone_mode:
556 do_standalone_mode(daemonize);
557 break;
558 case cmd_version:
559 printf("%s\n", version);
560 break;
561 case cmd_help:
562 show_help();
563 break;
564 default:
565 info("unknown cmd");
566 show_help();
569 return 0;