s6

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

ftrigr_subscribe.c (1341B)


      1 /* ISC license. */
      2 
      3 #include <sys/uio.h>
      4 #include <string.h>
      5 #include <stdint.h>
      6 #include <errno.h>
      7 #include <skalibs/uint16.h>
      8 #include <skalibs/uint32.h>
      9 #include <skalibs/tai.h>
     10 #include <skalibs/stralloc.h>
     11 #include <skalibs/gensetdyn.h>
     12 #include <skalibs/textclient.h>
     13 #include <s6/ftrigr.h>
     14 
     15 uint16_t ftrigr_subscribe (ftrigr_t *a, char const *path, char const *re, uint32_t options, tain const *deadline, tain *stamp)
     16 {
     17   size_t pathlen = strlen(path) ;
     18   size_t relen = strlen(re) ;
     19   ftrigr1_t ft = { .options = options, .state = FR1STATE_LISTENING, .what = STRALLOC_ZERO } ;
     20   uint32_t i ;
     21   char tmp[15] = "--L" ;
     22   struct iovec v[3] = { { .iov_base = tmp, .iov_len = 15 }, { .iov_base = (char *)path, .iov_len = pathlen + 1 }, { .iov_base = (char *)re, .iov_len = relen + 1 } } ;
     23   if (!gensetdyn_new(&a->data, &i)) return 0 ;
     24   if (i >= UINT16_MAX)
     25   {
     26     gensetdyn_delete(&a->data, i) ;
     27     return (errno = EMFILE, 0) ;
     28   }
     29   uint16_pack_big(tmp, (uint16_t)i) ;
     30   uint32_pack_big(tmp+3, options) ;
     31   uint32_pack_big(tmp+7, (uint32_t)pathlen) ;
     32   uint32_pack_big(tmp+11, (uint32_t)relen) ;
     33   if (!textclient_commandv(&a->connection, v, 3, deadline, stamp))
     34   {
     35     int e = errno ;
     36     gensetdyn_delete(&a->data, i) ;
     37     errno = e ;
     38     return 0 ;
     39   }
     40   *GENSETDYN_P(ftrigr1_t, &a->data, i) = ft ;
     41   return (uint16_t)(i+1) ;
     42 }