From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH ucspi-vsock 3/7] vsockserver: switch to a non-blocking socket
Date: Fri, 19 Mar 2021 02:56:47 +0000 [thread overview]
Message-ID: <20210319025648.17925-3-hi@alyssa.is> (raw)
In-Reply-To: <20210319025349.8839-2-hi@alyssa.is>
This is entirely an implementation detail, for the moment, but it
becomes important when we split out vsockserver into two seperate
programs, one for binding the socket, and one for accepting, like
s6-networking. Non-blocking sockets are the default there, so it
makes sense to share that behaviour.
I added the comment about blockingness of the connection sockets just
in case, but for now Linux is the only Unix with an AF_VSOCK
implementation, so I don't expect it to become relevant any time soon.
---
vsockserver.c | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/vsockserver.c b/vsockserver.c
index 58cd063..43307d2 100644
--- a/vsockserver.c
+++ b/vsockserver.c
@@ -4,7 +4,9 @@
#define _GNU_SOURCE
#include <errno.h>
+#include <fcntl.h>
#include <inttypes.h>
+#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -33,7 +35,7 @@ noreturn static void ex_usage(void)
int main(int argc, char *argv[])
{
bool notify = false;
- int opt, conn;
+ int opt;
pid_t child;
uint32_t lcid, lport, rcid, rport;
@@ -73,6 +75,9 @@ int main(int argc, char *argv[])
if (fd == -1)
diee(EX_OSERR, "socket");
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
+ diee(EX_OSERR, "fcntl");
+
if (vsock_bind(fd, lcid, lport) == -1)
diee(EX_OSERR, "bind");
@@ -93,7 +98,16 @@ int main(int argc, char *argv[])
ilog("listening as %" PRIu32 " on port %" PRIu32, lcid, lport);
- while ((conn = vsock_accept(fd, &rcid, &rport)) != -1) {
+ struct pollfd poll_fd = { .fd = fd, .events = POLL_IN, .revents = 0 };
+ while (poll(&poll_fd, 1, -1) != -1) {
+ // On Linux, conn will be blocking. On other
+ // platforms, this may not be the case. If other
+ // platforms are to be supported, we'd probably want
+ // to set O_NONBLOCK here.
+ int conn = vsock_accept(fd, &rcid, &rport);
+ if (conn == -1)
+ diee(EX_OSERR, "accept");
+
setenvf("VSOCKREMOTECID", 1, "%" PRIu32, rcid);
setenvf("VSOCKREMOTEPORT", 1, "%" PRIu32, rport);
@@ -117,5 +131,5 @@ int main(int argc, char *argv[])
close(conn);
}
- diee(EX_OSERR, "accept");
+ diee(EX_OSERR, "poll");
}
--
2.30.0
next prev parent reply other threads:[~2021-03-19 3:02 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-19 3:23 Broken threading fix Alyssa Ross
2021-03-19 2:56 ` [PATCH ucspi-vsock 1/7] vsock: get cid and port instead of just cid Alyssa Ross
2021-03-19 2:53 ` [PATCH ucspi-vsock 0/7] Extract vsockserver-socketbinder and vsockserverd Alyssa Ross
2021-03-19 3:19 ` Alyssa Ross
2021-03-19 2:56 ` [PATCH ucspi-vsock 2/7] vsock: check socket family before reading sockaddr Alyssa Ross
2021-03-19 2:56 ` Alyssa Ross [this message]
2021-03-19 2:56 ` [PATCH ucspi-vsock 4/7] configure: create, to generate config.h Alyssa Ross
2021-03-19 2:56 ` [PATCH ucspi-vsock 5/7] log: add die function Alyssa Ross
2021-03-19 2:56 ` [PATCH ucspi-vsock 6/7] exec: add execzp() Alyssa Ross
2021-03-19 3:17 ` [PATCH ucspi-vsock 7/7] Extract vsockserver-socketbinder and vsockserverd Alyssa Ross
2021-03-19 3:39 ` Cole Helbling
2021-03-20 20:24 ` Alyssa Ross
2021-03-21 2:52 ` Cole Helbling
2021-03-21 14:33 ` Alyssa Ross
2021-03-21 14:38 ` Alyssa Ross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210319025648.17925-3-hi@alyssa.is \
--to=hi@alyssa.is \
--cc=devel@spectrum-os.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://spectrum-os.org/git/crosvm
https://spectrum-os.org/git/doc
https://spectrum-os.org/git/mktuntap
https://spectrum-os.org/git/nixpkgs
https://spectrum-os.org/git/spectrum
https://spectrum-os.org/git/ucspi-vsock
https://spectrum-os.org/git/www
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).