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