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