textclient_startf.c (1371B)
1 /* ISC license. */ 2 3 #include <sys/uio.h> 4 #include <string.h> 5 #include <errno.h> 6 7 #include <skalibs/allreadwrite.h> 8 #include <skalibs/cspawn.h> 9 #include <skalibs/textmessage.h> 10 #include <skalibs/textclient.h> 11 #include <skalibs/posixishard.h> 12 13 int textclient_startf (textclient *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 const *deadline, tain *stamp) 14 { 15 struct iovec v ; 16 int fd[3] = { 0, 1 } ; 17 pid_t pid = child_spawn3(argv[0], argv, envp, fd) ; 18 if (!pid) return 0 ; 19 textmessage_receiver_init(&a->syncin, fd[0], a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ; 20 textmessage_receiver_init(&a->asyncin, fd[2], a->asyncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ; 21 textmessage_sender_init(&a->syncout, fd[1]) ; 22 a->pid = pid ; 23 a->options = options ; 24 if (!textclient_timed_send(a, before, beforelen, deadline, stamp)) goto err ; 25 if (sanitize_read(textmessage_timed_receive(&a->asyncin, &v, deadline, stamp)) <= 0) goto err ; 26 if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ; 27 if (!textclient_timed_get(a, &v, deadline, stamp)) goto err ; 28 if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto errproto ; 29 return 1 ; 30 31 errproto: 32 errno = EPROTO ; 33 err: 34 textclient_end(a) ; 35 return 0 ; 36 }