Simplifies the code and avoids having a Unix domain socket on disk. --- fuzzing/libnbd-fuzz-wrapper.c | 78 ++++++++++------------------------- 1 file changed, 21 insertions(+), 57 deletions(-) diff --git a/fuzzing/libnbd-fuzz-wrapper.c b/fuzzing/libnbd-fuzz-wrapper.c index ba3840a..bfaa072 100644 --- a/fuzzing/libnbd-fuzz-wrapper.c +++ b/fuzzing/libnbd-fuzz-wrapper.c @@ -35,22 +35,18 @@ #include <sys/types.h> #include <sys/socket.h> #include <sys/wait.h> -#include <sys/un.h> #include <libnbd.h> -static void client (const char *sockpath); +static void client (int s); static void server (int fd, int s); int main (int argc, char *argv[]) { int fd; - char tmpdir[] = "/tmp/sockXXXXXX"; - char *sockpath; - struct sockaddr_un addr; pid_t pid; - int s, r, status; + int sv[2], r, status; if (argc != 2) { fprintf (stderr, "libnbd-fuzz-wrapper testcase\n"); @@ -64,29 +60,9 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - /* Create a randomly named Unix domain socket. */ - if (mkdtemp (tmpdir) == NULL) { - perror ("mkdtemp"); - exit (EXIT_FAILURE); - } - if (asprintf (&sockpath, "%s/sock", tmpdir) == -1) { - perror ("asprintf"); - exit (EXIT_FAILURE); - } - - s = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); - if (s == -1) { - perror ("socket"); - exit (EXIT_FAILURE); - } - addr.sun_family = AF_UNIX; - memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); - if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { - perror (sockpath); - exit (EXIT_FAILURE); - } - if (listen (s, 1) == -1) { - perror ("listen"); + /* Create a connected socket. */ + if (socketpair (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, sv) == -1) { + perror ("socketpair"); exit (EXIT_FAILURE); } @@ -101,9 +77,12 @@ main (int argc, char *argv[]) if (pid > 0) { /* Parent: libnbd client. */ - close (s); + close (sv[1]); close (fd); - client (sockpath); + + client (sv[0]); + + close (sv[0]); r = wait (&status); if (r == -1) { @@ -117,19 +96,17 @@ main (int argc, char *argv[]) } /* Child: phony NBD server. */ - server (fd, s); + close (sv[0]); + + server (fd, sv[1]); - /* Clean up the socket and directory. */ - close (s); - close (fd); - unlink (sockpath); - rmdir (tmpdir); + close (sv[1]); _exit (EXIT_SUCCESS); } static void -client (const char *sockpath) +client (int sock) { struct nbd_handle *nbd; char buf[512]; @@ -144,7 +121,7 @@ client (const char *sockpath) * interested in whether the process crashes. */ /* This tests the handshake phase. */ - nbd_connect_unix (nbd, sockpath); + nbd_connect_socket (nbd, sock); /* XXX We should do some more complicated operations here, * eg exercising structured reads. @@ -155,28 +132,15 @@ client (const char *sockpath) } static void -server (int fd, int listen_sock) +server (int fd, int sock) { - int s; struct pollfd pfds[1]; char rbuf[512], wbuf[512]; size_t wsize = 0; ssize_t r; - int flags; - - /* Accept a single connection on the socket. */ - s = accept (listen_sock, NULL, NULL); - if (s == -1) { - perror ("accept"); - _exit (EXIT_FAILURE); - } - - /* Set the accepted socket to non-blocking mode. */ - flags = fcntl (s, F_GETFL, 0); - if (flags >= 0) fcntl (s, F_SETFL, flags|O_NONBLOCK); for (;;) { - pfds[0].fd = s; + pfds[0].fd = sock; pfds[0].events = POLLIN; if (wsize >= 0 || fd >= 0) pfds[0].events |= POLLOUT; pfds[0].revents = 0; @@ -191,7 +155,7 @@ server (int fd, int listen_sock) /* We can read from the client socket. Just throw away anything sent. */ if ((pfds[0].revents & POLLIN) != 0) { - r = read (s, rbuf, sizeof rbuf); + r = read (sock, rbuf, sizeof rbuf); if (r == -1 && errno != EINTR) { perror ("read"); return; @@ -205,7 +169,7 @@ server (int fd, int listen_sock) /* Write more data from the wbuf. */ if (wsize > 0) { morewrite: - r = write (s, wbuf, wsize); + r = write (sock, wbuf, wsize); if (r == -1 && errno != EAGAIN && errno != EWOULDBLOCK) { perror ("write"); return; @@ -224,7 +188,7 @@ server (int fd, int listen_sock) } else if (r == 0) { fd = -1; /* ignore the file from now on */ - shutdown (s, SHUT_WR); + shutdown (sock, SHUT_WR); } else { wsize = r; -- 2.23.0