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