about summary refs log tree commit diff
path: root/src/libunixonacid
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2016-04-22 11:54:31 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2016-04-22 11:54:31 +0000
commitf6d5fcfa0fa2a62f9583fa44dbee035c0adfcc88 (patch)
tree206cd7e9606bf5488f70e6eddb3aadbb35bafe1b /src/libunixonacid
parente9fce4390ad8673239a51facd6bbd9ffb5eb7e68 (diff)
downloadskalibs-f6d5fcfa0fa2a62f9583fa44dbee035c0adfcc88.tar.gz
skalibs-f6d5fcfa0fa2a62f9583fa44dbee035c0adfcc88.tar.xz
skalibs-f6d5fcfa0fa2a62f9583fa44dbee035c0adfcc88.zip
Fix closecb support wherever a unixmessage may get dropped; add closecb support to unixconnection
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/unixconnection_init.c3
-rw-r--r--src/libunixonacid/unixconnection_init_withclosecb.c10
-rw-r--r--src/libunixonacid/unixmessage_put.c7
-rw-r--r--src/libunixonacid/unixmessage_sender_flush.c2
-rw-r--r--src/libunixonacid/unixmessage_sender_free.c4
-rw-r--r--src/libunixonacid/unixmessage_unput.c13
6 files changed, 24 insertions, 15 deletions
diff --git a/src/libunixonacid/unixconnection_init.c b/src/libunixonacid/unixconnection_init.c
index 7831d6a..52ecd44 100644
--- a/src/libunixonacid/unixconnection_init.c
+++ b/src/libunixonacid/unixconnection_init.c
@@ -5,6 +5,5 @@
 
 void unixconnection_init (unixconnection_t *io, int fdin, int fdout)
 {
-  unixmessage_receiver_init(&io->in, fdin, io->mainbuf, UNIXMESSAGE_BUFSIZE, io->auxbuf, UNIXMESSAGE_AUXBUFSIZE) ;
-  unixmessage_sender_init(&io->out, fdout) ;
+  unixconnection_init_withclosecb(io, fdin, fdout, &unixmessage_sender_closecb, 0) ;
 }
diff --git a/src/libunixonacid/unixconnection_init_withclosecb.c b/src/libunixonacid/unixconnection_init_withclosecb.c
new file mode 100644
index 0000000..b89b3ee
--- /dev/null
+++ b/src/libunixonacid/unixconnection_init_withclosecb.c
@@ -0,0 +1,10 @@
+ /* ISC license. */
+
+#include <skalibs/unixmessage.h>
+#include <skalibs/unixconnection.h>
+
+void unixconnection_init_withclosecb (unixconnection_t *io, int fdin, int fdout, unixmessage_sender_closecb_func_t_ref f, void *p)
+{
+  unixmessage_receiver_init(&io->in, fdin, io->mainbuf, UNIXMESSAGE_BUFSIZE, io->auxbuf, UNIXMESSAGE_AUXBUFSIZE) ;
+  unixmessage_sender_init_withclosecb(&io->out, fdout, f, p) ;
+}
diff --git a/src/libunixonacid/unixmessage_put.c b/src/libunixonacid/unixmessage_put.c
index 015264a..43091c9 100644
--- a/src/libunixonacid/unixmessage_put.c
+++ b/src/libunixonacid/unixmessage_put.c
@@ -14,7 +14,7 @@
 #include <skalibs/siovec.h>
 #include <skalibs/unixmessage.h>
 
-static inline int copyfds (char *s, int const *fds, unsigned int n, unsigned char const *bits)
+static inline int copyfds (char *s, int const *fds, unsigned int n, unsigned char const *bits, unixmessage_sender_closecb_func_t_ref closecb, void *closecbdata)
 {
   register unsigned int i = 0 ;
   for (; i < n ; i++)
@@ -33,13 +33,12 @@ static inline int copyfds (char *s, int const *fds, unsigned int n, unsigned cha
         {
           s -= sizeof(int) ;
           byte_copy((char *)fd, sizeof(int), s) ;
-          if (fd >= 0) fd_close(fd) ;
+          if (fd >= 0) (*closecb)(fd, closecbdata) ;
         }
         errno = e ;
         return 0 ;
       }
     }
-#else
 #endif
     byte_copy(s, sizeof(int), (char const *)&fd) ;
     s += sizeof(int) ;
@@ -56,7 +55,7 @@ static int reserve_and_copy (unixmessage_sender_t *b, unsigned int len, int cons
    || !genalloc_readyplus(int, &b->fds, nfds)
    || !stralloc_readyplus(&b->data, len))
     return 0 ;
-  if (!copyfds(b->fds.s + b->fds.len, fds, nfds, bits)) return 0 ;
+  if (!copyfds(b->fds.s + b->fds.len, fds, nfds, bits, b->closecb, b->closecbdata)) return 0 ;
   genalloc_setlen(int, &b->fds, cur.right + nfds) ;
   byte_copy(b->offsets.s + b->offsets.len, sizeof(diuint), (char const *)&cur) ;
   b->offsets.len += sizeof(diuint) ;
diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c
index 6559083..57c5d6e 100644
--- a/src/libunixonacid/unixmessage_sender_flush.c
+++ b/src/libunixonacid/unixmessage_sender_flush.c
@@ -97,7 +97,7 @@ int unixmessage_sender_flush (unixmessage_sender_t *b)
     while (r < 0 && errno == EINTR) ;
     if (r <= 0) return 0 ;
 #ifndef SKALIBS_HASANCILAUTOCLOSE
-    if (nfds && b->closecb)
+    if (nfds)
     {
       register unsigned int i = 0 ;
       for (; i < nfds ; i++)
diff --git a/src/libunixonacid/unixmessage_sender_free.c b/src/libunixonacid/unixmessage_sender_free.c
index 6ef0f42..a976827 100644
--- a/src/libunixonacid/unixmessage_sender_free.c
+++ b/src/libunixonacid/unixmessage_sender_free.c
@@ -16,9 +16,9 @@ void unixmessage_sender_free (unixmessage_sender_t *b)
     for (; i < n ; i++)
     {
       register int fd = genalloc_s(int, &b->fds)[i] ;
-      if (fd < 0) fd_close(-(fd+1)) ;
+      if (fd < 0) (*b->closecb)(-(fd+1), b->closecbdata) ;
 #ifdef SKALIBS_HASANCILAUTOCLOSE
-      else fd_close(fd) ;
+      else (*b->closecb)(fd, b->closecbdata) ;
 #endif
     }
   }
diff --git a/src/libunixonacid/unixmessage_unput.c b/src/libunixonacid/unixmessage_unput.c
index 63e94b8..e869110 100644
--- a/src/libunixonacid/unixmessage_unput.c
+++ b/src/libunixonacid/unixmessage_unput.c
@@ -9,14 +9,15 @@ int unixmessage_unput_and_maybe_drop (unixmessage_sender_t *b, int drop)
 {
   diuint *start ;
   unsigned int n = genalloc_len(diuint, &b->offsets) ;
+  register int *fds ;
+  register unsigned int i ;
   if (!n) return 0 ;
   start = genalloc_s(diuint, &b->offsets) + n - 1 ;
-  if (drop)
-  {
-    register int *fds = genalloc_s(int, &b->fds) + start->right ;
-    register unsigned int i = genalloc_len(int, &b->fds) - start->right ;
-    while (i--) fd_close(fds[i] < 0 ? -(fds[i]+1) : fds[i]) ;
-  }
+  fds = genalloc_s(int, &b->fds) + start->right ;
+  i = genalloc_len(int, &b->fds) - start->right ;
+  while (i--)
+    if (fds[i] < 0) (*b->closecb)(-(fds[i]+1), b->closecbdata) ;
+    else if (drop) fd_close(fds[i]) ;
   b->data.len = start->left ;
   genalloc_setlen(int, &b->fds, start->right) ;
   genalloc_setlen(diuint, &b->offsets, n-1) ;