chiark / gitweb /
util: Provide async_linebuf_read
[secnet.git] / udp.c
diff --git a/udp.c b/udp.c
index 8f0bb8bde3fdeb33b46c2f7bb2b3cd46d6c4505e..5ad0fd44fd6b175e34d2aaf0c1b60135854d6873 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -183,6 +183,14 @@ static bool_t udp_sendmsg(void *commst, struct buffer_if *buf,
     return True;
 }
 
+void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us)
+{
+    if (us->fd>=0) {
+       close(us->fd);
+       us->fd=-1;
+    }
+}
+
 bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
                       int failmsgclass)
 {
@@ -198,8 +206,7 @@ bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
 
     us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
     if (us->fd<0) FAIL("socket");
-    if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1)
-       FAIL("fcntl(set O_NONBLOCK)");
+    setnonblock(us->fd);
     setcloexec(us->fd);
 #ifdef CONFIG_IPV6
     if (addr->sa.sa_family==AF_INET6) {
@@ -224,6 +231,7 @@ bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
            char *argv[5], addrstr[33], portstr[5];
            const char *addrfam;
            int port;
+           afterfork();
            switch (addr->sa.sa_family) {
            case AF_INET:
                sprintf(addrstr,"%08lX",(long)addr->sin.sin_addr.s_addr);
@@ -258,15 +266,15 @@ bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
            if (errno==EINTR) continue;
            FAIL("waitpid for authbind");
        }
-       if (WIFSIGNALED(status)) {
-           lg_perror(FAIL_LG,0,"authbind died on signal %s (%d)",
-                       strsignal(WTERMSIG(status)),WTERMSIG(status));
-           goto failed;
-       }
-       if (WIFEXITED(status) && WEXITSTATUS(status)!=0) {
-           lg_perror(FAIL_LG,0,
-                     "authbind died with error exit status %d",
-                     WEXITSTATUS(status));
+       if (status) {
+           if (WIFEXITED(status) && WEXITSTATUS(status)<127) {
+               int es=WEXITSTATUS(status);
+               lg_perror(FAIL_LG,es,
+                         "authbind exited with error exit status %d;"
+                         " indicates error",es);
+           } else {
+               lg_exitstatus(FAIL_LG,status,"authbind");
+           }
            goto failed;
        }
     } else {
@@ -276,10 +284,7 @@ bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
     return True;
 
 failed:
-    if (us->fd>=0) {
-       close(us->fd);
-       us->fd=-1;
-    }
+    udp_destroy_socket(uc,us);
     return False;
 
 #undef FAIL
@@ -288,7 +293,14 @@ failed:
 void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks)
 {
     socks->uc=uc;
-    register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
+    socks->interest=
+       register_for_poll(socks,udp_socks_beforepoll,udp_socks_afterpoll,"udp");
+}
+
+void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks)
+{
+    socks->uc=uc;
+    deregister_for_poll(socks->interest);
 }
 
 static void udp_phase_hook(void *sst, uint32_t new_phase)