about summary refs log tree commit diff
path: root/src/libunixonacid
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-10-26 14:21:53 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-10-26 14:21:53 +0000
commit7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac (patch)
tree20917d9312d38cf57e04f17cec119d89873b57b3 /src/libunixonacid
parentad90bc1107ef9a76f4ca530cc41e86d4f352e3d9 (diff)
downloadskalibs-7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac.tar.gz
skalibs-7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac.tar.xz
skalibs-7530e8cdd506ecec1f4ad3bbd55f94de5a6d63ac.zip
Separate and expose ancil_recv_fd/ancil_send_fd
 The goal is to make late channel creation easy, as opposed to
textclient which always creates a new channel at start time.

 This commit also moves posixishard.h inclusions as late as possible.
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/ancil_recv_fd.c69
-rw-r--r--src/libunixonacid/ancil_send_fd.c45
-rw-r--r--src/libunixonacid/ipc_timed_send.c2
-rw-r--r--src/libunixonacid/ipc_timed_sendv.c2
-rw-r--r--src/libunixonacid/skaclient_default_cb.c2
-rw-r--r--src/libunixonacid/skaclient_server_ack.c2
-rw-r--r--src/libunixonacid/skaclient_start_cb.c2
-rw-r--r--src/libunixonacid/textclient_command.c2
-rw-r--r--src/libunixonacid/textclient_commandv.c2
-rw-r--r--src/libunixonacid/textclient_server_init_frompipe.c2
-rw-r--r--src/libunixonacid/textclient_server_init_fromsocket.c41
-rw-r--r--src/libunixonacid/textclient_start.c65
-rw-r--r--src/libunixonacid/textclient_startf.c2
-rw-r--r--src/libunixonacid/unixmessage_put.c2
-rw-r--r--src/libunixonacid/unixmessage_receive.c2
-rw-r--r--src/libunixonacid/unixmessage_sender_flush.c2
16 files changed, 137 insertions, 107 deletions
diff --git a/src/libunixonacid/ancil_recv_fd.c b/src/libunixonacid/ancil_recv_fd.c
new file mode 100644
index 0000000..70f2c4f
--- /dev/null
+++ b/src/libunixonacid/ancil_recv_fd.c
@@ -0,0 +1,69 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+#include <skalibs/nonposix.h>
+
+#include <errno.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/ancil.h>
+#include <skalibs/posixishard.h>
+
+union aligner_u
+{
+  struct cmsghdr cmsghdr ;
+  int i ;
+} ;
+
+int ancil_recv_fd (int sock, char expected_ch)
+{
+  static int const awesomeflags =
+#ifdef SKALIBS_HASMSGDONTWAIT
+    MSG_DONTWAIT
+#else
+    0
+#endif
+    |
+#ifdef SKALIBS_HASCMSGCLOEXEC
+    MSG_CMSG_CLOEXEC
+#else
+    0
+#endif
+    ;
+  struct cmsghdr *c ;
+  ssize_t r ;
+  char ch ;
+  struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
+  union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
+  struct msghdr msghdr =
+  {
+    .msg_name = 0,
+    .msg_namelen = 0,
+    .msg_iov = &v,
+    .msg_iovlen = 1,
+    .msg_flags = 0,
+    .msg_control = ancilbuf,
+    .msg_controllen = CMSG_SPACE(sizeof(int))
+  } ;
+  do r = recvmsg(sock, &msghdr, awesomeflags) ;
+  while (r < 0 && errno == EINTR) ;
+  if (r < 0) return r ;
+  if (!r) return (errno = EPIPE, -1) ;
+  c = CMSG_FIRSTHDR(&msghdr) ;
+  if (ch != expected_ch
+   || !c
+   || c->cmsg_level != SOL_SOCKET
+   || c->cmsg_type != SCM_RIGHTS
+   || (size_t)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) != sizeof(int)) return (errno = EPROTO, -1) ;
+#ifndef SKALIBS_HASCMSGCLOEXEC
+  if (coe(*(int *)CMSG_DATA(c)) < 0)
+  {
+    fd_close(*(int *)CMSG_DATA(c)) ;
+    return -1 ;
+  }
+#endif
+  return *(int *)CMSG_DATA(c) ;
+}
diff --git a/src/libunixonacid/ancil_send_fd.c b/src/libunixonacid/ancil_send_fd.c
new file mode 100644
index 0000000..98366c7
--- /dev/null
+++ b/src/libunixonacid/ancil_send_fd.c
@@ -0,0 +1,45 @@
+/* ISC license. */
+
+#include <skalibs/nonposix.h>
+
+#include <errno.h>
+#include <string.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/ancil.h>
+#include <skalibs/posixishard.h>
+
+union aligner_u
+{
+  struct cmsghdr cmsghdr ;
+  int i ;
+} ;
+
+int ancil_send_fd (int sock, int fd, char ch)
+{
+  ssize_t r ;
+  struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
+  union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
+  struct msghdr hdr =
+  {
+    .msg_name = 0,
+    .msg_namelen = 0,
+    .msg_iov = &v,
+    .msg_iovlen = 1,
+    .msg_control = ancilbuf,
+    .msg_controllen = CMSG_SPACE(sizeof(int))
+  } ;
+  struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ;
+  memset(hdr.msg_control, 0, hdr.msg_controllen) ;
+  c->cmsg_level = SOL_SOCKET ;
+  c->cmsg_type = SCM_RIGHTS ;
+  c->cmsg_len = CMSG_LEN(sizeof(int)) ;
+  *(int *)CMSG_DATA(c) = fd ;
+  do r = sendmsg(sock, &hdr, MSG_NOSIGNAL) ;
+  while (r < 0 && errno == EINTR) ;
+  if (r <= 0) return 0 ;
+  fd_close(fd) ;
+  return 1 ;
+}
diff --git a/src/libunixonacid/ipc_timed_send.c b/src/libunixonacid/ipc_timed_send.c
index e1e75a4..2f6ce88 100644
--- a/src/libunixonacid/ipc_timed_send.c
+++ b/src/libunixonacid/ipc_timed_send.c
@@ -5,10 +5,10 @@
 #include <sys/socket.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/error.h>
 #include <skalibs/iopause.h>
 #include <skalibs/unix-timed.h>
+#include <skalibs/posixishard.h>
 
 int ipc_timed_send (int fd, char const *s, size_t len, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/ipc_timed_sendv.c b/src/libunixonacid/ipc_timed_sendv.c
index 0fe5b1e..1bdae5b 100644
--- a/src/libunixonacid/ipc_timed_sendv.c
+++ b/src/libunixonacid/ipc_timed_sendv.c
@@ -5,11 +5,11 @@
 #include <sys/socket.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/error.h>
 #include <skalibs/iopause.h>
 #include <skalibs/siovec.h>
 #include <skalibs/unix-timed.h>
+#include <skalibs/posixishard.h>
 
 int ipc_timed_sendv (int fd, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/skaclient_default_cb.c b/src/libunixonacid/skaclient_default_cb.c
index 5bf0268..447d796 100644
--- a/src/libunixonacid/skaclient_default_cb.c
+++ b/src/libunixonacid/skaclient_default_cb.c
@@ -2,9 +2,9 @@
 
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/skaclient.h>
 #include <skalibs/unixmessage.h>
+#include <skalibs/posixishard.h>
 
 int skaclient_default_cb (unixmessage_t const *m, void *p)
 {
diff --git a/src/libunixonacid/skaclient_server_ack.c b/src/libunixonacid/skaclient_server_ack.c
index b55eb07..9bad7cc 100644
--- a/src/libunixonacid/skaclient_server_ack.c
+++ b/src/libunixonacid/skaclient_server_ack.c
@@ -3,11 +3,11 @@
 #include <errno.h>
 #include <string.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/skaclient.h>
 #include <skalibs/unixmessage.h>
 #include <skalibs/webipc.h>
+#include <skalibs/posixishard.h>
 
 int skaclient_server_ack (unixmessage_t const *clientmsg, unixmessage_sender_t *out, unixmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen)
 {
diff --git a/src/libunixonacid/skaclient_start_cb.c b/src/libunixonacid/skaclient_start_cb.c
index 0c4ef49..f385b6b 100644
--- a/src/libunixonacid/skaclient_start_cb.c
+++ b/src/libunixonacid/skaclient_start_cb.c
@@ -3,9 +3,9 @@
 #include <errno.h>
 #include <string.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/unixmessage.h>
 #include <skalibs/skaclient.h>
+#include <skalibs/posixishard.h>
 #include "skaclient-internal.h"
 
 int skaclient_start_cb (unixmessage_t const *m, skaclient_cbdata_t *blah)
diff --git a/src/libunixonacid/textclient_command.c b/src/libunixonacid/textclient_command.c
index 56c7122..a0015ac 100644
--- a/src/libunixonacid/textclient_command.c
+++ b/src/libunixonacid/textclient_command.c
@@ -3,8 +3,8 @@
 #include <sys/uio.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/textclient.h>
+#include <skalibs/posixishard.h>
 
 int textclient_command (textclient_t *a, char const *s, size_t len, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/textclient_commandv.c b/src/libunixonacid/textclient_commandv.c
index ec61498..267e584 100644
--- a/src/libunixonacid/textclient_commandv.c
+++ b/src/libunixonacid/textclient_commandv.c
@@ -3,8 +3,8 @@
 #include <sys/uio.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/textclient.h>
+#include <skalibs/posixishard.h>
 
 int textclient_commandv (textclient_t *a, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/textclient_server_init_frompipe.c b/src/libunixonacid/textclient_server_init_frompipe.c
index e606eeb..9cd549e 100644
--- a/src/libunixonacid/textclient_server_init_frompipe.c
+++ b/src/libunixonacid/textclient_server_init_frompipe.c
@@ -5,12 +5,12 @@
 #include <fcntl.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/types.h>
 #include <skalibs/allreadwrite.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
+#include <skalibs/posixishard.h>
 
 int textclient_server_init_frompipe (textmessage_receiver_t *in, textmessage_sender_t *syncout, textmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/textclient_server_init_fromsocket.c b/src/libunixonacid/textclient_server_init_fromsocket.c
index b7f4e84..4964524 100644
--- a/src/libunixonacid/textclient_server_init_fromsocket.c
+++ b/src/libunixonacid/textclient_server_init_fromsocket.c
@@ -1,25 +1,15 @@
 /* ISC license. */
 
-#include <skalibs/nonposix.h>
-
-#include <sys/uio.h>
 #include <errno.h>
 #include <string.h>
-#include <sys/socket.h>
+#include <sys/uio.h>
 
-#include <skalibs/posixishard.h>
-#include <skalibs/types.h>
-#include <skalibs/allreadwrite.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/unix-timed.h>
+#include <skalibs/ancil.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
-
-union aligner_u
-{
-  struct cmsghdr cmsghdr ;
-  int i ;
-} ;
+#include <skalibs/posixishard.h>
 
 static int getfd (void *p)
 {
@@ -35,30 +25,7 @@ static int one (void *p)
 static int sendit (void *p)
 {
   int *fd = p ;
-  union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
-  ssize_t r ;
-  char ch = '|' ;
-  struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
-  struct msghdr hdr =
-  {
-    .msg_name = 0,
-    .msg_namelen = 0,
-    .msg_iov = &v,
-    .msg_iovlen = 1,
-    .msg_control = ancilbuf,
-    .msg_controllen = CMSG_SPACE(sizeof(int))
-  } ;
-  struct cmsghdr *c = CMSG_FIRSTHDR(&hdr) ;
-  memset(hdr.msg_control, 0, hdr.msg_controllen) ;
-  c->cmsg_level = SOL_SOCKET ;
-  c->cmsg_type = SCM_RIGHTS ;
-  c->cmsg_len = CMSG_LEN(sizeof(int)) ;
-  *(int *)CMSG_DATA(c) = fd[1] ;
-  do r = sendmsg(fd[0], &hdr, MSG_NOSIGNAL) ;
-  while (r < 0 && errno == EINTR) ;
-  if (r <= 0) return 0 ;
-  fd_close(fd[1]) ;
-  return 1 ;
+  return ancil_send_fd(fd[0], fd[1], '|') ;
 }
 
 int textclient_server_init_fromsocket (textmessage_receiver_t *in, textmessage_sender_t *syncout, textmessage_sender_t *asyncout, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp)
diff --git a/src/libunixonacid/textclient_start.c b/src/libunixonacid/textclient_start.c
index 8eb8b13..3074ae3 100644
--- a/src/libunixonacid/textclient_start.c
+++ b/src/libunixonacid/textclient_start.c
@@ -1,26 +1,18 @@
 /* ISC license. */
 
-#include <skalibs/sysdeps.h>
-#include <skalibs/nonposix.h>
-
 #include <sys/uio.h>
 #include <string.h>
 #include <errno.h>
-#include <sys/socket.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/allreadwrite.h>
+#include <skalibs/error.h>
 #include <skalibs/webipc.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/unix-timed.h>
+#include <skalibs/ancil.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
-
-union aligner_u
-{
-  struct cmsghdr cmsghdr ;
-  int i ;
-} ;
+#include <skalibs/posixishard.h>
 
 static int getfd (void *p)
 {
@@ -29,56 +21,13 @@ static int getfd (void *p)
 
 static ssize_t get (void *p)
 {
-  static int const awesomeflags =
-#ifdef SKALIBS_HASMSGDONTWAIT
-    MSG_DONTWAIT
-#else
-    0
-#endif
-    |
-#ifdef SKALIBS_HASCMSGCLOEXEC
-    MSG_CMSG_CLOEXEC
-#else
-    0
-#endif
-    ;
-  struct cmsghdr *c ;
   int *fd = p ;
-  ssize_t r ;
-  union aligner_u ancilbuf[1 + (CMSG_SPACE(sizeof(int)) - 1) / sizeof(union aligner_u)] ;
-  char ch ;
-  struct iovec v = { .iov_base = &ch, .iov_len = 1 } ;
-  struct msghdr msghdr =
-  {
-    .msg_name = 0,
-    .msg_namelen = 0,
-    .msg_iov = &v,
-    .msg_iovlen = 1,
-    .msg_flags = 0,
-    .msg_control = ancilbuf,
-    .msg_controllen = CMSG_SPACE(sizeof(int))
-  } ;
-  do r = recvmsg(fd[0], &msghdr, awesomeflags) ;
-  while (r < 0 && errno == EINTR) ;
-  if (r <= 0) return sanitize_read(r) ;
-  c = CMSG_FIRSTHDR(&msghdr) ;
-  if (ch != '|'
-   || !c
-   || c->cmsg_level != SOL_SOCKET
-   || c->cmsg_type != SCM_RIGHTS
-   || (size_t)(c->cmsg_len - (CMSG_DATA(c) - (unsigned char *)c)) != sizeof(int)) return (errno = EPROTO, -1) ;
-#ifndef SKALIBS_HASCMSGCLOEXEC
-  if (coe(*(int *)CMSG_DATA(c)) < 0)
-  {
-    fd_close(*(int *)CMSG_DATA(c)) ;
-    return -1 ;
-  }
-#endif
-  fd[1] = *(int *)CMSG_DATA(c) ;
+  int r = ancil_recv_fd(fd[0], '|') ;
+  if (r < 0) return error_isagain(errno) ? (errno = 0, 0) : r ;
+  fd[1] = r ;
   return 1 ;
 }
-
-
+  
 int textclient_start (textclient_t *a, char const *path, uint32_t options, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp)
 {
   struct iovec v ;
diff --git a/src/libunixonacid/textclient_startf.c b/src/libunixonacid/textclient_startf.c
index 12c2b20..20ebe33 100644
--- a/src/libunixonacid/textclient_startf.c
+++ b/src/libunixonacid/textclient_startf.c
@@ -4,11 +4,11 @@
 #include <string.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/allreadwrite.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/textmessage.h>
 #include <skalibs/textclient.h>
+#include <skalibs/posixishard.h>
 
 int textclient_startf (textclient_t *a, char const *const *argv, char const *const *envp, uint32_t options, char const *before, size_t beforelen, char const *after, size_t afterlen, tain_t const *deadline, tain_t *stamp)
 {
diff --git a/src/libunixonacid/unixmessage_put.c b/src/libunixonacid/unixmessage_put.c
index a2ac302..9ce103d 100644
--- a/src/libunixonacid/unixmessage_put.c
+++ b/src/libunixonacid/unixmessage_put.c
@@ -3,13 +3,13 @@
 #include <string.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/bitarray.h>
 #include <skalibs/disize.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/genalloc.h>
 #include <skalibs/siovec.h>
 #include <skalibs/unixmessage.h>
+#include <skalibs/posixishard.h>
 
 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)
 {
diff --git a/src/libunixonacid/unixmessage_receive.c b/src/libunixonacid/unixmessage_receive.c
index 811baae..b4171f0 100644
--- a/src/libunixonacid/unixmessage_receive.c
+++ b/src/libunixonacid/unixmessage_receive.c
@@ -7,7 +7,6 @@
 #include <sys/socket.h>
 #include <sys/uio.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/uint16.h>
 #include <skalibs/uint32.h>
 #include <skalibs/cbuffer.h>
@@ -15,6 +14,7 @@
 #include <skalibs/allreadwrite.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/unixmessage.h>
+#include <skalibs/posixishard.h>
 
 union aligner_u
 {
diff --git a/src/libunixonacid/unixmessage_sender_flush.c b/src/libunixonacid/unixmessage_sender_flush.c
index 3f096f0..96f9112 100644
--- a/src/libunixonacid/unixmessage_sender_flush.c
+++ b/src/libunixonacid/unixmessage_sender_flush.c
@@ -9,7 +9,6 @@
 #include <unistd.h>
 #include <errno.h>
 
-#include <skalibs/posixishard.h>
 #include <skalibs/uint16.h>
 #include <skalibs/uint32.h>
 #include <skalibs/disize.h>
@@ -17,6 +16,7 @@
 #include <skalibs/genalloc.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/unixmessage.h>
+#include <skalibs/posixishard.h>
 
 union aligner_u
 {