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