Debian Codegraph @

src/udev/udevadm-lock.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <sys/file.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "blockdev-util.h"
8 #include "device-util.h"
9 #include "fd-util.h"
10 #include "fdset.h"
11 #include "format-table.h"
12 #include "glyph-util.h"
13 #include "hash-funcs.h"
14 #include "help-util.h"
15 #include "lock-util.h"
16 #include "options.h"
17 #include "path-util.h"
18 #include "pidref.h"
19 #include "process-util.h"
20 #include "signal-util.h"
21 #include "sort-util.h"
22 #include "static-destruct.h"
23 #include "strv.h"
24 #include "time-util.h"
25 #include "udevadm.h"
26
27 static usec_t arg_timeout_usec = USEC_INFINITY;
28 static char **arg_devices = NULL;
29 static char **arg_backing = NULL;
30 static char **arg_cmdline = NULL;
31 static bool arg_print = false;
32
33 STATIC_DESTRUCTOR_REGISTER(arg_devices, strv_freep);
34 STATIC_DESTRUCTOR_REGISTER(arg_backing, strv_freep);
35 STATIC_DESTRUCTOR_REGISTER(arg_cmdline, strv_freep);
36
37 static int help(void) {
38 _cleanup_(table_unrefp) Table *options = NULL;
39 int r;
40
41 r = option_parser_get_help_table_ns("udevadm-lock", &options);
42 if (r < 0)
43 return r;
44
45 help_cmdline("lock [OPTIONS...] COMMAND");
46 help_cmdline("lock [OPTIONS...] --print");
47 help_abstract("Lock a block device and run a command.");
48 help_section("Options");
49 r = table_print_or_warn(options);
50 if (r < 0)
51 return r;
52
53 help_man_page_reference("udevadm", "8");
54 return 0;
55 }
56
57 static int parse_argv(int argc, char *argv[]) {
58 int r;
59
60 assert(argc >= 0);
61 assert(argv);
62
63 OptionParser opts = { argc, argv, OPTION_PARSER_STOP_AT_FIRST_NONOPTION, "udevadm-lock" };
64
65 FOREACH_OPTION_OR_RETURN(c, &opts)
66 switch (c) {
67
68 OPTION_NAMESPACE("udevadm-lock"): {}
69
70 OPTION_COMMON_HELP:
71 return help();
72
73 OPTION('V', "version", NULL, "Show package version"):
74 return print_version();
75
76 OPTION('d', "device", "DEVICE", "Block device to lock"): {} /* fall through */
77 OPTION('b', "backing", "FILE", "File whose backing block device to lock"): {
78 _cleanup_free_ char *s = NULL;
79 char ***l = opts.opt->short_code == 'd' ? &arg_devices : &arg_backing;
80
81 r = path_make_absolute_cwd(opts.arg, &s);
82 if (r < 0)
83 return log_error_errno(r, "Failed to make path '%s' absolute: %m", opts.arg);
84
85 path_simplify(s);
86
87 if (strv_consume(l, TAKE_PTR(s)) < 0)
88 return log_oom();
89
90 strv_uniq(*l);
91 break;
92 }
93
94 OPTION('t', "timeout", "SECS", "Block at most the specified time waiting for lock"):
95 r = parse_sec(opts.arg, &arg_timeout_usec);
96 if (r < 0)
97 return log_error_errno(r, "Failed to parse --timeout= parameter: %s", opts.arg);
98 break;
99
100 OPTION('p', "print", NULL, "Only show which block device the lock would be taken on"):
101 arg_print = true;
102 break;
103 }
104
105 char **args = option_parser_get_args(&opts);
106 if (arg_print) {
107 if (!strv_isempty(args))
108 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No arguments expected.");
109 } else {
110 if (strv_isempty(args))
111 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too few arguments, command to execute.");
112
113 arg_cmdline = strv_copy(args);
114 if (!arg_cmdline)
115 return log_oom();
116 }
117
118 if (strv_isempty(arg_devices) && strv_isempty(arg_backing))
119 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No devices to lock specified, refusing.");
120
121 return 1;
122 }
123
124 static int find_devno(
125 dev_t **devnos,
126 size_t *n_devnos,
127 const char *device,
128 bool backing) {
129
130 dev_t devt;
131 int r;
132
133 assert(devnos);
134 assert(n_devnos);
135 assert(*devnos || *n_devnos == 0);
136 assert(device);
137
138 r = path_get_whole_disk(device, backing, &devt);
139 if (r < 0)
140 return log_error_errno(r, "Failed to find whole block device for '%s': %m", device);
141
142 if (typesafe_bsearch(&devt, *devnos, *n_devnos, devt_compare_func)) {
143 log_debug("Device %u:%u already listed for locking, ignoring.", major(devt), minor(devt));
144 return 0;
145 }
146
147 if (!GREEDY_REALLOC(*devnos, *n_devnos + 1))
148 return log_oom();
149
150 (*devnos)[(*n_devnos)++] = devt;
151
152 /* Immediately sort again, to ensure the binary search above will work for the next device we add */
153 typesafe_qsort(*devnos, *n_devnos, devt_compare_func);
154 return 1;
155 }
156
157 static int lock_device(
158 const char *path,
159 dev_t devno,
160 usec_t deadline) {
161
162 _cleanup_close_ int fd = -EBADF;
163 struct stat st;
164 int r;
165
166 /* We open in O_WRONLY mode here, to trigger a rescan in udev once we are done */
167 fd = open(path, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
168 if (fd < 0)
169 return log_error_errno(errno, "Failed to open '%s': %m", path);
170
171 if (fstat(fd, &st) < 0)
172 return log_error_errno(errno, "Failed to stat '%s': %m", path);
173
174 /* Extra safety: check that the device still refers to what we think it refers to */
175 if (!S_ISBLK(st.st_mode) || st.st_rdev != devno)
176 return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "Path '%s' no longer refers to specified block device %u:%u.", path, major(devno), minor(devno));
177
178 r = lock_generic(fd, LOCK_BSD, LOCK_EX|LOCK_NB);
179 if (r < 0) {
180 if (r != -EAGAIN)
181 return log_error_errno(r, "Failed to lock device '%s': %m", path);
182
183 if (deadline == 0)
184 return log_error_errno(SYNTHETIC_ERRNO(EBUSY), "Device '%s' is currently locked.", path);
185
186 if (deadline == USEC_INFINITY) {
187 log_info("Device '%s' is currently locked, waiting%s", path, glyph(GLYPH_ELLIPSIS));
188
189 r = lock_generic(fd, LOCK_BSD, LOCK_EX);
190 } else {
191 usec_t left = usec_sub_unsigned(deadline, now(CLOCK_MONOTONIC));
192
193 log_info("Device '%s' is currently locked, waiting %s%s",
194 path, FORMAT_TIMESPAN(left, 0),
195 glyph(GLYPH_ELLIPSIS));
196
197 r = lock_generic_with_timeout(fd, LOCK_BSD, LOCK_EX, left);
198 if (r == -ETIMEDOUT)
199 return log_error_errno(r, "Timeout reached.");
200 }
201 if (r < 0)
202 return log_error_errno(r, "Failed to lock device '%s': %m", path);
203 }
204
205 log_debug("Successfully locked %s (%u:%u)%s", path, major(devno), minor(devno), glyph(GLYPH_ELLIPSIS));
206
207 return TAKE_FD(fd);
208 }
209
210 int verb_lock_main(int argc, char *argv[], uintptr_t _data, void *userdata) {
211 _cleanup_fdset_free_ FDSet *fds = NULL;
212 _cleanup_free_ dev_t *devnos = NULL;
213 size_t n_devnos = 0;
214 usec_t deadline;
215 int r;
216
217 r = parse_argv(argc, argv);
218 if (r <= 0)
219 return r;
220
221 STRV_FOREACH(i, arg_devices) {
222 r = find_devno(&devnos, &n_devnos, *i, /* backing= */ false);
223 if (r < 0)
224 return r;
225 }
226
227 STRV_FOREACH(i, arg_backing) {
228 r = find_devno(&devnos, &n_devnos, *i, /* backing= */ true);
229 if (r < 0)
230 return r;
231 }
232
233 assert(n_devnos > 0);
234
235 fds = fdset_new();
236 if (!fds)
237 return log_oom();
238
239 if (!timestamp_is_set(arg_timeout_usec))
240 deadline = arg_timeout_usec;
241 else
242 deadline = usec_add(now(CLOCK_MONOTONIC), arg_timeout_usec);
243
244 for (size_t i = 0; i < n_devnos; i++) {
245 _cleanup_free_ char *node = NULL;
246
247 r = devname_from_devnum(S_IFBLK, devnos[i], &node);
248 if (r < 0)
249 return log_error_errno(r, "Failed to format block device path: %m");
250
251 if (arg_print)
252 printf("%s\n", node);
253 else {
254 _cleanup_close_ int fd = -EBADF;
255
256 fd = lock_device(node, devnos[i], deadline);
257 if (fd < 0)
258 return fd;
259
260 r = fdset_consume(fds, TAKE_FD(fd));
261 if (r < 0)
262 return log_oom();
263 }
264 }
265
266 if (arg_print)
267 return EXIT_SUCCESS;
268
269 /* Ignore SIGINT and allow the forked process to receive it */
270 (void) ignore_signals(SIGINT);
271
272 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
273 r = pidref_safe_fork(
274 "(lock)",
275 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG,
276 &pidref);
277 if (r < 0)
278 return r;
279 if (r == 0) {
280 /* Child */
281
282 execvp(arg_cmdline[0], arg_cmdline);
283 log_open();
284 log_error_errno(errno, "Failed to execute %s: %m", arg_cmdline[0]);
285 _exit(EXIT_FAILURE);
286 }
287
288 return pidref_wait_for_terminate_and_check(arg_cmdline[0], &pidref, 0);
289 }