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