summaryrefslogtreecommitdiff
path: root/src/libs6/s6-ftrigrd.c
blob: 202ea3136dd5707f736affc5fb7f448cfeafe698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* ISC license. */

#include <sys/uio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <signal.h>
#include <regex.h>

#include <skalibs/posixplz.h>
#include <skalibs/types.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/error.h>
#include <skalibs/strerr.h>
#include <skalibs/buffer.h>
#include <skalibs/stralloc.h>
#include <skalibs/genalloc.h>
#include <skalibs/sig.h>
#include <skalibs/tai.h>
#include <skalibs/djbunix.h>
#include <skalibs/iopause.h>
#include <skalibs/textmessage.h>
#include <skalibs/textclient.h>

#include "ftrig1.h"
#include <s6/ftrigr.h>

#include <skalibs/posixishard.h>

#define FTRIGRD_MAXREADS 32
#define FTRIGRD_BUFSIZE 16

#define dienomem() strerr_diefu1sys(111, "stralloc_catb")

typedef struct ftrigio_s ftrigio, *ftrigio_ref ;
struct ftrigio_s
{
  unsigned int xindex ;
  ftrig1_t trig ;
  buffer b ;
  char buf[FTRIGRD_BUFSIZE] ;
  regex_t re ;
  stralloc sa ;
  uint32_t options ;
  uint16_t id ; /* given by client */
} ;
#define FTRIGIO_ZERO { .xindex = 0, .trig = FTRIG1_ZERO, .b = BUFFER_INIT(0, -1, 0, 0), .buf = "", .sa = STRALLOC_ZERO, .options = 0, .id = 0 }

static genalloc g = GENALLOC_ZERO ; /* ftrigio */

static void trig (uint16_t id, char what, char info)
{
  char pack[4] ;
  uint16_pack_big(pack, id) ;
  pack[2] = what ; pack[3] = info ;
  if (!textmessage_put(textmessage_sender_x, pack, 4))
    strerr_diefu1sys(111, "build answer") ;
}

static void answer (unsigned char c)
{
  if (!textmessage_put(textmessage_sender_1, (char *)&c, 1))
    strerr_diefu1sys(111, "textmessage_put") ;
}

static void ftrigio_deepfree (ftrigio *p)
{
  ftrig1_free(&p->trig) ;
  stralloc_free(&p->sa) ;
  regfree(&p->re) ;
}

static void remove (size_t i)
{
  size_t n = genalloc_len(ftrigio, &g) ;
  ftrigio *a = genalloc_s(ftrigio, &g) ;
  ftrigio_deepfree(a + i) ;
  a[i] = a[--n] ;
  a[i].b.c.x = a[i].buf ;
  genalloc_setlen(ftrigio, &g, n) ;
}

static inline int ftrigio_read (ftrigio *p)
{
  unsigned int i = FTRIGRD_MAXREADS ;
  while (i--)
  {
    regmatch_t pmatch ;
    size_t blen ;
    ssize_t r = sanitize_read(buffer_fill(&p->b)) ;
    if (!r) break ;
    if (r < 0) return (trig(p->id, 'd', errno), 0) ;
    blen = buffer_len(&p->b) ;
    if (!stralloc_readyplus(&p->sa, blen+1)) dienomem() ;
    buffer_getnofill(&p->b, p->sa.s + p->sa.len, blen) ;
    p->sa.len += blen ;
    p->sa.s[p->sa.len] = 0 ;
    while (!regexec(&p->re, p->sa.s, 1, &pmatch, REG_NOTBOL | REG_NOTEOL))
    {
      trig(p->id, '!', p->sa.s[pmatch.rm_eo - 1]) ;
      if (!(p->options & FTRIGR_REPEAT)) return 0 ;
      memmove(p->sa.s, p->sa.s + pmatch.rm_eo, p->sa.len + 1 - pmatch.rm_eo) ;
      p->sa.len -= pmatch.rm_eo ;
    }
  }
  return 1 ;
}

static int parse_protocol (struct iovec const *v, void *context)
{
  char const *s = v->iov_base ;
  uint16_t id ;
  if (v->iov_len < 3)
    strerr_dief1x(100, "invalid client request") ;
  uint16_unpack_big(s, &id) ;
  switch (s[2])
  {
    case 'U' : /* unsubscribe */
    {
      size_t n = genalloc_len(ftrigio, &g) ;
      size_t i = 0 ;
      for (; i < n ; i++) if (genalloc_s(ftrigio, &g)[i].id == id) break ;
      if (i < n)
      {
        remove(i) ;
        answer(0) ;
      }
      else answer(EINVAL) ;
      break ;
    }
    case 'L' : /* subscribe to path and match re */
    {
      size_t n = genalloc_len(ftrigio, &g) ;
      ftrigio *p ;
      uint32_t options, pathlen, relen ;
      int r ;
      if (v->iov_len < 19)
      {
        answer(EPROTO) ;
        break ;
      }
      uint32_unpack_big(s + 3, &options) ;
      uint32_unpack_big(s + 7, &pathlen) ;
      uint32_unpack_big(s + 11, &relen) ;
      if (((pathlen + relen + 17) != v->iov_len) || s[15 + pathlen] || s[v->iov_len - 1])
      {
        answer(EPROTO) ;
        break ;
      }
      if (!genalloc_readyplus(ftrigio, &g, 1))
      {
        answer(ENOMEM) ;
        break ;
      }
      p = genalloc_s(ftrigio, &g) + n ;
      r = skalibs_regcomp(&p->re, s + 16 + pathlen, REG_EXTENDED) ;
      if (r)
      {
        answer(r == REG_ESPACE ? ENOMEM : EINVAL) ;
        break ;
      }
      if (!ftrig1_make(&p->trig, s + 15))
      {
        regfree(&p->re) ;
        answer(errno) ;
        break ;
      }
      buffer_init(&p->b, &buffer_read, p->trig.fd, p->buf, FTRIGRD_BUFSIZE) ;
      p->options = options ;
      p->id = id ;
      p->sa = stralloc_zero ;
      genalloc_setlen(ftrigio, &g, n+1) ;
      answer(0) ;
      break ;
    }
    default :
      strerr_dief1x(100, "invalid client request") ;
  }
  (void)context ;
  return 1 ;
}

int main (void)
{
  PROG = "s6-ftrigrd" ;

  if (ndelay_on(0) == -1 || ndelay_on(1) == -1)
    strerr_diefu1sys(111, "make fds nonblocking") ;
  if (!sig_altignore(SIGPIPE))
    strerr_diefu1sys(111, "ignore SIGPIPE") ;

  {
    tain deadline ;
    tain_now_set_stopwatch_g() ;
    tain_addsec_g(&deadline, 2) ;
    if (!textclient_server_01x_init_g(FTRIGR_BANNER1, FTRIGR_BANNER1_LEN, FTRIGR_BANNER2, FTRIGR_BANNER2_LEN, &deadline))
      strerr_diefu1sys(111, "sync with client") ;
  }

  for (;;)
  {
    size_t n = genalloc_len(ftrigio, &g) ;
    size_t i = 0 ;
    iopause_fd x[3 + n] ;

    x[0].fd = 0 ; x[0].events = IOPAUSE_EXCEPT | IOPAUSE_READ ;
    x[1].fd = 1 ; x[1].events = IOPAUSE_EXCEPT | (textmessage_sender_isempty(textmessage_sender_1) ? 0 : IOPAUSE_WRITE) ;
    x[2].fd = textmessage_sender_fd(textmessage_sender_x) ;
    x[2].events = IOPAUSE_EXCEPT | (textmessage_sender_isempty(textmessage_sender_x) ? 0 : IOPAUSE_WRITE) ;
    for (; i < n ; i++)
    {
      ftrigio *p = genalloc_s(ftrigio, &g) + i ;
      p->xindex = 3+i ;
      x[3+i].fd = p->trig.fd ;
      x[3+i].events = IOPAUSE_READ ;
    }

    if (iopause(x, 3 + n, 0, 0) < 0)
      strerr_diefu1sys(111, "iopause") ;

   /* client closed */
    if ((x[0].revents | x[1].revents) & IOPAUSE_EXCEPT) break ;

   /* client is reading */
    if (x[1].revents & IOPAUSE_WRITE)
      if (!textmessage_sender_flush(textmessage_sender_1) && !error_isagain(errno))
        strerr_diefu1sys(111, "flush stdout") ;
    if (x[2].revents & IOPAUSE_WRITE)
      if (!textmessage_sender_flush(textmessage_sender_x) && !error_isagain(errno))
        return 1 ;

   /* scan listening ftrigs */
    for (i = 0 ; i < genalloc_len(ftrigio, &g) ; i++)
    {
      ftrigio *p = genalloc_s(ftrigio, &g) + i ;
      if (x[p->xindex].revents & IOPAUSE_READ)
        if (!ftrigio_read(p)) remove(i--) ;
    }

   /* client is writing */
    if (!textmessage_receiver_isempty(textmessage_receiver_0) || x[0].revents & IOPAUSE_READ)
    {
      if (textmessage_handle(textmessage_receiver_0, &parse_protocol, 0) < 0)
      {
        if (errno == EPIPE) break ; /* normal exit */
        strerr_diefu1sys(111, "handle messages from client") ;
      }
    }
  }
  return 0 ;
}