~wb-munzinger/+junk/sanlock

« back to all changes in this revision

Viewing changes to lockfile.c

  • Committer: David Weber
  • Date: 2012-01-18 13:00:36 UTC
  • Revision ID: wb@munzinger.de-20120118130036-9a7wvhhmfuip7zx5
Tags: upstream-1.9
Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010-2011 Red Hat, Inc.
 
3
 *
 
4
 * This copyrighted material is made available to anyone wishing to use,
 
5
 * modify, copy, or redistribute it subject to the terms and conditions
 
6
 * of the GNU General Public License v2 or (at your option) any later version.
 
7
 */
 
8
 
 
9
#include <inttypes.h>
 
10
#include <unistd.h>
 
11
#include <stdio.h>
 
12
#include <stdlib.h>
 
13
#include <stdint.h>
 
14
#include <stddef.h>
 
15
#include <fcntl.h>
 
16
#include <string.h>
 
17
#include <errno.h>
 
18
#include <limits.h>
 
19
#include <time.h>
 
20
#include <syslog.h>
 
21
#include <sys/types.h>
 
22
#include <sys/time.h>
 
23
#include <sys/stat.h>
 
24
#include <sys/socket.h>
 
25
#include <sys/un.h>
 
26
 
 
27
#include "sanlock_internal.h"
 
28
#include "sanlock_sock.h"
 
29
#include "log.h"
 
30
#include "lockfile.h"
 
31
 
 
32
int lockfile(const char *dir, const char *name)
 
33
{
 
34
        char path[PATH_MAX];
 
35
        char buf[16];
 
36
        struct flock lock;
 
37
        mode_t old_umask;
 
38
        int fd, rv;
 
39
 
 
40
        old_umask = umask(0022);
 
41
        rv = mkdir(SANLK_RUN_DIR, 0777);
 
42
        if (rv < 0 && errno != EEXIST) {
 
43
                umask(old_umask);
 
44
                return rv;
 
45
        }
 
46
        umask(old_umask);
 
47
 
 
48
        snprintf(path, PATH_MAX, "%s/%s", dir, name);
 
49
 
 
50
        fd = open(path, O_CREAT|O_WRONLY|O_CLOEXEC, 0666);
 
51
        if (fd < 0) {
 
52
                log_error("lockfile open error %s: %s",
 
53
                          path, strerror(errno));
 
54
                return -1;
 
55
        }
 
56
 
 
57
        lock.l_type = F_WRLCK;
 
58
        lock.l_start = 0;
 
59
        lock.l_whence = SEEK_SET;
 
60
        lock.l_len = 0;
 
61
 
 
62
        rv = fcntl(fd, F_SETLK, &lock);
 
63
        if (rv < 0) {
 
64
                log_error("lockfile setlk error %s: %s",
 
65
                          path, strerror(errno));
 
66
                goto fail;
 
67
        }
 
68
 
 
69
        rv = ftruncate(fd, 0);
 
70
        if (rv < 0) {
 
71
                log_error("lockfile truncate error %s: %s",
 
72
                          path, strerror(errno));
 
73
                goto fail;
 
74
        }
 
75
 
 
76
        memset(buf, 0, sizeof(buf));
 
77
        snprintf(buf, sizeof(buf), "%d\n", getpid());
 
78
 
 
79
        rv = write(fd, buf, strlen(buf));
 
80
        if (rv <= 0) {
 
81
                log_error("lockfile write error %s: %s",
 
82
                          path, strerror(errno));
 
83
                goto fail;
 
84
        }
 
85
 
 
86
        return fd;
 
87
 fail:
 
88
        close(fd);
 
89
        return -1;
 
90
}
 
91
 
 
92
void unlink_lockfile(int fd, const char *dir, const char *name)
 
93
{
 
94
        char path[PATH_MAX];
 
95
 
 
96
        snprintf(path, PATH_MAX, "%s/%s", dir, name);
 
97
        unlink(path);
 
98
        close(fd);
 
99
}
 
100