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