]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/mount.c
pid1,shutdown: do not cunescape paths from libmount
[thirdparty/systemd.git] / src / core / mount.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
4f5dd394 16#include "fstab-util.h"
fb36b133 17#include "libmount-util.h"
4f5dd394
LP
18#include "log.h"
19#include "manager.h"
20#include "mkdir.h"
21#include "mount-setup.h"
22#include "mount.h"
049af8ad 23#include "mountpoint-util.h"
6bedfcbb 24#include "parse-util.h"
4f5dd394 25#include "path-util.h"
7b3e062c 26#include "process-util.h"
d68c645b 27#include "serialize.h"
4f5dd394 28#include "special.h"
8b43440b 29#include "string-table.h"
b5efdb8a 30#include "string-util.h"
4f5dd394
LP
31#include "strv.h"
32#include "unit-name.h"
33#include "unit.h"
5cb5a6ff 34
7d54a03a
LP
35#define RETRY_UMOUNT_MAX 32
36
f50e0a01
LP
37static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
38 [MOUNT_DEAD] = UNIT_INACTIVE,
39 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
b6ba0c16 40 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVATING,
f50e0a01 41 [MOUNT_MOUNTED] = UNIT_ACTIVE,
032ff4af 42 [MOUNT_REMOUNTING] = UNIT_RELOADING,
f50e0a01 43 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
032ff4af
LP
44 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
45 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
e537352b
LP
46 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
47 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 48 [MOUNT_FAILED] = UNIT_FAILED
f50e0a01 49};
5cb5a6ff 50
718db961
LP
51static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
52static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
53
c634f3d2
LP
54static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
55 return IN_SET(state,
56 MOUNT_MOUNTING,
57 MOUNT_MOUNTING_DONE,
58 MOUNT_REMOUNTING,
59 MOUNT_REMOUNTING_SIGTERM,
60 MOUNT_REMOUNTING_SIGKILL,
61 MOUNT_UNMOUNTING,
62 MOUNT_UNMOUNTING_SIGTERM,
63 MOUNT_UNMOUNTING_SIGKILL);
64}
65
7121cbcf
LP
66static bool mount_is_network(const MountParameters *p) {
67 assert(p);
68
69 if (fstab_test_option(p->options, "_netdev\0"))
fc676b00
ZJS
70 return true;
71
7121cbcf 72 if (p->fstype && fstype_is_network(p->fstype))
fc676b00
ZJS
73 return true;
74
75 return false;
76}
77
d3bd0986
MK
78static bool mount_is_loop(const MountParameters *p) {
79 assert(p);
80
81 if (fstab_test_option(p->options, "loop\0"))
82 return true;
83
84 return false;
85}
86
e6a7b9f4 87static bool mount_is_bind(const MountParameters *p) {
fc676b00
ZJS
88 assert(p);
89
d15d0333 90 if (fstab_test_option(p->options, "bind\0" "rbind\0"))
fc676b00
ZJS
91 return true;
92
d15d0333 93 if (p->fstype && STR_IN_SET(p->fstype, "bind", "rbind"))
fc676b00
ZJS
94 return true;
95
96 return false;
97}
98
ebc8968b
FB
99static bool mount_is_bound_to_device(const Mount *m) {
100 const MountParameters *p;
101
102 if (m->from_fragment)
103 return true;
104
105 p = &m->parameters_proc_self_mountinfo;
106 return fstab_test_option(p->options, "x-systemd.device-bound\0");
107}
108
06e457b1 109static bool mount_needs_quota(const MountParameters *p) {
fc676b00
ZJS
110 assert(p);
111
06e457b1
LP
112 /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via
113 * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */
11041c84 114 if (p->fstype && fstype_is_network(p->fstype))
fc676b00
ZJS
115 return false;
116
117 if (mount_is_bind(p))
118 return false;
119
d15d0333
ZJS
120 return fstab_test_option(p->options,
121 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
fc676b00
ZJS
122}
123
a16e1123
LP
124static void mount_init(Unit *u) {
125 Mount *m = MOUNT(u);
5cb5a6ff 126
a16e1123 127 assert(u);
ac155bb8 128 assert(u->load_state == UNIT_STUB);
a16e1123 129
1f19a534 130 m->timeout_usec = u->manager->default_timeout_start_usec;
5804e1b6
AJ
131
132 m->exec_context.std_output = u->manager->default_std_output;
133 m->exec_context.std_error = u->manager->default_std_error;
134
3e5235b0
LP
135 m->directory_mode = 0755;
136
f00929ad
DJL
137 /* We need to make sure that /usr/bin/mount is always called
138 * in the same process group as us, so that the autofs kernel
a16e1123
LP
139 * side doesn't send us another mount request while we are
140 * already trying to comply its last one. */
74922904 141 m->exec_context.same_pgrp = true;
8d567588 142
a16e1123 143 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
c8f4d764 144
5bcb0f2b 145 u->ignore_on_isolate = true;
8d567588
LP
146}
147
36c16a7c 148static int mount_arm_timer(Mount *m, usec_t usec) {
718db961
LP
149 int r;
150
151 assert(m);
152
718db961 153 if (m->timer_event_source) {
36c16a7c 154 r = sd_event_source_set_time(m->timer_event_source, usec);
718db961
LP
155 if (r < 0)
156 return r;
157
158 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
159 }
160
36c16a7c
LP
161 if (usec == USEC_INFINITY)
162 return 0;
163
7dfbe2e3 164 r = sd_event_add_time(
6a0f1f6d
LP
165 UNIT(m)->manager->event,
166 &m->timer_event_source,
167 CLOCK_MONOTONIC,
36c16a7c 168 usec, 0,
6a0f1f6d 169 mount_dispatch_timer, m);
7dfbe2e3
TG
170 if (r < 0)
171 return r;
172
173 (void) sd_event_source_set_description(m->timer_event_source, "mount-timer");
174
175 return 0;
718db961
LP
176}
177
a16e1123 178static void mount_unwatch_control_pid(Mount *m) {
5e94833f
LP
179 assert(m);
180
181 if (m->control_pid <= 0)
182 return;
183
184 unit_unwatch_pid(UNIT(m), m->control_pid);
185 m->control_pid = 0;
186}
187
e537352b
LP
188static void mount_parameters_done(MountParameters *p) {
189 assert(p);
190
a26592cf
LP
191 p->what = mfree(p->what);
192 p->options = mfree(p->options);
193 p->fstype = mfree(p->fstype);
e537352b
LP
194}
195
87f0e418 196static void mount_done(Unit *u) {
ef734fd6 197 Mount *m = MOUNT(u);
034c6ed7 198
ef734fd6 199 assert(m);
034c6ed7 200
a1e58e8e 201 m->where = mfree(m->where);
f50e0a01 202
e537352b
LP
203 mount_parameters_done(&m->parameters_proc_self_mountinfo);
204 mount_parameters_done(&m->parameters_fragment);
ef734fd6 205
e8a565cb 206 m->exec_runtime = exec_runtime_unref(m->exec_runtime, false);
e537352b
LP
207 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
208 m->control_command = NULL;
f50e0a01 209
29206d46
LP
210 dynamic_creds_unref(&m->dynamic_creds);
211
a16e1123 212 mount_unwatch_control_pid(m);
f50e0a01 213
718db961 214 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
f50e0a01
LP
215}
216
44a6b1b6 217_pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
cb39ed3f
LP
218 assert(m);
219
220 if (m->from_fragment)
221 return &m->parameters_fragment;
cb39ed3f
LP
222
223 return NULL;
224}
225
44a6b1b6 226_pure_ static MountParameters* get_mount_parameters(Mount *m) {
cb39ed3f
LP
227 assert(m);
228
229 if (m->from_proc_self_mountinfo)
230 return &m->parameters_proc_self_mountinfo;
231
6b1dc2bd 232 return get_mount_parameters_fragment(m);
cb39ed3f
LP
233}
234
b7bbf890
ZJS
235static int update_parameters_proc_self_mount_info(
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
244 p = &m->parameters_proc_self_mountinfo;
245
246 r = free_and_strdup(&p->what, what);
247 if (r < 0)
248 return r;
249
250 q = free_and_strdup(&p->options, options);
251 if (q < 0)
252 return q;
253
254 w = free_and_strdup(&p->fstype, fstype);
255 if (w < 0)
256 return w;
257
258 return r > 0 || q > 0 || w > 0;
259}
260
eef85c4a 261static int mount_add_mount_dependencies(Mount *m) {
5c78d8e2 262 MountParameters *pm;
ac155bb8 263 Unit *other;
a57f7e2c
LP
264 Iterator i;
265 Set *s;
b08d03ff
LP
266 int r;
267
6e2ef85b 268 assert(m);
b08d03ff 269
a57f7e2c 270 if (!path_equal(m->where, "/")) {
eef85c4a
LP
271 _cleanup_free_ char *parent = NULL;
272
273 /* Adds in links to other mount points that might lie further up in the hierarchy */
5f311f8c
LP
274
275 parent = dirname_malloc(m->where);
276 if (!parent)
277 return -ENOMEM;
01f78473 278
eef85c4a 279 r = unit_require_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT);
4f0eedb7 280 if (r < 0)
01f78473 281 return r;
4f0eedb7 282 }
01f78473 283
eef85c4a
LP
284 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
285 * or a loop mount) to be available. */
a57f7e2c 286 pm = get_mount_parameters_fragment(m);
fc676b00
ZJS
287 if (pm && pm->what &&
288 path_is_absolute(pm->what) &&
d3bd0986 289 (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) {
fc676b00 290
eef85c4a 291 r = unit_require_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE);
4f0eedb7 292 if (r < 0)
6e2ef85b 293 return r;
4f0eedb7 294 }
b08d03ff 295
eef85c4a 296 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
a57f7e2c
LP
297 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
298 SET_FOREACH(other, s, i) {
b08d03ff 299
a57f7e2c
LP
300 if (other->load_state != UNIT_LOADED)
301 continue;
b08d03ff 302
a57f7e2c
LP
303 if (other == UNIT(m))
304 continue;
b08d03ff 305
eef85c4a 306 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true, UNIT_DEPENDENCY_PATH);
4f0eedb7 307 if (r < 0)
6e2ef85b 308 return r;
b08d03ff 309
a57f7e2c
LP
310 if (UNIT(m)->fragment_path) {
311 /* If we have fragment configuration, then make this dependency required */
eef85c4a 312 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true, UNIT_DEPENDENCY_PATH);
a57f7e2c
LP
313 if (r < 0)
314 return r;
315 }
7c8fa05c
LP
316 }
317
318 return 0;
319}
320
eef85c4a 321static int mount_add_device_dependencies(Mount *m) {
eef85c4a
LP
322 UnitDependencyMask mask;
323 MountParameters *p;
ebc8968b 324 UnitDependency dep;
9fff8a1f 325 int r;
173a8d04
LP
326
327 assert(m);
328
06e97888 329 p = get_mount_parameters(m);
6b1dc2bd 330 if (!p)
173a8d04
LP
331 return 0;
332
9fff8a1f 333 if (!p->what)
173a8d04 334 return 0;
5c78d8e2 335
dd144c63
LP
336 if (mount_is_bind(p))
337 return 0;
338
339 if (!is_device_path(p->what))
340 return 0;
341
7ba2711d
LP
342 /* /dev/root is a really weird thing, it's not a real device,
343 * but just a path the kernel exports for the root file system
344 * specified on the kernel command line. Ignore it here. */
345 if (path_equal(p->what, "/dev/root"))
346 return 0;
347
dd144c63
LP
348 if (path_equal(m->where, "/"))
349 return 0;
350
ebc8968b
FB
351 /* Mount units from /proc/self/mountinfo are not bound to devices
352 * by default since they're subject to races when devices are
353 * unplugged. But the user can still force this dep with an
354 * appropriate option (or udev property) so the mount units are
355 * automatically stopped when the device disappears suddenly. */
356 dep = mount_is_bound_to_device(m) ? UNIT_BINDS_TO : UNIT_REQUIRES;
357
c52c2dc6
ZJS
358 /* We always use 'what' from /proc/self/mountinfo if mounted */
359 mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT : UNIT_DEPENDENCY_FILE;
eef85c4a 360
142b8142 361 r = unit_add_node_dependency(UNIT(m), p->what, false, dep, mask);
dd144c63
LP
362 if (r < 0)
363 return r;
9fff8a1f 364
9fff8a1f 365 return 0;
173a8d04
LP
366}
367
eef85c4a
LP
368static int mount_add_quota_dependencies(Mount *m) {
369 UnitDependencyMask mask;
6b1dc2bd 370 MountParameters *p;
eef85c4a 371 int r;
6b1dc2bd
LP
372
373 assert(m);
374
463d0d15 375 if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
6b1dc2bd
LP
376 return 0;
377
378 p = get_mount_parameters_fragment(m);
379 if (!p)
380 return 0;
381
06e457b1 382 if (!mount_needs_quota(p))
6b1dc2bd
LP
383 return 0;
384
eef85c4a
LP
385 mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT;
386
5a724170 387 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, true, mask);
6b1dc2bd
LP
388 if (r < 0)
389 return r;
390
5a724170 391 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, true, mask);
6b1dc2bd
LP
392 if (r < 0)
393 return r;
394
395 return 0;
396}
397
ad2706db 398static bool mount_is_extrinsic(Mount *m) {
88ac30a1 399 MountParameters *p;
ad2706db 400 assert(m);
88ac30a1 401
ad2706db
LP
402 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
403 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
404 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
405
406 if (!MANAGER_IS_SYSTEM(UNIT(m)->manager)) /* We only automatically manage mounts if we are in system mode */
407 return true;
408
409 if (PATH_IN_SET(m->where, /* Don't bother with the OS data itself */
410 "/",
411 "/usr"))
412 return true;
88ac30a1 413
ad2706db
LP
414 if (PATH_STARTSWITH_SET(m->where,
415 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
416 "/proc", /* All of this is API VFS */
417 "/sys", /* … dito … */
418 "/dev")) /* … dito … */
419 return true;
420
421 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
88ac30a1 422 p = get_mount_parameters(m);
ad2706db
LP
423 if (p && fstab_test_option(p->options, "x-initrd.mount\0") && !in_initrd())
424 return true;
88ac30a1 425
ad2706db 426 return false;
88ac30a1
TG
427}
428
2edd4434 429static int mount_add_default_dependencies(Mount *m) {
d54bab90 430 const char *after, *before;
eef85c4a 431 UnitDependencyMask mask;
9ddc4a26 432 MountParameters *p;
8c8203db 433 bool nofail;
d54bab90 434 int r;
2edd4434
LP
435
436 assert(m);
437
4c9ea260
LP
438 if (!UNIT(m)->default_dependencies)
439 return 0;
440
ad2706db
LP
441 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
442 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
443 * file systems, it's also going to be virtual, and hence not worth the effort. */
444 if (mount_is_extrinsic(m))
6b1dc2bd 445 return 0;
2edd4434 446
874d3404
LP
447 p = get_mount_parameters(m);
448 if (!p)
6b1dc2bd
LP
449 return 0;
450
eef85c4a 451 mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_DEFAULT;
8c8203db 452 nofail = m->from_fragment ? fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0") : false;
eef85c4a 453
e8d2f6cd 454 if (mount_is_network(p)) {
ea0ec5ce
LP
455 /* We order ourselves after network.target. This is
456 * primarily useful at shutdown: services that take
457 * down the network should order themselves before
458 * network.target, so that they are shut down only
459 * after this mount unit is stopped. */
6b1dc2bd 460
35d8c19a 461 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, true, mask);
a63a5c46
LP
462 if (r < 0)
463 return r;
a63a5c46 464
ea0ec5ce
LP
465 /* We pull in network-online.target, and order
466 * ourselves after it. This is useful at start-up to
467 * actively pull in tools that want to be started
468 * before we start mounting network file systems, and
469 * whose purpose it is to delay this until the network
470 * is "up". */
471
5a724170 472 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, true, mask);
e8d2f6cd
LP
473 if (r < 0)
474 return r;
ea0ec5ce
LP
475
476 after = SPECIAL_REMOTE_FS_PRE_TARGET;
d54bab90
LP
477 before = SPECIAL_REMOTE_FS_TARGET;
478 } else {
ea0ec5ce 479 after = SPECIAL_LOCAL_FS_PRE_TARGET;
d54bab90
LP
480 before = SPECIAL_LOCAL_FS_TARGET;
481 }
482
8c8203db
YW
483 if (!nofail) {
484 r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, true, mask);
485 if (r < 0)
486 return r;
487 }
ea0ec5ce 488
35d8c19a 489 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, true, mask);
ea0ec5ce
LP
490 if (r < 0)
491 return r;
e8d2f6cd 492
5a724170 493 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, mask);
ad2706db
LP
494 if (r < 0)
495 return r;
2edd4434 496
3e3852b3 497 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
74b1731c 498 if (streq_ptr(p->fstype, "tmpfs")) {
35d8c19a 499 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, true, mask);
3e3852b3
LP
500 if (r < 0)
501 return r;
502 }
503
2edd4434
LP
504 return 0;
505}
506
8d567588 507static int mount_verify(Mount *m) {
a57f7e2c 508 _cleanup_free_ char *e = NULL;
b294b79f 509 MountParameters *p;
7410616c 510 int r;
a57f7e2c 511
8d567588
LP
512 assert(m);
513
1124fe6f 514 if (UNIT(m)->load_state != UNIT_LOADED)
8d567588
LP
515 return 0;
516
1df96fcb 517 if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual)
8cbef760
LP
518 return -ENOENT;
519
7410616c
LP
520 r = unit_name_from_path(m->where, ".mount", &e);
521 if (r < 0)
f2341e0a 522 return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
8d567588 523
7410616c 524 if (!unit_has_name(UNIT(m), e)) {
f2341e0a 525 log_unit_error(UNIT(m), "Where= setting doesn't match unit name. Refusing.");
6f40aa45 526 return -ENOEXEC;
8d567588
LP
527 }
528
33ff02c9 529 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
f2341e0a 530 log_unit_error(UNIT(m), "Cannot create mount unit for API file system %s. Refusing.", m->where);
6f40aa45 531 return -ENOEXEC;
33ff02c9
LP
532 }
533
b294b79f
MO
534 p = get_mount_parameters_fragment(m);
535 if (p && !p->what) {
f2341e0a 536 log_unit_error(UNIT(m), "What= setting is missing. Refusing.");
6f40aa45 537 return -ENOEXEC;
4e85aff4
LP
538 }
539
4819ff03 540 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 541 log_unit_error(UNIT(m), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
6f40aa45 542 return -ENOEXEC;
4d0e5dbd
LP
543 }
544
8d567588
LP
545 return 0;
546}
547
1a4ac875
MS
548static int mount_add_extras(Mount *m) {
549 Unit *u = UNIT(m);
e537352b
LP
550 int r;
551
e821075a
LP
552 assert(m);
553
1f736476
LP
554 /* Note: this call might be called after we already have been loaded once (and even when it has already been
555 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
556 * to run with an already set up unit. */
557
e821075a 558 if (u->fragment_path)
1a4ac875 559 m->from_fragment = true;
e537352b 560
1a4ac875 561 if (!m->where) {
7410616c
LP
562 r = unit_name_to_path(u->id, &m->where);
563 if (r < 0)
564 return r;
1a4ac875 565 }
a16e1123 566
858d36c1 567 path_simplify(m->where, false);
e537352b 568
e821075a 569 if (!u->description) {
1a4ac875
MS
570 r = unit_set_description(u, m->where);
571 if (r < 0)
173a8d04 572 return r;
1a4ac875 573 }
6e2ef85b 574
eef85c4a 575 r = mount_add_device_dependencies(m);
1a4ac875
MS
576 if (r < 0)
577 return r;
6e2ef85b 578
eef85c4a 579 r = mount_add_mount_dependencies(m);
1a4ac875
MS
580 if (r < 0)
581 return r;
6e2ef85b 582
eef85c4a 583 r = mount_add_quota_dependencies(m);
1a4ac875
MS
584 if (r < 0)
585 return r;
e537352b 586
598459ce
LP
587 r = unit_patch_contexts(u);
588 if (r < 0)
589 return r;
4e67ddd6 590
598459ce 591 r = unit_add_exec_dependencies(u, &m->exec_context);
a016b922
LP
592 if (r < 0)
593 return r;
594
d79200e2 595 r = unit_set_default_slice(u);
1a4ac875
MS
596 if (r < 0)
597 return r;
598
4c9ea260
LP
599 r = mount_add_default_dependencies(m);
600 if (r < 0)
601 return r;
598459ce 602
1a4ac875
MS
603 return 0;
604}
605
11222d0f
LP
606static int mount_load_root_mount(Unit *u) {
607 assert(u);
608
609 if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
610 return 0;
611
612 u->perpetual = true;
613 u->default_dependencies = false;
614
615 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
616 MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL;
617 MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL;
618
619 if (!u->description)
620 u->description = strdup("Root Mount");
621
622 return 1;
623}
624
1a4ac875
MS
625static int mount_load(Unit *u) {
626 Mount *m = MOUNT(u);
780ae022 627 int r, q, w;
1a4ac875
MS
628
629 assert(u);
630 assert(u->load_state == UNIT_STUB);
631
11222d0f 632 r = mount_load_root_mount(u);
11222d0f
LP
633
634 if (m->from_proc_self_mountinfo || u->perpetual)
780ae022 635 q = unit_load_fragment_and_dropin_optional(u);
8eba616f 636 else
780ae022
LP
637 q = unit_load_fragment_and_dropin(u);
638
639 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
640 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
641 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
642 * 'slice' field for a unit once it is active. */
643 if (u->load_state == UNIT_LOADED || m->from_proc_self_mountinfo || u->perpetual)
644 w = mount_add_extras(m);
645 else
646 w = 0;
647
1a4ac875
MS
648 if (r < 0)
649 return r;
780ae022
LP
650 if (q < 0)
651 return q;
652 if (w < 0)
653 return w;
e537352b 654
8d567588 655 return mount_verify(m);
e537352b
LP
656}
657
658static void mount_set_state(Mount *m, MountState state) {
659 MountState old_state;
660 assert(m);
661
6fcbec6f
LP
662 if (m->state != state)
663 bus_unit_send_pending_change_signal(UNIT(m), false);
664
e537352b
LP
665 old_state = m->state;
666 m->state = state;
667
c634f3d2 668 if (!MOUNT_STATE_WITH_PROCESS(state)) {
718db961 669 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
a16e1123 670 mount_unwatch_control_pid(m);
e537352b 671 m->control_command = NULL;
a16e1123 672 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
e537352b
LP
673 }
674
675 if (state != old_state)
f2341e0a 676 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
e537352b 677
2ad2e41a
LP
678 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state],
679 m->reload_result == MOUNT_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE);
e537352b
LP
680}
681
be847e82 682static int mount_coldplug(Unit *u) {
e537352b 683 Mount *m = MOUNT(u);
a16e1123
LP
684 MountState new_state = MOUNT_DEAD;
685 int r;
e537352b
LP
686
687 assert(m);
688 assert(m->state == MOUNT_DEAD);
689
a16e1123
LP
690 if (m->deserialized_state != m->state)
691 new_state = m->deserialized_state;
692 else if (m->from_proc_self_mountinfo)
693 new_state = MOUNT_MOUNTED;
e537352b 694
5bcb0f2b
LP
695 if (new_state == m->state)
696 return 0;
e537352b 697
c386f588
LP
698 if (m->control_pid > 0 &&
699 pid_is_unwaited(m->control_pid) &&
c634f3d2 700 MOUNT_STATE_WITH_PROCESS(new_state)) {
5bcb0f2b 701
f75f613d 702 r = unit_watch_pid(UNIT(m), m->control_pid, false);
5bcb0f2b
LP
703 if (r < 0)
704 return r;
e537352b 705
36c16a7c 706 r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
5bcb0f2b
LP
707 if (r < 0)
708 return r;
a16e1123 709 }
e537352b 710
e8a565cb 711 if (!IN_SET(new_state, MOUNT_DEAD, MOUNT_FAILED)) {
29206d46 712 (void) unit_setup_dynamic_creds(u);
e8a565cb
YW
713 (void) unit_setup_exec_runtime(u);
714 }
29206d46 715
5bcb0f2b 716 mount_set_state(m, new_state);
e537352b 717 return 0;
e537352b
LP
718}
719
720static void mount_dump(Unit *u, FILE *f, const char *prefix) {
f2e18ef1 721 char buf[FORMAT_TIMESPAN_MAX];
e537352b
LP
722 Mount *m = MOUNT(u);
723 MountParameters *p;
724
725 assert(m);
726 assert(f);
727
cb39ed3f 728 p = get_mount_parameters(m);
e537352b
LP
729
730 fprintf(f,
731 "%sMount State: %s\n"
81a5c6d0 732 "%sResult: %s\n"
e537352b
LP
733 "%sWhere: %s\n"
734 "%sWhat: %s\n"
735 "%sFile System Type: %s\n"
736 "%sOptions: %s\n"
e537352b
LP
737 "%sFrom /proc/self/mountinfo: %s\n"
738 "%sFrom fragment: %s\n"
ad2706db 739 "%sExtrinsic: %s\n"
e520950a 740 "%sDirectoryMode: %04o\n"
49915de2 741 "%sSloppyOptions: %s\n"
4f8d40a9 742 "%sLazyUnmount: %s\n"
91899792 743 "%sForceUnmount: %s\n"
ac8956ef 744 "%sTimeoutSec: %s\n",
a16e1123 745 prefix, mount_state_to_string(m->state),
81a5c6d0 746 prefix, mount_result_to_string(m->result),
e537352b 747 prefix, m->where,
1e4fc9b1
HH
748 prefix, p ? strna(p->what) : "n/a",
749 prefix, p ? strna(p->fstype) : "n/a",
750 prefix, p ? strna(p->options) : "n/a",
e537352b
LP
751 prefix, yes_no(m->from_proc_self_mountinfo),
752 prefix, yes_no(m->from_fragment),
ad2706db 753 prefix, yes_no(mount_is_extrinsic(m)),
e520950a 754 prefix, m->directory_mode,
49915de2 755 prefix, yes_no(m->sloppy_options),
4f8d40a9 756 prefix, yes_no(m->lazy_unmount),
91899792
YW
757 prefix, yes_no(m->force_unmount),
758 prefix, format_timespan(buf, sizeof(buf), m->timeout_usec, USEC_PER_SEC));
e537352b
LP
759
760 if (m->control_pid > 0)
761 fprintf(f,
ccd06097
ZJS
762 "%sControl PID: "PID_FMT"\n",
763 prefix, m->control_pid);
e537352b
LP
764
765 exec_context_dump(&m->exec_context, f, prefix);
4819ff03 766 kill_context_dump(&m->kill_context, f, prefix);
18f573aa 767 cgroup_context_dump(&m->cgroup_context, f, prefix);
e537352b
LP
768}
769
a16e1123 770static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
3c7416b6 771
b9c04eaf 772 _cleanup_(exec_params_clear) ExecParameters exec_params = {
5686391b
LP
773 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
774 .stdin_fd = -1,
775 .stdout_fd = -1,
776 .stderr_fd = -1,
777 .exec_fd = -1,
9fa95f85 778 };
3c7416b6
LP
779 pid_t pid;
780 int r;
a16e1123
LP
781
782 assert(m);
783 assert(c);
784 assert(_pid);
785
3c7416b6 786 r = unit_prepare_exec(UNIT(m));
29206d46
LP
787 if (r < 0)
788 return r;
789
36c16a7c 790 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 791 if (r < 0)
36c16a7c 792 return r;
a16e1123 793
1ad6e8b3
LP
794 r = unit_set_exec_params(UNIT(m), &exec_params);
795 if (r < 0)
796 return r;
9fa95f85 797
f2341e0a
LP
798 r = exec_spawn(UNIT(m),
799 c,
4ad49000 800 &m->exec_context,
9fa95f85 801 &exec_params,
613b411c 802 m->exec_runtime,
29206d46 803 &m->dynamic_creds,
4ad49000
LP
804 &pid);
805 if (r < 0)
36c16a7c 806 return r;
a16e1123 807
f75f613d 808 r = unit_watch_pid(UNIT(m), pid, true);
4ad49000 809 if (r < 0)
36c16a7c 810 return r;
a16e1123
LP
811
812 *_pid = pid;
813
814 return 0;
a16e1123
LP
815}
816
9d2f5178 817static void mount_enter_dead(Mount *m, MountResult f) {
e537352b
LP
818 assert(m);
819
a0fef983 820 if (m->result == MOUNT_SUCCESS)
9d2f5178 821 m->result = f;
e537352b 822
aac99f30 823 unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
29206d46
LP
824 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
825
e8a565cb 826 m->exec_runtime = exec_runtime_unref(m->exec_runtime, true);
613b411c 827
3536f49e 828 exec_context_destroy_runtime_directory(&m->exec_context, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
e66cf1a3 829
00d9ef85
LP
830 unit_unref_uid_gid(UNIT(m), true);
831
29206d46 832 dynamic_creds_destroy(&m->dynamic_creds);
c52c2dc6
ZJS
833
834 /* Any dependencies based on /proc/self/mountinfo are now stale */
835 unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
e537352b
LP
836}
837
9d2f5178 838static void mount_enter_mounted(Mount *m, MountResult f) {
80876c20
LP
839 assert(m);
840
a0fef983 841 if (m->result == MOUNT_SUCCESS)
9d2f5178 842 m->result = f;
80876c20
LP
843
844 mount_set_state(m, MOUNT_MOUNTED);
845}
846
22af0e58
LP
847static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
848 assert(m);
849
850 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
851 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
852 * ultimately we just mirror the kernel's internal state on this. */
853
854 if (m->from_proc_self_mountinfo)
855 mount_enter_mounted(m, f);
856 else
857 mount_enter_dead(m, f);
858}
859
860static int state_to_kill_operation(MountState state) {
861 switch (state) {
862
863 case MOUNT_REMOUNTING_SIGTERM:
864 case MOUNT_UNMOUNTING_SIGTERM:
865 return KILL_TERMINATE;
866
867 case MOUNT_REMOUNTING_SIGKILL:
868 case MOUNT_UNMOUNTING_SIGKILL:
869 return KILL_KILL;
870
871 default:
872 return _KILL_OPERATION_INVALID;
873 }
874}
875
9d2f5178 876static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
e537352b
LP
877 int r;
878
879 assert(m);
880
a0fef983 881 if (m->result == MOUNT_SUCCESS)
9d2f5178 882 m->result = f;
e537352b 883
cd2086fe
LP
884 r = unit_kill_context(
885 UNIT(m),
886 &m->kill_context,
22af0e58 887 state_to_kill_operation(state),
cd2086fe
LP
888 -1,
889 m->control_pid,
890 false);
891 if (r < 0)
892 goto fail;
e537352b 893
cd2086fe 894 if (r > 0) {
36c16a7c 895 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 896 if (r < 0)
80876c20 897 goto fail;
e537352b 898
80876c20 899 mount_set_state(m, state);
22af0e58 900 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 901 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
22af0e58 902 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
9d2f5178 903 mount_enter_mounted(m, MOUNT_SUCCESS);
22af0e58 904 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 905 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
80876c20 906 else
22af0e58 907 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
908
909 return;
910
911fail:
f2341e0a 912 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
22af0e58 913 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
5261ba90
TT
914}
915
9d2f5178 916static void mount_enter_unmounting(Mount *m) {
e537352b
LP
917 int r;
918
919 assert(m);
920
7d54a03a
LP
921 /* Start counting our attempts */
922 if (!IN_SET(m->state,
923 MOUNT_UNMOUNTING,
924 MOUNT_UNMOUNTING_SIGTERM,
925 MOUNT_UNMOUNTING_SIGKILL))
926 m->n_retry_umount = 0;
927
a16e1123
LP
928 m->control_command_id = MOUNT_EXEC_UNMOUNT;
929 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b 930
83897d54 931 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
e520950a
BR
932 if (r >= 0 && m->lazy_unmount)
933 r = exec_command_append(m->control_command, "-l", NULL);
4f8d40a9
BR
934 if (r >= 0 && m->force_unmount)
935 r = exec_command_append(m->control_command, "-f", NULL);
7d54a03a 936 if (r < 0)
e537352b
LP
937 goto fail;
938
a16e1123 939 mount_unwatch_control_pid(m);
5e94833f 940
7d54a03a
LP
941 r = mount_spawn(m, m->control_command, &m->control_pid);
942 if (r < 0)
e537352b
LP
943 goto fail;
944
945 mount_set_state(m, MOUNT_UNMOUNTING);
946
947 return;
948
949fail:
f2341e0a 950 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
22af0e58 951 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
952}
953
8d567588 954static void mount_enter_mounting(Mount *m) {
e537352b 955 int r;
cb39ed3f 956 MountParameters *p;
e537352b
LP
957
958 assert(m);
959
25cd4964 960 r = unit_fail_if_noncanonical(UNIT(m), m->where);
f2341e0a
LP
961 if (r < 0)
962 goto fail;
963
964 (void) mkdir_p_label(m->where, m->directory_mode);
3e5235b0 965
f2341e0a 966 unit_warn_if_dir_nonempty(UNIT(m), m->where);
a4634b21
LP
967 unit_warn_leftover_processes(UNIT(m));
968
969 m->control_command_id = MOUNT_EXEC_MOUNT;
970 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
971
cb39ed3f 972 /* Create the source directory for bind-mounts if needed */
6b1dc2bd 973 p = get_mount_parameters_fragment(m);
cb39ed3f 974 if (p && mount_is_bind(p))
f2341e0a 975 (void) mkdir_p_label(p->what, m->directory_mode);
5261ba90 976
b294b79f 977 if (p) {
17a1c597
ZJS
978 _cleanup_free_ char *opts = NULL;
979
b294b79f 980 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
17a1c597
ZJS
981 if (r < 0)
982 goto fail;
983
b294b79f 984 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
e86b3761
ZJS
985 if (r >= 0 && m->sloppy_options)
986 r = exec_command_append(m->control_command, "-s", NULL);
b294b79f
MO
987 if (r >= 0 && p->fstype)
988 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
0c47569a 989 if (r >= 0 && !isempty(opts))
17a1c597 990 r = exec_command_append(m->control_command, "-o", opts, NULL);
e86b3761 991 } else
e537352b 992 r = -ENOENT;
e537352b
LP
993 if (r < 0)
994 goto fail;
995
a16e1123 996 mount_unwatch_control_pid(m);
5e94833f 997
257f1d8e
LP
998 r = mount_spawn(m, m->control_command, &m->control_pid);
999 if (r < 0)
e537352b
LP
1000 goto fail;
1001
1002 mount_set_state(m, MOUNT_MOUNTING);
1003
1004 return;
1005
1006fail:
f2341e0a 1007 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
22af0e58 1008 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1009}
1010
850b7410
LP
1011static void mount_set_reload_result(Mount *m, MountResult result) {
1012 assert(m);
1013
1014 /* Only store the first error we encounter */
1015 if (m->reload_result != MOUNT_SUCCESS)
1016 return;
1017
1018 m->reload_result = result;
1019}
1020
9d2f5178 1021static void mount_enter_remounting(Mount *m) {
e537352b 1022 int r;
b294b79f 1023 MountParameters *p;
e537352b
LP
1024
1025 assert(m);
1026
850b7410
LP
1027 /* Reset reload result when we are about to start a new remount operation */
1028 m->reload_result = MOUNT_SUCCESS;
1029
a16e1123
LP
1030 m->control_command_id = MOUNT_EXEC_REMOUNT;
1031 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b 1032
b294b79f
MO
1033 p = get_mount_parameters_fragment(m);
1034 if (p) {
e537352b
LP
1035 const char *o;
1036
b294b79f
MO
1037 if (p->options)
1038 o = strjoina("remount,", p->options);
718db961 1039 else
e537352b
LP
1040 o = "remount";
1041
f00929ad 1042 r = exec_command_set(m->control_command, MOUNT_PATH,
b294b79f 1043 p->what, m->where,
e86b3761 1044 "-o", o, NULL);
e86b3761
ZJS
1045 if (r >= 0 && m->sloppy_options)
1046 r = exec_command_append(m->control_command, "-s", NULL);
b294b79f
MO
1047 if (r >= 0 && p->fstype)
1048 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
6b1dc2bd 1049 } else
e537352b 1050 r = -ENOENT;
60b912f6 1051 if (r < 0)
e537352b 1052 goto fail;
e537352b 1053
a16e1123 1054 mount_unwatch_control_pid(m);
5e94833f 1055
718db961
LP
1056 r = mount_spawn(m, m->control_command, &m->control_pid);
1057 if (r < 0)
e537352b
LP
1058 goto fail;
1059
1060 mount_set_state(m, MOUNT_REMOUNTING);
1061
1062 return;
1063
1064fail:
f2341e0a 1065 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
850b7410 1066 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
22af0e58 1067 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1068}
1069
7eba1463
LP
1070static void mount_cycle_clear(Mount *m) {
1071 assert(m);
1072
1073 /* Clear all state we shall forget for this new cycle */
1074
1075 m->result = MOUNT_SUCCESS;
1076 m->reload_result = MOUNT_SUCCESS;
1077 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
1078 UNIT(m)->reset_accounting = true;
1079}
1080
e537352b
LP
1081static int mount_start(Unit *u) {
1082 Mount *m = MOUNT(u);
07299350 1083 int r;
e537352b
LP
1084
1085 assert(m);
1086
1087 /* We cannot fulfill this request right now, try again later
1088 * please! */
f2aed307
LP
1089 if (IN_SET(m->state,
1090 MOUNT_UNMOUNTING,
1091 MOUNT_UNMOUNTING_SIGTERM,
22af0e58 1092 MOUNT_UNMOUNTING_SIGKILL))
e537352b
LP
1093 return -EAGAIN;
1094
1095 /* Already on it! */
60b912f6 1096 if (m->state == MOUNT_MOUNTING)
e537352b
LP
1097 return 0;
1098
f2aed307 1099 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
e537352b 1100
97a3f4ee 1101 r = unit_test_start_limit(u);
07299350
LP
1102 if (r < 0) {
1103 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1104 return r;
1105 }
1106
4b58153d
LP
1107 r = unit_acquire_invocation_id(u);
1108 if (r < 0)
1109 return r;
1110
7eba1463 1111 mount_cycle_clear(m);
8d567588 1112 mount_enter_mounting(m);
7eba1463 1113
82a2b6bb 1114 return 1;
e537352b
LP
1115}
1116
1117static int mount_stop(Unit *u) {
1118 Mount *m = MOUNT(u);
1119
1120 assert(m);
1121
22af0e58
LP
1122 switch (m->state) {
1123
1124 case MOUNT_UNMOUNTING:
1125 case MOUNT_UNMOUNTING_SIGKILL:
1126 case MOUNT_UNMOUNTING_SIGTERM:
1127 /* Already on it */
e537352b
LP
1128 return 0;
1129
22af0e58
LP
1130 case MOUNT_MOUNTING:
1131 case MOUNT_MOUNTING_DONE:
1132 case MOUNT_REMOUNTING:
1133 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1134 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1135 return 0;
e537352b 1136
22af0e58
LP
1137 case MOUNT_REMOUNTING_SIGTERM:
1138 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1139 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1140 return 0;
1141
1142 case MOUNT_REMOUNTING_SIGKILL:
1143 /* as above */
1144 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1145 return 0;
1146
1147 case MOUNT_MOUNTED:
1148 mount_enter_unmounting(m);
1149 return 1;
1150
1151 default:
1152 assert_not_reached("Unexpected state.");
1153 }
e537352b
LP
1154}
1155
1156static int mount_reload(Unit *u) {
1157 Mount *m = MOUNT(u);
1158
1159 assert(m);
e537352b
LP
1160 assert(m->state == MOUNT_MOUNTED);
1161
9d2f5178 1162 mount_enter_remounting(m);
22af0e58 1163
2d018ae2 1164 return 1;
e537352b
LP
1165}
1166
a16e1123
LP
1167static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1168 Mount *m = MOUNT(u);
1169
1170 assert(m);
1171 assert(f);
1172 assert(fds);
1173
d68c645b
LP
1174 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1175 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1176 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
2c09fb81 1177 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
a16e1123
LP
1178
1179 if (m->control_pid > 0)
d68c645b 1180 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
a16e1123
LP
1181
1182 if (m->control_command_id >= 0)
d68c645b 1183 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
a16e1123
LP
1184
1185 return 0;
1186}
1187
1188static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1189 Mount *m = MOUNT(u);
2c09fb81 1190 int r;
a16e1123
LP
1191
1192 assert(u);
1193 assert(key);
1194 assert(value);
1195 assert(fds);
1196
1197 if (streq(key, "state")) {
1198 MountState state;
1199
1200 if ((state = mount_state_from_string(value)) < 0)
f2341e0a 1201 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
1202 else
1203 m->deserialized_state = state;
2c09fb81 1204
9d2f5178
LP
1205 } else if (streq(key, "result")) {
1206 MountResult f;
a16e1123 1207
9d2f5178
LP
1208 f = mount_result_from_string(value);
1209 if (f < 0)
f2341e0a 1210 log_unit_debug(u, "Failed to parse result value: %s", value);
9d2f5178
LP
1211 else if (f != MOUNT_SUCCESS)
1212 m->result = f;
1213
1214 } else if (streq(key, "reload-result")) {
1215 MountResult f;
1216
1217 f = mount_result_from_string(value);
1218 if (f < 0)
f2341e0a 1219 log_unit_debug(u, "Failed to parse reload result value: %s", value);
9d2f5178
LP
1220 else if (f != MOUNT_SUCCESS)
1221 m->reload_result = f;
a16e1123 1222
2c09fb81
LP
1223 } else if (streq(key, "n-retry-umount")) {
1224
1225 r = safe_atou(value, &m->n_retry_umount);
1226 if (r < 0)
1227 log_unit_debug(u, "Failed to parse n-retry-umount value: %s", value);
1228
a16e1123 1229 } else if (streq(key, "control-pid")) {
a16e1123 1230
3f459cd9 1231 if (parse_pid(value, &m->control_pid) < 0)
f2341e0a 1232 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
3f459cd9 1233
a16e1123
LP
1234 } else if (streq(key, "control-command")) {
1235 MountExecCommand id;
1236
f2341e0a
LP
1237 id = mount_exec_command_from_string(value);
1238 if (id < 0)
1239 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
1240 else {
1241 m->control_command_id = id;
1242 m->control_command = m->exec_command + id;
1243 }
a16e1123 1244 } else
f2341e0a 1245 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
1246
1247 return 0;
1248}
1249
44a6b1b6 1250_pure_ static UnitActiveState mount_active_state(Unit *u) {
e537352b
LP
1251 assert(u);
1252
1253 return state_translation_table[MOUNT(u)->state];
1254}
1255
44a6b1b6 1256_pure_ static const char *mount_sub_state_to_string(Unit *u) {
10a94420
LP
1257 assert(u);
1258
a16e1123 1259 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1260}
1261
f2f725e5 1262_pure_ static bool mount_may_gc(Unit *u) {
701cc384
LP
1263 Mount *m = MOUNT(u);
1264
1265 assert(m);
1266
f2f725e5
ZJS
1267 if (m->from_proc_self_mountinfo)
1268 return false;
1269
1270 return true;
701cc384
LP
1271}
1272
e537352b
LP
1273static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1274 Mount *m = MOUNT(u);
9d2f5178 1275 MountResult f;
e537352b
LP
1276
1277 assert(m);
1278 assert(pid >= 0);
1279
8c47c732
LP
1280 if (pid != m->control_pid)
1281 return;
e537352b 1282
e537352b
LP
1283 m->control_pid = 0;
1284
1f0958f6 1285 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
9d2f5178
LP
1286 f = MOUNT_SUCCESS;
1287 else if (code == CLD_EXITED)
1288 f = MOUNT_FAILURE_EXIT_CODE;
1289 else if (code == CLD_KILLED)
1290 f = MOUNT_FAILURE_SIGNAL;
1291 else if (code == CLD_DUMPED)
1292 f = MOUNT_FAILURE_CORE_DUMP;
1293 else
1294 assert_not_reached("Unknown code");
1295
850b7410
LP
1296 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1297 mount_set_reload_result(m, f);
1298 else if (m->result == MOUNT_SUCCESS)
9d2f5178 1299 m->result = f;
8c47c732 1300
a16e1123 1301 if (m->control_command) {
6ea832a2 1302 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
9d2f5178 1303
a16e1123
LP
1304 m->control_command = NULL;
1305 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1306 }
1307
91bbd9b7
LP
1308 unit_log_process_exit(
1309 u, f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1310 "Mount process",
1311 mount_exec_command_to_string(m->control_command_id),
1312 code, status);
e537352b 1313
006aabbd
AJ
1314 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1315 * before we process the SIGCHLD for the mount command. */
e537352b
LP
1316
1317 switch (m->state) {
1318
1319 case MOUNT_MOUNTING:
006aabbd 1320 /* Our mount point has not appeared in mountinfo. Something went wrong. */
e537352b 1321
006aabbd
AJ
1322 if (f == MOUNT_SUCCESS) {
1323 /* Either /bin/mount has an unexpected definition of success,
1324 * or someone raced us and we lost. */
1325 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1326 f = MOUNT_FAILURE_PROTOCOL;
1327 }
1328 mount_enter_dead(m, f);
1329 break;
1330
1331 case MOUNT_MOUNTING_DONE:
1332 mount_enter_mounted(m, f);
e537352b
LP
1333 break;
1334
e2f3b44c 1335 case MOUNT_REMOUNTING:
e2f3b44c 1336 case MOUNT_REMOUNTING_SIGTERM:
22af0e58
LP
1337 case MOUNT_REMOUNTING_SIGKILL:
1338 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e2f3b44c
LP
1339 break;
1340
e537352b 1341 case MOUNT_UNMOUNTING:
e537352b 1342
3cc96856 1343 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
22af0e58
LP
1344
1345 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
57018361
AJ
1346 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1347 * but we will stop as soon as any one umount times out. */
22af0e58
LP
1348
1349 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1350 log_unit_debug(u, "Mount still present, trying again.");
1351 m->n_retry_umount++;
1352 mount_enter_unmounting(m);
1353 } else {
006aabbd 1354 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
22af0e58
LP
1355 mount_enter_mounted(m, f);
1356 }
1357 } else
3cc96856 1358 mount_enter_dead_or_mounted(m, f);
22af0e58 1359
e537352b 1360 break;
57018361
AJ
1361
1362 case MOUNT_UNMOUNTING_SIGKILL:
1363 case MOUNT_UNMOUNTING_SIGTERM:
1364 mount_enter_dead_or_mounted(m, f);
1365 break;
e537352b
LP
1366
1367 default:
1368 assert_not_reached("Uh, control process died at wrong time.");
1369 }
c4e2ceae
LP
1370
1371 /* Notify clients about changed exit status */
1372 unit_add_to_dbus_queue(u);
e537352b
LP
1373}
1374
718db961
LP
1375static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1376 Mount *m = MOUNT(userdata);
e537352b
LP
1377
1378 assert(m);
718db961 1379 assert(m->timer_event_source == source);
e537352b
LP
1380
1381 switch (m->state) {
1382
1383 case MOUNT_MOUNTING:
1384 case MOUNT_MOUNTING_DONE:
22af0e58
LP
1385 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1386 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1387 break;
1388
1389 case MOUNT_REMOUNTING:
79aafbd1 1390 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
22af0e58
LP
1391 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1392 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
e537352b
LP
1393 break;
1394
22af0e58
LP
1395 case MOUNT_REMOUNTING_SIGTERM:
1396 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
e537352b 1397
4819ff03 1398 if (m->kill_context.send_sigkill) {
22af0e58
LP
1399 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1400 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
ba035df2 1401 } else {
22af0e58
LP
1402 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1403 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
ba035df2 1404 }
e537352b
LP
1405 break;
1406
22af0e58
LP
1407 case MOUNT_REMOUNTING_SIGKILL:
1408 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1409
22af0e58
LP
1410 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1411 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1412 break;
1413
1414 case MOUNT_UNMOUNTING:
79aafbd1 1415 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
22af0e58 1416 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1417 break;
1418
1419 case MOUNT_UNMOUNTING_SIGTERM:
4819ff03 1420 if (m->kill_context.send_sigkill) {
22af0e58 1421 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
9d2f5178 1422 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
ba035df2 1423 } else {
22af0e58
LP
1424 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1425 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1426 }
e537352b
LP
1427 break;
1428
e537352b 1429 case MOUNT_UNMOUNTING_SIGKILL:
22af0e58
LP
1430 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1431 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1432 break;
1433
1434 default:
1435 assert_not_reached("Timeout at wrong time.");
1436 }
718db961
LP
1437
1438 return 0;
e537352b
LP
1439}
1440
03b8cfed 1441static int mount_setup_new_unit(
839ee058
LP
1442 Manager *m,
1443 const char *name,
03b8cfed
FB
1444 const char *what,
1445 const char *where,
1446 const char *options,
1447 const char *fstype,
ec88d1ea 1448 MountProcFlags *ret_flags,
839ee058 1449 Unit **ret) {
03b8cfed 1450
839ee058 1451 _cleanup_(unit_freep) Unit *u = NULL;
bbee24bc 1452 int r;
03b8cfed 1453
839ee058
LP
1454 assert(m);
1455 assert(name);
ec88d1ea 1456 assert(ret_flags);
839ee058
LP
1457 assert(ret);
1458
1459 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1460 if (r < 0)
1461 return r;
03b8cfed 1462
e10fe042
LP
1463 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1464 if (r < 0)
1465 return r;
03b8cfed 1466
e10fe042
LP
1467 r = free_and_strdup(&MOUNT(u)->where, where);
1468 if (r < 0)
1469 return r;
03b8cfed 1470
bbee24bc
LP
1471 r = update_parameters_proc_self_mount_info(MOUNT(u), what, options, fstype);
1472 if (r < 0)
1473 return r;
03b8cfed 1474
6d7e89b0
LP
1475 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1476 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1477 * to.*/
1478 MOUNT(u)->from_proc_self_mountinfo = true;
1479
1480 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1481 * loaded in now. */
cfcd4318 1482 unit_add_to_load_queue(u);
26e35b16 1483
ec88d1ea 1484 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
839ee058 1485 *ret = TAKE_PTR(u);
03b8cfed
FB
1486 return 0;
1487}
1488
1489static int mount_setup_existing_unit(
1490 Unit *u,
1491 const char *what,
1492 const char *where,
1493 const char *options,
1494 const char *fstype,
ec88d1ea 1495 MountProcFlags *ret_flags) {
03b8cfed 1496
ec88d1ea 1497 MountProcFlags flags = MOUNT_PROC_IS_MOUNTED;
bbee24bc 1498 int r;
03b8cfed
FB
1499
1500 assert(u);
1501 assert(flags);
1502
1503 if (!MOUNT(u)->where) {
1504 MOUNT(u)->where = strdup(where);
1505 if (!MOUNT(u)->where)
1506 return -ENOMEM;
1507 }
1508
bbee24bc
LP
1509 r = update_parameters_proc_self_mount_info(MOUNT(u), what, options, fstype);
1510 if (r < 0)
1511 return r;
ec88d1ea
LP
1512 if (r > 0)
1513 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed 1514
d253a45e 1515 if (!MOUNT(u)->from_proc_self_mountinfo || FLAGS_SET(MOUNT(u)->proc_flags, MOUNT_PROC_JUST_MOUNTED))
ec88d1ea 1516 flags |= MOUNT_PROC_JUST_MOUNTED;
d253a45e
YW
1517
1518 MOUNT(u)->from_proc_self_mountinfo = true;
03b8cfed 1519
f8064c4f
LP
1520 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1521 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1522 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
03b8cfed
FB
1523 u->load_state = UNIT_LOADED;
1524 u->load_error = 0;
1525
ec88d1ea 1526 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed
FB
1527 }
1528
ec88d1ea 1529 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
a3742204
LP
1530 /* If things changed, then make sure that all deps are regenerated. Let's
1531 * first remove all automatic deps, and then add in the new ones. */
1532
1533 unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1534
1535 r = mount_add_extras(MOUNT(u));
1536 if (r < 0)
1537 return r;
1538 }
03b8cfed 1539
ec88d1ea 1540 *ret_flags = flags;
03b8cfed
FB
1541 return 0;
1542}
1543
628c89cc 1544static int mount_setup_unit(
e537352b
LP
1545 Manager *m,
1546 const char *what,
1547 const char *where,
1548 const char *options,
1549 const char *fstype,
e537352b 1550 bool set_flags) {
057d9ab8 1551
03b8cfed 1552 _cleanup_free_ char *e = NULL;
ec88d1ea 1553 MountProcFlags flags;
057d9ab8
LP
1554 Unit *u;
1555 int r;
b08d03ff 1556
f50e0a01 1557 assert(m);
b08d03ff
LP
1558 assert(what);
1559 assert(where);
e537352b
LP
1560 assert(options);
1561 assert(fstype);
1562
e537352b
LP
1563 /* Ignore API mount points. They should never be referenced in
1564 * dependencies ever. */
33ff02c9 1565 if (mount_point_is_api(where) || mount_point_ignore(where))
57f2a956 1566 return 0;
b08d03ff 1567
8d567588
LP
1568 if (streq(fstype, "autofs"))
1569 return 0;
1570
4e85aff4
LP
1571 /* probably some kind of swap, ignore */
1572 if (!is_path(where))
b08d03ff
LP
1573 return 0;
1574
7410616c
LP
1575 r = unit_name_from_path(where, ".mount", &e);
1576 if (r < 0)
60b8c203 1577 return log_error_errno(r, "Failed to generate unit name from path '%s': %m", where);
b08d03ff 1578
7d17cfbc 1579 u = manager_get_unit(m, e);
839ee058 1580 if (u)
03b8cfed 1581 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
839ee058
LP
1582 else
1583 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1584 * by the sysadmin having called mount(8) directly. */
1585 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
03b8cfed 1586 if (r < 0)
60b8c203 1587 return log_warning_errno(r, "Failed to set up mount unit: %m");
ff5f34d0 1588
ec88d1ea
LP
1589 /* If the mount changed properties or state, let's notify our clients */
1590 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
ff5f34d0 1591 unit_add_to_dbus_queue(u);
c1e1601e 1592
ec88d1ea
LP
1593 if (set_flags)
1594 MOUNT(u)->proc_flags = flags;
1595
b08d03ff 1596 return 0;
b08d03ff
LP
1597}
1598
ef734fd6 1599static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
628c89cc
LP
1600 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
1601 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
ba0d56f5 1602 int r;
b08d03ff
LP
1603
1604 assert(m);
1605
628c89cc 1606 t = mnt_new_table();
628c89cc 1607 i = mnt_new_iter(MNT_ITER_FORWARD);
95b862b0 1608 if (!t || !i)
628c89cc
LP
1609 return log_oom();
1610
1611 r = mnt_table_parse_mtab(t, NULL);
5cca8def 1612 if (r < 0)
628c89cc 1613 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
e537352b 1614
5cca8def 1615 for (;;) {
95b862b0 1616 struct libmnt_fs *fs;
8d3ae2bd 1617 const char *device, *path, *options, *fstype;
8d3ae2bd 1618 int k;
b08d03ff 1619
628c89cc 1620 k = mnt_table_next_fs(t, i, &fs);
5cca8def
ZJS
1621 if (k == 1)
1622 break;
628c89cc
LP
1623 if (k < 0)
1624 return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
5cca8def 1625
8d3ae2bd
CL
1626 device = mnt_fs_get_source(fs);
1627 path = mnt_fs_get_target(fs);
1628 options = mnt_fs_get_options(fs);
1629 fstype = mnt_fs_get_fstype(fs);
a2e0f3d3 1630
c0a7f8d3
DM
1631 if (!device || !path)
1632 continue;
1633
9d1b2b22 1634 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
628c89cc 1635
9d1b2b22 1636 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
b08d03ff
LP
1637 }
1638
ba0d56f5 1639 return 0;
e537352b
LP
1640}
1641
1642static void mount_shutdown(Manager *m) {
1643 assert(m);
1644
718db961
LP
1645 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1646
d379d442
KZ
1647 mnt_unref_monitor(m->mount_monitor);
1648 m->mount_monitor = NULL;
b08d03ff
LP
1649}
1650
7a7821c8 1651static int mount_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1652 Mount *m = MOUNT(u);
7a7821c8 1653 usec_t t;
68db7a3b
ZJS
1654 int r;
1655
1656 if (!m->timer_event_source)
1657 return 0;
1658
7a7821c8 1659 r = sd_event_source_get_time(m->timer_event_source, &t);
68db7a3b
ZJS
1660 if (r < 0)
1661 return r;
7a7821c8
LP
1662 if (t == USEC_INFINITY)
1663 return 0;
68db7a3b 1664
7a7821c8 1665 *timeout = t;
68db7a3b
ZJS
1666 return 1;
1667}
1668
04eb582a 1669static void mount_enumerate_perpetual(Manager *m) {
11222d0f
LP
1670 Unit *u;
1671 int r;
1672
1673 assert(m);
1674
1675 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1676 * unconditionally synthesize it here and mark it as perpetual. */
1677
1678 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1679 if (!u) {
a581e45a 1680 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
04eb582a
LP
1681 if (r < 0) {
1682 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1683 return;
1684 }
11222d0f
LP
1685 }
1686
1687 u->perpetual = true;
1688 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1689
1690 unit_add_to_load_queue(u);
1691 unit_add_to_dbus_queue(u);
1692}
1693
1694static bool mount_is_mounted(Mount *m) {
1695 assert(m);
1696
ec88d1ea 1697 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
11222d0f
LP
1698}
1699
ba64af90 1700static void mount_enumerate(Manager *m) {
b08d03ff 1701 int r;
d379d442 1702
b08d03ff
LP
1703 assert(m);
1704
8d3ae2bd
CL
1705 mnt_init_debug(0);
1706
d379d442
KZ
1707 if (!m->mount_monitor) {
1708 int fd;
ef734fd6 1709
d379d442
KZ
1710 m->mount_monitor = mnt_new_monitor();
1711 if (!m->mount_monitor) {
ba64af90 1712 log_oom();
718db961 1713 goto fail;
d379d442 1714 }
29083707 1715
d379d442 1716 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
ba64af90
LP
1717 if (r < 0) {
1718 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
29083707 1719 goto fail;
ba64af90
LP
1720 }
1721
d379d442 1722 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
ba64af90
LP
1723 if (r < 0) {
1724 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f7c1ad4f 1725 goto fail;
ba64af90 1726 }
90598531 1727
d379d442
KZ
1728 /* mnt_unref_monitor() will close the fd */
1729 fd = r = mnt_monitor_get_fd(m->mount_monitor);
ba64af90
LP
1730 if (r < 0) {
1731 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f7c1ad4f 1732 goto fail;
ba64af90 1733 }
befb6d54 1734
d379d442 1735 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
ba64af90
LP
1736 if (r < 0) {
1737 log_error_errno(r, "Failed to watch mount file descriptor: %m");
befb6d54 1738 goto fail;
ba64af90 1739 }
befb6d54 1740
83231637 1741 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1742 if (r < 0) {
1743 log_error_errno(r, "Failed to adjust mount watch priority: %m");
befb6d54 1744 goto fail;
ba64af90 1745 }
7dfbe2e3 1746
d379d442 1747 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
befb6d54
CL
1748 }
1749
e62d8c39
ZJS
1750 r = mount_load_proc_self_mountinfo(m, false);
1751 if (r < 0)
b08d03ff
LP
1752 goto fail;
1753
ba64af90 1754 return;
b08d03ff
LP
1755
1756fail:
1757 mount_shutdown(m);
5cb5a6ff
LP
1758}
1759
718db961 1760static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
ec8126d7 1761 _cleanup_set_free_free_ Set *around = NULL, *gone = NULL;
718db961 1762 Manager *m = userdata;
ec8126d7
ZJS
1763 const char *what;
1764 Iterator i;
1765 Unit *u;
ef734fd6
LP
1766 int r;
1767
1768 assert(m);
d379d442 1769 assert(revents & EPOLLIN);
ef734fd6 1770
d379d442 1771 if (fd == mnt_monitor_get_fd(m->mount_monitor)) {
df63dda6 1772 bool rescan = false;
befb6d54 1773
d379d442
KZ
1774 /* Drain all events and verify that the event is valid.
1775 *
1776 * Note that libmount also monitors /run/mount mkdir if the
1777 * directory does not exist yet. The mkdir may generate event
1778 * which is irrelevant for us.
1779 *
1780 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1781 do {
1782 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1783 if (r == 0)
1784 rescan = true;
1785 else if (r < 0)
5e1ee764 1786 return log_error_errno(r, "Failed to drain libmount events: %m");
d379d442
KZ
1787 } while (r == 0);
1788
1789 log_debug("libmount event [rescan: %s]", yes_no(rescan));
befb6d54
CL
1790 if (!rescan)
1791 return 0;
1792 }
ef734fd6 1793
4f0eedb7
ZJS
1794 r = mount_load_proc_self_mountinfo(m, true);
1795 if (r < 0) {
e537352b 1796 /* Reset flags, just in case, for later calls */
ec88d1ea
LP
1797 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
1798 MOUNT(u)->proc_flags = 0;
e537352b 1799
ec8126d7 1800 return 0;
ef734fd6
LP
1801 }
1802
1803 manager_dispatch_load_queue(m);
1804
595ed347
MS
1805 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1806 Mount *mount = MOUNT(u);
ef734fd6 1807
11222d0f 1808 if (!mount_is_mounted(mount)) {
e537352b 1809
394763f6
LP
1810 /* A mount point is not around right now. It
1811 * might be gone, or might never have
1812 * existed. */
1813
1814 if (mount->from_proc_self_mountinfo &&
1815 mount->parameters_proc_self_mountinfo.what) {
1816
1817 /* Remember that this device might just have disappeared */
548f6937 1818 if (set_ensure_allocated(&gone, &path_hash_ops) < 0 ||
b6418dc9 1819 set_put_strdup(gone, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6
LP
1820 log_oom(); /* we don't care too much about OOM here... */
1821 }
fcd8b266 1822
ef734fd6 1823 mount->from_proc_self_mountinfo = false;
c52c2dc6 1824 assert_se(update_parameters_proc_self_mount_info(mount, NULL, NULL, NULL) >= 0);
e537352b
LP
1825
1826 switch (mount->state) {
1827
1828 case MOUNT_MOUNTED:
7eba1463 1829 /* This has just been unmounted by somebody else, follow the state change. */
9d2f5178 1830 mount_enter_dead(mount, MOUNT_SUCCESS);
e537352b
LP
1831 break;
1832
1833 default:
e537352b 1834 break;
e537352b
LP
1835 }
1836
ec88d1ea 1837 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
e537352b 1838
fcd8b266 1839 /* A mount point was added or changed */
e537352b
LP
1840
1841 switch (mount->state) {
1842
1843 case MOUNT_DEAD:
fdf20a31 1844 case MOUNT_FAILED:
4b58153d
LP
1845
1846 /* This has just been mounted by somebody else, follow the state change, but let's
1847 * generate a new invocation ID for this implicitly and automatically. */
7eba1463
LP
1848 (void) unit_acquire_invocation_id(u);
1849 mount_cycle_clear(mount);
9d2f5178 1850 mount_enter_mounted(mount, MOUNT_SUCCESS);
e537352b
LP
1851 break;
1852
1853 case MOUNT_MOUNTING:
5bcb0f2b 1854 mount_set_state(mount, MOUNT_MOUNTING_DONE);
e537352b
LP
1855 break;
1856
1857 default:
1858 /* Nothing really changed, but let's
1859 * issue an notification call
1860 * nonetheless, in case somebody is
1861 * waiting for this. (e.g. file system
1862 * ro/rw remounts.) */
1863 mount_set_state(mount, mount->state);
1864 break;
1865 }
394763f6 1866 }
fcd8b266 1867
11222d0f 1868 if (mount_is_mounted(mount) &&
394763f6
LP
1869 mount->from_proc_self_mountinfo &&
1870 mount->parameters_proc_self_mountinfo.what) {
b6418dc9 1871 /* Track devices currently used */
fcd8b266 1872
548f6937 1873 if (set_ensure_allocated(&around, &path_hash_ops) < 0 ||
b6418dc9 1874 set_put_strdup(around, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 1875 log_oom();
e537352b
LP
1876 }
1877
1878 /* Reset the flags for later calls */
ec88d1ea 1879 mount->proc_flags = 0;
e537352b 1880 }
718db961 1881
fcd8b266
LP
1882 SET_FOREACH(what, gone, i) {
1883 if (set_contains(around, what))
1884 continue;
1885
1886 /* Let the device units know that the device is no longer mounted */
485ae697 1887 device_found_node(m, what, 0, DEVICE_FOUND_MOUNT);
fcd8b266 1888 }
ec8126d7
ZJS
1889
1890 return 0;
e537352b
LP
1891}
1892
fdf20a31 1893static void mount_reset_failed(Unit *u) {
5632e374
LP
1894 Mount *m = MOUNT(u);
1895
1896 assert(m);
1897
fdf20a31 1898 if (m->state == MOUNT_FAILED)
5632e374
LP
1899 mount_set_state(m, MOUNT_DEAD);
1900
9d2f5178
LP
1901 m->result = MOUNT_SUCCESS;
1902 m->reload_result = MOUNT_SUCCESS;
5632e374
LP
1903}
1904
718db961 1905static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
22af0e58
LP
1906 Mount *m = MOUNT(u);
1907
1908 assert(m);
1909
ee36fed4 1910 return unit_kill_common(u, who, signo, -1, m->control_pid, error);
8a0867d6
LP
1911}
1912
291d565a
LP
1913static int mount_control_pid(Unit *u) {
1914 Mount *m = MOUNT(u);
1915
1916 assert(m);
1917
1918 return m->control_pid;
1919}
1920
a16e1123
LP
1921static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1922 [MOUNT_EXEC_MOUNT] = "ExecMount",
1923 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1924 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1925};
1926
1927DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1928
9d2f5178
LP
1929static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1930 [MOUNT_SUCCESS] = "success",
1931 [MOUNT_FAILURE_RESOURCES] = "resources",
1932 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1933 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1934 [MOUNT_FAILURE_SIGNAL] = "signal",
07299350
LP
1935 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
1936 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
006aabbd 1937 [MOUNT_FAILURE_PROTOCOL] = "protocol",
9d2f5178
LP
1938};
1939
1940DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1941
87f0e418 1942const UnitVTable mount_vtable = {
7d17cfbc 1943 .object_size = sizeof(Mount),
718db961
LP
1944 .exec_context_offset = offsetof(Mount, exec_context),
1945 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1946 .kill_context_offset = offsetof(Mount, kill_context),
613b411c 1947 .exec_runtime_offset = offsetof(Mount, exec_runtime),
29206d46 1948 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
3ef63c31 1949
f975e971
LP
1950 .sections =
1951 "Unit\0"
1952 "Mount\0"
1953 "Install\0",
4ad49000 1954 .private_section = "Mount",
71645aca 1955
e537352b
LP
1956 .init = mount_init,
1957 .load = mount_load,
034c6ed7 1958 .done = mount_done,
e537352b 1959
f50e0a01
LP
1960 .coldplug = mount_coldplug,
1961
034c6ed7 1962 .dump = mount_dump,
5cb5a6ff 1963
e537352b
LP
1964 .start = mount_start,
1965 .stop = mount_stop,
1966 .reload = mount_reload,
1967
8a0867d6
LP
1968 .kill = mount_kill,
1969
a16e1123
LP
1970 .serialize = mount_serialize,
1971 .deserialize_item = mount_deserialize_item,
1972
f50e0a01 1973 .active_state = mount_active_state,
10a94420 1974 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 1975
f2f725e5 1976 .may_gc = mount_may_gc,
701cc384 1977
e537352b 1978 .sigchld_event = mount_sigchld_event,
e537352b 1979
fdf20a31 1980 .reset_failed = mount_reset_failed,
291d565a
LP
1981
1982 .control_pid = mount_control_pid,
5632e374 1983
718db961 1984 .bus_vtable = bus_mount_vtable,
74c964d3
LP
1985 .bus_set_property = bus_mount_set_property,
1986 .bus_commit_properties = bus_mount_commit_properties,
4139c1b2 1987
68db7a3b
ZJS
1988 .get_timeout = mount_get_timeout,
1989
0e252f6b
TG
1990 .can_transient = true,
1991
04eb582a 1992 .enumerate_perpetual = mount_enumerate_perpetual,
f50e0a01 1993 .enumerate = mount_enumerate,
c6918296
MS
1994 .shutdown = mount_shutdown,
1995
1996 .status_message_formats = {
1997 .starting_stopping = {
1998 [0] = "Mounting %s...",
1999 [1] = "Unmounting %s...",
2000 },
2001 .finished_start_job = {
2002 [JOB_DONE] = "Mounted %s.",
2003 [JOB_FAILED] = "Failed to mount %s.",
c6918296
MS
2004 [JOB_TIMEOUT] = "Timed out mounting %s.",
2005 },
2006 .finished_stop_job = {
2007 [JOB_DONE] = "Unmounted %s.",
2008 [JOB_FAILED] = "Failed unmounting %s.",
2009 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2010 },
2011 },
5cb5a6ff 2012};