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

#include <errno.h>

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

#include "s6lockd.h"

int s6lockd_openandlock (char const *file, int ex, int nb)
{
  int fd, r ;
  if (ex)
  {
    fd = open_create(file) ;
    if (fd < 0) strerr_diefu3sys(111, "open ", file, " for writing") ;
  }
  else
  {
    fd = open_read(file) ;
    if (fd < 0)
    {
      if (errno != ENOENT) strerr_diefu3sys(111, "open ", file, " for reading") ;
      fd = open_create(file) ;
      if (fd < 0) strerr_diefu2sys(111, "create ", file) ;
      fd_close(fd) ;
      fd = open_read(file) ;
      if (fd < 0) strerr_diefu3sys(111, "open ", file, " for reading") ;
    }
  }
  r = fd_lock(fd, ex, nb) ;
  if (!r) errno = EBUSY ;
  if (r < 1) strerr_diefu2sys(1, "lock ", file) ;
  return fd ;
}