summaryrefslogtreecommitdiff
path: root/src/libs6/ftrigw_notifyb_nosig.c
blob: 0a21419c31847a5e2d5550e2125aa4ece7e60de7 (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
/* ISC license. */

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <skalibs/direntry.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/bytestr.h>
#include <skalibs/djbunix.h>
#include "ftrig1.h"
#include <s6/ftrigw.h>

int ftrigw_notifyb_nosig (char const *path, char const *s, size_t len)
{
  unsigned int i = 0 ;
  DIR *dir = opendir(path) ;
  if (!dir) return -1 ;
  {
    size_t pathlen = str_len(path) ;
    char tmp[pathlen + FTRIG1_PREFIXLEN + 45] ;
    byte_copy(tmp, pathlen, path) ;
    tmp[pathlen] = '/' ; tmp[pathlen + FTRIG1_PREFIXLEN + 44] = 0 ;
    for (;;)
    {
      direntry *d ;
      int fd ;
      errno = 0 ;
      d = readdir(dir) ;
      if (!d) break ;
      if (str_diffn(d->d_name, FTRIG1_PREFIX, FTRIG1_PREFIXLEN)) continue ;
      if (str_len(d->d_name) != FTRIG1_PREFIXLEN + 43) continue ;
      byte_copy(tmp + pathlen + 1, FTRIG1_PREFIXLEN + 43, d->d_name) ;
      fd = open_write(tmp) ;
      if (fd == -1)
      {
        if (errno == ENXIO) unlink(tmp) ;
      }
      else
      {
        register ssize_t r = fd_write(fd, s, len) ;
        if ((r < 0) || (size_t)r < len)
        {
          if (errno == EPIPE) unlink(tmp) ;
           /* what to do if EGAIN ? full fifo -> fix the reader !
              There's a race condition in extreme cases though ;
              but it's still better to be nonblocking - the writer
              shouldn't get in trouble because of a bad reader. */
          fd_close(fd) ;
        }
        else
        {
          fd_close(fd) ;
          i++ ;
        }
      }
    }
  }
  {
    int e = errno ;
    dir_close(dir) ;
    return e ? (errno = e, -1) : (int)i ;
  }
}