]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
4f5dd394 | 3 | #include <signal.h> |
b08d03ff | 4 | #include <stdio.h> |
836e4e7e DDM |
5 | #include <stdlib.h> |
6 | #include <sys/socket.h> | |
5cb5a6ff | 7 | |
20ad4cfd | 8 | #include "sd-messages.h" |
4f5dd394 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
38c35970 | 11 | #include "chase.h" |
4139c1b2 | 12 | #include "dbus-mount.h" |
6fcbec6f | 13 | #include "dbus-unit.h" |
57b7a260 | 14 | #include "device.h" |
836e4e7e | 15 | #include "errno-util.h" |
ae5c4aa6 | 16 | #include "exec-credential.h" |
9a57c629 | 17 | #include "exit-status.h" |
38c35970 | 18 | #include "fd-util.h" |
f97b34a6 | 19 | #include "format-util.h" |
4f5dd394 | 20 | #include "fstab-util.h" |
836e4e7e | 21 | #include "glyph-util.h" |
baa6a42d | 22 | #include "initrd-util.h" |
fb36b133 | 23 | #include "libmount-util.h" |
4f5dd394 LP |
24 | #include "log.h" |
25 | #include "manager.h" | |
35cd0ba5 | 26 | #include "mkdir-label.h" |
836e4e7e | 27 | #include "mount-util.h" |
4f5dd394 | 28 | #include "mount.h" |
1cf40697 | 29 | #include "mount-setup.h" |
049af8ad | 30 | #include "mountpoint-util.h" |
6bedfcbb | 31 | #include "parse-util.h" |
4f5dd394 | 32 | #include "path-util.h" |
7b3e062c | 33 | #include "process-util.h" |
d68c645b | 34 | #include "serialize.h" |
836e4e7e | 35 | #include "set.h" |
4f5dd394 | 36 | #include "special.h" |
218cfe23 | 37 | #include "stat-util.h" |
8b43440b | 38 | #include "string-table.h" |
b5efdb8a | 39 | #include "string-util.h" |
4f5dd394 | 40 | #include "strv.h" |
4f5dd394 | 41 | #include "unit.h" |
1cf40697 | 42 | #include "unit-name.h" |
8dbab37d | 43 | #include "utf8.h" |
5cb5a6ff | 44 | |
7d54a03a LP |
45 | #define RETRY_UMOUNT_MAX 32 |
46 | ||
f50e0a01 | 47 | static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = { |
17f6b640 YW |
48 | [MOUNT_DEAD] = UNIT_INACTIVE, |
49 | [MOUNT_MOUNTING] = UNIT_ACTIVATING, | |
50 | [MOUNT_MOUNTING_DONE] = UNIT_ACTIVATING, | |
51 | [MOUNT_MOUNTED] = UNIT_ACTIVE, | |
52 | [MOUNT_REMOUNTING] = UNIT_RELOADING, | |
032ff4af LP |
53 | [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING, |
54 | [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING, | |
c7c6cf20 | 55 | [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING, |
e537352b LP |
56 | [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING, |
57 | [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING, | |
17f6b640 YW |
58 | [MOUNT_FAILED] = UNIT_FAILED, |
59 | [MOUNT_CLEANING] = UNIT_MAINTENANCE, | |
f50e0a01 | 60 | }; |
5cb5a6ff | 61 | |
718db961 LP |
62 | static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
63 | static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
e3783068 | 64 | static void mount_enter_dead(Mount *m, MountResult f, bool flush_result); |
01400460 YW |
65 | static void mount_enter_mounted(Mount *m, MountResult f); |
66 | static void mount_cycle_clear(Mount *m); | |
35080486 | 67 | static int mount_process_proc_self_mountinfo(Manager *m); |
718db961 | 68 | |
c634f3d2 LP |
69 | static bool MOUNT_STATE_WITH_PROCESS(MountState state) { |
70 | return IN_SET(state, | |
71 | MOUNT_MOUNTING, | |
72 | MOUNT_MOUNTING_DONE, | |
73 | MOUNT_REMOUNTING, | |
74 | MOUNT_REMOUNTING_SIGTERM, | |
75 | MOUNT_REMOUNTING_SIGKILL, | |
76 | MOUNT_UNMOUNTING, | |
77 | MOUNT_UNMOUNTING_SIGTERM, | |
17e9d53d YW |
78 | MOUNT_UNMOUNTING_SIGKILL, |
79 | MOUNT_CLEANING); | |
c634f3d2 LP |
80 | } |
81 | ||
47cd17ea LP |
82 | static MountParameters* get_mount_parameters_fragment(Mount *m) { |
83 | assert(m); | |
84 | ||
85 | if (m->from_fragment) | |
86 | return &m->parameters_fragment; | |
87 | ||
88 | return NULL; | |
89 | } | |
90 | ||
91 | static MountParameters* get_mount_parameters(Mount *m) { | |
92 | assert(m); | |
93 | ||
94 | if (m->from_proc_self_mountinfo) | |
95 | return &m->parameters_proc_self_mountinfo; | |
96 | ||
97 | return get_mount_parameters_fragment(m); | |
98 | } | |
99 | ||
7121cbcf LP |
100 | static bool mount_is_network(const MountParameters *p) { |
101 | assert(p); | |
102 | ||
103 | if (fstab_test_option(p->options, "_netdev\0")) | |
fc676b00 ZJS |
104 | return true; |
105 | ||
7121cbcf | 106 | if (p->fstype && fstype_is_network(p->fstype)) |
fc676b00 ZJS |
107 | return true; |
108 | ||
109 | return false; | |
110 | } | |
111 | ||
5a7c4f4f FB |
112 | static bool mount_is_nofail(const Mount *m) { |
113 | assert(m); | |
114 | ||
115 | if (!m->from_fragment) | |
116 | return false; | |
117 | ||
118 | return fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0"); | |
119 | } | |
120 | ||
d3bd0986 MK |
121 | static bool mount_is_loop(const MountParameters *p) { |
122 | assert(p); | |
123 | ||
124 | if (fstab_test_option(p->options, "loop\0")) | |
125 | return true; | |
126 | ||
127 | return false; | |
128 | } | |
129 | ||
e6a7b9f4 | 130 | static bool mount_is_bind(const MountParameters *p) { |
fc676b00 | 131 | assert(p); |
35df78cd | 132 | return fstab_is_bind(p->options, p->fstype); |
fc676b00 ZJS |
133 | } |
134 | ||
707ecf14 MY |
135 | static int mount_is_bound_to_device(Mount *m) { |
136 | _cleanup_free_ char *value = NULL; | |
ebc8968b | 137 | const MountParameters *p; |
707ecf14 | 138 | int r; |
ebc8968b | 139 | |
47cd17ea LP |
140 | assert(m); |
141 | ||
142 | /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do | |
707ecf14 | 143 | * this by checking for the x-systemd.device-bound= mount option. If it is enabled we use BindsTo=, |
47cd17ea LP |
144 | * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see |
145 | * below. */ | |
146 | ||
147 | p = get_mount_parameters(m); | |
148 | if (!p) | |
149 | return false; | |
ebc8968b | 150 | |
707ecf14 MY |
151 | r = fstab_filter_options(p->options, "x-systemd.device-bound\0", NULL, &value, NULL, NULL); |
152 | if (r < 0) | |
153 | return r; | |
154 | if (r == 0) | |
155 | return -EIDRM; /* If unspecified at all, return recognizable error */ | |
156 | ||
157 | if (isempty(value)) | |
158 | return true; | |
159 | ||
160 | return parse_boolean(value); | |
ebc8968b FB |
161 | } |
162 | ||
47cd17ea | 163 | static bool mount_propagate_stop(Mount *m) { |
707ecf14 MY |
164 | int r; |
165 | ||
47cd17ea LP |
166 | assert(m); |
167 | ||
707ecf14 MY |
168 | r = mount_is_bound_to_device(m); |
169 | if (r >= 0) | |
170 | /* If x-systemd.device-bound=no is explicitly requested by user, don't try to set StopPropagatedFrom=. | |
171 | * Also don't bother if true, since with BindsTo= the stop propagation is implicit. */ | |
47cd17ea | 172 | return false; |
707ecf14 MY |
173 | if (r != -EIDRM) |
174 | log_debug_errno(r, "Failed to get x-systemd.device-bound= option, ignoring: %m"); | |
47cd17ea LP |
175 | |
176 | return m->from_fragment; /* let's propagate stop whenever this is an explicitly configured unit, | |
177 | * otherwise let's not bother. */ | |
178 | } | |
179 | ||
a16e1123 | 180 | static void mount_init(Unit *u) { |
e9fa1bf7 | 181 | Mount *m = ASSERT_PTR(MOUNT(u)); |
5cb5a6ff | 182 | |
ac155bb8 | 183 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 184 | |
c9e120e0 | 185 | m->timeout_usec = u->manager->defaults.timeout_start_usec; |
5804e1b6 | 186 | |
c9e120e0 LP |
187 | m->exec_context.std_output = u->manager->defaults.std_output; |
188 | m->exec_context.std_error = u->manager->defaults.std_error; | |
5804e1b6 | 189 | |
3e5235b0 LP |
190 | m->directory_mode = 0755; |
191 | ||
f00929ad DJL |
192 | /* We need to make sure that /usr/bin/mount is always called |
193 | * in the same process group as us, so that the autofs kernel | |
a16e1123 LP |
194 | * side doesn't send us another mount request while we are |
195 | * already trying to comply its last one. */ | |
74922904 | 196 | m->exec_context.same_pgrp = true; |
8d567588 | 197 | |
360f384f | 198 | m->control_pid = PIDREF_NULL; |
a16e1123 | 199 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; |
c8f4d764 | 200 | |
5bcb0f2b | 201 | u->ignore_on_isolate = true; |
8d567588 LP |
202 | } |
203 | ||
e9276800 | 204 | static int mount_arm_timer(Mount *m, bool relative, usec_t usec) { |
718db961 LP |
205 | assert(m); |
206 | ||
e9276800 | 207 | return unit_arm_timer(UNIT(m), &m->timer_event_source, relative, usec, mount_dispatch_timer); |
718db961 LP |
208 | } |
209 | ||
a16e1123 | 210 | static void mount_unwatch_control_pid(Mount *m) { |
5e94833f | 211 | assert(m); |
ea1e0bf1 | 212 | unit_unwatch_pidref_done(UNIT(m), &m->control_pid); |
5e94833f LP |
213 | } |
214 | ||
e537352b LP |
215 | static void mount_parameters_done(MountParameters *p) { |
216 | assert(p); | |
217 | ||
a26592cf LP |
218 | p->what = mfree(p->what); |
219 | p->options = mfree(p->options); | |
220 | p->fstype = mfree(p->fstype); | |
e537352b LP |
221 | } |
222 | ||
87f0e418 | 223 | static void mount_done(Unit *u) { |
e9fa1bf7 | 224 | Mount *m = ASSERT_PTR(MOUNT(u)); |
034c6ed7 | 225 | |
a1e58e8e | 226 | m->where = mfree(m->where); |
f50e0a01 | 227 | |
e537352b LP |
228 | mount_parameters_done(&m->parameters_proc_self_mountinfo); |
229 | mount_parameters_done(&m->parameters_fragment); | |
ef734fd6 | 230 | |
28135da3 | 231 | m->exec_runtime = exec_runtime_free(m->exec_runtime); |
9cc54544 | 232 | |
e537352b LP |
233 | exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); |
234 | m->control_command = NULL; | |
f50e0a01 | 235 | |
a16e1123 | 236 | mount_unwatch_control_pid(m); |
f50e0a01 | 237 | |
5dcadb4c | 238 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
f50e0a01 LP |
239 | } |
240 | ||
9ddaa3e4 | 241 | static int update_parameters_proc_self_mountinfo( |
b7bbf890 ZJS |
242 | Mount *m, |
243 | const char *what, | |
244 | const char *options, | |
245 | const char *fstype) { | |
246 | ||
247 | MountParameters *p; | |
248 | int r, q, w; | |
249 | ||
e9fa1bf7 MY |
250 | assert(m); |
251 | ||
b7bbf890 ZJS |
252 | p = &m->parameters_proc_self_mountinfo; |
253 | ||
bcf58ff5 YW |
254 | r = free_and_strdup(&p->what, what); |
255 | if (r < 0) | |
256 | return r; | |
aaf7b0e4 | 257 | |
bcf58ff5 YW |
258 | q = free_and_strdup(&p->options, options); |
259 | if (q < 0) | |
260 | return q; | |
b7bbf890 ZJS |
261 | |
262 | w = free_and_strdup(&p->fstype, fstype); | |
263 | if (w < 0) | |
264 | return w; | |
265 | ||
266 | return r > 0 || q > 0 || w > 0; | |
267 | } | |
268 | ||
eef85c4a | 269 | static int mount_add_mount_dependencies(Mount *m) { |
5c78d8e2 | 270 | MountParameters *pm; |
b08d03ff LP |
271 | int r; |
272 | ||
6e2ef85b | 273 | assert(m); |
b08d03ff | 274 | |
a57f7e2c | 275 | if (!path_equal(m->where, "/")) { |
eef85c4a LP |
276 | _cleanup_free_ char *parent = NULL; |
277 | ||
278 | /* Adds in links to other mount points that might lie further up in the hierarchy */ | |
5f311f8c | 279 | |
45519d13 LP |
280 | r = path_extract_directory(m->where, &parent); |
281 | if (r < 0) | |
282 | return r; | |
01f78473 | 283 | |
9e615fa3 | 284 | r = unit_add_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT, UNIT_MOUNT_REQUIRES); |
4f0eedb7 | 285 | if (r < 0) |
01f78473 | 286 | return r; |
4f0eedb7 | 287 | } |
01f78473 | 288 | |
eef85c4a LP |
289 | /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount |
290 | * or a loop mount) to be available. */ | |
a57f7e2c | 291 | pm = get_mount_parameters_fragment(m); |
fc676b00 ZJS |
292 | if (pm && pm->what && |
293 | path_is_absolute(pm->what) && | |
d3bd0986 | 294 | (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) { |
fc676b00 | 295 | |
9e615fa3 | 296 | r = unit_add_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES); |
4f0eedb7 | 297 | if (r < 0) |
6e2ef85b | 298 | return r; |
4f0eedb7 | 299 | } |
b08d03ff | 300 | |
eef85c4a | 301 | /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */ |
9e615fa3 LB |
302 | for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; ++t) { |
303 | Unit *other; | |
304 | Set *s = manager_get_units_needing_mounts_for(UNIT(m)->manager, m->where, t); | |
305 | ||
306 | SET_FOREACH(other, s) { | |
307 | if (other->load_state != UNIT_LOADED) | |
308 | continue; | |
309 | ||
310 | if (other == UNIT(m)) | |
311 | continue; | |
312 | ||
313 | r = unit_add_dependency( | |
314 | other, | |
315 | UNIT_AFTER, | |
316 | UNIT(m), | |
317 | /* add_reference= */ true, | |
318 | UNIT_DEPENDENCY_PATH); | |
a57f7e2c LP |
319 | if (r < 0) |
320 | return r; | |
9e615fa3 LB |
321 | |
322 | if (UNIT(m)->fragment_path) { | |
323 | /* If we have fragment configuration, then make this dependency required/wanted */ | |
324 | r = unit_add_dependency( | |
325 | other, | |
326 | unit_mount_dependency_type_to_dependency_type(t), | |
327 | UNIT(m), | |
328 | /* add_reference= */ true, | |
329 | UNIT_DEPENDENCY_PATH); | |
330 | if (r < 0) | |
331 | return r; | |
332 | } | |
a57f7e2c | 333 | } |
7c8fa05c LP |
334 | } |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
eef85c4a | 339 | static int mount_add_device_dependencies(Mount *m) { |
87c734ee | 340 | UnitDependencyMask mask; |
eef85c4a | 341 | MountParameters *p; |
ebc8968b | 342 | UnitDependency dep; |
9fff8a1f | 343 | int r; |
173a8d04 LP |
344 | |
345 | assert(m); | |
346 | ||
556b6f4e DDM |
347 | log_unit_trace(UNIT(m), "Processing implicit device dependencies"); |
348 | ||
06e97888 | 349 | p = get_mount_parameters(m); |
556b6f4e DDM |
350 | if (!p) { |
351 | log_unit_trace(UNIT(m), "Missing mount parameters, skipping implicit device dependencies"); | |
173a8d04 | 352 | return 0; |
556b6f4e | 353 | } |
173a8d04 | 354 | |
556b6f4e DDM |
355 | if (!p->what) { |
356 | log_unit_trace(UNIT(m), "Missing mount source, skipping implicit device dependencies"); | |
173a8d04 | 357 | return 0; |
556b6f4e | 358 | } |
5c78d8e2 | 359 | |
556b6f4e DDM |
360 | if (mount_is_bind(p)) { |
361 | log_unit_trace(UNIT(m), "Mount unit is a bind mount, skipping implicit device dependencies"); | |
dd144c63 | 362 | return 0; |
556b6f4e | 363 | } |
dd144c63 | 364 | |
556b6f4e DDM |
365 | if (!is_device_path(p->what)) { |
366 | log_unit_trace(UNIT(m), "Mount source is not a device path, skipping implicit device dependencies"); | |
dd144c63 | 367 | return 0; |
556b6f4e | 368 | } |
dd144c63 | 369 | |
5de0acf4 LP |
370 | /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for |
371 | * the root file system specified on the kernel command line. Ignore it here. */ | |
556b6f4e DDM |
372 | if (PATH_IN_SET(p->what, "/dev/root", "/dev/nfs")) { |
373 | log_unit_trace(UNIT(m), "Mount source is in /dev/root or /dev/nfs, skipping implicit device dependencies"); | |
7ba2711d | 374 | return 0; |
556b6f4e | 375 | } |
7ba2711d | 376 | |
556b6f4e DDM |
377 | if (path_equal(m->where, "/")) { |
378 | log_unit_trace(UNIT(m), "Mount destination is '/', skipping implicit device dependencies"); | |
dd144c63 | 379 | return 0; |
556b6f4e | 380 | } |
dd144c63 | 381 | |
44b0d1fd | 382 | /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to |
47cd17ea LP |
383 | * races when mounts are established by other tools with different backing devices than what we |
384 | * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or | |
385 | * udev property) so the mount units are automatically stopped when the device disappears | |
44b0d1fd | 386 | * suddenly. */ |
707ecf14 | 387 | dep = mount_is_bound_to_device(m) > 0 ? UNIT_BINDS_TO : UNIT_REQUIRES; |
ebc8968b | 388 | |
87c734ee FB |
389 | /* We always use 'what' from /proc/self/mountinfo if mounted */ |
390 | mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE; | |
391 | ||
392 | r = unit_add_node_dependency(UNIT(m), p->what, dep, mask); | |
dd144c63 LP |
393 | if (r < 0) |
394 | return r; | |
556b6f4e DDM |
395 | if (r > 0) |
396 | log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(dep), p->what); | |
397 | ||
47cd17ea | 398 | if (mount_propagate_stop(m)) { |
87c734ee | 399 | r = unit_add_node_dependency(UNIT(m), p->what, UNIT_STOP_PROPAGATED_FROM, mask); |
47cd17ea LP |
400 | if (r < 0) |
401 | return r; | |
556b6f4e DDM |
402 | if (r > 0) |
403 | log_unit_trace(UNIT(m), "Added %s dependency on %s", | |
404 | unit_dependency_to_string(UNIT_STOP_PROPAGATED_FROM), p->what); | |
47cd17ea | 405 | } |
9fff8a1f | 406 | |
87c734ee | 407 | r = unit_add_blockdev_dependency(UNIT(m), p->what, mask); |
556b6f4e DDM |
408 | if (r > 0) |
409 | log_unit_trace(UNIT(m), "Added %s dependency on %s", unit_dependency_to_string(UNIT_AFTER), p->what); | |
410 | ||
49267b1b | 411 | return 0; |
173a8d04 LP |
412 | } |
413 | ||
39c79477 | 414 | static bool mount_is_extrinsic(Unit *u) { |
e9fa1bf7 | 415 | Mount *m = ASSERT_PTR(MOUNT(u)); |
88ac30a1 TG |
416 | MountParameters *p; |
417 | ||
bc9e5a4c FB |
418 | /* Returns true for all units that are "magic" and should be excluded from the usual |
419 | * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally | |
420 | * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them | |
421 | * ourselves but it's fine if the user operates on them with us. */ | |
ad2706db | 422 | |
bc9e5a4c | 423 | /* We only automatically manage mounts if we are in system mode */ |
39c79477 | 424 | if (MANAGER_IS_USER(u->manager)) |
b8e5776d LP |
425 | return true; |
426 | ||
88ac30a1 | 427 | p = get_mount_parameters(m); |
bc9e5a4c | 428 | if (p && fstab_is_extrinsic(m->where, p->options)) |
ad2706db | 429 | return true; |
88ac30a1 | 430 | |
ad2706db | 431 | return false; |
88ac30a1 TG |
432 | } |
433 | ||
87c734ee | 434 | static int mount_add_default_ordering_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) { |
83cdc870 | 435 | const char *after, *before, *e; |
d54bab90 | 436 | int r; |
2edd4434 LP |
437 | |
438 | assert(m); | |
439 | ||
83cdc870 FB |
440 | e = path_startswith(m->where, "/sysroot"); |
441 | if (e && in_initrd()) { | |
442 | /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW, | |
443 | * it's not technically part of the basic initrd filesystem itself, and so | |
6e017b19 WF |
444 | * shouldn't inherit the default Before=local-fs.target dependency. However, |
445 | * these mounts still need to start after local-fs-pre.target, as a sync point | |
760e99bb | 446 | * for things like systemd-hibernate-resume.service that should start before |
6e017b19 | 447 | * any mounts. */ |
83cdc870 | 448 | |
6e017b19 | 449 | after = SPECIAL_LOCAL_FS_PRE_TARGET; |
83cdc870 FB |
450 | before = isempty(e) ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_INITRD_FS_TARGET; |
451 | ||
dbfc0960 | 452 | } else if (in_initrd() && path_startswith(m->where, "/sysusr/usr")) { |
6e017b19 | 453 | after = SPECIAL_LOCAL_FS_PRE_TARGET; |
dbfc0960 YW |
454 | before = SPECIAL_INITRD_USR_FS_TARGET; |
455 | ||
3c0a1b1e | 456 | } else if (mount_is_network(p)) { |
ea0ec5ce | 457 | after = SPECIAL_REMOTE_FS_PRE_TARGET; |
d54bab90 | 458 | before = SPECIAL_REMOTE_FS_TARGET; |
2ec15c4f | 459 | |
d54bab90 | 460 | } else { |
ea0ec5ce | 461 | after = SPECIAL_LOCAL_FS_PRE_TARGET; |
d54bab90 LP |
462 | before = SPECIAL_LOCAL_FS_TARGET; |
463 | } | |
464 | ||
92e64e9a | 465 | if (before && !mount_is_nofail(m)) { |
87c734ee | 466 | r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, /* add_reference= */ true, mask); |
8c8203db YW |
467 | if (r < 0) |
468 | return r; | |
469 | } | |
ea0ec5ce | 470 | |
92e64e9a LP |
471 | if (after) { |
472 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, /* add_reference= */ true, mask); | |
473 | if (r < 0) | |
474 | return r; | |
475 | } | |
476 | ||
477 | r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, | |
478 | /* add_reference= */ true, mask); | |
6e017b19 WF |
479 | if (r < 0) |
480 | return r; | |
e8d2f6cd | 481 | |
92e64e9a | 482 | /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */ |
88188e1f | 483 | if (streq_ptr(p->fstype, "tmpfs")) { |
92e64e9a LP |
484 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, |
485 | /* add_reference= */ true, mask); | |
486 | if (r < 0) | |
487 | return r; | |
488 | } | |
489 | ||
490 | return 0; | |
61154cf9 FB |
491 | } |
492 | ||
75b09529 LP |
493 | static int mount_add_default_network_dependencies(Mount *m, MountParameters *p, UnitDependencyMask mask) { |
494 | int r; | |
495 | ||
496 | assert(m); | |
497 | ||
498 | if (!mount_is_network(p)) | |
499 | return 0; | |
500 | ||
501 | /* We order ourselves after network.target. This is primarily useful at shutdown: services that take | |
502 | * down the network should order themselves before network.target, so that they are shut down only | |
503 | * after this mount unit is stopped. */ | |
504 | ||
505 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, | |
506 | /* add_reference= */ true, mask); | |
6e017b19 WF |
507 | if (r < 0) |
508 | return r; | |
e8d2f6cd | 509 | |
75b09529 LP |
510 | /* We pull in network-online.target, and order ourselves after it. This is useful at start-up to |
511 | * actively pull in tools that want to be started before we start mounting network file systems, and | |
512 | * whose purpose it is to delay this until the network is "up". */ | |
513 | ||
514 | return unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, | |
87c734ee | 515 | /* add_reference= */ true, mask); |
61154cf9 FB |
516 | } |
517 | ||
2edd4434 | 518 | static int mount_add_default_dependencies(Mount *m) { |
87c734ee | 519 | UnitDependencyMask mask; |
9ddc4a26 | 520 | MountParameters *p; |
d54bab90 | 521 | int r; |
2edd4434 LP |
522 | |
523 | assert(m); | |
524 | ||
4c9ea260 LP |
525 | if (!UNIT(m)->default_dependencies) |
526 | return 0; | |
527 | ||
61154cf9 FB |
528 | /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are |
529 | * guaranteed to stay mounted the whole time, since our system is on it. Also, don't | |
530 | * bother with anything mounted below virtual file systems, it's also going to be virtual, | |
531 | * and hence not worth the effort. */ | |
39c79477 | 532 | if (mount_is_extrinsic(UNIT(m))) |
6b1dc2bd | 533 | return 0; |
2edd4434 | 534 | |
874d3404 LP |
535 | p = get_mount_parameters(m); |
536 | if (!p) | |
6b1dc2bd LP |
537 | return 0; |
538 | ||
87c734ee FB |
539 | mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO : UNIT_DEPENDENCY_MOUNT_FILE; |
540 | ||
541 | r = mount_add_default_ordering_dependencies(m, p, mask); | |
ad2706db LP |
542 | if (r < 0) |
543 | return r; | |
eef85c4a | 544 | |
75b09529 LP |
545 | r = mount_add_default_network_dependencies(m, p, mask); |
546 | if (r < 0) | |
547 | return r; | |
3e3852b3 | 548 | |
84214541 | 549 | return 0; |
2edd4434 LP |
550 | } |
551 | ||
8d567588 | 552 | static int mount_verify(Mount *m) { |
a57f7e2c | 553 | _cleanup_free_ char *e = NULL; |
b294b79f | 554 | MountParameters *p; |
7410616c | 555 | int r; |
a57f7e2c | 556 | |
8d567588 | 557 | assert(m); |
75193d41 | 558 | assert(UNIT(m)->load_state == UNIT_LOADED); |
8d567588 | 559 | |
1df96fcb | 560 | if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual) |
8cbef760 LP |
561 | return -ENOENT; |
562 | ||
7410616c LP |
563 | r = unit_name_from_path(m->where, ".mount", &e); |
564 | if (r < 0) | |
f2341e0a | 565 | return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m"); |
8d567588 | 566 | |
d85ff944 YW |
567 | if (!unit_has_name(UNIT(m), e)) |
568 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing."); | |
8d567588 | 569 | |
d85ff944 | 570 | if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) |
88188e1f MY |
571 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), |
572 | "Cannot create mount unit for API file system '%s'. Refusing.", m->where); | |
573 | ||
ae5c4aa6 | 574 | if (mount_point_is_credentials(UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME], m->where)) |
88188e1f MY |
575 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), |
576 | "Cannot create mount unit for credential mount '%s'. Refusing.", m->where); | |
33ff02c9 | 577 | |
b294b79f | 578 | p = get_mount_parameters_fragment(m); |
0879fbd6 | 579 | if (p && !p->what && !UNIT(m)->perpetual) |
88188e1f | 580 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "What= setting is missing. Refusing."); |
4e85aff4 | 581 | |
8d567588 LP |
582 | return 0; |
583 | } | |
584 | ||
bf7eedbf LP |
585 | static int mount_add_non_exec_dependencies(Mount *m) { |
586 | int r; | |
49267b1b | 587 | |
bf7eedbf LP |
588 | assert(m); |
589 | ||
9c77ffc7 FB |
590 | /* We may be called due to this mount appearing in /proc/self/mountinfo, hence we clear all existing |
591 | * dependencies that were initialized from the unit file but whose final value really depends on the | |
592 | * content of /proc/self/mountinfo. Some (such as m->where) might have become stale now. */ | |
87c734ee | 593 | unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO | UNIT_DEPENDENCY_MOUNT_FILE); |
49267b1b YW |
594 | |
595 | if (!m->where) | |
596 | return 0; | |
597 | ||
bf7eedbf LP |
598 | /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies |
599 | * resulting from the ExecContext and such. */ | |
600 | ||
601 | r = mount_add_device_dependencies(m); | |
602 | if (r < 0) | |
603 | return r; | |
604 | ||
605 | r = mount_add_mount_dependencies(m); | |
606 | if (r < 0) | |
607 | return r; | |
608 | ||
bf7eedbf LP |
609 | r = mount_add_default_dependencies(m); |
610 | if (r < 0) | |
611 | return r; | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
1a4ac875 | 616 | static int mount_add_extras(Mount *m) { |
e9fa1bf7 | 617 | Unit *u = UNIT(ASSERT_PTR(m)); |
e537352b LP |
618 | int r; |
619 | ||
1f736476 LP |
620 | /* Note: this call might be called after we already have been loaded once (and even when it has already been |
621 | * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready | |
622 | * to run with an already set up unit. */ | |
623 | ||
e821075a | 624 | if (u->fragment_path) |
1a4ac875 | 625 | m->from_fragment = true; |
e537352b | 626 | |
1a4ac875 | 627 | if (!m->where) { |
7410616c | 628 | r = unit_name_to_path(u->id, &m->where); |
1d0727e7 MS |
629 | if (r == -ENAMETOOLONG) |
630 | log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. " | |
631 | "Set \"Where=\" in the unit file explicitly."); | |
7410616c LP |
632 | if (r < 0) |
633 | return r; | |
1a4ac875 | 634 | } |
a16e1123 | 635 | |
4ff361cc | 636 | path_simplify(m->where); |
e537352b | 637 | |
e821075a | 638 | if (!u->description) { |
222b0b05 LP |
639 | _cleanup_free_ char *w = mount_get_where_escaped(m); |
640 | if (!w) | |
641 | return log_oom(); | |
642 | ||
643 | r = unit_set_description(u, w); | |
1a4ac875 | 644 | if (r < 0) |
173a8d04 | 645 | return r; |
1a4ac875 | 646 | } |
6e2ef85b | 647 | |
598459ce LP |
648 | r = unit_patch_contexts(u); |
649 | if (r < 0) | |
650 | return r; | |
4e67ddd6 | 651 | |
598459ce | 652 | r = unit_add_exec_dependencies(u, &m->exec_context); |
a016b922 LP |
653 | if (r < 0) |
654 | return r; | |
655 | ||
d79200e2 | 656 | r = unit_set_default_slice(u); |
1a4ac875 MS |
657 | if (r < 0) |
658 | return r; | |
659 | ||
bf7eedbf | 660 | r = mount_add_non_exec_dependencies(m); |
4c9ea260 LP |
661 | if (r < 0) |
662 | return r; | |
598459ce | 663 | |
1a4ac875 MS |
664 | return 0; |
665 | } | |
666 | ||
75193d41 | 667 | static void mount_load_root_mount(Unit *u) { |
e9fa1bf7 | 668 | Mount *m = ASSERT_PTR(MOUNT(u)); |
11222d0f LP |
669 | |
670 | if (!unit_has_name(u, SPECIAL_ROOT_MOUNT)) | |
75193d41 | 671 | return; |
11222d0f LP |
672 | |
673 | u->perpetual = true; | |
674 | u->default_dependencies = false; | |
675 | ||
676 | /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */ | |
e9fa1bf7 MY |
677 | m->exec_context.std_output = EXEC_OUTPUT_NULL; |
678 | m->exec_context.std_input = EXEC_INPUT_NULL; | |
11222d0f LP |
679 | |
680 | if (!u->description) | |
681 | u->description = strdup("Root Mount"); | |
11222d0f LP |
682 | } |
683 | ||
1a4ac875 | 684 | static int mount_load(Unit *u) { |
e9fa1bf7 MY |
685 | Mount *m = ASSERT_PTR(MOUNT(u)); |
686 | int r; | |
1a4ac875 | 687 | |
1a4ac875 MS |
688 | assert(u->load_state == UNIT_STUB); |
689 | ||
75193d41 | 690 | mount_load_root_mount(u); |
11222d0f | 691 | |
4ecb673e MY |
692 | bool from_kernel = m->from_proc_self_mountinfo || u->perpetual; |
693 | ||
694 | r = unit_load_fragment_and_dropin(u, /* fragment_required = */ !from_kernel); | |
780ae022 LP |
695 | |
696 | /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the | |
697 | * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus | |
698 | * we need to update the state in our unit to track it. After all, consider that we don't allow changing the | |
699 | * 'slice' field for a unit once it is active. */ | |
4ecb673e MY |
700 | if (u->load_state == UNIT_LOADED || from_kernel) |
701 | RET_GATHER(r, mount_add_extras(m)); | |
780ae022 | 702 | |
1a4ac875 MS |
703 | if (r < 0) |
704 | return r; | |
4ecb673e | 705 | |
75193d41 ZJS |
706 | if (u->load_state != UNIT_LOADED) |
707 | return 0; | |
e537352b | 708 | |
8d567588 | 709 | return mount_verify(m); |
e537352b LP |
710 | } |
711 | ||
712 | static void mount_set_state(Mount *m, MountState state) { | |
713 | MountState old_state; | |
e9fa1bf7 | 714 | |
e537352b LP |
715 | assert(m); |
716 | ||
6fcbec6f LP |
717 | if (m->state != state) |
718 | bus_unit_send_pending_change_signal(UNIT(m), false); | |
719 | ||
e537352b LP |
720 | old_state = m->state; |
721 | m->state = state; | |
722 | ||
c634f3d2 | 723 | if (!MOUNT_STATE_WITH_PROCESS(state)) { |
5dcadb4c | 724 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
a16e1123 | 725 | mount_unwatch_control_pid(m); |
e537352b | 726 | m->control_command = NULL; |
a16e1123 | 727 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; |
e537352b LP |
728 | } |
729 | ||
730 | if (state != old_state) | |
f2341e0a | 731 | log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state)); |
e537352b | 732 | |
96b09de5 | 733 | unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS); |
e537352b LP |
734 | } |
735 | ||
be847e82 | 736 | static int mount_coldplug(Unit *u) { |
e9fa1bf7 | 737 | Mount *m = ASSERT_PTR(MOUNT(u)); |
a16e1123 | 738 | int r; |
e537352b | 739 | |
e537352b LP |
740 | assert(m->state == MOUNT_DEAD); |
741 | ||
01400460 | 742 | if (m->deserialized_state == m->state) |
5bcb0f2b | 743 | return 0; |
e537352b | 744 | |
360f384f | 745 | if (pidref_is_set(&m->control_pid) && |
4d9f092b | 746 | pidref_is_unwaited(&m->control_pid) > 0 && |
01400460 | 747 | MOUNT_STATE_WITH_PROCESS(m->deserialized_state)) { |
5bcb0f2b | 748 | |
495e75ed | 749 | r = unit_watch_pidref(UNIT(m), &m->control_pid, /* exclusive= */ false); |
5bcb0f2b LP |
750 | if (r < 0) |
751 | return r; | |
e537352b | 752 | |
e9276800 | 753 | r = mount_arm_timer(m, /* relative= */ false, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec)); |
5bcb0f2b LP |
754 | if (r < 0) |
755 | return r; | |
a16e1123 | 756 | } |
e537352b | 757 | |
f26b2ec4 | 758 | if (!IN_SET(m->deserialized_state, MOUNT_DEAD, MOUNT_FAILED)) |
e8a565cb | 759 | (void) unit_setup_exec_runtime(u); |
29206d46 | 760 | |
01400460 | 761 | mount_set_state(m, m->deserialized_state); |
e537352b | 762 | return 0; |
e537352b LP |
763 | } |
764 | ||
01400460 | 765 | static void mount_catchup(Unit *u) { |
e9fa1bf7 | 766 | Mount *m = ASSERT_PTR(MOUNT(u)); |
01400460 YW |
767 | |
768 | /* Adjust the deserialized state. See comments in mount_process_proc_self_mountinfo(). */ | |
769 | if (m->from_proc_self_mountinfo) | |
770 | switch (m->state) { | |
771 | case MOUNT_DEAD: | |
772 | case MOUNT_FAILED: | |
360f384f | 773 | assert(!pidref_is_set(&m->control_pid)); |
039f4284 | 774 | (void) unit_acquire_invocation_id(u); |
01400460 YW |
775 | mount_cycle_clear(m); |
776 | mount_enter_mounted(m, MOUNT_SUCCESS); | |
777 | break; | |
778 | case MOUNT_MOUNTING: | |
360f384f | 779 | assert(pidref_is_set(&m->control_pid)); |
01400460 YW |
780 | mount_set_state(m, MOUNT_MOUNTING_DONE); |
781 | break; | |
782 | default: | |
5c9feb2d | 783 | ; |
01400460 YW |
784 | } |
785 | else | |
786 | switch (m->state) { | |
787 | case MOUNT_MOUNTING_DONE: | |
360f384f | 788 | assert(pidref_is_set(&m->control_pid)); |
01400460 YW |
789 | mount_set_state(m, MOUNT_MOUNTING); |
790 | break; | |
791 | case MOUNT_MOUNTED: | |
360f384f | 792 | assert(!pidref_is_set(&m->control_pid)); |
e3783068 | 793 | mount_enter_dead(m, MOUNT_SUCCESS, /* flush_result = */ false); |
01400460 YW |
794 | break; |
795 | default: | |
5c9feb2d | 796 | ; |
01400460 YW |
797 | } |
798 | } | |
799 | ||
e537352b | 800 | static void mount_dump(Unit *u, FILE *f, const char *prefix) { |
e9fa1bf7 | 801 | Mount *m = ASSERT_PTR(MOUNT(u)); |
e537352b | 802 | MountParameters *p; |
b9e4d9ba | 803 | const char *prefix2; |
e537352b | 804 | |
e537352b LP |
805 | assert(f); |
806 | ||
b9e4d9ba LP |
807 | prefix = strempty(prefix); |
808 | prefix2 = strjoina(prefix, "\t"); | |
809 | ||
cb39ed3f | 810 | p = get_mount_parameters(m); |
e537352b LP |
811 | |
812 | fprintf(f, | |
813 | "%sMount State: %s\n" | |
81a5c6d0 | 814 | "%sResult: %s\n" |
17e9d53d | 815 | "%sClean Result: %s\n" |
e537352b LP |
816 | "%sWhere: %s\n" |
817 | "%sWhat: %s\n" | |
818 | "%sFile System Type: %s\n" | |
819 | "%sOptions: %s\n" | |
e537352b LP |
820 | "%sFrom /proc/self/mountinfo: %s\n" |
821 | "%sFrom fragment: %s\n" | |
ad2706db | 822 | "%sExtrinsic: %s\n" |
e520950a | 823 | "%sDirectoryMode: %04o\n" |
49915de2 | 824 | "%sSloppyOptions: %s\n" |
4f8d40a9 | 825 | "%sLazyUnmount: %s\n" |
91899792 | 826 | "%sForceUnmount: %s\n" |
c600357b | 827 | "%sReadWriteOnly: %s\n" |
ac8956ef | 828 | "%sTimeoutSec: %s\n", |
a16e1123 | 829 | prefix, mount_state_to_string(m->state), |
81a5c6d0 | 830 | prefix, mount_result_to_string(m->result), |
17e9d53d | 831 | prefix, mount_result_to_string(m->clean_result), |
e537352b | 832 | prefix, m->where, |
1e4fc9b1 HH |
833 | prefix, p ? strna(p->what) : "n/a", |
834 | prefix, p ? strna(p->fstype) : "n/a", | |
835 | prefix, p ? strna(p->options) : "n/a", | |
e537352b LP |
836 | prefix, yes_no(m->from_proc_self_mountinfo), |
837 | prefix, yes_no(m->from_fragment), | |
39c79477 | 838 | prefix, yes_no(mount_is_extrinsic(u)), |
e520950a | 839 | prefix, m->directory_mode, |
49915de2 | 840 | prefix, yes_no(m->sloppy_options), |
4f8d40a9 | 841 | prefix, yes_no(m->lazy_unmount), |
91899792 | 842 | prefix, yes_no(m->force_unmount), |
c600357b | 843 | prefix, yes_no(m->read_write_only), |
5291f26d | 844 | prefix, FORMAT_TIMESPAN(m->timeout_usec, USEC_PER_SEC)); |
e537352b | 845 | |
360f384f | 846 | if (pidref_is_set(&m->control_pid)) |
e537352b | 847 | fprintf(f, |
ccd06097 | 848 | "%sControl PID: "PID_FMT"\n", |
360f384f | 849 | prefix, m->control_pid.pid); |
e537352b LP |
850 | |
851 | exec_context_dump(&m->exec_context, f, prefix); | |
4819ff03 | 852 | kill_context_dump(&m->kill_context, f, prefix); |
bc0623df | 853 | cgroup_context_dump(UNIT(m), f, prefix); |
b9e4d9ba LP |
854 | |
855 | for (MountExecCommand c = 0; c < _MOUNT_EXEC_COMMAND_MAX; c++) { | |
856 | if (!m->exec_command[c].argv) | |
857 | continue; | |
858 | ||
859 | fprintf(f, "%s%s %s:\n", | |
1ae9b0cf | 860 | prefix, glyph(GLYPH_ARROW_RIGHT), mount_exec_command_to_string(c)); |
b9e4d9ba LP |
861 | |
862 | exec_command_dump(m->exec_command + c, f, prefix2); | |
863 | } | |
e537352b LP |
864 | } |
865 | ||
6577cf1b | 866 | static ExecFlags mount_exec_flags(MountState state) { |
867 | ExecFlags flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN; | |
868 | ||
869 | assert(IN_SET(state, MOUNT_MOUNTING, MOUNT_REMOUNTING, MOUNT_UNMOUNTING)); | |
870 | ||
871 | if (IN_SET(state, MOUNT_MOUNTING, MOUNT_REMOUNTING)) | |
872 | flags |= EXEC_SETUP_CREDENTIALS; | |
873 | ||
874 | return flags; | |
875 | } | |
876 | ||
877 | static int mount_spawn(Mount *m, ExecCommand *c, ExecFlags flags, PidRef *ret_pid) { | |
878 | _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT(flags); | |
360f384f | 879 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
3c7416b6 | 880 | int r; |
a16e1123 LP |
881 | |
882 | assert(m); | |
883 | assert(c); | |
ef25552e | 884 | assert(ret_pid); |
a16e1123 | 885 | |
3c7416b6 | 886 | r = unit_prepare_exec(UNIT(m)); |
29206d46 LP |
887 | if (r < 0) |
888 | return r; | |
889 | ||
e9276800 | 890 | r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec); |
36697dc0 | 891 | if (r < 0) |
36c16a7c | 892 | return r; |
a16e1123 | 893 | |
1ad6e8b3 LP |
894 | r = unit_set_exec_params(UNIT(m), &exec_params); |
895 | if (r < 0) | |
896 | return r; | |
9fa95f85 | 897 | |
8144537a ŁS |
898 | /* Assume the label inherited from systemd as the fallback */ |
899 | exec_params.fallback_smack_process_label = NULL; | |
900 | ||
f2341e0a LP |
901 | r = exec_spawn(UNIT(m), |
902 | c, | |
4ad49000 | 903 | &m->exec_context, |
9fa95f85 | 904 | &exec_params, |
613b411c | 905 | m->exec_runtime, |
6bb00842 | 906 | &m->cgroup_context, |
556d2bc4 | 907 | &pidref); |
4ad49000 | 908 | if (r < 0) |
36c16a7c | 909 | return r; |
a16e1123 | 910 | |
495e75ed | 911 | r = unit_watch_pidref(UNIT(m), &pidref, /* exclusive= */ true); |
360f384f LP |
912 | if (r < 0) |
913 | return r; | |
914 | ||
915 | *ret_pid = TAKE_PIDREF(pidref); | |
a16e1123 | 916 | return 0; |
a16e1123 LP |
917 | } |
918 | ||
e3783068 | 919 | static void mount_enter_dead(Mount *m, MountResult f, bool flush_result) { |
e537352b LP |
920 | assert(m); |
921 | ||
e3783068 | 922 | if (m->result == MOUNT_SUCCESS || flush_result) |
9d2f5178 | 923 | m->result = f; |
e537352b | 924 | |
aac99f30 | 925 | unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result)); |
95e631da | 926 | unit_warn_leftover_processes(UNIT(m), /* start = */ false); |
4c425434 | 927 | |
29206d46 LP |
928 | mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD); |
929 | ||
28135da3 | 930 | m->exec_runtime = exec_runtime_destroy(m->exec_runtime); |
613b411c | 931 | |
703b1b7f | 932 | unit_destroy_runtime_data(UNIT(m), &m->exec_context, /* destroy_runtime_dir = */ true); |
e66cf1a3 | 933 | |
00d9ef85 LP |
934 | unit_unref_uid_gid(UNIT(m), true); |
935 | ||
49267b1b YW |
936 | /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from |
937 | * .mount unit. */ | |
938 | (void) mount_add_non_exec_dependencies(m); | |
e537352b LP |
939 | } |
940 | ||
9d2f5178 | 941 | static void mount_enter_mounted(Mount *m, MountResult f) { |
80876c20 LP |
942 | assert(m); |
943 | ||
a0fef983 | 944 | if (m->result == MOUNT_SUCCESS) |
9d2f5178 | 945 | m->result = f; |
80876c20 | 946 | |
8abeebdf MY |
947 | /* Refer to service_set_state() handling of SERVICE_EXITED state for rationale. In particular |
948 | * for mount units let's not leave cred mounts around, otherwise the actual number of mounts would be | |
949 | * somewhat doubled. | |
950 | * | |
951 | * Note that this effectively means fresh creds are used during remount, but that's mostly what | |
952 | * users would expect anyways. */ | |
953 | unit_destroy_runtime_data(UNIT(m), &m->exec_context, /* destroy_runtime_dir = */ false); | |
954 | ||
80876c20 LP |
955 | mount_set_state(m, MOUNT_MOUNTED); |
956 | } | |
957 | ||
e3783068 | 958 | static void mount_enter_dead_or_mounted(Mount *m, MountResult f, bool flush_result) { |
22af0e58 LP |
959 | assert(m); |
960 | ||
e3783068 MY |
961 | /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. |
962 | * We use this whenever we executed an operation, so that our internal state reflects what | |
963 | * the kernel says again, after all ultimately we just mirror the kernel's internal state on this. | |
964 | * | |
965 | * Note that flush_result only applies to mount_enter_dead(), since that's when the result gets | |
966 | * turned into unit end state. */ | |
22af0e58 LP |
967 | |
968 | if (m->from_proc_self_mountinfo) | |
969 | mount_enter_mounted(m, f); | |
970 | else | |
e3783068 | 971 | mount_enter_dead(m, f, flush_result); |
22af0e58 LP |
972 | } |
973 | ||
974 | static int state_to_kill_operation(MountState state) { | |
975 | switch (state) { | |
976 | ||
977 | case MOUNT_REMOUNTING_SIGTERM: | |
a232ebcc ZJS |
978 | return KILL_RESTART; |
979 | ||
22af0e58 LP |
980 | case MOUNT_UNMOUNTING_SIGTERM: |
981 | return KILL_TERMINATE; | |
982 | ||
983 | case MOUNT_REMOUNTING_SIGKILL: | |
984 | case MOUNT_UNMOUNTING_SIGKILL: | |
985 | return KILL_KILL; | |
986 | ||
987 | default: | |
988 | return _KILL_OPERATION_INVALID; | |
989 | } | |
990 | } | |
991 | ||
9d2f5178 | 992 | static void mount_enter_signal(Mount *m, MountState state, MountResult f) { |
e537352b LP |
993 | int r; |
994 | ||
995 | assert(m); | |
996 | ||
a0fef983 | 997 | if (m->result == MOUNT_SUCCESS) |
9d2f5178 | 998 | m->result = f; |
e537352b | 999 | |
b826e317 | 1000 | r = unit_kill_context(UNIT(m), state_to_kill_operation(state)); |
d30eb0b5 LP |
1001 | if (r < 0) { |
1002 | log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m"); | |
cd2086fe | 1003 | goto fail; |
d30eb0b5 | 1004 | } |
e537352b | 1005 | |
cd2086fe | 1006 | if (r > 0) { |
e9276800 | 1007 | r = mount_arm_timer(m, /* relative= */ true, m->timeout_usec); |
d30eb0b5 LP |
1008 | if (r < 0) { |
1009 | log_unit_warning_errno(UNIT(m), r, "Failed to install timer: %m"); | |
80876c20 | 1010 | goto fail; |
d30eb0b5 | 1011 | } |
e537352b | 1012 | |
80876c20 | 1013 | mount_set_state(m, state); |
22af0e58 | 1014 | } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill) |
ac84d1fb | 1015 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS); |
22af0e58 | 1016 | else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL)) |
9d2f5178 | 1017 | mount_enter_mounted(m, MOUNT_SUCCESS); |
22af0e58 | 1018 | else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill) |
ac84d1fb | 1019 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS); |
80876c20 | 1020 | else |
e3783068 | 1021 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result = */ false); |
e537352b LP |
1022 | |
1023 | return; | |
1024 | ||
1025 | fail: | |
e3783068 | 1026 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES, /* flush_result = */ false); |
5261ba90 TT |
1027 | } |
1028 | ||
d30eb0b5 LP |
1029 | static int mount_set_umount_command(Mount *m, ExecCommand *c) { |
1030 | int r; | |
1031 | ||
1032 | assert(m); | |
1033 | assert(c); | |
1034 | ||
1035 | r = exec_command_set(c, UMOUNT_PATH, m->where, "-c", NULL); | |
1036 | if (r < 0) | |
1037 | return r; | |
1038 | ||
1039 | if (m->lazy_unmount) { | |
1040 | r = exec_command_append(c, "-l", NULL); | |
1041 | if (r < 0) | |
1042 | return r; | |
1043 | } | |
1044 | ||
1045 | if (m->force_unmount) { | |
1046 | r = exec_command_append(c, "-f", NULL); | |
1047 | if (r < 0) | |
1048 | return r; | |
1049 | } | |
1050 | ||
1051 | return 0; | |
1052 | } | |
1053 | ||
9d2f5178 | 1054 | static void mount_enter_unmounting(Mount *m) { |
e537352b LP |
1055 | int r; |
1056 | ||
1057 | assert(m); | |
1058 | ||
7d54a03a LP |
1059 | /* Start counting our attempts */ |
1060 | if (!IN_SET(m->state, | |
1061 | MOUNT_UNMOUNTING, | |
1062 | MOUNT_UNMOUNTING_SIGTERM, | |
e323d2e4 | 1063 | MOUNT_UNMOUNTING_SIGKILL)) |
7d54a03a LP |
1064 | m->n_retry_umount = 0; |
1065 | ||
c7c6cf20 MY |
1066 | mount_unwatch_control_pid(m); |
1067 | ||
a16e1123 LP |
1068 | m->control_command_id = MOUNT_EXEC_UNMOUNT; |
1069 | m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT; | |
e537352b | 1070 | |
d30eb0b5 LP |
1071 | r = mount_set_umount_command(m, m->control_command); |
1072 | if (r < 0) { | |
1073 | log_unit_warning_errno(UNIT(m), r, "Failed to prepare umount command line: %m"); | |
e537352b | 1074 | goto fail; |
d30eb0b5 | 1075 | } |
e537352b | 1076 | |
6577cf1b | 1077 | r = mount_spawn(m, m->control_command, mount_exec_flags(MOUNT_UNMOUNTING), &m->control_pid); |
d30eb0b5 LP |
1078 | if (r < 0) { |
1079 | log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'umount' task: %m"); | |
e537352b | 1080 | goto fail; |
d30eb0b5 | 1081 | } |
e537352b LP |
1082 | |
1083 | mount_set_state(m, MOUNT_UNMOUNTING); | |
e537352b LP |
1084 | return; |
1085 | ||
1086 | fail: | |
e3783068 | 1087 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES, /* flush_result = */ false); |
e537352b LP |
1088 | } |
1089 | ||
0d76f1c4 MY |
1090 | static int mount_apply_graceful_options(Mount *m, const MountParameters *p, char **opts) { |
1091 | _cleanup_strv_free_ char **graceful = NULL; | |
1092 | _cleanup_free_ char *filtered = NULL; | |
09fbff57 LP |
1093 | int r; |
1094 | ||
1095 | assert(m); | |
1096 | assert(p); | |
1097 | assert(opts); | |
1098 | ||
0d76f1c4 MY |
1099 | r = fstab_filter_options(*opts, "x-systemd.graceful-option\0", NULL, NULL, &graceful, &filtered); |
1100 | if (r <= 0) | |
1101 | return r; | |
09fbff57 LP |
1102 | |
1103 | if (!p->fstype) { | |
0d76f1c4 | 1104 | log_unit_warning(UNIT(m), "x-systemd.graceful-option= used but file system type not known, suppressing all graceful options."); |
09fbff57 LP |
1105 | return 0; |
1106 | } | |
1107 | ||
0d76f1c4 | 1108 | STRV_FOREACH(o, graceful) { |
09fbff57 LP |
1109 | _cleanup_free_ char *k = NULL, *v = NULL; |
1110 | ||
1111 | r = split_pair(*o, "=", &k, &v); | |
1112 | if (r < 0 && r != -EINVAL) /* EINVAL → not a key/value pair */ | |
1113 | return r; | |
1114 | ||
1115 | r = mount_option_supported(p->fstype, k ?: *o, v); | |
f565e5a9 MY |
1116 | if (r == -EAGAIN) { |
1117 | log_unit_warning_errno(UNIT(m), r, | |
1118 | "x-systemd.graceful-option= used but not supported by file system %s, suppressing all.", | |
1119 | p->fstype); | |
1120 | break; | |
1121 | } | |
09fbff57 | 1122 | if (r < 0) |
0d76f1c4 MY |
1123 | log_unit_warning_errno(UNIT(m), r, |
1124 | "x-systemd.graceful-option=%s specified, but cannot determine availability, suppressing: %m", *o); | |
09fbff57 | 1125 | else if (r == 0) |
0d76f1c4 | 1126 | log_unit_info(UNIT(m), "x-systemd.graceful-option=%s specified, but option is not available, suppressing.", *o); |
09fbff57 | 1127 | else { |
0d76f1c4 | 1128 | log_unit_debug(UNIT(m), "x-systemd.graceful-option=%s specified and supported, appending to mount option string.", *o); |
09fbff57 | 1129 | |
0d76f1c4 | 1130 | if (!strextend_with_separator(&filtered, ",", *o)) |
09fbff57 LP |
1131 | return -ENOMEM; |
1132 | } | |
1133 | } | |
1134 | ||
0d76f1c4 | 1135 | return free_and_replace(*opts, filtered); |
09fbff57 LP |
1136 | } |
1137 | ||
0d76f1c4 | 1138 | static int mount_set_mount_command(Mount *m, ExecCommand *c, const MountParameters *p, bool remount) { |
d30eb0b5 LP |
1139 | int r; |
1140 | ||
1141 | assert(m); | |
1142 | assert(c); | |
1143 | assert(p); | |
1144 | ||
1145 | r = exec_command_set(c, MOUNT_PATH, p->what, m->where, NULL); | |
1146 | if (r < 0) | |
1147 | return r; | |
1148 | ||
1149 | if (m->sloppy_options) { | |
1150 | r = exec_command_append(c, "-s", NULL); | |
1151 | if (r < 0) | |
1152 | return r; | |
1153 | } | |
1154 | ||
1155 | if (m->read_write_only) { | |
1156 | r = exec_command_append(c, "-w", NULL); | |
1157 | if (r < 0) | |
1158 | return r; | |
1159 | } | |
1160 | ||
1161 | if (p->fstype) { | |
1162 | r = exec_command_append(c, "-t", p->fstype, NULL); | |
1163 | if (r < 0) | |
1164 | return r; | |
1165 | } | |
1166 | ||
1167 | _cleanup_free_ char *opts = NULL; | |
7e9a78d6 | 1168 | r = fstab_filter_options(p->options, "nofail\0" "fail\0" "noauto\0" "auto\0", NULL, NULL, NULL, &opts); |
d30eb0b5 LP |
1169 | if (r < 0) |
1170 | return r; | |
1171 | ||
0d76f1c4 | 1172 | r = mount_apply_graceful_options(m, p, &opts); |
09fbff57 LP |
1173 | if (r < 0) |
1174 | return r; | |
1175 | ||
d9bf8150 MY |
1176 | if (remount) |
1177 | if (!strprepend_with_separator(&opts, ",", "remount")) | |
0d76f1c4 | 1178 | return -ENOMEM; |
0d76f1c4 | 1179 | |
d30eb0b5 LP |
1180 | if (!isempty(opts)) { |
1181 | r = exec_command_append(c, "-o", opts, NULL); | |
1182 | if (r < 0) | |
1183 | return r; | |
1184 | } | |
1185 | ||
1186 | return 0; | |
1187 | } | |
1188 | ||
8d567588 | 1189 | static void mount_enter_mounting(Mount *m) { |
38c35970 LP |
1190 | _cleanup_close_ int fd = -EBADF; |
1191 | _cleanup_free_ char *fn = NULL; | |
e9fa1bf7 | 1192 | int r; |
e537352b LP |
1193 | |
1194 | assert(m); | |
1195 | ||
38c35970 LP |
1196 | /* Validate that the path we are overmounting does not contain any symlinks, because if it does, we |
1197 | * couldn't support that reasonably: the mounts in /proc/self/mountinfo would not be recognizable to | |
1198 | * us anymore. */ | |
1199 | fd = chase_and_open_parent(m->where, /* root= */ NULL, CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755, &fn); | |
1200 | if (fd == -EREMCHG) { | |
1201 | r = unit_log_noncanonical_mount_path(UNIT(m), m->where); | |
1202 | goto fail; | |
1203 | } | |
1204 | if (fd < 0) { | |
1205 | log_unit_error_errno(UNIT(m), fd, "Failed to resolve parent of mount point '%s': %m", m->where); | |
f2341e0a | 1206 | goto fail; |
38c35970 | 1207 | } |
f2341e0a | 1208 | |
38c35970 | 1209 | MountParameters *p = get_mount_parameters_fragment(m); |
65bc0c03 MY |
1210 | if (!p) { |
1211 | r = log_unit_warning_errno(UNIT(m), SYNTHETIC_ERRNO(ENOENT), "No mount parameters to operate on."); | |
1212 | goto fail; | |
1213 | } | |
1214 | ||
38c35970 | 1215 | bool source_is_dir = true; |
65bc0c03 | 1216 | if (mount_is_bind(p)) { |
218cfe23 DT |
1217 | r = is_dir(p->what, /* follow = */ true); |
1218 | if (r < 0 && r != -ENOENT) | |
1219 | log_unit_info_errno(UNIT(m), r, "Failed to determine type of bind mount source '%s', ignoring: %m", p->what); | |
1220 | else if (r == 0) | |
1221 | source_is_dir = false; | |
1222 | } | |
3e5235b0 | 1223 | |
38c35970 LP |
1224 | r = make_mount_point_inode_from_mode( |
1225 | fd, | |
1226 | fn, | |
1227 | source_is_dir ? S_IFDIR : S_IFREG, | |
1228 | m->directory_mode); | |
ce427d0e DDM |
1229 | if (r < 0 && r != -EEXIST) |
1230 | log_unit_warning_errno(UNIT(m), r, "Failed to create mount point '%s', ignoring: %m", m->where); | |
218cfe23 | 1231 | |
6c75eff6 | 1232 | /* If we are asked to create an OverlayFS, create the upper/work directories if they are missing */ |
65bc0c03 | 1233 | if (streq_ptr(p->fstype, "overlay")) { |
6c75eff6 LB |
1234 | _cleanup_strv_free_ char **dirs = NULL; |
1235 | ||
1236 | r = fstab_filter_options( | |
1237 | p->options, | |
1238 | "upperdir\0workdir\0", | |
1239 | /* ret_namefound= */ NULL, | |
1240 | /* ret_value= */ NULL, | |
1241 | &dirs, | |
1242 | /* ret_filtered= */ NULL); | |
1243 | if (r < 0) | |
1244 | log_unit_warning_errno( | |
1245 | UNIT(m), | |
1246 | r, | |
1247 | "Failed to determine upper directory for OverlayFS, ignoring: %m"); | |
1248 | else | |
1249 | STRV_FOREACH(d, dirs) { | |
1250 | r = mkdir_p_label(*d, m->directory_mode); | |
1251 | if (r < 0 && r != -EEXIST) | |
1252 | log_unit_warning_errno( | |
1253 | UNIT(m), | |
1254 | r, | |
1255 | "Failed to create overlay directory '%s', ignoring: %m", | |
1256 | *d); | |
1257 | } | |
1258 | } | |
1259 | ||
218cfe23 DT |
1260 | if (source_is_dir) |
1261 | unit_warn_if_dir_nonempty(UNIT(m), m->where); | |
a4634b21 | 1262 | |
cb39ed3f | 1263 | /* Create the source directory for bind-mounts if needed */ |
65bc0c03 | 1264 | if (mount_is_bind(p)) { |
5dc60faa | 1265 | r = mkdir_p_label(p->what, m->directory_mode); |
237ecfee | 1266 | /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is |
574febda YW |
1267 | * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be |
1268 | * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned | |
1269 | * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */ | |
e5e6b7c2 | 1270 | if (r < 0 && r != -EEXIST) |
574febda YW |
1271 | log_unit_full_errno(UNIT(m), |
1272 | (r == -EROFS || ERRNO_IS_PRIVILEGE(r)) ? LOG_DEBUG : LOG_WARNING, | |
1273 | r, "Failed to make bind mount source '%s', ignoring: %m", p->what); | |
5dc60faa | 1274 | } |
5261ba90 | 1275 | |
65bc0c03 MY |
1276 | mount_unwatch_control_pid(m); |
1277 | unit_warn_leftover_processes(UNIT(m), /* start = */ true); | |
1278 | ||
1279 | m->control_command_id = MOUNT_EXEC_MOUNT; | |
1280 | m->control_command = m->exec_command + MOUNT_EXEC_MOUNT; | |
1281 | ||
0d76f1c4 | 1282 | r = mount_set_mount_command(m, m->control_command, p, /* remount = */ false); |
65bc0c03 | 1283 | if (r < 0) { |
0d76f1c4 | 1284 | log_unit_error_errno(UNIT(m), r, "Failed to prepare mount command line: %m"); |
e537352b | 1285 | goto fail; |
d30eb0b5 | 1286 | } |
e537352b | 1287 | |
6577cf1b | 1288 | r = mount_spawn(m, m->control_command, mount_exec_flags(MOUNT_MOUNTING), &m->control_pid); |
d30eb0b5 LP |
1289 | if (r < 0) { |
1290 | log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'mount' task: %m"); | |
e537352b | 1291 | goto fail; |
d30eb0b5 | 1292 | } |
e537352b LP |
1293 | |
1294 | mount_set_state(m, MOUNT_MOUNTING); | |
e537352b LP |
1295 | return; |
1296 | ||
1297 | fail: | |
e3783068 | 1298 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES, /* flush_result = */ false); |
e537352b LP |
1299 | } |
1300 | ||
850b7410 LP |
1301 | static void mount_set_reload_result(Mount *m, MountResult result) { |
1302 | assert(m); | |
1303 | ||
1304 | /* Only store the first error we encounter */ | |
1305 | if (m->reload_result != MOUNT_SUCCESS) | |
1306 | return; | |
1307 | ||
1308 | m->reload_result = result; | |
1309 | } | |
1310 | ||
9d2f5178 | 1311 | static void mount_enter_remounting(Mount *m) { |
b294b79f | 1312 | MountParameters *p; |
e9fa1bf7 | 1313 | int r; |
e537352b LP |
1314 | |
1315 | assert(m); | |
1316 | ||
65bc0c03 MY |
1317 | p = get_mount_parameters_fragment(m); |
1318 | if (!p) { | |
1319 | r = log_unit_warning_errno(UNIT(m), SYNTHETIC_ERRNO(ENOENT), "No mount parameters to operate on."); | |
1320 | goto fail; | |
1321 | } | |
1322 | ||
1323 | mount_unwatch_control_pid(m); | |
850b7410 LP |
1324 | m->reload_result = MOUNT_SUCCESS; |
1325 | ||
a16e1123 LP |
1326 | m->control_command_id = MOUNT_EXEC_REMOUNT; |
1327 | m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT; | |
e537352b | 1328 | |
0d76f1c4 | 1329 | r = mount_set_mount_command(m, m->control_command, p, /* remount = */ true); |
65bc0c03 | 1330 | if (r < 0) { |
0d76f1c4 | 1331 | log_unit_error_errno(UNIT(m), r, "Failed to prepare remount command line: %m"); |
e537352b | 1332 | goto fail; |
d30eb0b5 | 1333 | } |
e537352b | 1334 | |
6577cf1b | 1335 | r = mount_spawn(m, m->control_command, mount_exec_flags(MOUNT_REMOUNTING), &m->control_pid); |
d30eb0b5 LP |
1336 | if (r < 0) { |
1337 | log_unit_warning_errno(UNIT(m), r, "Failed to spawn 'remount' task: %m"); | |
e537352b | 1338 | goto fail; |
d30eb0b5 | 1339 | } |
e537352b LP |
1340 | |
1341 | mount_set_state(m, MOUNT_REMOUNTING); | |
e537352b LP |
1342 | return; |
1343 | ||
1344 | fail: | |
850b7410 | 1345 | mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES); |
e3783068 | 1346 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result = */ false); |
e537352b LP |
1347 | } |
1348 | ||
7eba1463 LP |
1349 | static void mount_cycle_clear(Mount *m) { |
1350 | assert(m); | |
1351 | ||
1352 | /* Clear all state we shall forget for this new cycle */ | |
1353 | ||
1354 | m->result = MOUNT_SUCCESS; | |
1355 | m->reload_result = MOUNT_SUCCESS; | |
1356 | exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); | |
9cc54544 LP |
1357 | |
1358 | if (m->cgroup_runtime) | |
1359 | m->cgroup_runtime->reset_accounting = true; | |
7eba1463 LP |
1360 | } |
1361 | ||
e537352b | 1362 | static int mount_start(Unit *u) { |
e9fa1bf7 | 1363 | Mount *m = ASSERT_PTR(MOUNT(u)); |
07299350 | 1364 | int r; |
e537352b | 1365 | |
e537352b LP |
1366 | /* We cannot fulfill this request right now, try again later |
1367 | * please! */ | |
f2aed307 LP |
1368 | if (IN_SET(m->state, |
1369 | MOUNT_UNMOUNTING, | |
1370 | MOUNT_UNMOUNTING_SIGTERM, | |
17e9d53d YW |
1371 | MOUNT_UNMOUNTING_SIGKILL, |
1372 | MOUNT_CLEANING)) | |
e537352b LP |
1373 | return -EAGAIN; |
1374 | ||
1375 | /* Already on it! */ | |
db39a627 | 1376 | if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE)) |
e537352b LP |
1377 | return 0; |
1378 | ||
f2aed307 | 1379 | assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED)); |
e537352b | 1380 | |
4b58153d LP |
1381 | r = unit_acquire_invocation_id(u); |
1382 | if (r < 0) | |
1383 | return r; | |
1384 | ||
7eba1463 | 1385 | mount_cycle_clear(m); |
8d567588 | 1386 | mount_enter_mounting(m); |
7eba1463 | 1387 | |
82a2b6bb | 1388 | return 1; |
e537352b LP |
1389 | } |
1390 | ||
1391 | static int mount_stop(Unit *u) { | |
e9fa1bf7 | 1392 | Mount *m = ASSERT_PTR(MOUNT(u)); |
e537352b | 1393 | |
22af0e58 LP |
1394 | switch (m->state) { |
1395 | ||
1396 | case MOUNT_UNMOUNTING: | |
1397 | case MOUNT_UNMOUNTING_SIGKILL: | |
1398 | case MOUNT_UNMOUNTING_SIGTERM: | |
1399 | /* Already on it */ | |
e537352b LP |
1400 | return 0; |
1401 | ||
22af0e58 LP |
1402 | case MOUNT_MOUNTING: |
1403 | case MOUNT_MOUNTING_DONE: | |
1404 | case MOUNT_REMOUNTING: | |
1405 | /* If we are still waiting for /bin/mount, we go directly into kill mode. */ | |
1406 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS); | |
1407 | return 0; | |
e537352b | 1408 | |
22af0e58 LP |
1409 | case MOUNT_REMOUNTING_SIGTERM: |
1410 | /* If we are already waiting for a hung remount, convert this to the matching unmounting state */ | |
1411 | mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM); | |
1412 | return 0; | |
1413 | ||
1414 | case MOUNT_REMOUNTING_SIGKILL: | |
1415 | /* as above */ | |
1416 | mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL); | |
1417 | return 0; | |
1418 | ||
1419 | case MOUNT_MOUNTED: | |
1420 | mount_enter_unmounting(m); | |
1421 | return 1; | |
1422 | ||
17e9d53d YW |
1423 | case MOUNT_CLEANING: |
1424 | /* If we are currently cleaning, then abort it, brutally. */ | |
1425 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS); | |
1426 | return 0; | |
1427 | ||
22af0e58 | 1428 | default: |
04499a70 | 1429 | assert_not_reached(); |
22af0e58 | 1430 | } |
e537352b LP |
1431 | } |
1432 | ||
1433 | static int mount_reload(Unit *u) { | |
e9fa1bf7 | 1434 | Mount *m = ASSERT_PTR(MOUNT(u)); |
e537352b | 1435 | |
e537352b LP |
1436 | assert(m->state == MOUNT_MOUNTED); |
1437 | ||
9d2f5178 | 1438 | mount_enter_remounting(m); |
22af0e58 | 1439 | |
2d018ae2 | 1440 | return 1; |
e537352b LP |
1441 | } |
1442 | ||
74c0d972 MY |
1443 | static bool mount_can_reload(Unit *u) { |
1444 | Mount *m = ASSERT_PTR(MOUNT(u)); | |
1445 | ||
1446 | return get_mount_parameters_fragment(m); | |
1447 | } | |
1448 | ||
a16e1123 | 1449 | static int mount_serialize(Unit *u, FILE *f, FDSet *fds) { |
e9fa1bf7 | 1450 | Mount *m = ASSERT_PTR(MOUNT(u)); |
a16e1123 | 1451 | |
a16e1123 LP |
1452 | assert(f); |
1453 | assert(fds); | |
1454 | ||
d68c645b LP |
1455 | (void) serialize_item(f, "state", mount_state_to_string(m->state)); |
1456 | (void) serialize_item(f, "result", mount_result_to_string(m->result)); | |
1457 | (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result)); | |
2c09fb81 | 1458 | (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount); |
2a7451dc | 1459 | (void) serialize_pidref(f, fds, "control-pid", &m->control_pid); |
a16e1123 LP |
1460 | |
1461 | if (m->control_command_id >= 0) | |
d68c645b | 1462 | (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id)); |
a16e1123 LP |
1463 | |
1464 | return 0; | |
1465 | } | |
1466 | ||
1467 | static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
e9fa1bf7 | 1468 | Mount *m = ASSERT_PTR(MOUNT(u)); |
2c09fb81 | 1469 | int r; |
a16e1123 | 1470 | |
a16e1123 LP |
1471 | assert(key); |
1472 | assert(value); | |
1473 | assert(fds); | |
1474 | ||
1475 | if (streq(key, "state")) { | |
1476 | MountState state; | |
1477 | ||
7fb1d980 YW |
1478 | state = mount_state_from_string(value); |
1479 | if (state < 0) | |
1480 | log_unit_debug_errno(u, state, "Failed to parse state value: %s", value); | |
a16e1123 LP |
1481 | else |
1482 | m->deserialized_state = state; | |
2c09fb81 | 1483 | |
9d2f5178 LP |
1484 | } else if (streq(key, "result")) { |
1485 | MountResult f; | |
a16e1123 | 1486 | |
9d2f5178 LP |
1487 | f = mount_result_from_string(value); |
1488 | if (f < 0) | |
7fb1d980 | 1489 | log_unit_debug_errno(u, f, "Failed to parse result value: %s", value); |
9d2f5178 LP |
1490 | else if (f != MOUNT_SUCCESS) |
1491 | m->result = f; | |
1492 | ||
1493 | } else if (streq(key, "reload-result")) { | |
1494 | MountResult f; | |
1495 | ||
1496 | f = mount_result_from_string(value); | |
1497 | if (f < 0) | |
7fb1d980 | 1498 | log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value); |
9d2f5178 LP |
1499 | else if (f != MOUNT_SUCCESS) |
1500 | m->reload_result = f; | |
a16e1123 | 1501 | |
2c09fb81 LP |
1502 | } else if (streq(key, "n-retry-umount")) { |
1503 | ||
1504 | r = safe_atou(value, &m->n_retry_umount); | |
1505 | if (r < 0) | |
7fb1d980 | 1506 | log_unit_debug_errno(u, r, "Failed to parse n-retry-umount value: %s", value); |
2c09fb81 | 1507 | |
a16e1123 | 1508 | } else if (streq(key, "control-pid")) { |
a16e1123 | 1509 | |
aaa872a7 DDM |
1510 | if (!pidref_is_set(&m->control_pid)) |
1511 | (void) deserialize_pidref(fds, value, &m->control_pid); | |
3f459cd9 | 1512 | |
a16e1123 LP |
1513 | } else if (streq(key, "control-command")) { |
1514 | MountExecCommand id; | |
1515 | ||
f2341e0a LP |
1516 | id = mount_exec_command_from_string(value); |
1517 | if (id < 0) | |
7fb1d980 | 1518 | log_unit_debug_errno(u, id, "Failed to parse exec-command value: %s", value); |
a16e1123 LP |
1519 | else { |
1520 | m->control_command_id = id; | |
1521 | m->control_command = m->exec_command + id; | |
1522 | } | |
a16e1123 | 1523 | } else |
f2341e0a | 1524 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
1525 | |
1526 | return 0; | |
1527 | } | |
1528 | ||
d1e8e8b5 | 1529 | static UnitActiveState mount_active_state(Unit *u) { |
e9fa1bf7 | 1530 | Mount *m = ASSERT_PTR(MOUNT(u)); |
e537352b | 1531 | |
e9fa1bf7 | 1532 | return state_translation_table[m->state]; |
e537352b LP |
1533 | } |
1534 | ||
d1e8e8b5 | 1535 | static const char *mount_sub_state_to_string(Unit *u) { |
e9fa1bf7 | 1536 | Mount *m = ASSERT_PTR(MOUNT(u)); |
10a94420 | 1537 | |
e9fa1bf7 | 1538 | return mount_state_to_string(m->state); |
10a94420 LP |
1539 | } |
1540 | ||
d1e8e8b5 | 1541 | static bool mount_may_gc(Unit *u) { |
e9fa1bf7 | 1542 | Mount *m = ASSERT_PTR(MOUNT(u)); |
701cc384 | 1543 | |
f2f725e5 ZJS |
1544 | if (m->from_proc_self_mountinfo) |
1545 | return false; | |
1546 | ||
1547 | return true; | |
701cc384 LP |
1548 | } |
1549 | ||
e537352b | 1550 | static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
e9fa1bf7 | 1551 | Mount *m = ASSERT_PTR(MOUNT(u)); |
9d2f5178 | 1552 | MountResult f; |
e537352b | 1553 | |
e537352b LP |
1554 | assert(pid >= 0); |
1555 | ||
360f384f | 1556 | if (pid != m->control_pid.pid) |
8c47c732 | 1557 | return; |
e537352b | 1558 | |
35080486 LP |
1559 | /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether |
1560 | * they established/remove a mount. This is important when mounting, but even more so when unmounting | |
1561 | * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat | |
1562 | * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from | |
1563 | * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we | |
1564 | * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we | |
1565 | * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got | |
1566 | * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just | |
1567 | * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might | |
1568 | * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for | |
1569 | * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that | |
1570 | * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount | |
1571 | * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see | |
1572 | * /proc/self/mountinfo changes before our mount/umount exits. */ | |
1573 | (void) mount_process_proc_self_mountinfo(u->manager); | |
1574 | ||
360f384f | 1575 | pidref_done(&m->control_pid); |
e537352b | 1576 | |
1f0958f6 | 1577 | if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) |
9d2f5178 LP |
1578 | f = MOUNT_SUCCESS; |
1579 | else if (code == CLD_EXITED) | |
1580 | f = MOUNT_FAILURE_EXIT_CODE; | |
1581 | else if (code == CLD_KILLED) | |
1582 | f = MOUNT_FAILURE_SIGNAL; | |
1583 | else if (code == CLD_DUMPED) | |
1584 | f = MOUNT_FAILURE_CORE_DUMP; | |
1585 | else | |
04499a70 | 1586 | assert_not_reached(); |
9d2f5178 | 1587 | |
850b7410 LP |
1588 | if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM)) |
1589 | mount_set_reload_result(m, f); | |
8e94bb62 MY |
1590 | else if (m->result == MOUNT_SUCCESS && !IN_SET(m->state, MOUNT_MOUNTING, MOUNT_UNMOUNTING)) |
1591 | /* MOUNT_MOUNTING and MOUNT_UNMOUNTING states need to be patched, see below. */ | |
9d2f5178 | 1592 | m->result = f; |
8c47c732 | 1593 | |
a16e1123 | 1594 | if (m->control_command) { |
6ea832a2 | 1595 | exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status); |
9d2f5178 | 1596 | |
a16e1123 LP |
1597 | m->control_command = NULL; |
1598 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
1599 | } | |
1600 | ||
91bbd9b7 | 1601 | unit_log_process_exit( |
5cc2cd1c | 1602 | u, |
91bbd9b7 LP |
1603 | "Mount process", |
1604 | mount_exec_command_to_string(m->control_command_id), | |
5cc2cd1c | 1605 | f == MOUNT_SUCCESS, |
91bbd9b7 | 1606 | code, status); |
e537352b | 1607 | |
006aabbd AJ |
1608 | /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded |
1609 | * before we process the SIGCHLD for the mount command. */ | |
e537352b LP |
1610 | |
1611 | switch (m->state) { | |
1612 | ||
1613 | case MOUNT_MOUNTING: | |
8e94bb62 | 1614 | /* Our mount point has not appeared in mountinfo. Something went wrong. */ |
e537352b | 1615 | |
006aabbd | 1616 | if (f == MOUNT_SUCCESS) { |
8e94bb62 MY |
1617 | /* Either /bin/mount has an unexpected definition of success, or someone raced us |
1618 | * and we lost. */ | |
006aabbd AJ |
1619 | log_unit_warning(UNIT(m), "Mount process finished, but there is no mount."); |
1620 | f = MOUNT_FAILURE_PROTOCOL; | |
1621 | } | |
e3783068 | 1622 | mount_enter_dead(m, f, /* flush_result = */ false); |
006aabbd AJ |
1623 | break; |
1624 | ||
1625 | case MOUNT_MOUNTING_DONE: | |
1626 | mount_enter_mounted(m, f); | |
e537352b LP |
1627 | break; |
1628 | ||
e2f3b44c | 1629 | case MOUNT_REMOUNTING: |
e2f3b44c | 1630 | case MOUNT_REMOUNTING_SIGTERM: |
22af0e58 | 1631 | case MOUNT_REMOUNTING_SIGKILL: |
e3783068 | 1632 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result = */ false); |
e2f3b44c LP |
1633 | break; |
1634 | ||
e537352b | 1635 | case MOUNT_UNMOUNTING: |
3cc96856 | 1636 | if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) { |
22af0e58 | 1637 | /* Still a mount point? If so, let's try again. Most likely there were multiple mount points |
57018361 AJ |
1638 | * stacked on top of each other. We might exceed the timeout specified by the user overall, |
1639 | * but we will stop as soon as any one umount times out. */ | |
22af0e58 LP |
1640 | |
1641 | if (m->n_retry_umount < RETRY_UMOUNT_MAX) { | |
1642 | log_unit_debug(u, "Mount still present, trying again."); | |
1643 | m->n_retry_umount++; | |
1644 | mount_enter_unmounting(m); | |
1645 | } else { | |
006aabbd | 1646 | log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount); |
9c7c3d9c | 1647 | mount_enter_mounted(m, MOUNT_FAILURE_PROTOCOL); |
22af0e58 | 1648 | } |
8e94bb62 MY |
1649 | } else if (f == MOUNT_FAILURE_EXIT_CODE && !m->from_proc_self_mountinfo) { |
1650 | /* Hmm, umount process spawned by us failed, but the mount disappeared anyway? | |
1651 | * Maybe someone else is trying to unmount at the same time. */ | |
1652 | log_unit_notice(u, "Mount disappeared even though umount process failed, continuing."); | |
e3783068 | 1653 | mount_enter_dead(m, MOUNT_SUCCESS, /* flush_result = */ true); |
22af0e58 | 1654 | } else |
e3783068 MY |
1655 | /* At this point, either the unmount succeeded or unexpected error occurred. We usually |
1656 | * remember the first error in 'result', but here let's update that forcibly, since | |
1657 | * there could previous failed attempts yet we only care about the most recent | |
1658 | * attempt. IOW, if we eventually managed to unmount the stuff, don't enter failed | |
1659 | * end state. */ | |
1660 | mount_enter_dead_or_mounted(m, f, /* flush_result = */ true); | |
22af0e58 | 1661 | |
e537352b | 1662 | break; |
57018361 | 1663 | |
57018361 | 1664 | case MOUNT_UNMOUNTING_SIGTERM: |
8e94bb62 | 1665 | case MOUNT_UNMOUNTING_SIGKILL: |
e3783068 | 1666 | mount_enter_dead_or_mounted(m, f, /* flush_result = */ false); |
57018361 | 1667 | break; |
e537352b | 1668 | |
17e9d53d YW |
1669 | case MOUNT_CLEANING: |
1670 | if (m->clean_result == MOUNT_SUCCESS) | |
1671 | m->clean_result = f; | |
1672 | ||
e3783068 | 1673 | mount_enter_dead(m, MOUNT_SUCCESS, /* flush_result = */ false); |
17e9d53d YW |
1674 | break; |
1675 | ||
e537352b | 1676 | default: |
04499a70 | 1677 | assert_not_reached(); |
e537352b | 1678 | } |
c4e2ceae LP |
1679 | |
1680 | /* Notify clients about changed exit status */ | |
1681 | unit_add_to_dbus_queue(u); | |
e537352b LP |
1682 | } |
1683 | ||
718db961 | 1684 | static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
e9fa1bf7 | 1685 | Mount *m = ASSERT_PTR(MOUNT(userdata)); |
e537352b | 1686 | |
718db961 | 1687 | assert(m->timer_event_source == source); |
e537352b LP |
1688 | |
1689 | switch (m->state) { | |
1690 | ||
1691 | case MOUNT_MOUNTING: | |
1692 | case MOUNT_MOUNTING_DONE: | |
22af0e58 LP |
1693 | log_unit_warning(UNIT(m), "Mounting timed out. Terminating."); |
1694 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT); | |
e537352b LP |
1695 | break; |
1696 | ||
1697 | case MOUNT_REMOUNTING: | |
79aafbd1 | 1698 | log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process."); |
22af0e58 LP |
1699 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); |
1700 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS); | |
e537352b LP |
1701 | break; |
1702 | ||
22af0e58 LP |
1703 | case MOUNT_REMOUNTING_SIGTERM: |
1704 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); | |
e537352b | 1705 | |
4819ff03 | 1706 | if (m->kill_context.send_sigkill) { |
22af0e58 LP |
1707 | log_unit_warning(UNIT(m), "Remounting timed out. Killing."); |
1708 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS); | |
ba035df2 | 1709 | } else { |
22af0e58 | 1710 | log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring."); |
e3783068 | 1711 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result = */ false); |
ba035df2 | 1712 | } |
e537352b LP |
1713 | break; |
1714 | ||
22af0e58 LP |
1715 | case MOUNT_REMOUNTING_SIGKILL: |
1716 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); | |
ba035df2 | 1717 | |
22af0e58 | 1718 | log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring."); |
e3783068 | 1719 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS, /* flush_result = */ false); |
22af0e58 LP |
1720 | break; |
1721 | ||
1722 | case MOUNT_UNMOUNTING: | |
79aafbd1 | 1723 | log_unit_warning(UNIT(m), "Unmounting timed out. Terminating."); |
22af0e58 | 1724 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT); |
e537352b LP |
1725 | break; |
1726 | ||
1727 | case MOUNT_UNMOUNTING_SIGTERM: | |
4819ff03 | 1728 | if (m->kill_context.send_sigkill) { |
22af0e58 | 1729 | log_unit_warning(UNIT(m), "Mount process timed out. Killing."); |
9d2f5178 | 1730 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT); |
ba035df2 | 1731 | } else { |
22af0e58 | 1732 | log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring."); |
e3783068 | 1733 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT, /* flush_result = */ false); |
ba035df2 | 1734 | } |
e537352b LP |
1735 | break; |
1736 | ||
e537352b | 1737 | case MOUNT_UNMOUNTING_SIGKILL: |
22af0e58 | 1738 | log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring."); |
e3783068 | 1739 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT, /* flush_result = */ false); |
e537352b LP |
1740 | break; |
1741 | ||
17e9d53d YW |
1742 | case MOUNT_CLEANING: |
1743 | log_unit_warning(UNIT(m), "Cleaning timed out. killing."); | |
1744 | ||
1745 | if (m->clean_result == MOUNT_SUCCESS) | |
1746 | m->clean_result = MOUNT_FAILURE_TIMEOUT; | |
1747 | ||
1748 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0); | |
1749 | break; | |
1750 | ||
e537352b | 1751 | default: |
04499a70 | 1752 | assert_not_reached(); |
e537352b | 1753 | } |
718db961 LP |
1754 | |
1755 | return 0; | |
e537352b LP |
1756 | } |
1757 | ||
03b8cfed | 1758 | static int mount_setup_new_unit( |
839ee058 LP |
1759 | Manager *m, |
1760 | const char *name, | |
03b8cfed FB |
1761 | const char *what, |
1762 | const char *where, | |
1763 | const char *options, | |
1764 | const char *fstype, | |
ec88d1ea | 1765 | MountProcFlags *ret_flags, |
839ee058 | 1766 | Unit **ret) { |
03b8cfed | 1767 | |
839ee058 | 1768 | _cleanup_(unit_freep) Unit *u = NULL; |
f1dfc20a | 1769 | Mount *mnt; |
bbee24bc | 1770 | int r; |
03b8cfed | 1771 | |
839ee058 LP |
1772 | assert(m); |
1773 | assert(name); | |
ec88d1ea | 1774 | assert(ret_flags); |
839ee058 LP |
1775 | assert(ret); |
1776 | ||
1777 | r = unit_new_for_name(m, sizeof(Mount), name, &u); | |
1778 | if (r < 0) | |
1779 | return r; | |
03b8cfed | 1780 | |
f1dfc20a MY |
1781 | mnt = ASSERT_PTR(MOUNT(u)); |
1782 | ||
e10fe042 LP |
1783 | r = free_and_strdup(&u->source_path, "/proc/self/mountinfo"); |
1784 | if (r < 0) | |
1785 | return r; | |
03b8cfed | 1786 | |
f1dfc20a | 1787 | r = free_and_strdup(&mnt->where, where); |
e10fe042 LP |
1788 | if (r < 0) |
1789 | return r; | |
03b8cfed | 1790 | |
f1dfc20a | 1791 | r = update_parameters_proc_self_mountinfo(mnt, what, options, fstype); |
bbee24bc LP |
1792 | if (r < 0) |
1793 | return r; | |
03b8cfed | 1794 | |
09f4d843 ZJS |
1795 | /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the |
1796 | * time we load the unit file for it (and thus add in extra deps right after) we know what source to | |
1797 | * attributes the deps to. */ | |
f1dfc20a | 1798 | mnt->from_proc_self_mountinfo = true; |
6d7e89b0 | 1799 | |
09f4d843 ZJS |
1800 | /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything |
1801 | * else is loaded in now. */ | |
cfcd4318 | 1802 | unit_add_to_load_queue(u); |
26e35b16 | 1803 | |
ec88d1ea | 1804 | *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED; |
839ee058 | 1805 | *ret = TAKE_PTR(u); |
03b8cfed FB |
1806 | return 0; |
1807 | } | |
1808 | ||
1809 | static int mount_setup_existing_unit( | |
1810 | Unit *u, | |
1811 | const char *what, | |
1812 | const char *where, | |
1813 | const char *options, | |
1814 | const char *fstype, | |
ec88d1ea | 1815 | MountProcFlags *ret_flags) { |
03b8cfed | 1816 | |
e9fa1bf7 | 1817 | Mount *m = ASSERT_PTR(MOUNT(u)); |
bbee24bc | 1818 | int r; |
03b8cfed FB |
1819 | |
1820 | assert(u); | |
f1dfc20a | 1821 | assert(where); |
4bb68f2f | 1822 | assert(ret_flags); |
03b8cfed | 1823 | |
e9fa1bf7 MY |
1824 | if (!m->where) { |
1825 | m->where = strdup(where); | |
1826 | if (!m->where) | |
03b8cfed FB |
1827 | return -ENOMEM; |
1828 | } | |
1829 | ||
4bb68f2f LP |
1830 | /* In case we have multiple mounts established on the same mount point, let's merge flags set already |
1831 | * for the current unit. Note that the flags field is reset on each iteration of reading | |
1832 | * /proc/self/mountinfo, hence we know for sure anything already set here is from the current | |
1833 | * iteration and thus worthy of taking into account. */ | |
e9fa1bf7 | 1834 | MountProcFlags flags = m->proc_flags | MOUNT_PROC_IS_MOUNTED; |
4bb68f2f | 1835 | |
e9fa1bf7 | 1836 | r = update_parameters_proc_self_mountinfo(m, what, options, fstype); |
bbee24bc LP |
1837 | if (r < 0) |
1838 | return r; | |
ec88d1ea LP |
1839 | if (r > 0) |
1840 | flags |= MOUNT_PROC_JUST_CHANGED; | |
03b8cfed | 1841 | |
4bb68f2f LP |
1842 | /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in |
1843 | * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the | |
1844 | * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then | |
1845 | * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems | |
1846 | * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is | |
1847 | * reached when we wait for the mount to appear we hence can assume that if we are in it, we are | |
1848 | * actually seeing it established for the first time. */ | |
e9fa1bf7 | 1849 | if (!m->from_proc_self_mountinfo || m->state == MOUNT_MOUNTING) |
ec88d1ea | 1850 | flags |= MOUNT_PROC_JUST_MOUNTED; |
d253a45e | 1851 | |
e9fa1bf7 | 1852 | m->from_proc_self_mountinfo = true; |
03b8cfed | 1853 | |
dc4c5871 | 1854 | if (UNIT_IS_LOAD_ERROR(u->load_state)) { |
f8064c4f | 1855 | /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in |
fee2f5ca YW |
1856 | * /proc/self/mountinfo we should reconsider that. Hence, let's reset the load state and load |
1857 | * error, and add the unit to load queue. */ | |
1858 | u->load_state = UNIT_STUB; | |
03b8cfed | 1859 | u->load_error = 0; |
fee2f5ca | 1860 | unit_add_to_load_queue(u); |
03b8cfed | 1861 | |
ec88d1ea | 1862 | flags |= MOUNT_PROC_JUST_CHANGED; |
03b8cfed FB |
1863 | } |
1864 | ||
26186503 YW |
1865 | if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED) && u->load_state == UNIT_LOADED) { |
1866 | /* If things changed, and we have successfully loaded the unit, then make sure that all deps | |
1867 | * are regenerated. Let's first remove all automatic deps, and then add in the new ones. */ | |
e9fa1bf7 | 1868 | r = mount_add_non_exec_dependencies(m); |
a3742204 LP |
1869 | if (r < 0) |
1870 | return r; | |
1871 | } | |
03b8cfed | 1872 | |
ec88d1ea | 1873 | *ret_flags = flags; |
03b8cfed FB |
1874 | return 0; |
1875 | } | |
1876 | ||
628c89cc | 1877 | static int mount_setup_unit( |
e537352b LP |
1878 | Manager *m, |
1879 | const char *what, | |
1880 | const char *where, | |
1881 | const char *options, | |
1882 | const char *fstype, | |
e537352b | 1883 | bool set_flags) { |
057d9ab8 | 1884 | |
03b8cfed | 1885 | _cleanup_free_ char *e = NULL; |
ec88d1ea | 1886 | MountProcFlags flags; |
057d9ab8 LP |
1887 | Unit *u; |
1888 | int r; | |
b08d03ff | 1889 | |
f50e0a01 | 1890 | assert(m); |
b08d03ff LP |
1891 | assert(what); |
1892 | assert(where); | |
e537352b LP |
1893 | assert(options); |
1894 | assert(fstype); | |
1895 | ||
88188e1f | 1896 | /* Ignore API and credential mount points. They should never be referenced in dependencies ever. |
ae5c4aa6 MY |
1897 | * Furthermore, the lifetime of credential mounts is strictly bound to the owning services, |
1898 | * so mount units make little sense for them. */ | |
1899 | if (mount_point_is_api(where) || mount_point_ignore(where) || | |
1900 | mount_point_is_credentials(m->prefix[EXEC_DIRECTORY_RUNTIME], where)) | |
57f2a956 | 1901 | return 0; |
b08d03ff | 1902 | |
8d567588 LP |
1903 | if (streq(fstype, "autofs")) |
1904 | return 0; | |
1905 | ||
4e85aff4 LP |
1906 | /* probably some kind of swap, ignore */ |
1907 | if (!is_path(where)) | |
b08d03ff LP |
1908 | return 0; |
1909 | ||
7410616c | 1910 | r = unit_name_from_path(where, ".mount", &e); |
03e52e8c | 1911 | if (r < 0) |
2c905207 | 1912 | return log_struct_errno( |
03e52e8c | 1913 | LOG_WARNING, r, |
3cf6a3a3 YW |
1914 | LOG_MESSAGE_ID(SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR), |
1915 | LOG_ITEM("MOUNT_POINT=%s", where), | |
03e52e8c YW |
1916 | LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m", |
1917 | where)); | |
b08d03ff | 1918 | |
7d17cfbc | 1919 | u = manager_get_unit(m, e); |
839ee058 | 1920 | if (u) |
03b8cfed | 1921 | r = mount_setup_existing_unit(u, what, where, options, fstype, &flags); |
839ee058 | 1922 | else |
09f4d843 ZJS |
1923 | /* First time we see this mount point meaning that it's not been initiated by a mount unit |
1924 | * but rather by the sysadmin having called mount(8) directly. */ | |
839ee058 | 1925 | r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u); |
03b8cfed | 1926 | if (r < 0) |
2c905207 | 1927 | return log_warning_errno(r, "Failed to set up mount unit for '%s': %m", where); |
ff5f34d0 | 1928 | |
ec88d1ea LP |
1929 | /* If the mount changed properties or state, let's notify our clients */ |
1930 | if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED)) | |
ff5f34d0 | 1931 | unit_add_to_dbus_queue(u); |
c1e1601e | 1932 | |
ec88d1ea LP |
1933 | if (set_flags) |
1934 | MOUNT(u)->proc_flags = flags; | |
1935 | ||
b08d03ff | 1936 | return 0; |
b08d03ff LP |
1937 | } |
1938 | ||
ef734fd6 | 1939 | static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) { |
13dcfe46 ZJS |
1940 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
1941 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
00ad3f02 | 1942 | _cleanup_set_free_ Set *devices = NULL; |
ba0d56f5 | 1943 | int r; |
b08d03ff LP |
1944 | |
1945 | assert(m); | |
1946 | ||
93f6cee9 | 1947 | r = libmount_parse_with_utab(&table, &iter); |
5cca8def | 1948 | if (r < 0) |
628c89cc | 1949 | return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m"); |
e537352b | 1950 | |
5cca8def | 1951 | for (;;) { |
95b862b0 | 1952 | struct libmnt_fs *fs; |
8d3ae2bd | 1953 | const char *device, *path, *options, *fstype; |
b08d03ff | 1954 | |
13dcfe46 ZJS |
1955 | r = mnt_table_next_fs(table, iter, &fs); |
1956 | if (r == 1) | |
5cca8def | 1957 | break; |
13dcfe46 ZJS |
1958 | if (r < 0) |
1959 | return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
5cca8def | 1960 | |
8d3ae2bd CL |
1961 | device = mnt_fs_get_source(fs); |
1962 | path = mnt_fs_get_target(fs); | |
1963 | options = mnt_fs_get_options(fs); | |
1964 | fstype = mnt_fs_get_fstype(fs); | |
a2e0f3d3 | 1965 | |
c0a7f8d3 DM |
1966 | if (!device || !path) |
1967 | continue; | |
1968 | ||
d866b013 | 1969 | /* Just to achieve device name uniqueness. Note that the suppression of the duplicate |
00ad3f02 CG |
1970 | * processing is merely an optimization, hence in case of OOM (unlikely) we'll just process |
1971 | * it twice. */ | |
1972 | if (set_put_strdup_full(&devices, &path_hash_ops_free, device) != 0) | |
1973 | device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT); | |
628c89cc | 1974 | |
9d1b2b22 | 1975 | (void) mount_setup_unit(m, device, path, options, fstype, set_flags); |
b08d03ff LP |
1976 | } |
1977 | ||
ba0d56f5 | 1978 | return 0; |
e537352b LP |
1979 | } |
1980 | ||
1981 | static void mount_shutdown(Manager *m) { | |
1982 | assert(m); | |
1983 | ||
5dcadb4c | 1984 | m->mount_event_source = sd_event_source_disable_unref(m->mount_event_source); |
718db961 | 1985 | |
d379d442 KZ |
1986 | mnt_unref_monitor(m->mount_monitor); |
1987 | m->mount_monitor = NULL; | |
b08d03ff LP |
1988 | } |
1989 | ||
19ae8986 LP |
1990 | static void mount_handoff_timestamp( |
1991 | Unit *u, | |
1992 | const struct ucred *ucred, | |
1993 | const dual_timestamp *ts) { | |
1994 | ||
1995 | Mount *m = ASSERT_PTR(MOUNT(u)); | |
1996 | ||
1997 | assert(ucred); | |
1998 | assert(ts); | |
1999 | ||
2000 | if (m->control_pid.pid == ucred->pid && m->control_command) { | |
2001 | exec_status_handoff(&m->control_command->exec_status, ucred, ts); | |
2002 | unit_add_to_dbus_queue(u); | |
2003 | } | |
2004 | } | |
2005 | ||
7a7821c8 | 2006 | static int mount_get_timeout(Unit *u, usec_t *timeout) { |
e9fa1bf7 | 2007 | Mount *m = ASSERT_PTR(MOUNT(u)); |
7a7821c8 | 2008 | usec_t t; |
68db7a3b ZJS |
2009 | int r; |
2010 | ||
2011 | if (!m->timer_event_source) | |
2012 | return 0; | |
2013 | ||
7a7821c8 | 2014 | r = sd_event_source_get_time(m->timer_event_source, &t); |
68db7a3b ZJS |
2015 | if (r < 0) |
2016 | return r; | |
7a7821c8 LP |
2017 | if (t == USEC_INFINITY) |
2018 | return 0; | |
68db7a3b | 2019 | |
7a7821c8 | 2020 | *timeout = t; |
68db7a3b ZJS |
2021 | return 1; |
2022 | } | |
2023 | ||
04eb582a | 2024 | static void mount_enumerate_perpetual(Manager *m) { |
11222d0f LP |
2025 | Unit *u; |
2026 | int r; | |
2027 | ||
2028 | assert(m); | |
2029 | ||
2030 | /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's | |
2031 | * unconditionally synthesize it here and mark it as perpetual. */ | |
2032 | ||
2033 | u = manager_get_unit(m, SPECIAL_ROOT_MOUNT); | |
2034 | if (!u) { | |
a581e45a | 2035 | r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u); |
2b2ca7ff ZJS |
2036 | if (r < 0) |
2037 | return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m", | |
2038 | SPECIAL_ROOT_MOUNT); | |
11222d0f LP |
2039 | } |
2040 | ||
2041 | u->perpetual = true; | |
2042 | MOUNT(u)->deserialized_state = MOUNT_MOUNTED; | |
2043 | ||
2044 | unit_add_to_load_queue(u); | |
2045 | unit_add_to_dbus_queue(u); | |
2046 | } | |
2047 | ||
2048 | static bool mount_is_mounted(Mount *m) { | |
2049 | assert(m); | |
2050 | ||
ec88d1ea | 2051 | return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED); |
11222d0f LP |
2052 | } |
2053 | ||
edc027b4 | 2054 | static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) { |
99534007 | 2055 | Manager *m = ASSERT_PTR(userdata); |
c29e6a95 | 2056 | Job *j; |
edc027b4 | 2057 | |
c29e6a95 MS |
2058 | /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */ |
2059 | HASHMAP_FOREACH(j, m->jobs) { | |
2060 | if (j->unit->type != UNIT_MOUNT) | |
2061 | continue; | |
2062 | ||
2063 | job_add_to_run_queue(j); | |
2064 | } | |
2065 | ||
b0c4b282 LP |
2066 | /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so |
2067 | * let's make sure we dispatch them in the next iteration. */ | |
2068 | manager_trigger_run_queue(m); | |
edc027b4 MS |
2069 | |
2070 | return 0; | |
2071 | } | |
2072 | ||
ba64af90 | 2073 | static void mount_enumerate(Manager *m) { |
b08d03ff | 2074 | int r; |
d379d442 | 2075 | |
b08d03ff LP |
2076 | assert(m); |
2077 | ||
8d3ae2bd CL |
2078 | mnt_init_debug(0); |
2079 | ||
d379d442 | 2080 | if (!m->mount_monitor) { |
cc2030f9 | 2081 | usec_t mount_rate_limit_interval = 1 * USEC_PER_SEC; |
24a4542c | 2082 | unsigned mount_rate_limit_burst = 5; |
d379d442 | 2083 | int fd; |
ef734fd6 | 2084 | |
d379d442 KZ |
2085 | m->mount_monitor = mnt_new_monitor(); |
2086 | if (!m->mount_monitor) { | |
ba64af90 | 2087 | log_oom(); |
718db961 | 2088 | goto fail; |
d379d442 | 2089 | } |
29083707 | 2090 | |
d379d442 | 2091 | r = mnt_monitor_enable_kernel(m->mount_monitor, 1); |
ba64af90 LP |
2092 | if (r < 0) { |
2093 | log_error_errno(r, "Failed to enable watching of kernel mount events: %m"); | |
29083707 | 2094 | goto fail; |
ba64af90 LP |
2095 | } |
2096 | ||
d379d442 | 2097 | r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL); |
ba64af90 LP |
2098 | if (r < 0) { |
2099 | log_error_errno(r, "Failed to enable watching of userspace mount events: %m"); | |
f7c1ad4f | 2100 | goto fail; |
ba64af90 | 2101 | } |
90598531 | 2102 | |
d379d442 KZ |
2103 | /* mnt_unref_monitor() will close the fd */ |
2104 | fd = r = mnt_monitor_get_fd(m->mount_monitor); | |
ba64af90 LP |
2105 | if (r < 0) { |
2106 | log_error_errno(r, "Failed to acquire watch file descriptor: %m"); | |
f7c1ad4f | 2107 | goto fail; |
ba64af90 | 2108 | } |
befb6d54 | 2109 | |
d379d442 | 2110 | r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m); |
ba64af90 LP |
2111 | if (r < 0) { |
2112 | log_error_errno(r, "Failed to watch mount file descriptor: %m"); | |
befb6d54 | 2113 | goto fail; |
ba64af90 | 2114 | } |
befb6d54 | 2115 | |
d42b61d2 | 2116 | r = sd_event_source_set_priority(m->mount_event_source, EVENT_PRIORITY_MOUNT_TABLE); |
ba64af90 LP |
2117 | if (r < 0) { |
2118 | log_error_errno(r, "Failed to adjust mount watch priority: %m"); | |
befb6d54 | 2119 | goto fail; |
ba64af90 | 2120 | } |
7dfbe2e3 | 2121 | |
24a4542c | 2122 | /* Let users override the default (5 in 1s), as it stalls the boot sequence on busy systems. */ |
cc2030f9 | 2123 | const char *e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC"); |
2124 | if (e) { | |
2125 | r = parse_sec(e, &mount_rate_limit_interval); | |
2126 | if (r < 0) | |
2127 | log_debug_errno(r, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_INTERVAL_SEC, ignoring: %s", e); | |
2128 | } | |
2129 | ||
2130 | e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST"); | |
24a4542c LB |
2131 | if (e) { |
2132 | r = safe_atou(e, &mount_rate_limit_burst); | |
2133 | if (r < 0) | |
cc2030f9 | 2134 | log_debug_errno(r, "Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST, ignoring: %s", e); |
24a4542c LB |
2135 | } |
2136 | ||
cc2030f9 | 2137 | r = sd_event_source_set_ratelimit(m->mount_event_source, mount_rate_limit_interval, mount_rate_limit_burst); |
d586f642 MS |
2138 | if (r < 0) { |
2139 | log_error_errno(r, "Failed to enable rate limit for mount events: %m"); | |
2140 | goto fail; | |
2141 | } | |
2142 | ||
edc027b4 MS |
2143 | r = sd_event_source_set_ratelimit_expire_callback(m->mount_event_source, mount_on_ratelimit_expire); |
2144 | if (r < 0) { | |
2145 | log_error_errno(r, "Failed to enable rate limit for mount events: %m"); | |
2146 | goto fail; | |
2147 | } | |
2148 | ||
d379d442 | 2149 | (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch"); |
befb6d54 CL |
2150 | } |
2151 | ||
e62d8c39 ZJS |
2152 | r = mount_load_proc_self_mountinfo(m, false); |
2153 | if (r < 0) | |
b08d03ff LP |
2154 | goto fail; |
2155 | ||
ba64af90 | 2156 | return; |
b08d03ff LP |
2157 | |
2158 | fail: | |
2159 | mount_shutdown(m); | |
5cb5a6ff LP |
2160 | } |
2161 | ||
fcd8e119 LP |
2162 | static int drain_libmount(Manager *m) { |
2163 | bool rescan = false; | |
2164 | int r; | |
2165 | ||
2166 | assert(m); | |
2167 | ||
2168 | /* Drain all events and verify that the event is valid. | |
2169 | * | |
2170 | * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir | |
2171 | * may generate event which is irrelevant for us. | |
2172 | * | |
2173 | * error: r < 0; valid: r == 0, false positive: r == 1 */ | |
2174 | do { | |
2175 | r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL); | |
2176 | if (r < 0) | |
2177 | return log_error_errno(r, "Failed to drain libmount events: %m"); | |
2178 | if (r == 0) | |
2179 | rescan = true; | |
2180 | } while (r == 0); | |
2181 | ||
2182 | return rescan; | |
2183 | } | |
2184 | ||
35080486 | 2185 | static int mount_process_proc_self_mountinfo(Manager *m) { |
6688b72d | 2186 | _cleanup_set_free_ Set *around = NULL, *gone = NULL; |
ec8126d7 | 2187 | const char *what; |
ef734fd6 LP |
2188 | int r; |
2189 | ||
2190 | assert(m); | |
ef734fd6 | 2191 | |
fcd8e119 LP |
2192 | r = drain_libmount(m); |
2193 | if (r <= 0) | |
2194 | return r; | |
ef734fd6 | 2195 | |
4f0eedb7 ZJS |
2196 | r = mount_load_proc_self_mountinfo(m, true); |
2197 | if (r < 0) { | |
e537352b | 2198 | /* Reset flags, just in case, for later calls */ |
ec88d1ea LP |
2199 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) |
2200 | MOUNT(u)->proc_flags = 0; | |
e537352b | 2201 | |
ec8126d7 | 2202 | return 0; |
ef734fd6 LP |
2203 | } |
2204 | ||
2205 | manager_dispatch_load_queue(m); | |
2206 | ||
595ed347 MS |
2207 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) { |
2208 | Mount *mount = MOUNT(u); | |
ef734fd6 | 2209 | |
11222d0f | 2210 | if (!mount_is_mounted(mount)) { |
e537352b | 2211 | |
1483892a | 2212 | /* A mount point is not around right now. It might be gone, or might never have |
394763f6 LP |
2213 | * existed. */ |
2214 | ||
2215 | if (mount->from_proc_self_mountinfo && | |
6688b72d | 2216 | mount->parameters_proc_self_mountinfo.what) |
394763f6 | 2217 | /* Remember that this device might just have disappeared */ |
6688b72d | 2218 | if (set_put_strdup_full(&gone, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0) |
394763f6 | 2219 | log_oom(); /* we don't care too much about OOM here... */ |
fcd8b266 | 2220 | |
ef734fd6 | 2221 | mount->from_proc_self_mountinfo = false; |
9ddaa3e4 | 2222 | assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0); |
e537352b LP |
2223 | |
2224 | switch (mount->state) { | |
2225 | ||
2226 | case MOUNT_MOUNTED: | |
e3783068 MY |
2227 | /* This has just been unmounted by somebody else, follow the state change. |
2228 | * Also explicitly override the result (see the comment in mount_sigchld_event()), | |
2229 | * but more aggressively here since the state change is extrinsic. */ | |
2230 | mount_cycle_clear(mount); | |
2231 | mount_enter_dead(mount, MOUNT_SUCCESS, /* flush_result = */ true); | |
e537352b LP |
2232 | break; |
2233 | ||
2fa0bd7d YW |
2234 | case MOUNT_MOUNTING_DONE: |
2235 | /* The mount command may add the corresponding proc mountinfo entry and | |
2236 | * then remove it because of an internal error. E.g., fuse.sshfs seems | |
2237 | * to do that when the connection fails. See #17617. To handle such the | |
2238 | * case, let's once set the state back to mounting. Then, the unit can | |
8e94bb62 | 2239 | * correctly enter the failed state later in mount_sigchld_event(). */ |
2fa0bd7d YW |
2240 | mount_set_state(mount, MOUNT_MOUNTING); |
2241 | break; | |
2242 | ||
e537352b | 2243 | default: |
5c9feb2d | 2244 | ; |
e537352b LP |
2245 | } |
2246 | ||
ec88d1ea | 2247 | } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) { |
e537352b | 2248 | |
fcd8b266 | 2249 | /* A mount point was added or changed */ |
e537352b LP |
2250 | |
2251 | switch (mount->state) { | |
2252 | ||
2253 | case MOUNT_DEAD: | |
fdf20a31 | 2254 | case MOUNT_FAILED: |
4b58153d LP |
2255 | |
2256 | /* This has just been mounted by somebody else, follow the state change, but let's | |
2257 | * generate a new invocation ID for this implicitly and automatically. */ | |
7eba1463 LP |
2258 | (void) unit_acquire_invocation_id(u); |
2259 | mount_cycle_clear(mount); | |
9d2f5178 | 2260 | mount_enter_mounted(mount, MOUNT_SUCCESS); |
e537352b LP |
2261 | break; |
2262 | ||
2263 | case MOUNT_MOUNTING: | |
5bcb0f2b | 2264 | mount_set_state(mount, MOUNT_MOUNTING_DONE); |
e537352b LP |
2265 | break; |
2266 | ||
2267 | default: | |
1483892a LP |
2268 | /* Nothing really changed, but let's issue an notification call nonetheless, |
2269 | * in case somebody is waiting for this. (e.g. file system ro/rw | |
2270 | * remounts.) */ | |
e537352b | 2271 | mount_set_state(mount, mount->state); |
e537352b | 2272 | } |
394763f6 | 2273 | } |
fcd8b266 | 2274 | |
11222d0f | 2275 | if (mount_is_mounted(mount) && |
394763f6 | 2276 | mount->from_proc_self_mountinfo && |
6688b72d | 2277 | mount->parameters_proc_self_mountinfo.what) |
b6418dc9 | 2278 | /* Track devices currently used */ |
6688b72d | 2279 | if (set_put_strdup_full(&around, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0) |
394763f6 | 2280 | log_oom(); |
e537352b LP |
2281 | |
2282 | /* Reset the flags for later calls */ | |
ec88d1ea | 2283 | mount->proc_flags = 0; |
e537352b | 2284 | } |
718db961 | 2285 | |
90e74a66 | 2286 | SET_FOREACH(what, gone) { |
fcd8b266 LP |
2287 | if (set_contains(around, what)) |
2288 | continue; | |
2289 | ||
2290 | /* Let the device units know that the device is no longer mounted */ | |
b1ba0ce8 | 2291 | device_found_node(m, what, DEVICE_NOT_FOUND, DEVICE_FOUND_MOUNT); |
fcd8b266 | 2292 | } |
ec8126d7 ZJS |
2293 | |
2294 | return 0; | |
e537352b LP |
2295 | } |
2296 | ||
35080486 | 2297 | static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 2298 | Manager *m = ASSERT_PTR(userdata); |
35080486 | 2299 | |
35080486 LP |
2300 | assert(revents & EPOLLIN); |
2301 | ||
2302 | return mount_process_proc_self_mountinfo(m); | |
2303 | } | |
2304 | ||
fdf20a31 | 2305 | static void mount_reset_failed(Unit *u) { |
5632e374 LP |
2306 | Mount *m = MOUNT(u); |
2307 | ||
2308 | assert(m); | |
2309 | ||
fdf20a31 | 2310 | if (m->state == MOUNT_FAILED) |
5632e374 LP |
2311 | mount_set_state(m, MOUNT_DEAD); |
2312 | ||
9d2f5178 LP |
2313 | m->result = MOUNT_SUCCESS; |
2314 | m->reload_result = MOUNT_SUCCESS; | |
17e9d53d | 2315 | m->clean_result = MOUNT_SUCCESS; |
5632e374 LP |
2316 | } |
2317 | ||
37eb258e LP |
2318 | static PidRef* mount_control_pid(Unit *u) { |
2319 | return &ASSERT_PTR(MOUNT(u))->control_pid; | |
291d565a LP |
2320 | } |
2321 | ||
17e9d53d YW |
2322 | static int mount_clean(Unit *u, ExecCleanMask mask) { |
2323 | _cleanup_strv_free_ char **l = NULL; | |
2324 | Mount *m = MOUNT(u); | |
2325 | int r; | |
2326 | ||
2327 | assert(m); | |
2328 | assert(mask != 0); | |
2329 | ||
2330 | if (m->state != MOUNT_DEAD) | |
2331 | return -EBUSY; | |
2332 | ||
2333 | r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l); | |
2334 | if (r < 0) | |
2335 | return r; | |
2336 | ||
2337 | if (strv_isempty(l)) | |
2338 | return -EUNATCH; | |
2339 | ||
2340 | mount_unwatch_control_pid(m); | |
2341 | m->clean_result = MOUNT_SUCCESS; | |
2342 | m->control_command = NULL; | |
2343 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
2344 | ||
e9276800 | 2345 | r = mount_arm_timer(m, /* relative= */ true, m->exec_context.timeout_clean_usec); |
d30eb0b5 LP |
2346 | if (r < 0) { |
2347 | log_unit_warning_errno(u, r, "Failed to install timer: %m"); | |
17e9d53d | 2348 | goto fail; |
d30eb0b5 | 2349 | } |
17e9d53d | 2350 | |
4775b55d | 2351 | r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid); |
d30eb0b5 LP |
2352 | if (r < 0) { |
2353 | log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m"); | |
360f384f | 2354 | goto fail; |
d30eb0b5 | 2355 | } |
17e9d53d | 2356 | |
360f384f | 2357 | mount_set_state(m, MOUNT_CLEANING); |
17e9d53d YW |
2358 | return 0; |
2359 | ||
2360 | fail: | |
17e9d53d | 2361 | m->clean_result = MOUNT_FAILURE_RESOURCES; |
5dcadb4c | 2362 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
17e9d53d YW |
2363 | return r; |
2364 | } | |
2365 | ||
2366 | static int mount_can_clean(Unit *u, ExecCleanMask *ret) { | |
e9fa1bf7 | 2367 | Mount *m = ASSERT_PTR(MOUNT(u)); |
17e9d53d YW |
2368 | |
2369 | return exec_context_get_clean_mask(&m->exec_context, ret); | |
2370 | } | |
2371 | ||
705578c3 | 2372 | static int mount_can_start(Unit *u) { |
e9fa1bf7 | 2373 | Mount *m = ASSERT_PTR(MOUNT(u)); |
9727f242 DDM |
2374 | int r; |
2375 | ||
9727f242 DDM |
2376 | r = unit_test_start_limit(u); |
2377 | if (r < 0) { | |
e3783068 | 2378 | mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT, /* flush_result = */ false); |
9727f242 DDM |
2379 | return r; |
2380 | } | |
2381 | ||
74c0d972 MY |
2382 | if (!get_mount_parameters_fragment(m)) |
2383 | return -ENOENT; | |
2384 | ||
705578c3 | 2385 | return 1; |
9727f242 DDM |
2386 | } |
2387 | ||
47261967 LP |
2388 | static int mount_subsystem_ratelimited(Manager *m) { |
2389 | assert(m); | |
2390 | ||
2391 | if (!m->mount_event_source) | |
2392 | return false; | |
2393 | ||
2394 | return sd_event_source_is_ratelimited(m->mount_event_source); | |
2395 | } | |
2396 | ||
222b0b05 LP |
2397 | char* mount_get_where_escaped(const Mount *m) { |
2398 | assert(m); | |
2399 | ||
2400 | if (!m->where) | |
2401 | return strdup(""); | |
2402 | ||
2403 | return utf8_escape_invalid(m->where); | |
2404 | } | |
2405 | ||
8dbab37d DDM |
2406 | char* mount_get_what_escaped(const Mount *m) { |
2407 | _cleanup_free_ char *escaped = NULL; | |
2408 | const char *s = NULL; | |
2409 | ||
2410 | assert(m); | |
2411 | ||
2412 | if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what) | |
2413 | s = m->parameters_proc_self_mountinfo.what; | |
2414 | else if (m->from_fragment && m->parameters_fragment.what) | |
2415 | s = m->parameters_fragment.what; | |
2416 | ||
2417 | if (s) { | |
2418 | escaped = utf8_escape_invalid(s); | |
2419 | if (!escaped) | |
2420 | return NULL; | |
2421 | } | |
2422 | ||
2423 | return escaped ? TAKE_PTR(escaped) : strdup(""); | |
2424 | } | |
2425 | ||
2426 | char* mount_get_options_escaped(const Mount *m) { | |
8dbab37d DDM |
2427 | const char *s = NULL; |
2428 | ||
2429 | assert(m); | |
2430 | ||
2431 | if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.options) | |
2432 | s = m->parameters_proc_self_mountinfo.options; | |
2433 | else if (m->from_fragment && m->parameters_fragment.options) | |
2434 | s = m->parameters_fragment.options; | |
3cea9c40 MY |
2435 | if (!s) |
2436 | return strdup(""); | |
8dbab37d | 2437 | |
3cea9c40 | 2438 | return utf8_escape_invalid(s); |
8dbab37d DDM |
2439 | } |
2440 | ||
2441 | const char* mount_get_fstype(const Mount *m) { | |
2442 | assert(m); | |
2443 | ||
2444 | if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.fstype) | |
2445 | return m->parameters_proc_self_mountinfo.fstype; | |
2446 | ||
2447 | if (m->from_fragment && m->parameters_fragment.fstype) | |
2448 | return m->parameters_fragment.fstype; | |
2449 | ||
2450 | return NULL; | |
2451 | } | |
2452 | ||
a16e1123 | 2453 | static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = { |
48d83e33 | 2454 | [MOUNT_EXEC_MOUNT] = "ExecMount", |
a16e1123 LP |
2455 | [MOUNT_EXEC_UNMOUNT] = "ExecUnmount", |
2456 | [MOUNT_EXEC_REMOUNT] = "ExecRemount", | |
2457 | }; | |
2458 | ||
2459 | DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand); | |
2460 | ||
9d2f5178 | 2461 | static const char* const mount_result_table[_MOUNT_RESULT_MAX] = { |
48d83e33 ZJS |
2462 | [MOUNT_SUCCESS] = "success", |
2463 | [MOUNT_FAILURE_RESOURCES] = "resources", | |
2464 | [MOUNT_FAILURE_TIMEOUT] = "timeout", | |
2465 | [MOUNT_FAILURE_EXIT_CODE] = "exit-code", | |
2466 | [MOUNT_FAILURE_SIGNAL] = "signal", | |
2467 | [MOUNT_FAILURE_CORE_DUMP] = "core-dump", | |
07299350 | 2468 | [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
48d83e33 | 2469 | [MOUNT_FAILURE_PROTOCOL] = "protocol", |
9d2f5178 LP |
2470 | }; |
2471 | ||
2472 | DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult); | |
2473 | ||
87f0e418 | 2474 | const UnitVTable mount_vtable = { |
7d17cfbc | 2475 | .object_size = sizeof(Mount), |
718db961 LP |
2476 | .exec_context_offset = offsetof(Mount, exec_context), |
2477 | .cgroup_context_offset = offsetof(Mount, cgroup_context), | |
2478 | .kill_context_offset = offsetof(Mount, kill_context), | |
613b411c | 2479 | .exec_runtime_offset = offsetof(Mount, exec_runtime), |
9cc54544 | 2480 | .cgroup_runtime_offset = offsetof(Mount, cgroup_runtime), |
3ef63c31 | 2481 | |
f975e971 LP |
2482 | .sections = |
2483 | "Unit\0" | |
2484 | "Mount\0" | |
2485 | "Install\0", | |
4ad49000 | 2486 | .private_section = "Mount", |
71645aca | 2487 | |
c80a9a33 LP |
2488 | .can_transient = true, |
2489 | .can_fail = true, | |
755021d4 | 2490 | .exclude_from_switch_root_serialization = true, |
c80a9a33 | 2491 | |
e537352b LP |
2492 | .init = mount_init, |
2493 | .load = mount_load, | |
034c6ed7 | 2494 | .done = mount_done, |
e537352b | 2495 | |
f50e0a01 | 2496 | .coldplug = mount_coldplug, |
01400460 | 2497 | .catchup = mount_catchup, |
f50e0a01 | 2498 | |
034c6ed7 | 2499 | .dump = mount_dump, |
5cb5a6ff | 2500 | |
e537352b LP |
2501 | .start = mount_start, |
2502 | .stop = mount_stop, | |
74c0d972 | 2503 | |
e537352b | 2504 | .reload = mount_reload, |
74c0d972 | 2505 | .can_reload = mount_can_reload, |
e537352b | 2506 | |
17e9d53d YW |
2507 | .clean = mount_clean, |
2508 | .can_clean = mount_can_clean, | |
8a0867d6 | 2509 | |
a16e1123 LP |
2510 | .serialize = mount_serialize, |
2511 | .deserialize_item = mount_deserialize_item, | |
2512 | ||
f50e0a01 | 2513 | .active_state = mount_active_state, |
10a94420 | 2514 | .sub_state_to_string = mount_sub_state_to_string, |
b08d03ff | 2515 | |
52a12341 YW |
2516 | .will_restart = unit_will_restart_default, |
2517 | ||
f2f725e5 | 2518 | .may_gc = mount_may_gc, |
39c79477 | 2519 | .is_extrinsic = mount_is_extrinsic, |
701cc384 | 2520 | |
e537352b | 2521 | .sigchld_event = mount_sigchld_event, |
e537352b | 2522 | |
fdf20a31 | 2523 | .reset_failed = mount_reset_failed, |
291d565a | 2524 | |
19ae8986 LP |
2525 | .notify_handoff_timestamp = mount_handoff_timestamp, |
2526 | ||
291d565a | 2527 | .control_pid = mount_control_pid, |
5632e374 | 2528 | |
74c964d3 LP |
2529 | .bus_set_property = bus_mount_set_property, |
2530 | .bus_commit_properties = bus_mount_commit_properties, | |
4139c1b2 | 2531 | |
68db7a3b ZJS |
2532 | .get_timeout = mount_get_timeout, |
2533 | ||
04eb582a | 2534 | .enumerate_perpetual = mount_enumerate_perpetual, |
f50e0a01 | 2535 | .enumerate = mount_enumerate, |
c6918296 | 2536 | .shutdown = mount_shutdown, |
47261967 | 2537 | .subsystem_ratelimited = mount_subsystem_ratelimited, |
c6918296 MS |
2538 | |
2539 | .status_message_formats = { | |
2540 | .starting_stopping = { | |
2541 | [0] = "Mounting %s...", | |
2542 | [1] = "Unmounting %s...", | |
2543 | }, | |
2544 | .finished_start_job = { | |
2545 | [JOB_DONE] = "Mounted %s.", | |
2546 | [JOB_FAILED] = "Failed to mount %s.", | |
c6918296 MS |
2547 | [JOB_TIMEOUT] = "Timed out mounting %s.", |
2548 | }, | |
2549 | .finished_stop_job = { | |
2550 | [JOB_DONE] = "Unmounted %s.", | |
2551 | [JOB_FAILED] = "Failed unmounting %s.", | |
2552 | [JOB_TIMEOUT] = "Timed out unmounting %s.", | |
2553 | }, | |
2554 | }, | |
9727f242 | 2555 | |
705578c3 | 2556 | .can_start = mount_can_start, |
b2bfd121 LP |
2557 | |
2558 | .notify_plymouth = true, | |
5cb5a6ff | 2559 | }; |