summary refs log tree commit diff
path: root/pkgs/os-specific/linux/usbip/usbipd-add-s6-readiness-notification.patch
blob: dc74e7df9b34656b2ff7849807dfb448fb932f4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
From 112a36a9dfddfbcb757c61a81cd7fe528622a307 Mon Sep 17 00:00:00 2001
From: Alyssa Ross <hi@alyssa.is>
Date: Sat, 5 Sep 2020 11:45:03 +0000
Subject: [PATCH] usbip: tools: usbipd: add s6 readiness notification

This implements the s6 readiness notification protocol[1].  When the
daemon has started and is listening on its socket(s), it will emit a
readiness notification on a user-specified file descriptor.

systemd-based systems can also use the readiness notification, using
the sdnotify-wrapper program[2].

[1]: https://skarnet.org/software/s6/s6-supervise.html
[2]: https://skarnet.org/software/misc/sdnotify-wrapper.c
---
 tools/usb/usbip/src/usbipd.c | 70 ++++++++++++++++++++++++++++--------
 1 file changed, 55 insertions(+), 15 deletions(-)

diff --git a/tools/usb/usbip/src/usbipd.c b/tools/usb/usbip/src/usbipd.c
index 48398a78e88af..4ee41040735f4 100644
--- a/tools/usb/usbip/src/usbipd.c
+++ b/tools/usb/usbip/src/usbipd.c
@@ -22,6 +22,7 @@
 #include <arpa/inet.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <fcntl.h>
 
 #ifdef HAVE_LIBWRAP
 #include <tcpd.h>
@@ -65,6 +66,10 @@ static const char usbipd_help_string[] =
 	"	-D, --daemon\n"
 	"		Run as a daemon process.\n"
 	"\n"
+	"	-nFD, --notify-fd FD\n"
+	"		Once ready to receive connections, write a line feed\n"
+	"		character to FD, then close it.\n"
+	"\n"
 	"	-d, --debug\n"
 	"		Print debugging information.\n"
 	"\n"
@@ -88,6 +93,30 @@ static void usbipd_help(void)
 	printf("%s\n", usbipd_help_string);
 }
 
+static int parse_notify_fd(char *arg)
+{
+	dbg("parsing notify-fd arg '%s'", arg);
+	char *end;
+	unsigned long int notify_fd = strtoul(arg, &end, 10);
+
+	if (end == arg) {
+		err("notify-fd: could not parse '%s' as decimal integer", arg);
+		return -1;
+	}
+
+	if (*end != '\0') {
+		err("notify-fd: garbage at end of '%s'", arg);
+		return -1;
+	}
+
+	if (fcntl(notify_fd, F_GETFD) == -1) {
+		err("notify-fd: %s", strerror(errno));
+		return -1;
+	}
+
+	return notify_fd;
+}
+
 static int recv_request_import(int sockfd)
 {
 	struct op_import_request req;
@@ -488,7 +517,7 @@ static void remove_pid_file(void)
 	}
 }
 
-static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
+static int do_standalone_mode(int daemonize, int notify_fd, int ipv4, int ipv6)
 {
 	struct addrinfo *ai_head;
 	int sockfdlist[MAXSOCKFD];
@@ -543,6 +572,13 @@ static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
 
 	dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
 
+	if (notify_fd != -1) {
+		const char lf[] = { '\n' };
+		if (write(notify_fd, lf, sizeof lf) == -1)
+			err("failed to notify readiness");
+		close(notify_fd);
+	}
+
 	fds = calloc(nsockfd, sizeof(struct pollfd));
 	for (i = 0; i < nsockfd; i++) {
 		fds[i].fd = sockfdlist[i];
@@ -586,17 +622,18 @@ static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
 int main(int argc, char *argv[])
 {
 	static const struct option longopts[] = {
-		{ "ipv4",     no_argument,       NULL, '4' },
-		{ "ipv6",     no_argument,       NULL, '6' },
-		{ "daemon",   no_argument,       NULL, 'D' },
-		{ "daemon",   no_argument,       NULL, 'D' },
-		{ "debug",    no_argument,       NULL, 'd' },
-		{ "device",   no_argument,       NULL, 'e' },
-		{ "pid",      optional_argument, NULL, 'P' },
-		{ "tcp-port", required_argument, NULL, 't' },
-		{ "help",     no_argument,       NULL, 'h' },
-		{ "version",  no_argument,       NULL, 'v' },
-		{ NULL,	      0,                 NULL,  0  }
+		{ "ipv4",      no_argument,       NULL, '4' },
+		{ "ipv6",      no_argument,       NULL, '6' },
+		{ "daemon",    no_argument,       NULL, 'D' },
+		{ "daemon",    no_argument,       NULL, 'D' },
+		{ "notify-fd", required_argument, NULL, 'n' },
+		{ "debug",     no_argument,       NULL, 'd' },
+		{ "device",    no_argument,       NULL, 'e' },
+		{ "pid",       optional_argument, NULL, 'P' },
+		{ "tcp-port",  required_argument, NULL, 't' },
+		{ "help",      no_argument,       NULL, 'h' },
+		{ "version",   no_argument,       NULL, 'v' },
+		{ NULL,	       0,                 NULL,  0  }
 	};
 
 	enum {
@@ -605,7 +642,7 @@ int main(int argc, char *argv[])
 		cmd_version
 	} cmd;
 
-	int daemonize = 0;
+	int daemonize = 0, notify_fd = -1;
 	int ipv4 = 0, ipv6 = 0;
 	int opt, rc = -1;
 
@@ -620,7 +657,7 @@ int main(int argc, char *argv[])
 	cmd = cmd_standalone_mode;
 	driver = &host_driver;
 	for (;;) {
-		opt = getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL);
+		opt = getopt_long(argc, argv, "46Dn:deP::t:hv", longopts, NULL);
 
 		if (opt == -1)
 			break;
@@ -635,6 +672,9 @@ int main(int argc, char *argv[])
 		case 'D':
 			daemonize = 1;
 			break;
+		case 'n':
+			notify_fd = parse_notify_fd(optarg);
+			break;
 		case 'd':
 			usbip_use_debug = 1;
 			break;
@@ -665,7 +705,7 @@ int main(int argc, char *argv[])
 
 	switch (cmd) {
 	case cmd_standalone_mode:
-		rc = do_standalone_mode(daemonize, ipv4, ipv6);
+		rc = do_standalone_mode(daemonize, notify_fd, ipv4, ipv6);
 		remove_pid_file();
 		break;
 	case cmd_version:
-- 
2.27.0