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