about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2024-06-20 20:08:44 +0000
committerLaurent Bercot <ska@appnovation.com>2024-06-20 20:08:44 +0000
commitd27671a3be471d26924a7a42b7f155eaa9dd121b (patch)
tree7cb9c5bab4cde084c3d3e952f17341cfefdb0b4c
parent6b9bfda00768647fba46a12ff4aad977e105849a (diff)
downloads6-d27671a3be471d26924a7a42b7f155eaa9dd121b.tar.gz
s6-d27671a3be471d26924a7a42b7f155eaa9dd121b.tar.xz
s6-d27671a3be471d26924a7a42b7f155eaa9dd121b.zip
Tentative fix for s6_fdholder_setdump()
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--src/libs6/s6_fdholder_setdump.c66
1 files changed, 34 insertions, 32 deletions
diff --git a/src/libs6/s6_fdholder_setdump.c b/src/libs6/s6_fdholder_setdump.c
index e1462ef..58d7003 100644
--- a/src/libs6/s6_fdholder_setdump.c
+++ b/src/libs6/s6_fdholder_setdump.c
@@ -20,8 +20,7 @@ int s6_fdholder_setdump (s6_fdholder_t *a, s6_fdholder_fd_t const *list, unsigne
 {
   uint32_t trips ;
   if (!ntot) return 1 ;
-  unsigned int i = 0 ;
-  for (; i < ntot ; i++)
+  for (unsigned int i = 0 ; i < ntot ; i++)
   {
     size_t zpos = byte_chr(list[i].id, S6_FDHOLDER_ID_SIZE + 1, 0) ;
     if (!zpos || zpos >= S6_FDHOLDER_ID_SIZE + 1) return (errno = EINVAL, 0) ;
@@ -39,42 +38,45 @@ int s6_fdholder_setdump (s6_fdholder_t *a, s6_fdholder_fd_t const *list, unsigne
     uint32_unpack_big(m.s + 1, &trips) ;
     if (trips != 1 + (ntot-1) / UNIXMESSAGE_MAXFDS) return (errno = EPROTO, 0) ;
   }
-  for (i = 0 ; i < trips ; i++, ntot -= UNIXMESSAGE_MAXFDS)
   {
+    struct iovec v[1 + (UNIXMESSAGE_MAXFDS << 1)] = { [0] = { .iov_base = ".", .iov_len = 1 } } ;
+    int fds[UNIXMESSAGE_MAXFDS] ;
+    char pack[UNIXMESSAGE_MAXFDS][TAIN_PACK + 1] ;
+    for (unsigned int j = 0 ; j < UNIXMESSAGE_MAXFDS ; j++)
     {
-      unsigned int n = ntot > UNIXMESSAGE_MAXFDS ? UNIXMESSAGE_MAXFDS : ntot ;
-      unsigned int j = 0 ;
-      struct iovec v[1 + (n<<1)] ;
-      int fds[n] ;
-      unixmessagev m = { .v = v, .vlen = 1 + (n<<1), .fds = fds, .nfds = n } ;
-      char pack[n * (TAIN_PACK+1)] ;
-      v[0].iov_base = "." ; v[0].iov_len = 1 ;
-      for (; j < n ; j++, list++, ntot--)
-      {
-        size_t len = strlen(list->id) ;
-        v[1 + (j<<1)].iov_base = pack + j * (TAIN_PACK+1) ;
-        v[1 + (j<<1)].iov_len = TAIN_PACK + 1 ;
-        tain_pack(pack + j * (TAIN_PACK+1), &list->limit) ;
-        pack[j * (TAIN_PACK+1) + TAIN_PACK] = (unsigned char)len ;
-        v[2 + (j<<1)].iov_base = (char *)list->id ;
-        v[2 + (j<<1)].iov_len = len + 1 ;
-        fds[j] = list->fd ;
-      }
-      if (!unixmessage_putv(&a->connection.out, &m)) return 0 ;
+      v[1 + (j<<1)].iov_base = pack[j] ;
+      v[1 + (j<<1)].iov_len = TAIN_PACK + 1 ;
     }
-    if (!unixmessage_sender_timed_flush(&a->connection.out, deadline, stamp)) return 0 ;
+    for (unsigned int i = 0 ; i < trips ; i++)
     {
-      unixmessage m ;
-      if (sanitize_read(unixmessage_timed_receive(&a->connection.in, &m, deadline, stamp)) < 0) return 0 ;
-      if (m.len != 1 || m.nfds)
       {
-        unixmessage_drop(&m) ;
-        return (errno = EPROTO, 0) ;
+        unsigned int n = ntot > UNIXMESSAGE_MAXFDS ? UNIXMESSAGE_MAXFDS : ntot ;
+        unixmessagev m = { .v = v, .vlen = 1 + (n<<1), .fds = fds, .nfds = n } ;
+        for (unsigned int j = 0 ; j < n ; j++, list++, ntot--)
+        {
+          size_t len = strlen(list->id) ;
+          tain_pack(pack[j], &list->limit) ;
+          pack[j][TAIN_PACK] = (unsigned char)len ;
+          v[2 + (j<<1)].iov_base = (char *)list->id ;
+          v[2 + (j<<1)].iov_len = len + 1 ;
+          fds[j] = list->fd ;
+        }
+        if (!unixmessage_putv(&a->connection.out, &m)) return 0 ;
+      }
+      if (!unixmessage_sender_timed_flush(&a->connection.out, deadline, stamp)) return 0 ;
+      {
+        unixmessage m ;
+        if (sanitize_read(unixmessage_timed_receive(&a->connection.in, &m, deadline, stamp)) < 0) return 0 ;
+        if (m.len != 1 || m.nfds)
+        {
+          unixmessage_drop(&m) ;
+          return (errno = EPROTO, 0) ;
+        }
+        if (!error_isagain((unsigned char)m.s[0]) && i < trips-1)
+          return errno = m.s[0] ? (unsigned char)m.s[0] : EPROTO, 0 ;
+        if (i == trips - 1 && m.s[0])
+          return errno = error_isagain((unsigned char)m.s[0]) ? EPROTO : (unsigned char)m.s[0], 0 ;
       }
-      if (!error_isagain((unsigned char)m.s[0]) && i < trips-1)
-        return errno = m.s[0] ? (unsigned char)m.s[0] : EPROTO, 0 ;
-      if (i == trips - 1 && m.s[0])
-        return errno = error_isagain((unsigned char)m.s[0]) ? EPROTO : (unsigned char)m.s[0], 0 ;
     }
   }
   return 1 ;