From 3c1ff1523587881324fc540fece06412c29c66de Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 22 Dec 2017 14:46:04 +0000 Subject: Add textclient, fix some textmessage issues --- .../textclient_server_init_fromsocket.c | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/libunixonacid/textclient_server_init_fromsocket.c (limited to 'src/libunixonacid/textclient_server_init_fromsocket.c') 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 ; +} -- cgit v1.2.3