File: lock.c

package info (click to toggle)
dnssec-trigger 0.17%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,896 kB
  • sloc: ansic: 18,697; sh: 977; makefile: 494; xml: 444; objc: 421; cpp: 18
file content (66 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download | duplicates (4)
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
56
57
58
59
60
61
62
63
64
65
66
#include "config.h"
#include "lock.h"

#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static const char *LF_PATH = __LOCK_FILE_PATH;
static size_t LF_PATH_LEN = sizeof(__LOCK_FILE_PATH);

static int check_dir = 1;
static int fd = 0;

void lock_acquire() {
    const char* path;
    struct flock f = {
        .l_type=F_WRLCK,
        .l_whence=SEEK_SET,
        .l_start=0,
        .l_len=0
    };
    int ret;
    if (check_dir) {
        // TODO check & create dir
    }
    path = LF_PATH;
    fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC, 0600);
    if (fd == -1) {
        // TODO error handling
        return;
    }
    ret = fcntl(fd, F_SETLKW, f);
    if (ret == -1) {
        // TODO error handling
        return;
    }
}

void lock_release() {
    struct flock f = {
        .l_type=F_UNLCK,
        .l_whence=SEEK_SET,
        .l_start=0,
        .l_len=0
    };
    int ret;
    if (fd == 0) {
        return;
    }
    ret = fcntl(fd, F_SETLKW, f);
    if (ret == -1) {
        // TODO error handling
        return;
    }
    
    close(fd);
    fd = 0;
}

void lock_override(const char *path, size_t len) {
    LF_PATH = path;
    LF_PATH_LEN = len;
    check_dir = 0;
}