Line data Source code
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 0 : int make_lock_file(const char *p, int operation, LockFile *ret) {
19 0 : _cleanup_close_ int fd = -1;
20 0 : _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 0 : t = strdup(p);
35 0 : if (!t)
36 0 : return -ENOMEM;
37 :
38 0 : for (;;) {
39 0 : struct flock fl = {
40 0 : .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
41 : .l_whence = SEEK_SET,
42 : };
43 : struct stat st;
44 :
45 0 : fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
46 0 : if (fd < 0)
47 0 : return -errno;
48 :
49 0 : r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
50 0 : if (r < 0) {
51 :
52 : /* If the kernel is too old, use good old BSD locks */
53 0 : if (errno == EINVAL)
54 0 : r = flock(fd, operation);
55 :
56 0 : if (r < 0)
57 0 : 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 0 : r = fstat(fd, &st);
67 0 : if (r < 0)
68 0 : return -errno;
69 0 : if (st.st_nlink > 0)
70 0 : break;
71 :
72 0 : fd = safe_close(fd);
73 : }
74 :
75 0 : ret->path = t;
76 0 : ret->fd = fd;
77 0 : ret->operation = operation;
78 :
79 0 : fd = -1;
80 0 : t = NULL;
81 :
82 0 : return r;
83 : }
84 :
85 0 : int make_lock_file_for(const char *p, int operation, LockFile *ret) {
86 : const char *fn;
87 : char *t;
88 :
89 0 : assert(p);
90 0 : assert(ret);
91 :
92 0 : fn = basename(p);
93 0 : if (!filename_is_valid(fn))
94 0 : return -EINVAL;
95 :
96 0 : t = newa(char, strlen(p) + 2 + 4 + 1);
97 0 : stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
98 :
99 0 : return make_lock_file(t, operation, ret);
100 : }
101 :
102 12 : void release_lock_file(LockFile *f) {
103 : int r;
104 :
105 12 : if (!f)
106 0 : return;
107 :
108 12 : 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 0 : if (f->fd >= 0 &&
115 0 : (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 0 : r = fcntl(f->fd, F_OFD_SETLK, &fl);
122 0 : if (r < 0 && errno == EINVAL)
123 0 : r = flock(f->fd, LOCK_EX|LOCK_NB);
124 :
125 0 : if (r >= 0)
126 0 : f->operation = LOCK_EX|LOCK_NB;
127 : }
128 :
129 0 : if ((f->operation & ~LOCK_NB) == LOCK_EX)
130 0 : unlink_noerrno(f->path);
131 :
132 0 : f->path = mfree(f->path);
133 : }
134 :
135 12 : f->fd = safe_close(f->fd);
136 12 : f->operation = 0;
137 : }
|