From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on atuin.qyliss.net X-Spam-Level: X-Spam-Status: No, score=-4.3 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.4 Received: by atuin.qyliss.net (Postfix, from userid 496) id 5443531C5; Mon, 15 Mar 2021 14:03:22 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by atuin.qyliss.net (Postfix) with ESMTP id C798631A1; Mon, 15 Mar 2021 14:03:13 +0000 (UTC) Received: by atuin.qyliss.net (Postfix, from userid 496) id 11C4E319E; Mon, 15 Mar 2021 14:03:12 +0000 (UTC) Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by atuin.qyliss.net (Postfix) with ESMTPS id C171A319D for ; Mon, 15 Mar 2021 14:03:08 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id E6DD75C00C1 for ; Mon, 15 Mar 2021 10:03:07 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 15 Mar 2021 10:03:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alyssa.is; h= from:to:subject:date:message-id:mime-version :content-transfer-encoding; s=fm2; bh=LOpiYSkYKTYoqw9hSYtocjX1Sp Cjo/GmM8PBTE2ONuE=; b=fpIxaUkDquG+McUCYuuRS6NPVHa0jYHXbDyEw96L+5 Rxm6PE4rAplUVn/gjZU4tjDnS5CXwgyoSd8Xqeh7YD1m+k043THpgE9M+tWhZt9Z 6zhp+bMSsl+QY7VttCTTBNZeVTGhmIbjd5Wgv4ndbwU38soxnkjckFDSmf4Mcn/q +LBssq1MrYRKsqZ83++P70+DQFbtHgb8LgmdHgbR/zIA+J3q+AA6rayr1Qm13Rrr seZUVmIKLxhdRkpCNRQ3847T89VUlB8Pi28G7GslAjJwa2yrmn0fRHvPcY8oQEQo VseA+BNjD1VOJkgRZZIeSipXI8NOH+ZwU2pRnF6jfVOw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=LOpiYSkYKTYoqw9hS YtocjX1SpCjo/GmM8PBTE2ONuE=; b=o8dEjWvwZAs0RgRsMSjrrj9WWvOSFoQhC hB/3Kv4zZX0gqhSzNkfl3jQxodfGlpcldJEfnCrWZAjxKBP4h6ZL8lEtLT4/iSgq L0HPc/XtrETTZWTu+OTMiiJTwMFprhuyhWdcF24xtzP5MEZahDhSFMpChtTb7x2e cVoIweWg/Qx5nFovKvpgKF/Ga/XyuBQVfmjzWULnzFfCGeVoqCxLXQXUGP7iX4VC pFDAdU6OD0iJPPHlJ62GO9p1u7/S6p8p4Pc8b5Ac8krKbfnhBLrEHMjP+ubai1l/ GZwNaJx5sLJIO5mp1d8Nedae4dKnLb2UEE3xqTLZLp3tN0yHDuilw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledruddvledgheekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvffufffkofgggfestdekredtre dttdenucfhrhhomheptehlhihsshgrucftohhsshcuoehhihesrghlhihsshgrrdhisheq necuggftrfgrthhtvghrnhepkefgueekieefvdduieeugfeiheektdeutdejledujedvvd ehteejkedttddvkeevnecuffhomhgrihhnpehskhgrrhhnvghtrdhorhhgnecukfhppeek gedrudekgedrvdefvddrudektdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehqhihlihhsshesgidvvddtrdhqhihlihhsshdrnhgvth X-ME-Proxy: Received: from x220.qyliss.net (p54b8e8b4.dip0.t-ipconnect.de [84.184.232.180]) by mail.messagingengine.com (Postfix) with ESMTPA id E9A8C240066 for ; Mon, 15 Mar 2021 10:03:06 -0400 (EDT) Received: by x220.qyliss.net (Postfix, from userid 1000) id 781261AA8; Mon, 15 Mar 2021 14:03:04 +0000 (UTC) From: Alyssa Ross To: devel@spectrum-os.org Subject: [PATCH linux] usbip: tools: usbipd: add s6 readiness notification Date: Mon, 15 Mar 2021 14:02:39 +0000 Message-Id: <20210315140239.8588-1-hi@alyssa.is> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: PTQXTFTBOAUDD4GOQULIO5ICY6QVBBW4 X-Message-ID-Hash: PTQXTFTBOAUDD4GOQULIO5ICY6QVBBW4 X-MailFrom: qyliss@x220.qyliss.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.3.1 Precedence: list List-Id: Patches and low-level development discussion Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: 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 Signed-off-by: Alyssa Ross --- This is important for us in Spectrum to be able to run usbipd to export USB devices to other VMs. I plan to add this patch to linuxPackages.usbip in Spectrum nixpkgs. I might attempt to upstream it to Linux, but I'm not sure yet -- the s6 protocol is perfect for our needs but fairly obsure, so I'm not sure if they'd like it. 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 #include #include +#include =20 #ifdef HAVE_LIBWRAP #include @@ -65,6 +66,10 @@ static const char usbipd_help_string[] =3D " -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); } =20 +static int parse_notify_fd(char *arg) +{ + dbg("parsing notify-fd arg '%s'", arg); + char *end; + unsigned long int notify_fd =3D strtoul(arg, &end, 10); + + if (end =3D=3D arg) { + err("notify-fd: could not parse '%s' as decimal integer", arg); + return -1; + } + + if (*end !=3D '\0') { + err("notify-fd: garbage at end of '%s'", arg); + return -1; + } + + if (fcntl(notify_fd, F_GETFD) =3D=3D -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) } } =20 -static int do_standalone_mode(int daemonize, int ipv4, int ipv6) +static int do_standalone_mode(int daemonize, int notify_fd, int ipv4, in= t ipv6) { struct addrinfo *ai_head; int sockfdlist[MAXSOCKFD]; @@ -543,6 +572,13 @@ static int do_standalone_mode(int daemonize, int ipv= 4, int ipv6) =20 dbg("listening on %d address%s", nsockfd, (nsockfd =3D=3D 1) ? "" : "es= "); =20 + if (notify_fd !=3D -1) { + const char lf[] =3D { '\n' }; + if (write(notify_fd, lf, sizeof lf) =3D=3D -1) + err("failed to notify readiness"); + close(notify_fd); + } + fds =3D calloc(nsockfd, sizeof(struct pollfd)); for (i =3D 0; i < nsockfd; i++) { fds[i].fd =3D sockfdlist[i]; @@ -586,17 +622,18 @@ static int do_standalone_mode(int daemonize, int ip= v4, int ipv6) int main(int argc, char *argv[]) { static const struct option longopts[] =3D { - { "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 } }; =20 enum { @@ -605,7 +642,7 @@ int main(int argc, char *argv[]) cmd_version } cmd; =20 - int daemonize =3D 0; + int daemonize =3D 0, notify_fd =3D -1; int ipv4 =3D 0, ipv6 =3D 0; int opt, rc =3D -1; =20 @@ -620,7 +657,7 @@ int main(int argc, char *argv[]) cmd =3D cmd_standalone_mode; driver =3D &host_driver; for (;;) { - opt =3D getopt_long(argc, argv, "46DdeP::t:hv", longopts, NULL); + opt =3D getopt_long(argc, argv, "46Dn:deP::t:hv", longopts, NULL); =20 if (opt =3D=3D -1) break; @@ -635,6 +672,9 @@ int main(int argc, char *argv[]) case 'D': daemonize =3D 1; break; + case 'n': + notify_fd =3D parse_notify_fd(optarg); + break; case 'd': usbip_use_debug =3D 1; break; @@ -665,7 +705,7 @@ int main(int argc, char *argv[]) =20 switch (cmd) { case cmd_standalone_mode: - rc =3D do_standalone_mode(daemonize, ipv4, ipv6); + rc =3D do_standalone_mode(daemonize, notify_fd, ipv4, ipv6); remove_pid_file(); break; case cmd_version: --=20 2.27.0