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

#include <errno.h>
#include <skalibs/bytestr.h>
#include <skalibs/diuint.h>
#include <skalibs/djbunix.h>
#include <s6-rc/s6rc-utils.h>

int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int cwhat, int *ccfd)
{
  int e = 0 ;
  int lfd = -1, cfd = -1 ;

  if (lwhat)
  {
    unsigned int llen = str_len(live) ;
    char lfn[llen + 6] ;
    byte_copy(lfn, llen, live) ;
    byte_copy(lfn + llen, 6, "/lock") ;
    lfd = open_create(lfn) ;
    if (lfd < 0) return 0 ;
    if ((lwhat > 1 ? lock_ex(lfd) : lock_sh(lfd)) < 0) { e = errno ; goto lerr ; }
  }

  if (cwhat)
  {
    unsigned int clen = str_len(compiled) ;
    char cfn[clen + 6] ;
    byte_copy(cfn, clen, compiled) ;
    byte_copy(cfn + clen, 6, "/lock") ;
    cfd = open_create(cfn) ;
    if (cfd < 0)
      if (cwhat > 1 || errno != EROFS) { e = errno ; goto lerr ; }
      else cfd = -errno ;
    else if ((cwhat > 1 ? lock_ex(cfd) : lock_sh(cfd)) < 0) { e = errno ; goto cerr ; }
  }

  if (lwhat) *llfd = lfd ;
  if (cwhat) *ccfd = cfd ;
  return 1 ;

 cerr:
  fd_close(cfd) ;
 lerr:
  if (lwhat) fd_close(lfd) ;
  errno = e ;
  return 0 ;
}