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