chiark / gitweb /
More graceful handling of test failure; the exception is now reported
[disorder] / tests / udplog.c
index eb87651b13d90aa037b30db84658d9c23e2b8dfe..a27252c49d8d869185a4f656e821ac3718ddc30d 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  * USA
  */
-
-#include <config.h>
-#include "types.h"
+/** @file tests/udplog.c
+ * @brief UDP logging utility
+ *
+ * Intended for low-level debugging.
+ */
+#include "common.h"
 
 #include <getopt.h>
 #include <sys/types.h>
@@ -78,15 +81,13 @@ int main(int argc, char **argv) {
     struct sockaddr_in6 sin6;
   } sa;
   socklen_t len;
+  fd_set fds;
+  struct timeval tv;
   static const struct addrinfo pref = {
-    0,                         /* ai_flags */
-    AF_UNSPEC,                 /* ai_family */
-    SOCK_DGRAM,                        /* ai_socktype */
-    IPPROTO_UDP,               /* ai_protocol */
-    0,
-    0,
-    0,
-    0
+    .ai_flags = 0,
+    .ai_family = AF_UNSPEC,
+    .ai_socktype = SOCK_DGRAM,
+    .ai_protocol = IPPROTO_UDP,
   };
   
   set_progname(argv);
@@ -110,9 +111,17 @@ int main(int argc, char **argv) {
   if(!(ai = get_address(&a, &pref, &name)))
     exit(1);
   fd = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+  nonblock(fd);
   if(bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
     fatal(errno, "error binding to %s", name);
   while(getppid() != 1) {
+    /* Wait for something to happen.  We don't just block forever in recvfrom()
+     * as otherwise we'd never die if the parent terminated uncontrolledly. */
+    FD_ZERO(&fds);
+    FD_SET(fd, &fds);
+    tv.tv_sec = 1;
+    tv.tv_usec = 0;
+    select(fd + 1, &fds, 0, 0, &tv);
     len = sizeof sa;
     n = recvfrom(fd, buffer, sizeof buffer, 0, &sa.sa, &len);
     if(n < 0) {