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