diff options
-rw-r--r-- | package/deps.mak | 12 | ||||
-rw-r--r-- | src/include/skalibs/djbunix.h | 2 | ||||
-rw-r--r-- | src/include/skalibs/textclient.h | 112 | ||||
-rw-r--r-- | src/include/skalibs/textmessage.h | 11 | ||||
-rw-r--r-- | src/include/skalibs/unixonacid.h | 1 | ||||
-rw-r--r-- | src/libunixonacid/textclient_command.c | 15 | ||||
-rw-r--r-- | src/libunixonacid/textclient_commandv.c | 15 | ||||
-rw-r--r-- | src/libunixonacid/textclient_end.c | 22 | ||||
-rw-r--r-- | src/libunixonacid/textclient_server_init.c | 12 | ||||
-rw-r--r-- | src/libunixonacid/textclient_server_init_frompipe.c | 30 | ||||
-rw-r--r-- | src/libunixonacid/textclient_server_init_fromsocket.c | 86 | ||||
-rw-r--r-- | src/libunixonacid/textclient_start.c | 116 | ||||
-rw-r--r-- | src/libunixonacid/textclient_startf.c | 34 | ||||
-rw-r--r-- | src/libunixonacid/textmessage_put.c | 2 | ||||
-rw-r--r-- | src/libunixonacid/textmessage_putv.c | 2 | ||||
-rw-r--r-- | src/libunixonacid/textmessage_receiver_0.c | 3 |
16 files changed, 464 insertions, 11 deletions
diff --git a/package/deps.mak b/package/deps.mak index 25adeff..ee8052f 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -48,13 +48,13 @@ src/include/skalibs/stddjb.h: src/include/skalibs/alarm.h src/include/skalibs/al src/include/skalibs/strerr.h: src/include/skalibs/gccattributes.h src/include/skalibs/strerr2.h: src/include/skalibs/strerr.h src/include/skalibs/tai.h: src/include/skalibs/gccattributes.h src/include/skalibs/uint64.h -src/include/skalibs/textclient.h: src/include/skalibs/tai.h src/include/skalibs/textmessage.h +src/include/skalibs/textclient.h: src/include/skalibs/allreadwrite.h src/include/skalibs/tai.h src/include/skalibs/textmessage.h src/include/skalibs/textmessage.h: src/include/skalibs/allreadwrite.h src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/gccattributes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/unix-timed.h: src/include/skalibs/bufalloc.h src/include/skalibs/buffer.h src/include/skalibs/functypes.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h src/include/skalibs/unix-transactional.h: src/include/skalibs/stralloc.h src/include/skalibs/uint64.h src/include/skalibs/unixconnection.h: src/include/skalibs/unixmessage.h src/include/skalibs/unixmessage.h: src/include/skalibs/buffer.h src/include/skalibs/cbuffer.h src/include/skalibs/gccattributes.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h src/include/skalibs/tai.h -src/include/skalibs/unixonacid.h: src/include/skalibs/kolbak.h src/include/skalibs/skaclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/include/skalibs/unix-transactional.h src/include/skalibs/unixconnection.h src/include/skalibs/unixmessage.h +src/include/skalibs/unixonacid.h: src/include/skalibs/kolbak.h src/include/skalibs/skaclient.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/include/skalibs/unix-transactional.h src/include/skalibs/unixconnection.h src/include/skalibs/unixmessage.h src/include/skalibs/webipc.h: src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h src/include/skalibs/tai.h src/libdatastruct/avlnode-internal.h: src/include/skalibs/avlnode.h src/librandom/random-internal.h: src/include/skalibs/surf.h @@ -758,6 +758,14 @@ src/libunixonacid/skaclient_startf_async.o src/libunixonacid/skaclient_startf_as src/libunixonacid/skaclient_syncify.o src/libunixonacid/skaclient_syncify.lo: src/libunixonacid/skaclient_syncify.c src/include/skalibs/skaclient.h src/libunixonacid/skaclient_zero.o src/libunixonacid/skaclient_zero.lo: src/libunixonacid/skaclient_zero.c src/include/skalibs/skaclient.h src/libunixonacid/stat_at.o src/libunixonacid/stat_at.lo: src/libunixonacid/stat_at.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/unix-transactional.h +src/libunixonacid/textclient_command.o src/libunixonacid/textclient_command.lo: src/libunixonacid/textclient_command.c src/include/skalibs/error.h src/include/skalibs/textclient.h +src/libunixonacid/textclient_commandv.o src/libunixonacid/textclient_commandv.lo: src/libunixonacid/textclient_commandv.c src/include/skalibs/error.h src/include/skalibs/textclient.h +src/libunixonacid/textclient_end.o src/libunixonacid/textclient_end.lo: src/libunixonacid/textclient_end.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h +src/libunixonacid/textclient_server_init.o src/libunixonacid/textclient_server_init.lo: src/libunixonacid/textclient_server_init.c src/include/skalibs/djbunix.h src/include/skalibs/textclient.h +src/libunixonacid/textclient_server_init_frompipe.o src/libunixonacid/textclient_server_init_frompipe.lo: src/libunixonacid/textclient_server_init_frompipe.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/types.h +src/libunixonacid/textclient_server_init_fromsocket.o src/libunixonacid/textclient_server_init_fromsocket.lo: src/libunixonacid/textclient_server_init_fromsocket.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/types.h src/include/skalibs/unix-timed.h +src/libunixonacid/textclient_start.o src/libunixonacid/textclient_start.lo: src/libunixonacid/textclient_start.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/include/skalibs/webipc.h +src/libunixonacid/textclient_startf.o src/libunixonacid/textclient_startf.lo: src/libunixonacid/textclient_startf.c src/include/skalibs/allreadwrite.h src/include/skalibs/djbunix.h src/include/skalibs/error.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/libunixonacid/textmessage_handle.o src/libunixonacid/textmessage_handle.lo: src/libunixonacid/textmessage_handle.c src/include/skalibs/textmessage.h src/libunixonacid/textmessage_put.o src/libunixonacid/textmessage_put.lo: src/libunixonacid/textmessage_put.c src/include/skalibs/bufalloc.h src/include/skalibs/textmessage.h src/include/skalibs/uint32.h src/libunixonacid/textmessage_putv.o src/libunixonacid/textmessage_putv.lo: src/libunixonacid/textmessage_putv.c src/include/skalibs/bufalloc.h src/include/skalibs/siovec.h src/include/skalibs/textmessage.h src/include/skalibs/uint32.h diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h index b095c82..00d8f00 100644 --- a/src/include/skalibs/djbunix.h +++ b/src/include/skalibs/djbunix.h @@ -190,6 +190,8 @@ extern pid_t child_spawn2 (char const *, char const *const *, char const *const * if n>=2, parent reads on even and writes on odd. */ +#define SKALIBS_CHILD_SPAWN_FDS_ENVVAR "SKALIBS_CHILD_SPAWN_FDS" + extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, unsigned int) ; #endif diff --git a/src/include/skalibs/textclient.h b/src/include/skalibs/textclient.h new file mode 100644 index 0000000..17d4fff --- /dev/null +++ b/src/include/skalibs/textclient.h @@ -0,0 +1,112 @@ +/* ISC license. */ + +#ifndef SKALIBS_TEXTCLIENT_H +#define SKALIBS_TEXTCLIENT_H + +#include <sys/types.h> +#include <sys/uio.h> +#include <stdint.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/tai.h> +#include <skalibs/textmessage.h> + + + /* + This is a simpler, smaller version of skaclient for basic cases: + - no fd-passing + - no asyncout + - no kolbak: client calls are always synchronous + - fixed-size buffers included in the client structure + */ + +#define TEXTCLIENT_BUFSIZE 4096 +#define TEXTCLIENT_OPTION_WAITPID 0x00000001U + + + /* Server-side functions */ + +extern int textclient_server_init (textmessage_receiver_t *, textmessage_sender_t *, textmessage_sender_t *, char const *, size_t, char const *, size_t, tain_t const *, tain_t *) ; +extern int textclient_server_init_frompipe (textmessage_receiver_t *, textmessage_sender_t *, textmessage_sender_t *, char const *, size_t, char const *, size_t, tain_t const *, tain_t *) ; +extern int textclient_server_init_fromsocket (textmessage_receiver_t *, textmessage_sender_t *, textmessage_sender_t *, char const *, size_t, char const *, size_t, tain_t const *, tain_t *) ; +#define textclient_server_init_g(in, syncout, asyncout, before, beforelen, after, afterlen, deadline) textclient_server_init(in, syncout, asyncout, before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_server_init_frompipe_g(in, syncout, asyncout, before, beforelen, after, afterlen, deadline) textclient_server_init_frompipe(in, syncout, asyncout, before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_server_init_fromsocket_g(in, syncout, asyncout, before, beforelen, after, afterlen, deadline) textclient_server_init_fromsocket(in, syncout, asyncout, before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_server_01x_init(before, beforelen, after, afterlen, deadline, stamp) textclient_server_init(textmessage_receiver_0, textmessage_sender_1, textmessage_sender_x, before, beforelen, after, afterlen, deadline, stamp) +#define textclient_server_01x_init_frompipe(before, beforelen, after, afterlen, deadline, stamp) textclient_server_init_frompipe(textmessage_receiver_0, textmessage_sender_1, textmessage_sender_x, before, beforelen, after, afterlen, deadline, stamp) +#define textclient_server_01x_init_fromsocket(before, beforelen, after, afterlen, deadline, stamp) textclient_server_init_fromsocket(textmessage_receiver_0, textmessage_sender_1, textmessage_sender_x, before, beforelen, after, afterlen, deadline, stamp) +#define textclient_server_01x_init_g(before, beforelen, after, afterlen, deadline) textclient_server_01x_init(before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_server_01x_init_frompipe_g(before, beforelen, after, afterlen, deadline) textclient_server_01x_init_frompipe(before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_server_01x_init_fromsocket_g(before, beforelen, after, afterlen, deadline) textclient_server_01x_init_fromsocket(before, beforelen, after, afterlen, (deadline), &STAMP) + + + /* User structure */ + +typedef struct textclient_s textclient_t, *textclient_t_ref ; +struct textclient_s +{ + textmessage_sender_t syncout ; + textmessage_receiver_t syncin ; + textmessage_receiver_t asyncin ; + pid_t pid ; + uint32_t options ; + char syncbuf[TEXTCLIENT_BUFSIZE] ; + char asyncbuf[TEXTCLIENT_BUFSIZE] ; +} ; +#define TEXTCLIENT_ZERO { .syncout = TEXTMESSAGE_SENDER_ZERO, .syncin = TEXTMESSAGE_RECEIVER_ZERO, .asyncin = TEXTMESSAGE_RECEIVER_ZERO, .pid = 0, .options = 0 } +extern textclient_t const textclient_zero ; + + + /* Starting and ending */ + +extern void textclient_end (textclient_t *) ; + +extern int textclient_start (textclient_t *, char const *, uint32_t, char const *, size_t, char const *, size_t, tain_t const *, tain_t *) ; +extern int textclient_startf (textclient_t *, char const *const *, char const *const *, uint32_t, char const *, size_t, char const *, size_t, tain_t const *, tain_t *) ; + +#define textclient_start_g(a, path, options, before, beforelen, after, afterlen, deadline) textclient_start(a, path, options, before, beforelen, after, afterlen, (deadline), &STAMP) +#define textclient_startf_g(a, argv, envp, options, before, beforelen, after, afterlen, deadline) textclient_startf_b(a, argv, envp, options, before, beforelen, after, afterlen, (deadline), &STAMP) + + + /* Writing */ + +#define textclient_put(a, s, len) textmessage_sender_put(&(a)->syncout, s, len) +#define textclient_putv(a, v, n) textmessage_sender_putv(&(a)->syncout, v, n) +#define textclient_flush(a) textmessage_sender_flush(&(a)->syncout) +#define textclient_timed_flush(a, deadline, stamp) textmessage_sender_timed_flush(&(a)->syncout, deadline, stamp) +#define textclient_timed_flush_g(a, deadline) textclient_timed_flush(a, (deadline), &STAMP) +#define textclient_send(a, s, len) textmessage_send(&(a)->syncout, s, len) +#define textclient_sendv(a, v, n) textmessage_sendv(&(a)->syncout, v, n) +#define textclient_timed_send(a, s, len, deadline, stamp) textmessage_timed_send(&(a)->syncout, s, len, deadline, stamp) +#define textclient_timed_sendv(a, v, n, deadline, stamp) textmessage_timed_sendv(&(a)->syncout, v, n, deadline, stamp) +#define textclient_timed_send_g(a, s, len, deadline) textclient_timed_send(a, s, len, (deadline), &STAMP) +#define textclient_timed_sendv_g(a, v, n, deadline) textclient_timed_sendv(a, v, n, (deadline), &STAMP) + + + /* Sync reading */ + +#define textclient_get(a, v) textmessage_receive(&(a)->syncin, v) +#define textclient_timed_get(a, v, deadline, stamp) (sanitize_read(textmessage_timed_receive(&(a)->syncin, v, deadline, stamp)) > 0) +#define textclient_timed_get_g(a, v, deadline) textclient_timed_get(a, v, (deadline), &STAMP) + + + /* Sync writing+reading */ + +#define textclient_exchange(a, s, len, ans, deadline, stamp) (textclient_timed_send(a, s, len, deadline, stamp) && textclient_timed_get(a, ans, deadline, stamp)) +#define textclient_exchangev(a, v, n, ans, deadline, stamp) (textclient_timed_sendv(a, v, n, deadline, stamp) && textclient_timed_get(a, ans, deadline, stamp)) +#define textclient_exchange_g(a, s, len, ans, deadline) textclient_exchange(a, s, len, ans, (deadline), &STAMP) +#define textclient_exchangev_g(a, v, n, ans, deadline) textclient_exchangev(a, v, n, ans, (deadline), &STAMP) + +extern int textclient_command (textclient_t *, char const *, size_t, tain_t const *, tain_t *) ; +extern int textclient_commandv (textclient_t *, struct iovec const *, unsigned int, tain_t const *, tain_t *) ; +#define textclient_command_g(a, s, len, deadline) textclient_command(a, s, len, (deadline), &STAMP) +#define textclient_commandv_g(a, v, n, deadline) textclient_commandv(a, v, n, (deadline), &STAMP) + + + /* Async reading */ + +#define textclient_fd(a) textmessage_receiver_fd(&(a)->asyncin) +#define textclient_update(a, f, p) textmessage_handle(&(a)->asyncin, f, p) +#define textclient_timed_update(a, f, p, deadline, stamp) textmessage_timed_handle(&(a)->asyncin, f, p, deadline, stamp) +#define textclient_timed_update_g(a, f, p, deadline) textclient_timed_update(a, f, p, (deadline), &STAMP) + +#endif diff --git a/src/include/skalibs/textmessage.h b/src/include/skalibs/textmessage.h index 9904534..fd393a9 100644 --- a/src/include/skalibs/textmessage.h +++ b/src/include/skalibs/textmessage.h @@ -13,6 +13,7 @@ #include <skalibs/tai.h> #define TEXTMESSAGE_MAXREADS 128 +#define TEXTMESSAGE_MAXLEN 0x01000000U /* Sender */ @@ -36,13 +37,13 @@ extern int textmessage_put (textmessage_sender_t *, char const *, size_t) ; extern int textmessage_putv (textmessage_sender_t *, struct iovec const *, unsigned int) ; extern int textmessage_sender_flush (textmessage_sender_t *) ; -extern int unixmessage_sender_timed_flush (textmessage_sender_t *, tain_t const *, tain_t *) ; +extern int textmessage_sender_timed_flush (textmessage_sender_t *, tain_t const *, tain_t *) ; #define textmessage_sender_timed_flush_g(ts, deadline) textmessage_sender_timed_flush(ts, (deadline), &STAMP) #define textmessage_send(ts, s, len) (textmessage_put(ts, s, len) && textmessage_sender_flush(ts)) #define textmessage_sendv(ts, v, n) (textmessage_putv(ts, v, n) && textmessage_sender_flush(ts)) -#define textmessage_timed_send(ts, s, len, deadline, stamp) (textmessage_put(ts, s, len) && texxtmessage_sender_timed_flush(ts, deadline, stamp)) -#define textmessage_timed_sendv(ts, v, n, deadline, stamp) (textmessage_putv(ts, v, n) && texxtmessage_sender_timed_flush(ts, deadline, stamp)) +#define textmessage_timed_send(ts, s, len, deadline, stamp) (textmessage_put(ts, s, len) && textmessage_sender_timed_flush(ts, deadline, stamp)) +#define textmessage_timed_sendv(ts, v, n, deadline, stamp) (textmessage_putv(ts, v, n) && textmessage_sender_timed_flush(ts, deadline, stamp)) #define textmessage_timed_send_g(ts, s, len, deadline) textmessage_timed_send(ts, s, len, (deadline), &STAMP) #define textmessage_timed_sendv_g(ts, v, n, deadline) textmessage_timed_sendv(ts, v, n, (deadline), &STAMP) @@ -71,14 +72,14 @@ extern int textmessage_receiver_hasmsginbuf (textmessage_receiver_t const *) gcc extern int textmessage_receive (textmessage_receiver_t *, struct iovec *) ; extern int textmessage_timed_receive (textmessage_receiver_t *, struct iovec *, tain_t const *, tain_t *) ; -#define textmessage_timed_receive_g(tr, s, max, deadline) textmessage_timed_receive(tr, s, max, (deadline), &STAMP) +#define textmessage_timed_receive_g(tr, v, deadline) textmessage_timed_receive(tr, v, (deadline), &STAMP) typedef int textmessage_handler_func_t (struct iovec const *, void *) ; typedef textmessage_handler_func_t *textmessage_handler_func_t_ref ; extern int textmessage_handle (textmessage_receiver_t *, textmessage_handler_func_t_ref, void *) ; extern int textmessage_timed_handle (textmessage_receiver_t *, textmessage_handler_func_t_ref, void *, tain_t const *, tain_t *) ; -#define textmessage_timed_handle_g(tr, f, p, deadline) unixmessage_timed_handle(tr, f, p, (deadline), &STAMP) +#define textmessage_timed_handle_g(tr, f, p, deadline) textmessage_timed_handle(tr, f, p, (deadline), &STAMP) diff --git a/src/include/skalibs/unixonacid.h b/src/include/skalibs/unixonacid.h index 43bd695..7132f1c 100644 --- a/src/include/skalibs/unixonacid.h +++ b/src/include/skalibs/unixonacid.h @@ -6,6 +6,7 @@ #include <skalibs/unix-transactional.h> #include <skalibs/unix-timed.h> #include <skalibs/textmessage.h> +#include <skalibs/textclient.h> #include <skalibs/unixmessage.h> #include <skalibs/unixconnection.h> #include <skalibs/kolbak.h> 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) ; |