about summary refs log tree commit diff
path: root/src/libs6/s6_svc_writectl.c
blob: e7f4a027499b038de62358e219f1402606fa14ab (plain) (blame)
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
/* ISC license. */

#include <skalibs/sysdeps.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-transactional.h>
#include <s6/supervise.h>

int s6_svc_writectl (char const *service, char const *subdir, char const *s, size_t len)
{
  size_t svlen = strlen(service) ;
  size_t sublen = strlen(subdir) ;
  int r ;
  char fn[svlen + sublen + 10] ;
  memcpy(fn, service, svlen) ;
  fn[svlen] = '/' ;
  memcpy(fn + svlen + 1, subdir, sublen) ;
  memcpy(fn + svlen + 1 + sublen, "/control", 9) ;
  r = s6_svc_write(fn, s, len) ;
  if (r != -2) return r ;

#ifdef SKALIBS_HASODIRECTORY

 /* Investigate what went wrong */

  {
    int fdsub ;
    int fd = open2(service, O_RDONLY | O_DIRECTORY) ;
    if (fd == -1) return -1 ;
    fdsub = open2_at(fd, subdir, O_RDONLY | O_DIRECTORY) ;
    fd_close(fd) ;
    if (fdsub == -1) return (errno == ENOENT) ? 0 : -2 ;
    fd_close(fdsub) ;
    return -2 ;
  }

#else

 /* Too bad, get a better system */

  return -2 ;

#endif
}