]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/lockfile-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / lockfile-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/file.h>
26 #include <sys/stat.h>
27
28 #include "alloc-util.h"
29 #include "fd-util.h"
30 #include "fs-util.h"
31 #include "lockfile-util.h"
32 #include "macro.h"
33 #include "path-util.h"
34
35 int make_lock_file(const char *p, int operation, LockFile *ret) {
36 _cleanup_close_ int fd = -1;
37 _cleanup_free_ char *t = NULL;
38 int r;
39
40 /*
41 * We use UNPOSIX locks if they are available. They have nice
42 * semantics, and are mostly compatible with NFS. However,
43 * they are only available on new kernels. When we detect we
44 * are running on an older kernel, then we fall back to good
45 * old BSD locks. They also have nice semantics, but are
46 * slightly problematic on NFS, where they are upgraded to
47 * POSIX locks, even though locally they are orthogonal to
48 * POSIX locks.
49 */
50
51 t = strdup(p);
52 if (!t)
53 return -ENOMEM;
54
55 for (;;) {
56 struct flock fl = {
57 .l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
58 .l_whence = SEEK_SET,
59 };
60 struct stat st;
61
62 fd = open(p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
63 if (fd < 0)
64 return -errno;
65
66 r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
67 if (r < 0) {
68
69 /* If the kernel is too old, use good old BSD locks */
70 if (errno == EINVAL)
71 r = flock(fd, operation);
72
73 if (r < 0)
74 return errno == EAGAIN ? -EBUSY : -errno;
75 }
76
77 /* If we acquired the lock, let's check if the file
78 * still exists in the file system. If not, then the
79 * previous exclusive owner removed it and then closed
80 * it. In such a case our acquired lock is worthless,
81 * hence try again. */
82
83 r = fstat(fd, &st);
84 if (r < 0)
85 return -errno;
86 if (st.st_nlink > 0)
87 break;
88
89 fd = safe_close(fd);
90 }
91
92 ret->path = t;
93 ret->fd = fd;
94 ret->operation = operation;
95
96 fd = -1;
97 t = NULL;
98
99 return r;
100 }
101
102 int make_lock_file_for(const char *p, int operation, LockFile *ret) {
103 const char *fn;
104 char *t;
105
106 assert(p);
107 assert(ret);
108
109 fn = basename(p);
110 if (!filename_is_valid(fn))
111 return -EINVAL;
112
113 t = newa(char, strlen(p) + 2 + 4 + 1);
114 stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck");
115
116 return make_lock_file(t, operation, ret);
117 }
118
119 void release_lock_file(LockFile *f) {
120 int r;
121
122 if (!f)
123 return;
124
125 if (f->path) {
126
127 /* If we are the exclusive owner we can safely delete
128 * the lock file itself. If we are not the exclusive
129 * owner, we can try becoming it. */
130
131 if (f->fd >= 0 &&
132 (f->operation & ~LOCK_NB) == LOCK_SH) {
133 static const struct flock fl = {
134 .l_type = F_WRLCK,
135 .l_whence = SEEK_SET,
136 };
137
138 r = fcntl(f->fd, F_OFD_SETLK, &fl);
139 if (r < 0 && errno == EINVAL)
140 r = flock(f->fd, LOCK_EX|LOCK_NB);
141
142 if (r >= 0)
143 f->operation = LOCK_EX|LOCK_NB;
144 }
145
146 if ((f->operation & ~LOCK_NB) == LOCK_EX)
147 unlink_noerrno(f->path);
148
149 f->path = mfree(f->path);
150 }
151
152 f->fd = safe_close(f->fd);
153 f->operation = 0;
154 }