1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2588b48caSValentina Manea /*
3588b48caSValentina Manea * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
4588b48caSValentina Manea * 2005-2007 Takahiro Hirofuchi
53391ba0eSKrzysztof Opasiak * Copyright (C) 2015-2016 Samsung Electronics
63391ba0eSKrzysztof Opasiak * Igor Kotrasinski <i.kotrasinsk@samsung.com>
73391ba0eSKrzysztof Opasiak * Krzysztof Opasiak <k.opasiak@samsung.com>
8588b48caSValentina Manea */
9588b48caSValentina Manea
10588b48caSValentina Manea #ifdef HAVE_CONFIG_H
11588b48caSValentina Manea #include "../config.h"
12588b48caSValentina Manea #endif
13588b48caSValentina Manea
14588b48caSValentina Manea #define _GNU_SOURCE
15588b48caSValentina Manea #include <errno.h>
16588b48caSValentina Manea #include <unistd.h>
17588b48caSValentina Manea #include <netdb.h>
18588b48caSValentina Manea #include <string.h>
19588b48caSValentina Manea #include <stdlib.h>
20588b48caSValentina Manea #include <sys/types.h>
21588b48caSValentina Manea #include <sys/stat.h>
22588b48caSValentina Manea #include <arpa/inet.h>
23588b48caSValentina Manea #include <sys/socket.h>
24588b48caSValentina Manea #include <netinet/in.h>
25588b48caSValentina Manea
26588b48caSValentina Manea #ifdef HAVE_LIBWRAP
27588b48caSValentina Manea #include <tcpd.h>
28588b48caSValentina Manea #endif
29588b48caSValentina Manea
30588b48caSValentina Manea #include <getopt.h>
31588b48caSValentina Manea #include <signal.h>
32588b48caSValentina Manea #include <poll.h>
33588b48caSValentina Manea
34588b48caSValentina Manea #include "usbip_host_driver.h"
353391ba0eSKrzysztof Opasiak #include "usbip_host_common.h"
36e0546fd8SIgor Kotrasinski #include "usbip_device_driver.h"
37588b48caSValentina Manea #include "usbip_common.h"
38588b48caSValentina Manea #include "usbip_network.h"
39588b48caSValentina Manea #include "list.h"
40588b48caSValentina Manea
41588b48caSValentina Manea #undef PROGNAME
42588b48caSValentina Manea #define PROGNAME "usbipd"
43588b48caSValentina Manea #define MAXSOCKFD 20
44588b48caSValentina Manea
45588b48caSValentina Manea #define MAIN_LOOP_TIMEOUT 10
46588b48caSValentina Manea
47588b48caSValentina Manea #define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
48588b48caSValentina Manea
49588b48caSValentina Manea static const char usbip_version_string[] = PACKAGE_STRING;
50588b48caSValentina Manea
51588b48caSValentina Manea static const char usbipd_help_string[] =
52588b48caSValentina Manea "usage: usbipd [options]\n"
53588b48caSValentina Manea "\n"
54588b48caSValentina Manea " -4, --ipv4\n"
55588b48caSValentina Manea " Bind to IPv4. Default is both.\n"
56588b48caSValentina Manea "\n"
57588b48caSValentina Manea " -6, --ipv6\n"
58588b48caSValentina Manea " Bind to IPv6. Default is both.\n"
59588b48caSValentina Manea "\n"
60e0546fd8SIgor Kotrasinski " -e, --device\n"
61e0546fd8SIgor Kotrasinski " Run in device mode.\n"
62e0546fd8SIgor Kotrasinski " Rather than drive an attached device, create\n"
63e0546fd8SIgor Kotrasinski " a virtual UDC to bind gadgets to.\n"
64e0546fd8SIgor Kotrasinski "\n"
65588b48caSValentina Manea " -D, --daemon\n"
66588b48caSValentina Manea " Run as a daemon process.\n"
67588b48caSValentina Manea "\n"
68588b48caSValentina Manea " -d, --debug\n"
69588b48caSValentina Manea " Print debugging information.\n"
70588b48caSValentina Manea "\n"
71588b48caSValentina Manea " -PFILE, --pid FILE\n"
72588b48caSValentina Manea " Write process id to FILE.\n"
73588b48caSValentina Manea " If no FILE specified, use " DEFAULT_PID_FILE "\n"
74588b48caSValentina Manea "\n"
75588b48caSValentina Manea " -tPORT, --tcp-port PORT\n"
76588b48caSValentina Manea " Listen on TCP/IP port PORT.\n"
77588b48caSValentina Manea "\n"
78588b48caSValentina Manea " -h, --help\n"
79588b48caSValentina Manea " Print this help.\n"
80588b48caSValentina Manea "\n"
81588b48caSValentina Manea " -v, --version\n"
82588b48caSValentina Manea " Show version.\n";
83588b48caSValentina Manea
843391ba0eSKrzysztof Opasiak static struct usbip_host_driver *driver;
853391ba0eSKrzysztof Opasiak
usbipd_help(void)86588b48caSValentina Manea static void usbipd_help(void)
87588b48caSValentina Manea {
88588b48caSValentina Manea printf("%s\n", usbipd_help_string);
89588b48caSValentina Manea }
90588b48caSValentina Manea
recv_request_import(int sockfd)91588b48caSValentina Manea static int recv_request_import(int sockfd)
92588b48caSValentina Manea {
93588b48caSValentina Manea struct op_import_request req;
94588b48caSValentina Manea struct usbip_exported_device *edev;
95588b48caSValentina Manea struct usbip_usb_device pdu_udev;
96588b48caSValentina Manea struct list_head *i;
97588b48caSValentina Manea int found = 0;
98ad81b15dSShuah Khan int status = ST_OK;
99588b48caSValentina Manea int rc;
100588b48caSValentina Manea
101588b48caSValentina Manea memset(&req, 0, sizeof(req));
102588b48caSValentina Manea
103588b48caSValentina Manea rc = usbip_net_recv(sockfd, &req, sizeof(req));
104588b48caSValentina Manea if (rc < 0) {
105588b48caSValentina Manea dbg("usbip_net_recv failed: import request");
106588b48caSValentina Manea return -1;
107588b48caSValentina Manea }
108588b48caSValentina Manea PACK_OP_IMPORT_REQUEST(0, &req);
109588b48caSValentina Manea
1103391ba0eSKrzysztof Opasiak list_for_each(i, &driver->edev_list) {
111588b48caSValentina Manea edev = list_entry(i, struct usbip_exported_device, node);
112588b48caSValentina Manea if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
113588b48caSValentina Manea info("found requested device: %s", req.busid);
114588b48caSValentina Manea found = 1;
115588b48caSValentina Manea break;
116588b48caSValentina Manea }
117588b48caSValentina Manea }
118588b48caSValentina Manea
119588b48caSValentina Manea if (found) {
120588b48caSValentina Manea /* should set TCP_NODELAY for usbip */
121588b48caSValentina Manea usbip_net_set_nodelay(sockfd);
122588b48caSValentina Manea
123588b48caSValentina Manea /* export device needs a TCP/IP socket descriptor */
124ad81b15dSShuah Khan status = usbip_export_device(edev, sockfd);
125ad81b15dSShuah Khan if (status < 0)
126ad81b15dSShuah Khan status = ST_NA;
127588b48caSValentina Manea } else {
128588b48caSValentina Manea info("requested device not found: %s", req.busid);
129ad81b15dSShuah Khan status = ST_NODEV;
130588b48caSValentina Manea }
131588b48caSValentina Manea
132ad81b15dSShuah Khan rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, status);
133588b48caSValentina Manea if (rc < 0) {
134588b48caSValentina Manea dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
135588b48caSValentina Manea return -1;
136588b48caSValentina Manea }
137588b48caSValentina Manea
138ad81b15dSShuah Khan if (status) {
139588b48caSValentina Manea dbg("import request busid %s: failed", req.busid);
140588b48caSValentina Manea return -1;
141588b48caSValentina Manea }
142588b48caSValentina Manea
143588b48caSValentina Manea memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
144588b48caSValentina Manea usbip_net_pack_usb_device(1, &pdu_udev);
145588b48caSValentina Manea
146588b48caSValentina Manea rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
147588b48caSValentina Manea if (rc < 0) {
148588b48caSValentina Manea dbg("usbip_net_send failed: devinfo");
149588b48caSValentina Manea return -1;
150588b48caSValentina Manea }
151588b48caSValentina Manea
152588b48caSValentina Manea dbg("import request busid %s: complete", req.busid);
153588b48caSValentina Manea
154588b48caSValentina Manea return 0;
155588b48caSValentina Manea }
156588b48caSValentina Manea
send_reply_devlist(int connfd)157588b48caSValentina Manea static int send_reply_devlist(int connfd)
158588b48caSValentina Manea {
159588b48caSValentina Manea struct usbip_exported_device *edev;
160588b48caSValentina Manea struct usbip_usb_device pdu_udev;
161588b48caSValentina Manea struct usbip_usb_interface pdu_uinf;
162588b48caSValentina Manea struct op_devlist_reply reply;
163588b48caSValentina Manea struct list_head *j;
164588b48caSValentina Manea int rc, i;
165588b48caSValentina Manea
1662699126bSShuah Khan /*
1672699126bSShuah Khan * Exclude devices that are already exported to a client from
1682699126bSShuah Khan * the exportable device list to avoid:
1692699126bSShuah Khan * - import requests for devices that are exported only to
1702699126bSShuah Khan * fail the request.
1712699126bSShuah Khan * - revealing devices that are imported by a client to
1722699126bSShuah Khan * another client.
1732699126bSShuah Khan */
1742699126bSShuah Khan
175588b48caSValentina Manea reply.ndev = 0;
176588b48caSValentina Manea /* number of exported devices */
1773391ba0eSKrzysztof Opasiak list_for_each(j, &driver->edev_list) {
1782699126bSShuah Khan edev = list_entry(j, struct usbip_exported_device, node);
1792699126bSShuah Khan if (edev->status != SDEV_ST_USED)
180588b48caSValentina Manea reply.ndev += 1;
181588b48caSValentina Manea }
182588b48caSValentina Manea info("exportable devices: %d", reply.ndev);
183588b48caSValentina Manea
184588b48caSValentina Manea rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
185588b48caSValentina Manea if (rc < 0) {
186588b48caSValentina Manea dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
187588b48caSValentina Manea return -1;
188588b48caSValentina Manea }
189588b48caSValentina Manea PACK_OP_DEVLIST_REPLY(1, &reply);
190588b48caSValentina Manea
191588b48caSValentina Manea rc = usbip_net_send(connfd, &reply, sizeof(reply));
192588b48caSValentina Manea if (rc < 0) {
193588b48caSValentina Manea dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
194588b48caSValentina Manea return -1;
195588b48caSValentina Manea }
196588b48caSValentina Manea
1973391ba0eSKrzysztof Opasiak list_for_each(j, &driver->edev_list) {
198588b48caSValentina Manea edev = list_entry(j, struct usbip_exported_device, node);
1992699126bSShuah Khan if (edev->status == SDEV_ST_USED)
2002699126bSShuah Khan continue;
2012699126bSShuah Khan
202588b48caSValentina Manea dump_usb_device(&edev->udev);
203588b48caSValentina Manea memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
204588b48caSValentina Manea usbip_net_pack_usb_device(1, &pdu_udev);
205588b48caSValentina Manea
206588b48caSValentina Manea rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
207588b48caSValentina Manea if (rc < 0) {
208588b48caSValentina Manea dbg("usbip_net_send failed: pdu_udev");
209588b48caSValentina Manea return -1;
210588b48caSValentina Manea }
211588b48caSValentina Manea
212588b48caSValentina Manea for (i = 0; i < edev->udev.bNumInterfaces; i++) {
213588b48caSValentina Manea dump_usb_interface(&edev->uinf[i]);
214588b48caSValentina Manea memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
215588b48caSValentina Manea usbip_net_pack_usb_interface(1, &pdu_uinf);
216588b48caSValentina Manea
217588b48caSValentina Manea rc = usbip_net_send(connfd, &pdu_uinf,
218588b48caSValentina Manea sizeof(pdu_uinf));
219588b48caSValentina Manea if (rc < 0) {
220588b48caSValentina Manea err("usbip_net_send failed: pdu_uinf");
221588b48caSValentina Manea return -1;
222588b48caSValentina Manea }
223588b48caSValentina Manea }
224588b48caSValentina Manea }
225588b48caSValentina Manea
226588b48caSValentina Manea return 0;
227588b48caSValentina Manea }
228588b48caSValentina Manea
recv_request_devlist(int connfd)229588b48caSValentina Manea static int recv_request_devlist(int connfd)
230588b48caSValentina Manea {
231588b48caSValentina Manea struct op_devlist_request req;
232588b48caSValentina Manea int rc;
233588b48caSValentina Manea
234588b48caSValentina Manea memset(&req, 0, sizeof(req));
235588b48caSValentina Manea
236588b48caSValentina Manea rc = usbip_net_recv(connfd, &req, sizeof(req));
237588b48caSValentina Manea if (rc < 0) {
238588b48caSValentina Manea dbg("usbip_net_recv failed: devlist request");
239588b48caSValentina Manea return -1;
240588b48caSValentina Manea }
241588b48caSValentina Manea
242588b48caSValentina Manea rc = send_reply_devlist(connfd);
243588b48caSValentina Manea if (rc < 0) {
244588b48caSValentina Manea dbg("send_reply_devlist failed");
245588b48caSValentina Manea return -1;
246588b48caSValentina Manea }
247588b48caSValentina Manea
248588b48caSValentina Manea return 0;
249588b48caSValentina Manea }
250588b48caSValentina Manea
recv_pdu(int connfd)251588b48caSValentina Manea static int recv_pdu(int connfd)
252588b48caSValentina Manea {
253588b48caSValentina Manea uint16_t code = OP_UNSPEC;
254588b48caSValentina Manea int ret;
255ad81b15dSShuah Khan int status;
256588b48caSValentina Manea
257ad81b15dSShuah Khan ret = usbip_net_recv_op_common(connfd, &code, &status);
258588b48caSValentina Manea if (ret < 0) {
259588b48caSValentina Manea dbg("could not receive opcode: %#0x", code);
260588b48caSValentina Manea return -1;
261588b48caSValentina Manea }
262588b48caSValentina Manea
2633391ba0eSKrzysztof Opasiak ret = usbip_refresh_device_list(driver);
264588b48caSValentina Manea if (ret < 0) {
265588b48caSValentina Manea dbg("could not refresh device list: %d", ret);
266588b48caSValentina Manea return -1;
267588b48caSValentina Manea }
268588b48caSValentina Manea
269588b48caSValentina Manea info("received request: %#0x(%d)", code, connfd);
270588b48caSValentina Manea switch (code) {
271588b48caSValentina Manea case OP_REQ_DEVLIST:
272588b48caSValentina Manea ret = recv_request_devlist(connfd);
273588b48caSValentina Manea break;
274588b48caSValentina Manea case OP_REQ_IMPORT:
275588b48caSValentina Manea ret = recv_request_import(connfd);
276588b48caSValentina Manea break;
277588b48caSValentina Manea case OP_REQ_DEVINFO:
278588b48caSValentina Manea case OP_REQ_CRYPKEY:
279588b48caSValentina Manea default:
280588b48caSValentina Manea err("received an unknown opcode: %#0x", code);
281588b48caSValentina Manea ret = -1;
282588b48caSValentina Manea }
283588b48caSValentina Manea
284588b48caSValentina Manea if (ret == 0)
285588b48caSValentina Manea info("request %#0x(%d): complete", code, connfd);
286588b48caSValentina Manea else
287588b48caSValentina Manea info("request %#0x(%d): failed", code, connfd);
288588b48caSValentina Manea
289588b48caSValentina Manea return ret;
290588b48caSValentina Manea }
291588b48caSValentina Manea
292588b48caSValentina Manea #ifdef HAVE_LIBWRAP
tcpd_auth(int connfd)293588b48caSValentina Manea static int tcpd_auth(int connfd)
294588b48caSValentina Manea {
295588b48caSValentina Manea struct request_info request;
296588b48caSValentina Manea int rc;
297588b48caSValentina Manea
298588b48caSValentina Manea request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
299588b48caSValentina Manea fromhost(&request);
300588b48caSValentina Manea rc = hosts_access(&request);
301588b48caSValentina Manea if (rc == 0)
302588b48caSValentina Manea return -1;
303588b48caSValentina Manea
304588b48caSValentina Manea return 0;
305588b48caSValentina Manea }
306588b48caSValentina Manea #endif
307588b48caSValentina Manea
do_accept(int listenfd)308588b48caSValentina Manea static int do_accept(int listenfd)
309588b48caSValentina Manea {
310588b48caSValentina Manea int connfd;
311588b48caSValentina Manea struct sockaddr_storage ss;
312588b48caSValentina Manea socklen_t len = sizeof(ss);
313588b48caSValentina Manea char host[NI_MAXHOST], port[NI_MAXSERV];
314588b48caSValentina Manea int rc;
315588b48caSValentina Manea
316588b48caSValentina Manea memset(&ss, 0, sizeof(ss));
317588b48caSValentina Manea
318588b48caSValentina Manea connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
319588b48caSValentina Manea if (connfd < 0) {
320588b48caSValentina Manea err("failed to accept connection");
321588b48caSValentina Manea return -1;
322588b48caSValentina Manea }
323588b48caSValentina Manea
324588b48caSValentina Manea rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
325588b48caSValentina Manea port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
326588b48caSValentina Manea if (rc)
327588b48caSValentina Manea err("getnameinfo: %s", gai_strerror(rc));
328588b48caSValentina Manea
329588b48caSValentina Manea #ifdef HAVE_LIBWRAP
330588b48caSValentina Manea rc = tcpd_auth(connfd);
331588b48caSValentina Manea if (rc < 0) {
332588b48caSValentina Manea info("denied access from %s", host);
333588b48caSValentina Manea close(connfd);
334588b48caSValentina Manea return -1;
335588b48caSValentina Manea }
336588b48caSValentina Manea #endif
337588b48caSValentina Manea info("connection from %s:%s", host, port);
338588b48caSValentina Manea
339588b48caSValentina Manea return connfd;
340588b48caSValentina Manea }
341588b48caSValentina Manea
process_request(int listenfd)342588b48caSValentina Manea int process_request(int listenfd)
343588b48caSValentina Manea {
344588b48caSValentina Manea pid_t childpid;
345588b48caSValentina Manea int connfd;
346588b48caSValentina Manea
347588b48caSValentina Manea connfd = do_accept(listenfd);
348588b48caSValentina Manea if (connfd < 0)
349588b48caSValentina Manea return -1;
350588b48caSValentina Manea childpid = fork();
351588b48caSValentina Manea if (childpid == 0) {
352588b48caSValentina Manea close(listenfd);
353588b48caSValentina Manea recv_pdu(connfd);
354588b48caSValentina Manea exit(0);
355588b48caSValentina Manea }
356588b48caSValentina Manea close(connfd);
357588b48caSValentina Manea return 0;
358588b48caSValentina Manea }
359588b48caSValentina Manea
addrinfo_to_text(struct addrinfo * ai,char buf[],const size_t buf_size)360588b48caSValentina Manea static void addrinfo_to_text(struct addrinfo *ai, char buf[],
361588b48caSValentina Manea const size_t buf_size)
362588b48caSValentina Manea {
363588b48caSValentina Manea char hbuf[NI_MAXHOST];
364588b48caSValentina Manea char sbuf[NI_MAXSERV];
365588b48caSValentina Manea int rc;
366588b48caSValentina Manea
367588b48caSValentina Manea buf[0] = '\0';
368588b48caSValentina Manea
369588b48caSValentina Manea rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
370588b48caSValentina Manea sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
371588b48caSValentina Manea if (rc)
372588b48caSValentina Manea err("getnameinfo: %s", gai_strerror(rc));
373588b48caSValentina Manea
374588b48caSValentina Manea snprintf(buf, buf_size, "%s:%s", hbuf, sbuf);
375588b48caSValentina Manea }
376588b48caSValentina Manea
listen_all_addrinfo(struct addrinfo * ai_head,int sockfdlist[],int maxsockfd)377588b48caSValentina Manea static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[],
378588b48caSValentina Manea int maxsockfd)
379588b48caSValentina Manea {
380588b48caSValentina Manea struct addrinfo *ai;
381588b48caSValentina Manea int ret, nsockfd = 0;
382588b48caSValentina Manea const size_t ai_buf_size = NI_MAXHOST + NI_MAXSERV + 2;
383588b48caSValentina Manea char ai_buf[ai_buf_size];
384588b48caSValentina Manea
385588b48caSValentina Manea for (ai = ai_head; ai && nsockfd < maxsockfd; ai = ai->ai_next) {
386588b48caSValentina Manea int sock;
387588b48caSValentina Manea
388588b48caSValentina Manea addrinfo_to_text(ai, ai_buf, ai_buf_size);
389588b48caSValentina Manea dbg("opening %s", ai_buf);
390588b48caSValentina Manea sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
391588b48caSValentina Manea if (sock < 0) {
392588b48caSValentina Manea err("socket: %s: %d (%s)",
393588b48caSValentina Manea ai_buf, errno, strerror(errno));
394588b48caSValentina Manea continue;
395588b48caSValentina Manea }
396588b48caSValentina Manea
397588b48caSValentina Manea usbip_net_set_reuseaddr(sock);
398588b48caSValentina Manea usbip_net_set_nodelay(sock);
399588b48caSValentina Manea /* We use seperate sockets for IPv4 and IPv6
400588b48caSValentina Manea * (see do_standalone_mode()) */
401588b48caSValentina Manea usbip_net_set_v6only(sock);
402588b48caSValentina Manea
403588b48caSValentina Manea ret = bind(sock, ai->ai_addr, ai->ai_addrlen);
404588b48caSValentina Manea if (ret < 0) {
405588b48caSValentina Manea err("bind: %s: %d (%s)",
406588b48caSValentina Manea ai_buf, errno, strerror(errno));
407588b48caSValentina Manea close(sock);
408588b48caSValentina Manea continue;
409588b48caSValentina Manea }
410588b48caSValentina Manea
411588b48caSValentina Manea ret = listen(sock, SOMAXCONN);
412588b48caSValentina Manea if (ret < 0) {
413588b48caSValentina Manea err("listen: %s: %d (%s)",
414588b48caSValentina Manea ai_buf, errno, strerror(errno));
415588b48caSValentina Manea close(sock);
416588b48caSValentina Manea continue;
417588b48caSValentina Manea }
418588b48caSValentina Manea
419588b48caSValentina Manea info("listening on %s", ai_buf);
420588b48caSValentina Manea sockfdlist[nsockfd++] = sock;
421588b48caSValentina Manea }
422588b48caSValentina Manea
423588b48caSValentina Manea return nsockfd;
424588b48caSValentina Manea }
425588b48caSValentina Manea
do_getaddrinfo(char * host,int ai_family)426588b48caSValentina Manea static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
427588b48caSValentina Manea {
428588b48caSValentina Manea struct addrinfo hints, *ai_head;
429588b48caSValentina Manea int rc;
430588b48caSValentina Manea
431588b48caSValentina Manea memset(&hints, 0, sizeof(hints));
432588b48caSValentina Manea hints.ai_family = ai_family;
433588b48caSValentina Manea hints.ai_socktype = SOCK_STREAM;
434588b48caSValentina Manea hints.ai_flags = AI_PASSIVE;
435588b48caSValentina Manea
436588b48caSValentina Manea rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head);
437588b48caSValentina Manea if (rc) {
438588b48caSValentina Manea err("failed to get a network address %s: %s", usbip_port_string,
439588b48caSValentina Manea gai_strerror(rc));
440588b48caSValentina Manea return NULL;
441588b48caSValentina Manea }
442588b48caSValentina Manea
443588b48caSValentina Manea return ai_head;
444588b48caSValentina Manea }
445588b48caSValentina Manea
signal_handler(int i)446588b48caSValentina Manea static void signal_handler(int i)
447588b48caSValentina Manea {
448588b48caSValentina Manea dbg("received '%s' signal", strsignal(i));
449588b48caSValentina Manea }
450588b48caSValentina Manea
set_signal(void)451588b48caSValentina Manea static void set_signal(void)
452588b48caSValentina Manea {
453588b48caSValentina Manea struct sigaction act;
454588b48caSValentina Manea
455588b48caSValentina Manea memset(&act, 0, sizeof(act));
456588b48caSValentina Manea act.sa_handler = signal_handler;
457588b48caSValentina Manea sigemptyset(&act.sa_mask);
458588b48caSValentina Manea sigaction(SIGTERM, &act, NULL);
459588b48caSValentina Manea sigaction(SIGINT, &act, NULL);
460588b48caSValentina Manea act.sa_handler = SIG_IGN;
46177be4c87SJulien BOIBESSOT sigaction(SIGCHLD, &act, NULL);
462588b48caSValentina Manea }
463588b48caSValentina Manea
464588b48caSValentina Manea static const char *pid_file;
465588b48caSValentina Manea
write_pid_file(void)466588b48caSValentina Manea static void write_pid_file(void)
467588b48caSValentina Manea {
468588b48caSValentina Manea if (pid_file) {
469588b48caSValentina Manea dbg("creating pid file %s", pid_file);
470588b48caSValentina Manea FILE *fp;
471588b48caSValentina Manea
472588b48caSValentina Manea fp = fopen(pid_file, "w");
473588b48caSValentina Manea if (!fp) {
474588b48caSValentina Manea err("pid_file: %s: %d (%s)",
475588b48caSValentina Manea pid_file, errno, strerror(errno));
476588b48caSValentina Manea return;
477588b48caSValentina Manea }
478588b48caSValentina Manea fprintf(fp, "%d\n", getpid());
479588b48caSValentina Manea fclose(fp);
480588b48caSValentina Manea }
481588b48caSValentina Manea }
482588b48caSValentina Manea
remove_pid_file(void)483588b48caSValentina Manea static void remove_pid_file(void)
484588b48caSValentina Manea {
485588b48caSValentina Manea if (pid_file) {
486588b48caSValentina Manea dbg("removing pid file %s", pid_file);
487588b48caSValentina Manea unlink(pid_file);
488588b48caSValentina Manea }
489588b48caSValentina Manea }
490588b48caSValentina Manea
do_standalone_mode(int daemonize,int ipv4,int ipv6)491588b48caSValentina Manea static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
492588b48caSValentina Manea {
493588b48caSValentina Manea struct addrinfo *ai_head;
494588b48caSValentina Manea int sockfdlist[MAXSOCKFD];
495588b48caSValentina Manea int nsockfd, family;
496588b48caSValentina Manea int i, terminate;
497588b48caSValentina Manea struct pollfd *fds;
498588b48caSValentina Manea struct timespec timeout;
499588b48caSValentina Manea sigset_t sigmask;
500588b48caSValentina Manea
5013391ba0eSKrzysztof Opasiak if (usbip_driver_open(driver))
502588b48caSValentina Manea return -1;
503588b48caSValentina Manea
504588b48caSValentina Manea if (daemonize) {
505588b48caSValentina Manea if (daemon(0, 0) < 0) {
506588b48caSValentina Manea err("daemonizing failed: %s", strerror(errno));
5073391ba0eSKrzysztof Opasiak usbip_driver_close(driver);
508588b48caSValentina Manea return -1;
509588b48caSValentina Manea }
510588b48caSValentina Manea umask(0);
511588b48caSValentina Manea usbip_use_syslog = 1;
512588b48caSValentina Manea }
513588b48caSValentina Manea set_signal();
514588b48caSValentina Manea write_pid_file();
515588b48caSValentina Manea
516588b48caSValentina Manea info("starting " PROGNAME " (%s)", usbip_version_string);
517588b48caSValentina Manea
518588b48caSValentina Manea /*
519588b48caSValentina Manea * To suppress warnings on systems with bindv6only disabled
520588b48caSValentina Manea * (default), we use seperate sockets for IPv6 and IPv4 and set
521588b48caSValentina Manea * IPV6_V6ONLY on the IPv6 sockets.
522588b48caSValentina Manea */
523588b48caSValentina Manea if (ipv4 && ipv6)
524588b48caSValentina Manea family = AF_UNSPEC;
525588b48caSValentina Manea else if (ipv4)
526588b48caSValentina Manea family = AF_INET;
527588b48caSValentina Manea else
528588b48caSValentina Manea family = AF_INET6;
529588b48caSValentina Manea
530588b48caSValentina Manea ai_head = do_getaddrinfo(NULL, family);
531588b48caSValentina Manea if (!ai_head) {
5323391ba0eSKrzysztof Opasiak usbip_driver_close(driver);
533588b48caSValentina Manea return -1;
534588b48caSValentina Manea }
535588b48caSValentina Manea nsockfd = listen_all_addrinfo(ai_head, sockfdlist,
536588b48caSValentina Manea sizeof(sockfdlist) / sizeof(*sockfdlist));
537588b48caSValentina Manea freeaddrinfo(ai_head);
538588b48caSValentina Manea if (nsockfd <= 0) {
539588b48caSValentina Manea err("failed to open a listening socket");
5403391ba0eSKrzysztof Opasiak usbip_driver_close(driver);
541588b48caSValentina Manea return -1;
542588b48caSValentina Manea }
543588b48caSValentina Manea
544588b48caSValentina Manea dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
545588b48caSValentina Manea
546588b48caSValentina Manea fds = calloc(nsockfd, sizeof(struct pollfd));
547588b48caSValentina Manea for (i = 0; i < nsockfd; i++) {
548588b48caSValentina Manea fds[i].fd = sockfdlist[i];
549588b48caSValentina Manea fds[i].events = POLLIN;
550588b48caSValentina Manea }
551588b48caSValentina Manea timeout.tv_sec = MAIN_LOOP_TIMEOUT;
552588b48caSValentina Manea timeout.tv_nsec = 0;
553588b48caSValentina Manea
554588b48caSValentina Manea sigfillset(&sigmask);
555588b48caSValentina Manea sigdelset(&sigmask, SIGTERM);
556588b48caSValentina Manea sigdelset(&sigmask, SIGINT);
557588b48caSValentina Manea
558588b48caSValentina Manea terminate = 0;
559588b48caSValentina Manea while (!terminate) {
560588b48caSValentina Manea int r;
561588b48caSValentina Manea
562588b48caSValentina Manea r = ppoll(fds, nsockfd, &timeout, &sigmask);
563588b48caSValentina Manea if (r < 0) {
564588b48caSValentina Manea dbg("%s", strerror(errno));
565588b48caSValentina Manea terminate = 1;
566588b48caSValentina Manea } else if (r) {
567588b48caSValentina Manea for (i = 0; i < nsockfd; i++) {
568588b48caSValentina Manea if (fds[i].revents & POLLIN) {
569588b48caSValentina Manea dbg("read event on fd[%d]=%d",
570588b48caSValentina Manea i, sockfdlist[i]);
571588b48caSValentina Manea process_request(sockfdlist[i]);
572588b48caSValentina Manea }
573588b48caSValentina Manea }
574588b48caSValentina Manea } else {
575588b48caSValentina Manea dbg("heartbeat timeout on ppoll()");
576588b48caSValentina Manea }
577588b48caSValentina Manea }
578588b48caSValentina Manea
579588b48caSValentina Manea info("shutting down " PROGNAME);
580588b48caSValentina Manea free(fds);
5813391ba0eSKrzysztof Opasiak usbip_driver_close(driver);
582588b48caSValentina Manea
583588b48caSValentina Manea return 0;
584588b48caSValentina Manea }
585588b48caSValentina Manea
main(int argc,char * argv[])586588b48caSValentina Manea int main(int argc, char *argv[])
587588b48caSValentina Manea {
588588b48caSValentina Manea static const struct option longopts[] = {
589588b48caSValentina Manea { "ipv4", no_argument, NULL, '4' },
590588b48caSValentina Manea { "ipv6", no_argument, NULL, '6' },
591588b48caSValentina Manea { "daemon", no_argument, NULL, 'D' },
592588b48caSValentina Manea { "daemon", no_argument, NULL, 'D' },
593588b48caSValentina Manea { "debug", no_argument, NULL, 'd' },
594e0546fd8SIgor Kotrasinski { "device", no_argument, NULL, 'e' },
595588b48caSValentina Manea { "pid", optional_argument, NULL, 'P' },
596588b48caSValentina Manea { "tcp-port", required_argument, NULL, 't' },
597588b48caSValentina Manea { "help", no_argument, NULL, 'h' },
598588b48caSValentina Manea { "version", no_argument, NULL, 'v' },
599588b48caSValentina Manea { NULL, 0, NULL, 0 }
600588b48caSValentina Manea };
601588b48caSValentina Manea
602588b48caSValentina Manea enum {
603588b48caSValentina Manea cmd_standalone_mode = 1,
604588b48caSValentina Manea cmd_help,
605588b48caSValentina Manea cmd_version
606588b48caSValentina Manea } cmd;
607588b48caSValentina Manea
608588b48caSValentina Manea int daemonize = 0;
609588b48caSValentina Manea int ipv4 = 0, ipv6 = 0;
610588b48caSValentina Manea int opt, rc = -1;
611588b48caSValentina Manea
612588b48caSValentina Manea pid_file = NULL;
613588b48caSValentina Manea
614588b48caSValentina Manea usbip_use_stderr = 1;
615588b48caSValentina Manea usbip_use_syslog = 0;
616588b48caSValentina Manea
617588b48caSValentina Manea if (geteuid() != 0)
618588b48caSValentina Manea err("not running as root?");
619588b48caSValentina Manea
620588b48caSValentina Manea cmd = cmd_standalone_mode;
6213391ba0eSKrzysztof Opasiak driver = &host_driver;
622588b48caSValentina Manea for (;;) {
623e0546fd8SIgor Kotrasinski opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL);
624588b48caSValentina Manea
625588b48caSValentina Manea if (opt == -1)
626588b48caSValentina Manea break;
627588b48caSValentina Manea
628588b48caSValentina Manea switch (opt) {
629588b48caSValentina Manea case '4':
630588b48caSValentina Manea ipv4 = 1;
631588b48caSValentina Manea break;
632588b48caSValentina Manea case '6':
633588b48caSValentina Manea ipv6 = 1;
634588b48caSValentina Manea break;
635588b48caSValentina Manea case 'D':
636588b48caSValentina Manea daemonize = 1;
637588b48caSValentina Manea break;
638588b48caSValentina Manea case 'd':
639588b48caSValentina Manea usbip_use_debug = 1;
640588b48caSValentina Manea break;
641588b48caSValentina Manea case 'h':
642588b48caSValentina Manea cmd = cmd_help;
643588b48caSValentina Manea break;
644588b48caSValentina Manea case 'P':
645588b48caSValentina Manea pid_file = optarg ? optarg : DEFAULT_PID_FILE;
646588b48caSValentina Manea break;
647588b48caSValentina Manea case 't':
648588b48caSValentina Manea usbip_setup_port_number(optarg);
649588b48caSValentina Manea break;
650588b48caSValentina Manea case 'v':
651588b48caSValentina Manea cmd = cmd_version;
652588b48caSValentina Manea break;
653e0546fd8SIgor Kotrasinski case 'e':
654e0546fd8SIgor Kotrasinski driver = &device_driver;
655e0546fd8SIgor Kotrasinski break;
656588b48caSValentina Manea case '?':
657588b48caSValentina Manea usbipd_help();
658588b48caSValentina Manea default:
659588b48caSValentina Manea goto err_out;
660588b48caSValentina Manea }
661588b48caSValentina Manea }
662588b48caSValentina Manea
663588b48caSValentina Manea if (!ipv4 && !ipv6)
664588b48caSValentina Manea ipv4 = ipv6 = 1;
665588b48caSValentina Manea
666588b48caSValentina Manea switch (cmd) {
667588b48caSValentina Manea case cmd_standalone_mode:
668588b48caSValentina Manea rc = do_standalone_mode(daemonize, ipv4, ipv6);
669588b48caSValentina Manea remove_pid_file();
670588b48caSValentina Manea break;
671588b48caSValentina Manea case cmd_version:
672588b48caSValentina Manea printf(PROGNAME " (%s)\n", usbip_version_string);
673588b48caSValentina Manea rc = 0;
674588b48caSValentina Manea break;
675588b48caSValentina Manea case cmd_help:
676588b48caSValentina Manea usbipd_help();
677588b48caSValentina Manea rc = 0;
678588b48caSValentina Manea break;
679588b48caSValentina Manea default:
680588b48caSValentina Manea usbipd_help();
681588b48caSValentina Manea goto err_out;
682588b48caSValentina Manea }
683588b48caSValentina Manea
684588b48caSValentina Manea err_out:
685588b48caSValentina Manea return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);
686588b48caSValentina Manea }
687