summaryrefslogtreecommitdiff
path: root/src/libunixonacid
diff options
context:
space:
mode:
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/textclient_command.c15
-rw-r--r--src/libunixonacid/textclient_commandv.c15
-rw-r--r--src/libunixonacid/textclient_end.c22
-rw-r--r--src/libunixonacid/textclient_server_init.c12
-rw-r--r--src/libunixonacid/textclient_server_init_frompipe.c30
-rw-r--r--src/libunixonacid/textclient_server_init_fromsocket.c86
-rw-r--r--src/libunixonacid/textclient_start.c116
-rw-r--r--src/libunixonacid/textclient_startf.c34
-rw-r--r--src/libunixonacid/textmessage_put.c2
-rw-r--r--src/libunixonacid/textmessage_putv.c2
-rw-r--r--src/libunixonacid/textmessage_receiver_0.c3
11 files changed, 333 insertions, 4 deletions
diff --git a/src/libunixonacid/textclient_command.c b/src/libunixonacid/textclient_command.c
new file mode 100644
index 0000000..2e72a4c
--- /dev/null
+++ b/src/libunixonacid/textclient_command.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <errno.h>
+#include <skalibs/error.h>
+#include <skalibs/textclient.h>
+
+int textclient_command (textclient_t *a, char const *s, size_t len, tain_t const *deadline, tain_t *stamp)
+{
+ struct iovec ans ;
+ if (!textclient_exchange(a, s, len, &ans, deadline, stamp)) return 0 ;
+ if (ans.iov_len != 1) return (errno = EPROTO, 0) ;
+ if (*(char *)ans.iov_base) return (errno = *(char *)ans.iov_base, 0) ;
+ return 1 ;
+}
diff --git a/src/libunixonacid/textclient_commandv.c b/src/libunixonacid/textclient_commandv.c
new file mode 100644
index 0000000..5bcd411
--- /dev/null
+++ b/src/libunixonacid/textclient_commandv.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <errno.h>
+#include <skalibs/error.h>
+#include <skalibs/textclient.h>
+
+int textclient_commandv (textclient_t *a, struct iovec const *v, unsigned int n, tain_t const *deadline, tain_t *stamp)
+{
+ struct iovec ans ;
+ if (!textclient_exchangev(a, v, n, &ans, deadline, stamp)) return 0 ;
+ if (ans.iov_len != 1) return (errno = EPROTO, 0) ;
+ if (*(char *)ans.iov_base) return (errno = *(char *)ans.iov_base, 0) ;
+ return 1 ;
+}
diff --git a/src/libunixonacid/textclient_end.c b/src/libunixonacid/textclient_end.c
new file mode 100644
index 0000000..58fc930
--- /dev/null
+++ b/src/libunixonacid/textclient_end.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <skalibs/djbunix.h>
+#include <skalibs/textmessage.h>
+#include <skalibs/textclient.h>
+
+void textclient_end (textclient_t *a)
+{
+ fd_close(textmessage_sender_fd(&a->syncout)) ;
+ if (textmessage_receiver_fd(&a->syncin) != textmessage_sender_fd(&a->syncout))
+ fd_close(textmessage_receiver_fd(&a->syncin)) ;
+ fd_close(textmessage_receiver_fd(&a->asyncin)) ;
+ textmessage_sender_free(&a->syncout) ;
+ textmessage_receiver_free(&a->syncin) ;
+ textmessage_receiver_free(&a->asyncin) ;
+ if (a->pid && a->options & TEXTCLIENT_OPTION_WAITPID)
+ {
+ int wstat ;
+ waitpid_nointr(a->pid, &wstat, 0) ;
+ }
+ *a = textclient_zero ;
+}
diff --git a/src/libunixonacid/textclient_server_init.c b/src/libunixonacid/textclient_server_init.c
new file mode 100644
index 0000000..27d3a20
--- /dev/null
+++ b/src/libunixonacid/textclient_server_init.c
@@ -0,0 +1,12 @@
+/* ISC license. */
+
+#include <stdlib.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/textclient.h>
+
+int textclient_server_init (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)
+{
+ return getenv(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ?
+ textclient_server_init_frompipe(in, syncout, asyncout, before, beforelen, after, afterlen, deadline, stamp) :
+ textclient_server_init_fromsocket(in, syncout, asyncout, before, beforelen, after, afterlen, deadline, stamp) ;
+}
diff --git a/src/libunixonacid/textclient_server_init_frompipe.c b/src/libunixonacid/textclient_server_init_frompipe.c
new file mode 100644
index 0000000..fb85b44
--- /dev/null
+++ b/src/libunixonacid/textclient_server_init_frompipe.c
@@ -0,0 +1,30 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <skalibs/types.h>
+#include <skalibs/error.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/textmessage.h>
+#include <skalibs/textclient.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)
+{
+ struct iovec v ;
+ unsigned int asyncfd ;
+ char *x = getenv(SKALIBS_CHILD_SPAWN_FDS_ENVVAR) ;
+ if (!x
+ || !uint0_scan(x, &asyncfd)
+ || asyncfd == textmessage_sender_fd(syncout)
+ || asyncfd == textmessage_receiver_fd(in)) return (errno = EPROTO, 0) ;
+ if (sanitize_read(textmessage_timed_receive(in, &v, deadline, stamp)) <= 0) return 0 ;
+ if (v.iov_len != beforelen || memcmp(v.iov_base, before, beforelen)) return (errno = EPROTO, 0) ;
+ if (fcntl(asyncfd, F_GETFD) < 0) return 0 ;
+ if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) return 0 ;
+ textmessage_sender_init(asyncout, asyncfd) ;
+ if (!textmessage_timed_send(asyncout, after, afterlen, deadline, stamp)) return 0 ;
+ return 1 ;
+}
diff --git a/src/libunixonacid/textclient_server_init_fromsocket.c b/src/libunixonacid/textclient_server_init_fromsocket.c
new file mode 100644
index 0000000..5b71463
--- /dev/null
+++ b/src/libunixonacid/textclient_server_init_fromsocket.c
@@ -0,0 +1,86 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+#include <skalibs/nonposix.h>
+#include <sys/uio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <skalibs/types.h>
+#include <skalibs/error.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/unix-timed.h>
+#include <skalibs/textmessage.h>
+#include <skalibs/textclient.h>
+
+#ifndef MSG_NOSIGNAL
+#define MSG_NOSIGNAL 0
+#endif
+
+union aligner_u
+{
+ struct cmsghdr cmsghdr ;
+ int i ;
+} ;
+
+static int getfd (void *p)
+{
+ return ((int *)p)[0] ;
+}
+
+static int one (void *p)
+{
+ (void)p ;
+ return 1 ;
+}
+
+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 ;
+#ifndef SKALIBS_HASANCILAUTOCLOSE
+ fd_close(fd[1]) ;
+#endif
+ return 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)
+{
+ int fd[3] = { textmessage_sender_fd(syncout) } ;
+ struct iovec v ;
+ if (sanitize_read(textmessage_timed_receive(in, &v, deadline, stamp)) <= 0) return 0 ;
+ if (v.iov_len != beforelen || memcmp(v.iov_base, before, beforelen)) return (errno = EPROTO, 0) ;
+ if (pipenbcoe(fd+1) < 0) return 0 ;
+ if (!timed_flush(fd, &getfd, &one, &sendit, deadline, stamp)) goto err ;
+ if (!textmessage_timed_send(syncout, after, afterlen, deadline, stamp)) goto err ;
+ textmessage_sender_init(asyncout, fd[2]) ;
+ if (!textmessage_timed_send(asyncout, after, afterlen, deadline, stamp)) goto err ;
+ return 1 ;
+
+ err:
+ fd_close(fd[2]) ;
+ fd_close(fd[1]) ;
+ return 0 ;
+}
diff --git a/src/libunixonacid/textclient_start.c b/src/libunixonacid/textclient_start.c
new file mode 100644
index 0000000..8cb7bcd
--- /dev/null
+++ b/src/libunixonacid/textclient_start.c
@@ -0,0 +1,116 @@
+/* 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/error.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/webipc.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/unix-timed.h>
+#include <skalibs/textmessage.h>
+#include <skalibs/textclient.h>
+
+union aligner_u
+{
+ struct cmsghdr cmsghdr ;
+ int i ;
+} ;
+
+static int getfd (void *p)
+{
+ return ((int *)p)[0] ;
+}
+
+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) ;
+ 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 ;
+ int fd[2] ;
+ ssize_t r ;
+ fd[0] = ipc_stream_nbcoe() ;
+ if (fd[0] < 0) return 0 ;
+ if (!ipc_timed_connect(fd[0], path, deadline, stamp)) goto err ;
+ textmessage_sender_init(&a->syncout, fd[0]) ;
+ if (!textmessage_timed_send(&a->syncout, before, beforelen, deadline, stamp)) goto ferr ;
+ textmessage_receiver_init(&a->syncin, fd[0], a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
+ r = timed_get(fd, &getfd, &get, deadline, stamp) ;
+ if (!r) errno = EPIPE ;
+ if (r <= 0) goto aerr ;
+ if (sanitize_read(textmessage_timed_receive(&a->syncin, &v, deadline, stamp)) <= 0) goto perr ;
+ if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) { errno = EPROTO ; goto perr ; }
+ textmessage_receiver_init(&a->asyncin, fd[1], a->asyncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
+ if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto serr ;
+ if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto berr ;
+ a->pid = 0 ;
+ a->options = options & ~TEXTCLIENT_OPTION_WAITPID ;
+ return 1 ;
+
+ berr:
+ errno = EPROTO ;
+ serr:
+ textmessage_receiver_free(&a->asyncin) ;
+ perr:
+ fd_close(fd[1]) ;
+ aerr:
+ textmessage_receiver_free(&a->syncin) ;
+ ferr:
+ textmessage_sender_free(&a->syncout) ;
+ err:
+ fd_close(fd[0]) ;
+ return 0 ;
+}
diff --git a/src/libunixonacid/textclient_startf.c b/src/libunixonacid/textclient_startf.c
new file mode 100644
index 0000000..fd724cd
--- /dev/null
+++ b/src/libunixonacid/textclient_startf.c
@@ -0,0 +1,34 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <string.h>
+#include <errno.h>
+#include <skalibs/error.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/textmessage.h>
+#include <skalibs/textclient.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)
+{
+ struct iovec v ;
+ int fd[3] ;
+ pid_t pid = child_spawn(argv[0], argv, envp, fd, 3) ;
+ if (!pid) return 0 ;
+ textmessage_receiver_init(&a->syncin, fd[0], a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
+ textmessage_receiver_init(&a->asyncin, fd[2], a->asyncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
+ textmessage_sender_init(&a->syncout, fd[1]) ;
+ a->pid = pid ;
+ a->options = options ;
+ if (!textclient_exchange(a, before, beforelen, &v, deadline, stamp)) goto err ;
+ if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ;
+ if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto err ;
+ if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ;
+ return 1 ;
+
+ errproto:
+ errno = EPROTO ;
+ err:
+ textclient_end(a) ;
+ return 0 ;
+}
diff --git a/src/libunixonacid/textmessage_put.c b/src/libunixonacid/textmessage_put.c
index a6eb6ca..5b7d944 100644
--- a/src/libunixonacid/textmessage_put.c
+++ b/src/libunixonacid/textmessage_put.c
@@ -15,7 +15,7 @@ int textmessage_put (textmessage_sender_t *ts, char const *s, size_t len)
{ .iov_base = pack, .iov_len = 4 },
{ .iov_base = (char *)s, .iov_len = len }
} ;
- if (len > UINT32_MAX) return (errno = EINVAL, 0) ;
+ if (len > TEXTMESSAGE_MAXLEN) return (errno = EINVAL, 0) ;
uint32_pack_big(pack, (uint32_t)len) ;
return bufalloc_putv(&ts->out, v, 2) ;
}
diff --git a/src/libunixonacid/textmessage_putv.c b/src/libunixonacid/textmessage_putv.c
index c144505..759ce84 100644
--- a/src/libunixonacid/textmessage_putv.c
+++ b/src/libunixonacid/textmessage_putv.c
@@ -13,7 +13,7 @@ int textmessage_putv (textmessage_sender_t *ts, struct iovec const *v, unsigned
size_t len = siovec_len(v, n) ;
char pack[4] ;
struct iovec vv[n+1] ;
- if (len > UINT32_MAX) return (errno = EINVAL, 0) ;
+ if (len > TEXTMESSAGE_MAXLEN) return (errno = EINVAL, 0) ;
vv[0].iov_base = pack ;
vv[0].iov_len = 4 ;
for (unsigned int i = 0 ; i < n ; i++) vv[i+1] = v[i] ;
diff --git a/src/libunixonacid/textmessage_receiver_0.c b/src/libunixonacid/textmessage_receiver_0.c
index f21b396..ab883de 100644
--- a/src/libunixonacid/textmessage_receiver_0.c
+++ b/src/libunixonacid/textmessage_receiver_0.c
@@ -2,9 +2,8 @@
/* MT-unsafe */
-#include <stdint.h>
#include <skalibs/buffer.h>
#include <skalibs/textmessage.h>
static char buf[BUFFER_INSIZE] ;
-textmessage_receiver_t textmessage_receiver_0_ = TEXTMESSAGE_RECEIVER_INIT(0, buf, BUFFER_INSIZE, UINT32_MAX) ;
+textmessage_receiver_t textmessage_receiver_0_ = TEXTMESSAGE_RECEIVER_INIT(0, buf, BUFFER_INSIZE, TEXTMESSAGE_MAXLEN) ;