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