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