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