chiark / gitweb /
SIGPIPE protection available.
authorian <ian>
Sun, 11 Apr 1999 21:01:49 +0000 (21:01 +0000)
committerian <ian>
Sun, 11 Apr 1999 21:01:49 +0000 (21:01 +0000)
src/adns.h
src/event.c
src/general.c
src/internal.h
src/transmit.c

index 32addba0bc25ac38443b16921d903e846c6325b0..5726e253d2c9a0fd18c83792dbfea07a54a99b2a 100644 (file)
@@ -42,6 +42,7 @@ typedef enum {
   adns_if_debug=        0x0008, /* enable all output to stderr plus debug msgs */
   adns_if_noautosys=    0x0010, /* do not make syscalls at every opportunity */
   adns_if_eintr=        0x0020, /* allow _wait and _synchronous to return EINTR */
+  adns_if_nosigpipe=    0x0040, /* applic has SIGPIPE set to SIG_IGN, do not protect */
 } adns_initflags;
 
 typedef enum {
index 5041f8ce02bc6cf23e69b52c214de682869330b1..db3dad68e776168a2c645aa14e801413cf837dd7 100644 (file)
@@ -284,7 +284,9 @@ static int internal_callback(adns_state ads, int maxfd,
     } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) {
       adns__tcp_broken(ads,"select","exceptional condition detected");
     } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
+      adns__sigpipe_protect(ads);
       r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
+      adns__sigpipe_unprotect(ads);
       if (r<0) {
        if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) {
          adns__tcp_broken(ads,"write",strerror(errno));
index 33946ad905c53663808db3b6c2645301fa6a80c7..b701b9f8b57975ecd9ee752ca6ab5d81878e873e 100644 (file)
@@ -270,3 +270,32 @@ void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
     }
   }
 }
+
+/* SIGPIPE protection. */
+
+void adns__sigpipe_protect(adns_state ads) {
+  sigset_t toblock;
+  struct sigaction sa;
+  int r;
+
+  if (ads->iflags & adns_if_nosigpipe) return;
+
+  sigfillset(&toblock);
+  sigdelset(&toblock,SIGPIPE);
+
+  sa.sa_handler= SIG_IGN;
+  sigfillset(&sa.sa_mask);
+  sa.sa_flags= 0;
+  
+  r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r);
+  r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r);
+}
+
+void adns__sigpipe_unprotect(adns_state ads) {
+  int r;
+
+  if (ads->iflags & adns_if_nosigpipe) return;
+
+  r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r);
+  r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r);
+}
index 5e42adeb0cc963a2a5bb6606f22d73417386ac2d..9a5f23d823ac1e282f8207aaedafa87d806771c0 100644 (file)
@@ -31,6 +31,7 @@ typedef unsigned char byte;
 #include <stdarg.h>
 #include <assert.h>
 #include <unistd.h>
+#include <signal.h>
 
 #include <sys/time.h>
 
@@ -249,6 +250,8 @@ struct adns__state {
   int nservers, nsortlist, tcpserver;
   enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
   struct timeval tcptimeout;
+  struct sigaction stdsigpipe;
+  sigset_t stdsigmask;
   struct server {
     struct in_addr addr;
   } servers[MAXSERVERS];
@@ -302,7 +305,15 @@ void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
  * sz bytes long.  needswap should return !0 if a>b (strictly, ie
  * wrong order) 0 if a<=b (ie, order is fine).
  */
-  
+
+void adns__sigpipe_protect(adns_state);
+void adns__sigpipe_unprotect(adns_state);
+/* If SIGPIPE protection is not disabled, will block all signals except
+ * SIGPIPE, and set SIGPIPE's disposition to SIG_IGN.  (And then restore.)
+ * Each call to _protect must be followed by a call to _unprotect before
+ * any significant amount of code gets to run.
+ */
+
 /* From transmit.c: */
 
 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
index d3978c3f713149e77923bfa31365f5a75e9eabe1..d0b9d9514ef6b50653e1ae9ab25b6fb508b004db 100644 (file)
@@ -179,7 +179,9 @@ void adns__query_tcp(adns_query qu, struct timeval now) {
     iov[0].iov_len= 2;
     iov[1].iov_base= qu->query_dgram;
     iov[1].iov_len= qu->query_dglen;
+    adns__sigpipe_protect(qu->ads);
     wr= writev(qu->ads->tcpsocket,iov,2);
+    adns__sigpipe_unprotect(qu->ads);
     if (wr < 0) {
       if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
            errno == ENOBUFS || errno == ENOMEM)) {