about summary refs log tree commit diff
path: root/src/libs6/s6_instance_chdirservice.c
blob: 356ee20006d6ff07d7fca17e4c31ed6d34e9d692 (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
/* ISC license. */

#include <string.h>
#include <unistd.h>

#include <skalibs/strerr.h>
#include <skalibs/djbunix.h>

#include <s6/supervise.h>

void s6_instance_chdirservice (char const *s)
{
  int fd, r ;
  size_t len = strlen(s) ;
  char fn[len + 10] ;
  if (!*s) strerr_dief1x(100, "invalid service path") ;
  memcpy(fn, s, len) ;
  memcpy(fn + len, "/instance", 10) ;
  if (chdir(fn) == -1) strerr_diefu2sys(111, "chdir to ", fn) ;
  fd = open_read(S6_SVSCAN_CTLDIR "/lock") ;
  if (fd < 0) strerr_diefu3sys(111, "open ", fn, "/" S6_SVSCAN_CTLDIR "/lock") ;
  r = fd_islocked(fd) ;
  if (r < 0) strerr_diefu3sys(111, "check lock on ", fn, "/" S6_SVSCAN_CTLDIR "/lock") ;
  if (!r) strerr_dief2x(1, "instanced service not running on ", s) ;
  fd_close(fd) ;
}