skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

textclient_start.c (1357B)


      1 /* ISC license. */
      2 
      3 #include <sys/uio.h>
      4 #include <string.h>
      5 #include <errno.h>
      6 
      7 #include <skalibs/socket.h>
      8 #include <skalibs/djbunix.h>
      9 #include <skalibs/textmessage.h>
     10 #include <skalibs/textclient.h>
     11 #include <skalibs/posixishard.h>
     12 
     13 int textclient_start (textclient *a, char const *path, 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 = ipc_stream_nbcoe() ;
     17   if (fd < 0) return 0 ;
     18   if (!ipc_timed_connect(fd, path, deadline, stamp)) goto err ;
     19   textmessage_sender_init(&a->syncout, fd) ;
     20   if (!textmessage_timed_send(&a->syncout, before, beforelen, deadline, stamp)) goto ferr ;
     21   if (!textmessage_recv_channel(fd, &a->asyncin, a->asyncbuf, TEXTCLIENT_BUFSIZE, after, afterlen, deadline, stamp)) goto ferr ;
     22   textmessage_receiver_init(&a->syncin, fd, a->syncbuf, TEXTCLIENT_BUFSIZE, TEXTMESSAGE_MAXLEN) ;
     23   if (!textclient_timed_get(a, &v, deadline, stamp)) goto aerr ;
     24   if (v.iov_len != afterlen || memcmp(v.iov_base, after, afterlen)) goto berr ;
     25   a->pid = 0 ;
     26   a->options = options & ~TEXTCLIENT_OPTION_WAITPID ;
     27   return 1 ;
     28 
     29  berr:
     30   errno = EPROTO ;
     31  aerr:
     32   textmessage_receiver_free(&a->syncin) ;
     33   textmessage_receiver_free(&a->asyncin) ;
     34  ferr:
     35   textmessage_sender_free(&a->syncout) ;
     36  err:
     37   fd_close(fd) ;
     38   return 0 ;
     39 }