]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/lockfile-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / basic / lockfile-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/file.h>
8 #include <sys/stat.h>
9
10 #include "alloc-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "lockfile-util.h"
14 #include "macro.h"
15 #include "path-util.h"
16
17 int make_lock_file(const char *p, int operation, LockFile *ret) {
18 _cleanup_close_ int fd = -1;
19 _cleanup_free_ char *t = NULL;
20 int r;
21
22 /*
23 * We use UNPOSIX locks if they are available. They have nice
24 * semantics, and are mostly compatible with NFS. However,
25 * they are only available on new kernels. When we detect we
26 * are running on an older kernel, then we fall back to good
27 * old BSD locks. They also have nice semantics, but are
28 * slightly problematic on NFS, where they are upgraded to
29 * POSIX locks, even though locally they are orthogonal to
30 * POSIX locks.
31 */
32
33 t = strdup(p);
34 if (!t)
35 return -ENOMEM;
36
37 for (;;) {
38 struct flock fl = {
39 .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
40 .l_whence = SEEK_SET,
41 };
42 struct stat st;
43
44 fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
45 if (fd < 0)
46 return -errno;
47
48 r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
49 if (r < 0) {
50
51 /* If the kernel is too old, use good old BSD locks */
52 if (errno == EINVAL)
53 r = flock(fd, operation);
54
55 if (r < 0)
56 return errno == EAGAIN ? -EBUSY : -errno;
57 }
58
59 /* If we acquired the lock, let's check if the file
60 * still exists in the file system. If not, then the
61 * previous exclusive owner removed it and then closed
62 * it. In such a case our acquired lock is worthless,
63 * hence try again. */
64
65 r = fstat(fd, &st);
66 if (r < 0)
67 return -errno;
68 if (st.st_nlink > 0)
69 break;
70
71 fd = safe_close(fd);
72 }
73
74 ret->path = t;
75 ret->fd = fd;
76 ret->operation = operation;
77
78 fd = -1;
79 t = NULL;
80
81 return r;
82 }
83
84 int make_lock_file_for(const char *p, int operation, LockFile *ret) {
85 const char *fn;
86 char *t;
87
88 assert(p);
89 assert(ret);
90
91 fn = basename(p);
92 if (!filename_is_valid(fn))
93 return -EINVAL;
94
95 t = newa(char, strlen(p) + 2 + 4 + 1);
96 stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
97
98 return make_lock_file(t, operation, ret);
99 }
100
101 void release_lock_file(LockFile *f) {
102 int r;
103
104 if (!f)
105 return;
106
107 if (f->path) {
108
109 /* If we are the exclusive owner we can safely delete
110 * the lock file itself. If we are not the exclusive
111 * owner, we can try becoming it. */
112
113 if (f->fd >= 0 &&
114 (f->operation & ~LOCK_NB) == LOCK_SH) {
115 static const struct flock fl = {
116 .l_type = F_WRLCK,
117 .l_whence = SEEK_SET,
118 };
119
120 r = fcntl(f->fd, F_OFD_SETLK, &fl);
121 if (r < 0 && errno == EINVAL)
122 r = flock(f->fd, LOCK_EX|LOCK_NB);
123
124 if (r >= 0)
125 f->operation = LOCK_EX|LOCK_NB;
126 }
127
128 if ((f->operation & ~LOCK_NB) == LOCK_EX)
129 unlink_noerrno(f->path);
130
131 f->path = mfree(f->path);
132 }
133
134 f->fd = safe_close(f->fd);
135 f->operation = 0;
136 }