summaryrefslogtreecommitdiff
path: root/src/libs6rc/s6rc_servicedir_manage.c
blob: ddcfe84bb5fc18e85f0bfee7f6053204815e9373 (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
/* ISC license. */

#include <string.h>
#include <errno.h>

#include <skalibs/direntry.h>
#include <skalibs/stralloc.h>

#include <s6/s6-supervise.h>

#include <s6-rc/s6rc-servicedir.h>

int s6rc_servicedir_manage (char const *live, char const *prefix, tain_t const *deadline, tain_t *stamp)
{
  stralloc names = STRALLOC_ZERO ;
  size_t livelen = strlen(live) ;
  size_t n = 0 ;
  DIR *dir ;
  char dirfn[livelen + 13] ;
  memcpy(dirfn, live, livelen) ;
  memcpy(dirfn + livelen, "/servicedirs", 13) ;
  dir = opendir(dirfn) ;
  if (!dir) return -1 ;
  for (;;)
  {
    direntry *d ;
    errno = 0 ;
    d = readdir(dir) ;
    if (!d) break ;
    if (d->d_name[0] == '.') continue ;
    if (!stralloc_catb(&names, dirfn, livelen + 12)
     || !stralloc_catb(&names, "/", 1)
     || !stralloc_cats(&names, d->d_name)
     || !stralloc_0(&names)) goto err ;
    n++ ;
  }
  if (errno) goto err ;
  dir_close(dir) ;
  if (!n) return 0 ;
  memcpy(dirfn + livelen + 1, "scandir", 8) ;
  {
    char const *p = names.s ;
    char const *servicedirs[n] ;
    int r ;
    for (size_t i = 0 ; i < n ; p += strlen(p) + 1) servicedirs[i++] = p ;
    r = s6_supervise_link(dirfn, servicedirs, n, prefix, 4, deadline, stamp) ;
    stralloc_free(&names) ;
    return r ;
  }

 err:
  dir_close(dir) ;
  stralloc_free(&names) ;
  return -1 ;
}