]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/mount.c
test: add ratelimiting test
[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
d85ff944
YW
552 if (!unit_has_name(UNIT(m), e))
553 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing.");
8d567588 554
d85ff944
YW
555 if (mount_point_is_api(m->where) || mount_point_ignore(m->where))
556 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Cannot create mount unit for API file system %s. Refusing.", m->where);
33ff02c9 557
b294b79f 558 p = get_mount_parameters_fragment(m);
0879fbd6
LP
559 if (p && !p->what && !UNIT(m)->perpetual)
560 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC),
561 "What= setting is missing. Refusing.");
4e85aff4 562
d85ff944
YW
563 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP)
564 return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
4d0e5dbd 565
8d567588
LP
566 return 0;
567}
568
bf7eedbf
LP
569static int mount_add_non_exec_dependencies(Mount *m) {
570 int r;
571 assert(m);
572
573 /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies
574 * resulting from the ExecContext and such. */
575
576 r = mount_add_device_dependencies(m);
577 if (r < 0)
578 return r;
579
580 r = mount_add_mount_dependencies(m);
581 if (r < 0)
582 return r;
583
584 r = mount_add_quota_dependencies(m);
585 if (r < 0)
586 return r;
587
588 r = mount_add_default_dependencies(m);
589 if (r < 0)
590 return r;
591
592 return 0;
593}
594
1a4ac875
MS
595static int mount_add_extras(Mount *m) {
596 Unit *u = UNIT(m);
e537352b
LP
597 int r;
598
e821075a
LP
599 assert(m);
600
1f736476
LP
601 /* Note: this call might be called after we already have been loaded once (and even when it has already been
602 * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready
603 * to run with an already set up unit. */
604
e821075a 605 if (u->fragment_path)
1a4ac875 606 m->from_fragment = true;
e537352b 607
1a4ac875 608 if (!m->where) {
7410616c
LP
609 r = unit_name_to_path(u->id, &m->where);
610 if (r < 0)
611 return r;
1a4ac875 612 }
a16e1123 613
858d36c1 614 path_simplify(m->where, false);
e537352b 615
e821075a 616 if (!u->description) {
1a4ac875
MS
617 r = unit_set_description(u, m->where);
618 if (r < 0)
173a8d04 619 return r;
1a4ac875 620 }
6e2ef85b 621
598459ce
LP
622 r = unit_patch_contexts(u);
623 if (r < 0)
624 return r;
4e67ddd6 625
598459ce 626 r = unit_add_exec_dependencies(u, &m->exec_context);
a016b922
LP
627 if (r < 0)
628 return r;
629
d79200e2 630 r = unit_set_default_slice(u);
1a4ac875
MS
631 if (r < 0)
632 return r;
633
bf7eedbf 634 r = mount_add_non_exec_dependencies(m);
4c9ea260
LP
635 if (r < 0)
636 return r;
598459ce 637
1a4ac875
MS
638 return 0;
639}
640
75193d41 641static void mount_load_root_mount(Unit *u) {
11222d0f
LP
642 assert(u);
643
644 if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
75193d41 645 return;
11222d0f
LP
646
647 u->perpetual = true;
648 u->default_dependencies = false;
649
650 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
651 MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL;
652 MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL;
653
654 if (!u->description)
655 u->description = strdup("Root Mount");
11222d0f
LP
656}
657
1a4ac875
MS
658static int mount_load(Unit *u) {
659 Mount *m = MOUNT(u);
75193d41 660 int r, q = 0;
1a4ac875
MS
661
662 assert(u);
663 assert(u->load_state == UNIT_STUB);
664
75193d41 665 mount_load_root_mount(u);
11222d0f 666
c3620770 667 bool fragment_optional = m->from_proc_self_mountinfo || u->perpetual;
75193d41 668 r = unit_load_fragment_and_dropin(u, !fragment_optional);
780ae022
LP
669
670 /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the
671 * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus
672 * we need to update the state in our unit to track it. After all, consider that we don't allow changing the
673 * 'slice' field for a unit once it is active. */
674 if (u->load_state == UNIT_LOADED || m->from_proc_self_mountinfo || u->perpetual)
75193d41 675 q = mount_add_extras(m);
780ae022 676
1a4ac875
MS
677 if (r < 0)
678 return r;
780ae022
LP
679 if (q < 0)
680 return q;
75193d41
ZJS
681 if (u->load_state != UNIT_LOADED)
682 return 0;
e537352b 683
8d567588 684 return mount_verify(m);
e537352b
LP
685}
686
687static void mount_set_state(Mount *m, MountState state) {
688 MountState old_state;
689 assert(m);
690
6fcbec6f
LP
691 if (m->state != state)
692 bus_unit_send_pending_change_signal(UNIT(m), false);
693
e537352b
LP
694 old_state = m->state;
695 m->state = state;
696
c634f3d2 697 if (!MOUNT_STATE_WITH_PROCESS(state)) {
718db961 698 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
a16e1123 699 mount_unwatch_control_pid(m);
e537352b 700 m->control_command = NULL;
a16e1123 701 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
e537352b
LP
702 }
703
704 if (state != old_state)
f2341e0a 705 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
e537352b 706
2ad2e41a
LP
707 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state],
708 m->reload_result == MOUNT_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE);
e537352b
LP
709}
710
be847e82 711static int mount_coldplug(Unit *u) {
e537352b 712 Mount *m = MOUNT(u);
a16e1123
LP
713 MountState new_state = MOUNT_DEAD;
714 int r;
e537352b
LP
715
716 assert(m);
717 assert(m->state == MOUNT_DEAD);
718
a16e1123
LP
719 if (m->deserialized_state != m->state)
720 new_state = m->deserialized_state;
721 else if (m->from_proc_self_mountinfo)
722 new_state = MOUNT_MOUNTED;
e537352b 723
5bcb0f2b
LP
724 if (new_state == m->state)
725 return 0;
e537352b 726
c386f588
LP
727 if (m->control_pid > 0 &&
728 pid_is_unwaited(m->control_pid) &&
c634f3d2 729 MOUNT_STATE_WITH_PROCESS(new_state)) {
5bcb0f2b 730
f75f613d 731 r = unit_watch_pid(UNIT(m), m->control_pid, false);
5bcb0f2b
LP
732 if (r < 0)
733 return r;
e537352b 734
36c16a7c 735 r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
5bcb0f2b
LP
736 if (r < 0)
737 return r;
a16e1123 738 }
e537352b 739
e8a565cb 740 if (!IN_SET(new_state, MOUNT_DEAD, MOUNT_FAILED)) {
29206d46 741 (void) unit_setup_dynamic_creds(u);
e8a565cb
YW
742 (void) unit_setup_exec_runtime(u);
743 }
29206d46 744
5bcb0f2b 745 mount_set_state(m, new_state);
e537352b 746 return 0;
e537352b
LP
747}
748
749static void mount_dump(Unit *u, FILE *f, const char *prefix) {
f2e18ef1 750 char buf[FORMAT_TIMESPAN_MAX];
e537352b
LP
751 Mount *m = MOUNT(u);
752 MountParameters *p;
753
754 assert(m);
755 assert(f);
756
cb39ed3f 757 p = get_mount_parameters(m);
e537352b
LP
758
759 fprintf(f,
760 "%sMount State: %s\n"
81a5c6d0 761 "%sResult: %s\n"
17e9d53d 762 "%sClean Result: %s\n"
e537352b
LP
763 "%sWhere: %s\n"
764 "%sWhat: %s\n"
765 "%sFile System Type: %s\n"
766 "%sOptions: %s\n"
e537352b
LP
767 "%sFrom /proc/self/mountinfo: %s\n"
768 "%sFrom fragment: %s\n"
ad2706db 769 "%sExtrinsic: %s\n"
e520950a 770 "%sDirectoryMode: %04o\n"
49915de2 771 "%sSloppyOptions: %s\n"
4f8d40a9 772 "%sLazyUnmount: %s\n"
91899792 773 "%sForceUnmount: %s\n"
c600357b 774 "%sReadWriteOnly: %s\n"
ac8956ef 775 "%sTimeoutSec: %s\n",
a16e1123 776 prefix, mount_state_to_string(m->state),
81a5c6d0 777 prefix, mount_result_to_string(m->result),
17e9d53d 778 prefix, mount_result_to_string(m->clean_result),
e537352b 779 prefix, m->where,
1e4fc9b1
HH
780 prefix, p ? strna(p->what) : "n/a",
781 prefix, p ? strna(p->fstype) : "n/a",
782 prefix, p ? strna(p->options) : "n/a",
e537352b
LP
783 prefix, yes_no(m->from_proc_self_mountinfo),
784 prefix, yes_no(m->from_fragment),
39c79477 785 prefix, yes_no(mount_is_extrinsic(u)),
e520950a 786 prefix, m->directory_mode,
49915de2 787 prefix, yes_no(m->sloppy_options),
4f8d40a9 788 prefix, yes_no(m->lazy_unmount),
91899792 789 prefix, yes_no(m->force_unmount),
c600357b 790 prefix, yes_no(m->read_write_only),
91899792 791 prefix, format_timespan(buf, sizeof(buf), m->timeout_usec, USEC_PER_SEC));
e537352b
LP
792
793 if (m->control_pid > 0)
794 fprintf(f,
ccd06097
ZJS
795 "%sControl PID: "PID_FMT"\n",
796 prefix, m->control_pid);
e537352b
LP
797
798 exec_context_dump(&m->exec_context, f, prefix);
4819ff03 799 kill_context_dump(&m->kill_context, f, prefix);
bc0623df 800 cgroup_context_dump(UNIT(m), f, prefix);
e537352b
LP
801}
802
a16e1123 803static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
3c7416b6 804
b9c04eaf 805 _cleanup_(exec_params_clear) ExecParameters exec_params = {
5686391b
LP
806 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
807 .stdin_fd = -1,
808 .stdout_fd = -1,
809 .stderr_fd = -1,
810 .exec_fd = -1,
9fa95f85 811 };
3c7416b6
LP
812 pid_t pid;
813 int r;
a16e1123
LP
814
815 assert(m);
816 assert(c);
817 assert(_pid);
818
3c7416b6 819 r = unit_prepare_exec(UNIT(m));
29206d46
LP
820 if (r < 0)
821 return r;
822
36c16a7c 823 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 824 if (r < 0)
36c16a7c 825 return r;
a16e1123 826
1ad6e8b3
LP
827 r = unit_set_exec_params(UNIT(m), &exec_params);
828 if (r < 0)
829 return r;
9fa95f85 830
f2341e0a
LP
831 r = exec_spawn(UNIT(m),
832 c,
4ad49000 833 &m->exec_context,
9fa95f85 834 &exec_params,
613b411c 835 m->exec_runtime,
29206d46 836 &m->dynamic_creds,
4ad49000
LP
837 &pid);
838 if (r < 0)
36c16a7c 839 return r;
a16e1123 840
f75f613d 841 r = unit_watch_pid(UNIT(m), pid, true);
4ad49000 842 if (r < 0)
36c16a7c 843 return r;
a16e1123
LP
844
845 *_pid = pid;
846
847 return 0;
a16e1123
LP
848}
849
9d2f5178 850static void mount_enter_dead(Mount *m, MountResult f) {
e537352b
LP
851 assert(m);
852
a0fef983 853 if (m->result == MOUNT_SUCCESS)
9d2f5178 854 m->result = f;
e537352b 855
aac99f30 856 unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
4c425434
LP
857 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_stop);
858
29206d46
LP
859 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
860
e8a565cb 861 m->exec_runtime = exec_runtime_unref(m->exec_runtime, true);
613b411c 862
bb0c0d6f 863 unit_destroy_runtime_data(UNIT(m), &m->exec_context);
e66cf1a3 864
00d9ef85
LP
865 unit_unref_uid_gid(UNIT(m), true);
866
29206d46 867 dynamic_creds_destroy(&m->dynamic_creds);
c52c2dc6
ZJS
868
869 /* Any dependencies based on /proc/self/mountinfo are now stale */
870 unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
e537352b
LP
871}
872
9d2f5178 873static void mount_enter_mounted(Mount *m, MountResult f) {
80876c20
LP
874 assert(m);
875
a0fef983 876 if (m->result == MOUNT_SUCCESS)
9d2f5178 877 m->result = f;
80876c20
LP
878
879 mount_set_state(m, MOUNT_MOUNTED);
880}
881
22af0e58
LP
882static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
883 assert(m);
884
885 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
886 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
887 * ultimately we just mirror the kernel's internal state on this. */
888
889 if (m->from_proc_self_mountinfo)
890 mount_enter_mounted(m, f);
891 else
892 mount_enter_dead(m, f);
893}
894
895static int state_to_kill_operation(MountState state) {
896 switch (state) {
897
898 case MOUNT_REMOUNTING_SIGTERM:
a232ebcc
ZJS
899 return KILL_RESTART;
900
22af0e58
LP
901 case MOUNT_UNMOUNTING_SIGTERM:
902 return KILL_TERMINATE;
903
904 case MOUNT_REMOUNTING_SIGKILL:
905 case MOUNT_UNMOUNTING_SIGKILL:
906 return KILL_KILL;
907
908 default:
909 return _KILL_OPERATION_INVALID;
910 }
911}
912
9d2f5178 913static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
e537352b
LP
914 int r;
915
916 assert(m);
917
a0fef983 918 if (m->result == MOUNT_SUCCESS)
9d2f5178 919 m->result = f;
e537352b 920
cd2086fe
LP
921 r = unit_kill_context(
922 UNIT(m),
923 &m->kill_context,
22af0e58 924 state_to_kill_operation(state),
cd2086fe
LP
925 -1,
926 m->control_pid,
927 false);
928 if (r < 0)
929 goto fail;
e537352b 930
cd2086fe 931 if (r > 0) {
36c16a7c 932 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 933 if (r < 0)
80876c20 934 goto fail;
e537352b 935
80876c20 936 mount_set_state(m, state);
22af0e58 937 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 938 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
22af0e58 939 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
9d2f5178 940 mount_enter_mounted(m, MOUNT_SUCCESS);
22af0e58 941 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 942 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
80876c20 943 else
22af0e58 944 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
945
946 return;
947
948fail:
f2341e0a 949 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
22af0e58 950 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
5261ba90
TT
951}
952
9d2f5178 953static void mount_enter_unmounting(Mount *m) {
e537352b
LP
954 int r;
955
956 assert(m);
957
7d54a03a
LP
958 /* Start counting our attempts */
959 if (!IN_SET(m->state,
960 MOUNT_UNMOUNTING,
961 MOUNT_UNMOUNTING_SIGTERM,
962 MOUNT_UNMOUNTING_SIGKILL))
963 m->n_retry_umount = 0;
964
a16e1123
LP
965 m->control_command_id = MOUNT_EXEC_UNMOUNT;
966 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b 967
83897d54 968 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
e520950a
BR
969 if (r >= 0 && m->lazy_unmount)
970 r = exec_command_append(m->control_command, "-l", NULL);
4f8d40a9
BR
971 if (r >= 0 && m->force_unmount)
972 r = exec_command_append(m->control_command, "-f", NULL);
7d54a03a 973 if (r < 0)
e537352b
LP
974 goto fail;
975
a16e1123 976 mount_unwatch_control_pid(m);
5e94833f 977
7d54a03a
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_UNMOUNTING);
983
984 return;
985
986fail:
f2341e0a 987 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
22af0e58 988 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
989}
990
8d567588 991static void mount_enter_mounting(Mount *m) {
e537352b 992 int r;
cb39ed3f 993 MountParameters *p;
e537352b
LP
994
995 assert(m);
996
25cd4964 997 r = unit_fail_if_noncanonical(UNIT(m), m->where);
f2341e0a
LP
998 if (r < 0)
999 goto fail;
1000
1001 (void) mkdir_p_label(m->where, m->directory_mode);
3e5235b0 1002
f2341e0a 1003 unit_warn_if_dir_nonempty(UNIT(m), m->where);
4c425434 1004 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_start);
a4634b21
LP
1005
1006 m->control_command_id = MOUNT_EXEC_MOUNT;
1007 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
1008
cb39ed3f 1009 /* Create the source directory for bind-mounts if needed */
6b1dc2bd 1010 p = get_mount_parameters_fragment(m);
5dc60faa
AP
1011 if (p && mount_is_bind(p)) {
1012 r = mkdir_p_label(p->what, m->directory_mode);
1013 if (r < 0)
1014 log_unit_error_errno(UNIT(m), r, "Failed to make bind mount source '%s': %m", p->what);
1015 }
5261ba90 1016
b294b79f 1017 if (p) {
17a1c597
ZJS
1018 _cleanup_free_ char *opts = NULL;
1019
b294b79f 1020 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
17a1c597
ZJS
1021 if (r < 0)
1022 goto fail;
1023
b294b79f 1024 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
e86b3761
ZJS
1025 if (r >= 0 && m->sloppy_options)
1026 r = exec_command_append(m->control_command, "-s", NULL);
c600357b
MH
1027 if (r >= 0 && m->read_write_only)
1028 r = exec_command_append(m->control_command, "-w", NULL);
b294b79f
MO
1029 if (r >= 0 && p->fstype)
1030 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
0c47569a 1031 if (r >= 0 && !isempty(opts))
17a1c597 1032 r = exec_command_append(m->control_command, "-o", opts, NULL);
e86b3761 1033 } else
e537352b 1034 r = -ENOENT;
e537352b
LP
1035 if (r < 0)
1036 goto fail;
1037
a16e1123 1038 mount_unwatch_control_pid(m);
5e94833f 1039
257f1d8e
LP
1040 r = mount_spawn(m, m->control_command, &m->control_pid);
1041 if (r < 0)
e537352b
LP
1042 goto fail;
1043
1044 mount_set_state(m, MOUNT_MOUNTING);
1045
1046 return;
1047
1048fail:
f2341e0a 1049 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
22af0e58 1050 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1051}
1052
850b7410
LP
1053static void mount_set_reload_result(Mount *m, MountResult result) {
1054 assert(m);
1055
1056 /* Only store the first error we encounter */
1057 if (m->reload_result != MOUNT_SUCCESS)
1058 return;
1059
1060 m->reload_result = result;
1061}
1062
9d2f5178 1063static void mount_enter_remounting(Mount *m) {
e537352b 1064 int r;
b294b79f 1065 MountParameters *p;
e537352b
LP
1066
1067 assert(m);
1068
850b7410
LP
1069 /* Reset reload result when we are about to start a new remount operation */
1070 m->reload_result = MOUNT_SUCCESS;
1071
a16e1123
LP
1072 m->control_command_id = MOUNT_EXEC_REMOUNT;
1073 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b 1074
b294b79f
MO
1075 p = get_mount_parameters_fragment(m);
1076 if (p) {
e537352b
LP
1077 const char *o;
1078
b294b79f
MO
1079 if (p->options)
1080 o = strjoina("remount,", p->options);
718db961 1081 else
e537352b
LP
1082 o = "remount";
1083
f00929ad 1084 r = exec_command_set(m->control_command, MOUNT_PATH,
b294b79f 1085 p->what, m->where,
e86b3761 1086 "-o", o, NULL);
e86b3761
ZJS
1087 if (r >= 0 && m->sloppy_options)
1088 r = exec_command_append(m->control_command, "-s", NULL);
c600357b
MH
1089 if (r >= 0 && m->read_write_only)
1090 r = exec_command_append(m->control_command, "-w", NULL);
b294b79f
MO
1091 if (r >= 0 && p->fstype)
1092 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
6b1dc2bd 1093 } else
e537352b 1094 r = -ENOENT;
60b912f6 1095 if (r < 0)
e537352b 1096 goto fail;
e537352b 1097
a16e1123 1098 mount_unwatch_control_pid(m);
5e94833f 1099
718db961
LP
1100 r = mount_spawn(m, m->control_command, &m->control_pid);
1101 if (r < 0)
e537352b
LP
1102 goto fail;
1103
1104 mount_set_state(m, MOUNT_REMOUNTING);
1105
1106 return;
1107
1108fail:
f2341e0a 1109 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
850b7410 1110 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
22af0e58 1111 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1112}
1113
7eba1463
LP
1114static void mount_cycle_clear(Mount *m) {
1115 assert(m);
1116
1117 /* Clear all state we shall forget for this new cycle */
1118
1119 m->result = MOUNT_SUCCESS;
1120 m->reload_result = MOUNT_SUCCESS;
1121 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
1122 UNIT(m)->reset_accounting = true;
1123}
1124
e537352b
LP
1125static int mount_start(Unit *u) {
1126 Mount *m = MOUNT(u);
07299350 1127 int r;
e537352b
LP
1128
1129 assert(m);
1130
1131 /* We cannot fulfill this request right now, try again later
1132 * please! */
f2aed307
LP
1133 if (IN_SET(m->state,
1134 MOUNT_UNMOUNTING,
1135 MOUNT_UNMOUNTING_SIGTERM,
17e9d53d
YW
1136 MOUNT_UNMOUNTING_SIGKILL,
1137 MOUNT_CLEANING))
e537352b
LP
1138 return -EAGAIN;
1139
1140 /* Already on it! */
db39a627 1141 if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE))
e537352b
LP
1142 return 0;
1143
f2aed307 1144 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
e537352b 1145
97a3f4ee 1146 r = unit_test_start_limit(u);
07299350
LP
1147 if (r < 0) {
1148 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1149 return r;
1150 }
1151
4b58153d
LP
1152 r = unit_acquire_invocation_id(u);
1153 if (r < 0)
1154 return r;
1155
7eba1463 1156 mount_cycle_clear(m);
8d567588 1157 mount_enter_mounting(m);
7eba1463 1158
82a2b6bb 1159 return 1;
e537352b
LP
1160}
1161
1162static int mount_stop(Unit *u) {
1163 Mount *m = MOUNT(u);
1164
1165 assert(m);
1166
22af0e58
LP
1167 switch (m->state) {
1168
1169 case MOUNT_UNMOUNTING:
1170 case MOUNT_UNMOUNTING_SIGKILL:
1171 case MOUNT_UNMOUNTING_SIGTERM:
1172 /* Already on it */
e537352b
LP
1173 return 0;
1174
22af0e58
LP
1175 case MOUNT_MOUNTING:
1176 case MOUNT_MOUNTING_DONE:
1177 case MOUNT_REMOUNTING:
1178 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1179 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1180 return 0;
e537352b 1181
22af0e58
LP
1182 case MOUNT_REMOUNTING_SIGTERM:
1183 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1184 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1185 return 0;
1186
1187 case MOUNT_REMOUNTING_SIGKILL:
1188 /* as above */
1189 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1190 return 0;
1191
1192 case MOUNT_MOUNTED:
1193 mount_enter_unmounting(m);
1194 return 1;
1195
17e9d53d
YW
1196 case MOUNT_CLEANING:
1197 /* If we are currently cleaning, then abort it, brutally. */
1198 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
1199 return 0;
1200
22af0e58
LP
1201 default:
1202 assert_not_reached("Unexpected state.");
1203 }
e537352b
LP
1204}
1205
1206static int mount_reload(Unit *u) {
1207 Mount *m = MOUNT(u);
1208
1209 assert(m);
e537352b
LP
1210 assert(m->state == MOUNT_MOUNTED);
1211
9d2f5178 1212 mount_enter_remounting(m);
22af0e58 1213
2d018ae2 1214 return 1;
e537352b
LP
1215}
1216
a16e1123
LP
1217static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1218 Mount *m = MOUNT(u);
1219
1220 assert(m);
1221 assert(f);
1222 assert(fds);
1223
d68c645b
LP
1224 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1225 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1226 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
2c09fb81 1227 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
a16e1123
LP
1228
1229 if (m->control_pid > 0)
d68c645b 1230 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
a16e1123
LP
1231
1232 if (m->control_command_id >= 0)
d68c645b 1233 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
a16e1123
LP
1234
1235 return 0;
1236}
1237
1238static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1239 Mount *m = MOUNT(u);
2c09fb81 1240 int r;
a16e1123
LP
1241
1242 assert(u);
1243 assert(key);
1244 assert(value);
1245 assert(fds);
1246
1247 if (streq(key, "state")) {
1248 MountState state;
1249
1250 if ((state = mount_state_from_string(value)) < 0)
f2341e0a 1251 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
1252 else
1253 m->deserialized_state = state;
2c09fb81 1254
9d2f5178
LP
1255 } else if (streq(key, "result")) {
1256 MountResult f;
a16e1123 1257
9d2f5178
LP
1258 f = mount_result_from_string(value);
1259 if (f < 0)
f2341e0a 1260 log_unit_debug(u, "Failed to parse result value: %s", value);
9d2f5178
LP
1261 else if (f != MOUNT_SUCCESS)
1262 m->result = f;
1263
1264 } else if (streq(key, "reload-result")) {
1265 MountResult f;
1266
1267 f = mount_result_from_string(value);
1268 if (f < 0)
f2341e0a 1269 log_unit_debug(u, "Failed to parse reload result value: %s", value);
9d2f5178
LP
1270 else if (f != MOUNT_SUCCESS)
1271 m->reload_result = f;
a16e1123 1272
2c09fb81
LP
1273 } else if (streq(key, "n-retry-umount")) {
1274
1275 r = safe_atou(value, &m->n_retry_umount);
1276 if (r < 0)
1277 log_unit_debug(u, "Failed to parse n-retry-umount value: %s", value);
1278
a16e1123 1279 } else if (streq(key, "control-pid")) {
a16e1123 1280
3f459cd9 1281 if (parse_pid(value, &m->control_pid) < 0)
f2341e0a 1282 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
3f459cd9 1283
a16e1123
LP
1284 } else if (streq(key, "control-command")) {
1285 MountExecCommand id;
1286
f2341e0a
LP
1287 id = mount_exec_command_from_string(value);
1288 if (id < 0)
1289 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
1290 else {
1291 m->control_command_id = id;
1292 m->control_command = m->exec_command + id;
1293 }
a16e1123 1294 } else
f2341e0a 1295 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
1296
1297 return 0;
1298}
1299
44a6b1b6 1300_pure_ static UnitActiveState mount_active_state(Unit *u) {
e537352b
LP
1301 assert(u);
1302
1303 return state_translation_table[MOUNT(u)->state];
1304}
1305
44a6b1b6 1306_pure_ static const char *mount_sub_state_to_string(Unit *u) {
10a94420
LP
1307 assert(u);
1308
a16e1123 1309 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1310}
1311
f2f725e5 1312_pure_ static bool mount_may_gc(Unit *u) {
701cc384
LP
1313 Mount *m = MOUNT(u);
1314
1315 assert(m);
1316
f2f725e5
ZJS
1317 if (m->from_proc_self_mountinfo)
1318 return false;
1319
1320 return true;
701cc384
LP
1321}
1322
e537352b
LP
1323static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1324 Mount *m = MOUNT(u);
9d2f5178 1325 MountResult f;
e537352b
LP
1326
1327 assert(m);
1328 assert(pid >= 0);
1329
8c47c732
LP
1330 if (pid != m->control_pid)
1331 return;
e537352b 1332
35080486
LP
1333 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1334 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1335 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1336 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1337 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1338 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1339 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1340 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1341 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1342 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1343 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1344 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1345 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1346 * /proc/self/mountinfo changes before our mount/umount exits. */
1347 (void) mount_process_proc_self_mountinfo(u->manager);
1348
e537352b
LP
1349 m->control_pid = 0;
1350
1f0958f6 1351 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
9d2f5178
LP
1352 f = MOUNT_SUCCESS;
1353 else if (code == CLD_EXITED)
1354 f = MOUNT_FAILURE_EXIT_CODE;
1355 else if (code == CLD_KILLED)
1356 f = MOUNT_FAILURE_SIGNAL;
1357 else if (code == CLD_DUMPED)
1358 f = MOUNT_FAILURE_CORE_DUMP;
1359 else
1360 assert_not_reached("Unknown code");
1361
850b7410
LP
1362 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1363 mount_set_reload_result(m, f);
1364 else if (m->result == MOUNT_SUCCESS)
9d2f5178 1365 m->result = f;
8c47c732 1366
a16e1123 1367 if (m->control_command) {
6ea832a2 1368 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
9d2f5178 1369
a16e1123
LP
1370 m->control_command = NULL;
1371 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1372 }
1373
91bbd9b7 1374 unit_log_process_exit(
5cc2cd1c 1375 u,
91bbd9b7
LP
1376 "Mount process",
1377 mount_exec_command_to_string(m->control_command_id),
5cc2cd1c 1378 f == MOUNT_SUCCESS,
91bbd9b7 1379 code, status);
e537352b 1380
006aabbd
AJ
1381 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1382 * before we process the SIGCHLD for the mount command. */
e537352b
LP
1383
1384 switch (m->state) {
1385
1386 case MOUNT_MOUNTING:
006aabbd 1387 /* Our mount point has not appeared in mountinfo. Something went wrong. */
e537352b 1388
006aabbd
AJ
1389 if (f == MOUNT_SUCCESS) {
1390 /* Either /bin/mount has an unexpected definition of success,
1391 * or someone raced us and we lost. */
1392 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1393 f = MOUNT_FAILURE_PROTOCOL;
1394 }
1395 mount_enter_dead(m, f);
1396 break;
1397
1398 case MOUNT_MOUNTING_DONE:
1399 mount_enter_mounted(m, f);
e537352b
LP
1400 break;
1401
e2f3b44c 1402 case MOUNT_REMOUNTING:
e2f3b44c 1403 case MOUNT_REMOUNTING_SIGTERM:
22af0e58
LP
1404 case MOUNT_REMOUNTING_SIGKILL:
1405 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e2f3b44c
LP
1406 break;
1407
e537352b 1408 case MOUNT_UNMOUNTING:
e537352b 1409
3cc96856 1410 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
22af0e58
LP
1411
1412 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
57018361
AJ
1413 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1414 * but we will stop as soon as any one umount times out. */
22af0e58
LP
1415
1416 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1417 log_unit_debug(u, "Mount still present, trying again.");
1418 m->n_retry_umount++;
1419 mount_enter_unmounting(m);
1420 } else {
006aabbd 1421 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
22af0e58
LP
1422 mount_enter_mounted(m, f);
1423 }
1424 } else
3cc96856 1425 mount_enter_dead_or_mounted(m, f);
22af0e58 1426
e537352b 1427 break;
57018361
AJ
1428
1429 case MOUNT_UNMOUNTING_SIGKILL:
1430 case MOUNT_UNMOUNTING_SIGTERM:
1431 mount_enter_dead_or_mounted(m, f);
1432 break;
e537352b 1433
17e9d53d
YW
1434 case MOUNT_CLEANING:
1435 if (m->clean_result == MOUNT_SUCCESS)
1436 m->clean_result = f;
1437
1438 mount_enter_dead(m, MOUNT_SUCCESS);
1439 break;
1440
e537352b
LP
1441 default:
1442 assert_not_reached("Uh, control process died at wrong time.");
1443 }
c4e2ceae
LP
1444
1445 /* Notify clients about changed exit status */
1446 unit_add_to_dbus_queue(u);
e537352b
LP
1447}
1448
718db961
LP
1449static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1450 Mount *m = MOUNT(userdata);
e537352b
LP
1451
1452 assert(m);
718db961 1453 assert(m->timer_event_source == source);
e537352b
LP
1454
1455 switch (m->state) {
1456
1457 case MOUNT_MOUNTING:
1458 case MOUNT_MOUNTING_DONE:
22af0e58
LP
1459 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1460 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1461 break;
1462
1463 case MOUNT_REMOUNTING:
79aafbd1 1464 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
22af0e58
LP
1465 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1466 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
e537352b
LP
1467 break;
1468
22af0e58
LP
1469 case MOUNT_REMOUNTING_SIGTERM:
1470 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
e537352b 1471
4819ff03 1472 if (m->kill_context.send_sigkill) {
22af0e58
LP
1473 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1474 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
ba035df2 1475 } else {
22af0e58
LP
1476 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1477 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
ba035df2 1478 }
e537352b
LP
1479 break;
1480
22af0e58
LP
1481 case MOUNT_REMOUNTING_SIGKILL:
1482 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1483
22af0e58
LP
1484 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1485 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1486 break;
1487
1488 case MOUNT_UNMOUNTING:
79aafbd1 1489 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
22af0e58 1490 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1491 break;
1492
1493 case MOUNT_UNMOUNTING_SIGTERM:
4819ff03 1494 if (m->kill_context.send_sigkill) {
22af0e58 1495 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
9d2f5178 1496 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
ba035df2 1497 } else {
22af0e58
LP
1498 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1499 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1500 }
e537352b
LP
1501 break;
1502
e537352b 1503 case MOUNT_UNMOUNTING_SIGKILL:
22af0e58
LP
1504 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1505 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1506 break;
1507
17e9d53d
YW
1508 case MOUNT_CLEANING:
1509 log_unit_warning(UNIT(m), "Cleaning timed out. killing.");
1510
1511 if (m->clean_result == MOUNT_SUCCESS)
1512 m->clean_result = MOUNT_FAILURE_TIMEOUT;
1513
1514 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0);
1515 break;
1516
e537352b
LP
1517 default:
1518 assert_not_reached("Timeout at wrong time.");
1519 }
718db961
LP
1520
1521 return 0;
e537352b
LP
1522}
1523
03b8cfed 1524static int mount_setup_new_unit(
839ee058
LP
1525 Manager *m,
1526 const char *name,
03b8cfed
FB
1527 const char *what,
1528 const char *where,
1529 const char *options,
1530 const char *fstype,
ec88d1ea 1531 MountProcFlags *ret_flags,
839ee058 1532 Unit **ret) {
03b8cfed 1533
839ee058 1534 _cleanup_(unit_freep) Unit *u = NULL;
bbee24bc 1535 int r;
03b8cfed 1536
839ee058
LP
1537 assert(m);
1538 assert(name);
ec88d1ea 1539 assert(ret_flags);
839ee058
LP
1540 assert(ret);
1541
1542 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1543 if (r < 0)
1544 return r;
03b8cfed 1545
e10fe042
LP
1546 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1547 if (r < 0)
1548 return r;
03b8cfed 1549
e10fe042
LP
1550 r = free_and_strdup(&MOUNT(u)->where, where);
1551 if (r < 0)
1552 return r;
03b8cfed 1553
9ddaa3e4 1554 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1555 if (r < 0)
1556 return r;
03b8cfed 1557
6d7e89b0
LP
1558 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1559 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1560 * to.*/
1561 MOUNT(u)->from_proc_self_mountinfo = true;
1562
1563 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1564 * loaded in now. */
cfcd4318 1565 unit_add_to_load_queue(u);
26e35b16 1566
ec88d1ea 1567 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
839ee058 1568 *ret = TAKE_PTR(u);
03b8cfed
FB
1569 return 0;
1570}
1571
1572static int mount_setup_existing_unit(
1573 Unit *u,
1574 const char *what,
1575 const char *where,
1576 const char *options,
1577 const char *fstype,
ec88d1ea 1578 MountProcFlags *ret_flags) {
03b8cfed 1579
bbee24bc 1580 int r;
03b8cfed
FB
1581
1582 assert(u);
4bb68f2f 1583 assert(ret_flags);
03b8cfed
FB
1584
1585 if (!MOUNT(u)->where) {
1586 MOUNT(u)->where = strdup(where);
1587 if (!MOUNT(u)->where)
1588 return -ENOMEM;
1589 }
1590
4bb68f2f
LP
1591 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1592 * for the current unit. Note that the flags field is reset on each iteration of reading
1593 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1594 * iteration and thus worthy of taking into account. */
1595 MountProcFlags flags =
1596 MOUNT(u)->proc_flags | MOUNT_PROC_IS_MOUNTED;
1597
9ddaa3e4 1598 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1599 if (r < 0)
1600 return r;
ec88d1ea
LP
1601 if (r > 0)
1602 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed 1603
4bb68f2f
LP
1604 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1605 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1606 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1607 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1608 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1609 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1610 * actually seeing it established for the first time. */
1611 if (!MOUNT(u)->from_proc_self_mountinfo || MOUNT(u)->state == MOUNT_MOUNTING)
ec88d1ea 1612 flags |= MOUNT_PROC_JUST_MOUNTED;
d253a45e
YW
1613
1614 MOUNT(u)->from_proc_self_mountinfo = true;
03b8cfed 1615
f8064c4f
LP
1616 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1617 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1618 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
03b8cfed
FB
1619 u->load_state = UNIT_LOADED;
1620 u->load_error = 0;
1621
ec88d1ea 1622 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed
FB
1623 }
1624
ec88d1ea 1625 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
a3742204
LP
1626 /* If things changed, then make sure that all deps are regenerated. Let's
1627 * first remove all automatic deps, and then add in the new ones. */
1628
1629 unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1630
bf7eedbf 1631 r = mount_add_non_exec_dependencies(MOUNT(u));
a3742204
LP
1632 if (r < 0)
1633 return r;
1634 }
03b8cfed 1635
ec88d1ea 1636 *ret_flags = flags;
03b8cfed
FB
1637 return 0;
1638}
1639
628c89cc 1640static int mount_setup_unit(
e537352b
LP
1641 Manager *m,
1642 const char *what,
1643 const char *where,
1644 const char *options,
1645 const char *fstype,
e537352b 1646 bool set_flags) {
057d9ab8 1647
03b8cfed 1648 _cleanup_free_ char *e = NULL;
ec88d1ea 1649 MountProcFlags flags;
057d9ab8
LP
1650 Unit *u;
1651 int r;
b08d03ff 1652
f50e0a01 1653 assert(m);
b08d03ff
LP
1654 assert(what);
1655 assert(where);
e537352b
LP
1656 assert(options);
1657 assert(fstype);
1658
e537352b
LP
1659 /* Ignore API mount points. They should never be referenced in
1660 * dependencies ever. */
33ff02c9 1661 if (mount_point_is_api(where) || mount_point_ignore(where))
57f2a956 1662 return 0;
b08d03ff 1663
8d567588
LP
1664 if (streq(fstype, "autofs"))
1665 return 0;
1666
4e85aff4
LP
1667 /* probably some kind of swap, ignore */
1668 if (!is_path(where))
b08d03ff
LP
1669 return 0;
1670
2c905207
LP
1671 /* Mount unit names have to be (like all other unit names) short enough to fit into file names. This
1672 * means there's a good chance that overly long mount point paths after mangling them to look like a
1673 * unit name would result in unit names we don't actually consider valid. This should be OK however
1674 * as such long mount point paths should not happen on regular systems — and if they appear
1675 * nonetheless they are generally synthesized by software, and thus managed by that other
1676 * software. Having such long names just means you cannot use systemd to manage those specific mount
1677 * points, which should be an OK restriction to make. After all we don't have to be able to manage
1678 * all mount points in the world — as long as we don't choke on them when we encounter them. */
7410616c 1679 r = unit_name_from_path(where, ".mount", &e);
2c905207
LP
1680 if (r < 0) {
1681 static RateLimit rate_limit = { /* Let's log about this at warning level at most once every
1682 * 5s. Given that we generate this whenever we read the file
1683 * otherwise we probably shouldn't flood the logs with
1684 * this */
1685 .interval = 5 * USEC_PER_SEC,
1686 .burst = 1,
1687 };
1688
1689 return log_struct_errno(
1690 ratelimit_below(&rate_limit) ? LOG_WARNING : LOG_DEBUG, r,
1691 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR,
1692 "MOUNT_POINT=%s", where,
1693 LOG_MESSAGE("Failed to generate valid unit name from path '%s', ignoring mount point: %m", where));
1694 }
b08d03ff 1695
7d17cfbc 1696 u = manager_get_unit(m, e);
839ee058 1697 if (u)
03b8cfed 1698 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
839ee058
LP
1699 else
1700 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1701 * by the sysadmin having called mount(8) directly. */
1702 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
03b8cfed 1703 if (r < 0)
2c905207 1704 return log_warning_errno(r, "Failed to set up mount unit for '%s': %m", where);
ff5f34d0 1705
ec88d1ea
LP
1706 /* If the mount changed properties or state, let's notify our clients */
1707 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
ff5f34d0 1708 unit_add_to_dbus_queue(u);
c1e1601e 1709
ec88d1ea
LP
1710 if (set_flags)
1711 MOUNT(u)->proc_flags = flags;
1712
b08d03ff 1713 return 0;
b08d03ff
LP
1714}
1715
ef734fd6 1716static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
13dcfe46
ZJS
1717 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1718 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
ba0d56f5 1719 int r;
b08d03ff
LP
1720
1721 assert(m);
1722
e2857b3d 1723 r = libmount_parse(NULL, NULL, &table, &iter);
5cca8def 1724 if (r < 0)
628c89cc 1725 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
e537352b 1726
5cca8def 1727 for (;;) {
95b862b0 1728 struct libmnt_fs *fs;
8d3ae2bd 1729 const char *device, *path, *options, *fstype;
b08d03ff 1730
13dcfe46
ZJS
1731 r = mnt_table_next_fs(table, iter, &fs);
1732 if (r == 1)
5cca8def 1733 break;
13dcfe46
ZJS
1734 if (r < 0)
1735 return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
5cca8def 1736
8d3ae2bd
CL
1737 device = mnt_fs_get_source(fs);
1738 path = mnt_fs_get_target(fs);
1739 options = mnt_fs_get_options(fs);
1740 fstype = mnt_fs_get_fstype(fs);
a2e0f3d3 1741
c0a7f8d3
DM
1742 if (!device || !path)
1743 continue;
1744
9d1b2b22 1745 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
628c89cc 1746
9d1b2b22 1747 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
b08d03ff
LP
1748 }
1749
ba0d56f5 1750 return 0;
e537352b
LP
1751}
1752
1753static void mount_shutdown(Manager *m) {
1754 assert(m);
1755
718db961
LP
1756 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1757
d379d442
KZ
1758 mnt_unref_monitor(m->mount_monitor);
1759 m->mount_monitor = NULL;
b08d03ff
LP
1760}
1761
7a7821c8 1762static int mount_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1763 Mount *m = MOUNT(u);
7a7821c8 1764 usec_t t;
68db7a3b
ZJS
1765 int r;
1766
1767 if (!m->timer_event_source)
1768 return 0;
1769
7a7821c8 1770 r = sd_event_source_get_time(m->timer_event_source, &t);
68db7a3b
ZJS
1771 if (r < 0)
1772 return r;
7a7821c8
LP
1773 if (t == USEC_INFINITY)
1774 return 0;
68db7a3b 1775
7a7821c8 1776 *timeout = t;
68db7a3b
ZJS
1777 return 1;
1778}
1779
04eb582a 1780static void mount_enumerate_perpetual(Manager *m) {
11222d0f
LP
1781 Unit *u;
1782 int r;
1783
1784 assert(m);
1785
1786 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1787 * unconditionally synthesize it here and mark it as perpetual. */
1788
1789 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1790 if (!u) {
a581e45a 1791 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
04eb582a
LP
1792 if (r < 0) {
1793 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1794 return;
1795 }
11222d0f
LP
1796 }
1797
1798 u->perpetual = true;
1799 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1800
1801 unit_add_to_load_queue(u);
1802 unit_add_to_dbus_queue(u);
1803}
1804
1805static bool mount_is_mounted(Mount *m) {
1806 assert(m);
1807
ec88d1ea 1808 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
11222d0f
LP
1809}
1810
ba64af90 1811static void mount_enumerate(Manager *m) {
b08d03ff 1812 int r;
d379d442 1813
b08d03ff
LP
1814 assert(m);
1815
8d3ae2bd
CL
1816 mnt_init_debug(0);
1817
d379d442
KZ
1818 if (!m->mount_monitor) {
1819 int fd;
ef734fd6 1820
d379d442
KZ
1821 m->mount_monitor = mnt_new_monitor();
1822 if (!m->mount_monitor) {
ba64af90 1823 log_oom();
718db961 1824 goto fail;
d379d442 1825 }
29083707 1826
d379d442 1827 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
ba64af90
LP
1828 if (r < 0) {
1829 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
29083707 1830 goto fail;
ba64af90
LP
1831 }
1832
d379d442 1833 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
ba64af90
LP
1834 if (r < 0) {
1835 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f7c1ad4f 1836 goto fail;
ba64af90 1837 }
90598531 1838
d379d442
KZ
1839 /* mnt_unref_monitor() will close the fd */
1840 fd = r = mnt_monitor_get_fd(m->mount_monitor);
ba64af90
LP
1841 if (r < 0) {
1842 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f7c1ad4f 1843 goto fail;
ba64af90 1844 }
befb6d54 1845
d379d442 1846 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
ba64af90
LP
1847 if (r < 0) {
1848 log_error_errno(r, "Failed to watch mount file descriptor: %m");
befb6d54 1849 goto fail;
ba64af90 1850 }
befb6d54 1851
83231637 1852 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1853 if (r < 0) {
1854 log_error_errno(r, "Failed to adjust mount watch priority: %m");
befb6d54 1855 goto fail;
ba64af90 1856 }
7dfbe2e3 1857
d379d442 1858 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
befb6d54
CL
1859 }
1860
e62d8c39
ZJS
1861 r = mount_load_proc_self_mountinfo(m, false);
1862 if (r < 0)
b08d03ff
LP
1863 goto fail;
1864
ba64af90 1865 return;
b08d03ff
LP
1866
1867fail:
1868 mount_shutdown(m);
5cb5a6ff
LP
1869}
1870
fcd8e119
LP
1871static int drain_libmount(Manager *m) {
1872 bool rescan = false;
1873 int r;
1874
1875 assert(m);
1876
1877 /* Drain all events and verify that the event is valid.
1878 *
1879 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1880 * may generate event which is irrelevant for us.
1881 *
1882 * error: r < 0; valid: r == 0, false positive: r == 1 */
1883 do {
1884 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1885 if (r < 0)
1886 return log_error_errno(r, "Failed to drain libmount events: %m");
1887 if (r == 0)
1888 rescan = true;
1889 } while (r == 0);
1890
1891 return rescan;
1892}
1893
35080486 1894static int mount_process_proc_self_mountinfo(Manager *m) {
ec8126d7 1895 _cleanup_set_free_free_ Set *around = NULL, *gone = NULL;
ec8126d7 1896 const char *what;
ec8126d7 1897 Unit *u;
ef734fd6
LP
1898 int r;
1899
1900 assert(m);
ef734fd6 1901
fcd8e119
LP
1902 r = drain_libmount(m);
1903 if (r <= 0)
1904 return r;
ef734fd6 1905
4f0eedb7
ZJS
1906 r = mount_load_proc_self_mountinfo(m, true);
1907 if (r < 0) {
e537352b 1908 /* Reset flags, just in case, for later calls */
ec88d1ea
LP
1909 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
1910 MOUNT(u)->proc_flags = 0;
e537352b 1911
ec8126d7 1912 return 0;
ef734fd6
LP
1913 }
1914
1915 manager_dispatch_load_queue(m);
1916
595ed347
MS
1917 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1918 Mount *mount = MOUNT(u);
ef734fd6 1919
11222d0f 1920 if (!mount_is_mounted(mount)) {
e537352b 1921
394763f6
LP
1922 /* A mount point is not around right now. It
1923 * might be gone, or might never have
1924 * existed. */
1925
1926 if (mount->from_proc_self_mountinfo &&
1927 mount->parameters_proc_self_mountinfo.what) {
1928
1929 /* Remember that this device might just have disappeared */
548f6937 1930 if (set_ensure_allocated(&gone, &path_hash_ops) < 0 ||
be327321 1931 set_put_strdup(&gone, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6
LP
1932 log_oom(); /* we don't care too much about OOM here... */
1933 }
fcd8b266 1934
ef734fd6 1935 mount->from_proc_self_mountinfo = false;
9ddaa3e4 1936 assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
e537352b
LP
1937
1938 switch (mount->state) {
1939
1940 case MOUNT_MOUNTED:
7eba1463 1941 /* This has just been unmounted by somebody else, follow the state change. */
9d2f5178 1942 mount_enter_dead(mount, MOUNT_SUCCESS);
e537352b
LP
1943 break;
1944
2fa0bd7d
YW
1945 case MOUNT_MOUNTING_DONE:
1946 /* The mount command may add the corresponding proc mountinfo entry and
1947 * then remove it because of an internal error. E.g., fuse.sshfs seems
1948 * to do that when the connection fails. See #17617. To handle such the
1949 * case, let's once set the state back to mounting. Then, the unit can
1950 * correctly enter the failed state later in mount_sigchld(). */
1951 mount_set_state(mount, MOUNT_MOUNTING);
1952 break;
1953
e537352b 1954 default:
e537352b 1955 break;
e537352b
LP
1956 }
1957
ec88d1ea 1958 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
e537352b 1959
fcd8b266 1960 /* A mount point was added or changed */
e537352b
LP
1961
1962 switch (mount->state) {
1963
1964 case MOUNT_DEAD:
fdf20a31 1965 case MOUNT_FAILED:
4b58153d
LP
1966
1967 /* This has just been mounted by somebody else, follow the state change, but let's
1968 * generate a new invocation ID for this implicitly and automatically. */
7eba1463
LP
1969 (void) unit_acquire_invocation_id(u);
1970 mount_cycle_clear(mount);
9d2f5178 1971 mount_enter_mounted(mount, MOUNT_SUCCESS);
e537352b
LP
1972 break;
1973
1974 case MOUNT_MOUNTING:
5bcb0f2b 1975 mount_set_state(mount, MOUNT_MOUNTING_DONE);
e537352b
LP
1976 break;
1977
1978 default:
1979 /* Nothing really changed, but let's
1980 * issue an notification call
1981 * nonetheless, in case somebody is
1982 * waiting for this. (e.g. file system
1983 * ro/rw remounts.) */
1984 mount_set_state(mount, mount->state);
1985 break;
1986 }
394763f6 1987 }
fcd8b266 1988
11222d0f 1989 if (mount_is_mounted(mount) &&
394763f6
LP
1990 mount->from_proc_self_mountinfo &&
1991 mount->parameters_proc_self_mountinfo.what) {
b6418dc9 1992 /* Track devices currently used */
fcd8b266 1993
548f6937 1994 if (set_ensure_allocated(&around, &path_hash_ops) < 0 ||
be327321 1995 set_put_strdup(&around, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 1996 log_oom();
e537352b
LP
1997 }
1998
1999 /* Reset the flags for later calls */
ec88d1ea 2000 mount->proc_flags = 0;
e537352b 2001 }
718db961 2002
90e74a66 2003 SET_FOREACH(what, gone) {
fcd8b266
LP
2004 if (set_contains(around, what))
2005 continue;
2006
2007 /* Let the device units know that the device is no longer mounted */
485ae697 2008 device_found_node(m, what, 0, DEVICE_FOUND_MOUNT);
fcd8b266 2009 }
ec8126d7
ZJS
2010
2011 return 0;
e537352b
LP
2012}
2013
35080486
LP
2014static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2015 Manager *m = userdata;
2016
2017 assert(m);
2018 assert(revents & EPOLLIN);
2019
2020 return mount_process_proc_self_mountinfo(m);
2021}
2022
fdf20a31 2023static void mount_reset_failed(Unit *u) {
5632e374
LP
2024 Mount *m = MOUNT(u);
2025
2026 assert(m);
2027
fdf20a31 2028 if (m->state == MOUNT_FAILED)
5632e374
LP
2029 mount_set_state(m, MOUNT_DEAD);
2030
9d2f5178
LP
2031 m->result = MOUNT_SUCCESS;
2032 m->reload_result = MOUNT_SUCCESS;
17e9d53d 2033 m->clean_result = MOUNT_SUCCESS;
5632e374
LP
2034}
2035
718db961 2036static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
22af0e58
LP
2037 Mount *m = MOUNT(u);
2038
2039 assert(m);
2040
ee36fed4 2041 return unit_kill_common(u, who, signo, -1, m->control_pid, error);
8a0867d6
LP
2042}
2043
291d565a
LP
2044static int mount_control_pid(Unit *u) {
2045 Mount *m = MOUNT(u);
2046
2047 assert(m);
2048
2049 return m->control_pid;
2050}
2051
17e9d53d
YW
2052static int mount_clean(Unit *u, ExecCleanMask mask) {
2053 _cleanup_strv_free_ char **l = NULL;
2054 Mount *m = MOUNT(u);
2055 int r;
2056
2057 assert(m);
2058 assert(mask != 0);
2059
2060 if (m->state != MOUNT_DEAD)
2061 return -EBUSY;
2062
2063 r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l);
2064 if (r < 0)
2065 return r;
2066
2067 if (strv_isempty(l))
2068 return -EUNATCH;
2069
2070 mount_unwatch_control_pid(m);
2071 m->clean_result = MOUNT_SUCCESS;
2072 m->control_command = NULL;
2073 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
2074
2075 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->exec_context.timeout_clean_usec));
2076 if (r < 0)
2077 goto fail;
2078
2079 r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
2080 if (r < 0)
2081 goto fail;
2082
2083 mount_set_state(m, MOUNT_CLEANING);
2084
2085 return 0;
2086
2087fail:
2088 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
2089 m->clean_result = MOUNT_FAILURE_RESOURCES;
2090 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
2091 return r;
2092}
2093
2094static int mount_can_clean(Unit *u, ExecCleanMask *ret) {
2095 Mount *m = MOUNT(u);
2096
2097 assert(m);
2098
2099 return exec_context_get_clean_mask(&m->exec_context, ret);
2100}
2101
a16e1123
LP
2102static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
2103 [MOUNT_EXEC_MOUNT] = "ExecMount",
2104 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
2105 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
2106};
2107
2108DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
2109
9d2f5178
LP
2110static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
2111 [MOUNT_SUCCESS] = "success",
2112 [MOUNT_FAILURE_RESOURCES] = "resources",
2113 [MOUNT_FAILURE_TIMEOUT] = "timeout",
2114 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
2115 [MOUNT_FAILURE_SIGNAL] = "signal",
07299350
LP
2116 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
2117 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
006aabbd 2118 [MOUNT_FAILURE_PROTOCOL] = "protocol",
9d2f5178
LP
2119};
2120
2121DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
2122
87f0e418 2123const UnitVTable mount_vtable = {
7d17cfbc 2124 .object_size = sizeof(Mount),
718db961
LP
2125 .exec_context_offset = offsetof(Mount, exec_context),
2126 .cgroup_context_offset = offsetof(Mount, cgroup_context),
2127 .kill_context_offset = offsetof(Mount, kill_context),
613b411c 2128 .exec_runtime_offset = offsetof(Mount, exec_runtime),
29206d46 2129 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
3ef63c31 2130
f975e971
LP
2131 .sections =
2132 "Unit\0"
2133 "Mount\0"
2134 "Install\0",
4ad49000 2135 .private_section = "Mount",
71645aca 2136
c80a9a33
LP
2137 .can_transient = true,
2138 .can_fail = true,
2139
e537352b
LP
2140 .init = mount_init,
2141 .load = mount_load,
034c6ed7 2142 .done = mount_done,
e537352b 2143
f50e0a01
LP
2144 .coldplug = mount_coldplug,
2145
034c6ed7 2146 .dump = mount_dump,
5cb5a6ff 2147
e537352b
LP
2148 .start = mount_start,
2149 .stop = mount_stop,
2150 .reload = mount_reload,
2151
8a0867d6 2152 .kill = mount_kill,
17e9d53d
YW
2153 .clean = mount_clean,
2154 .can_clean = mount_can_clean,
8a0867d6 2155
a16e1123
LP
2156 .serialize = mount_serialize,
2157 .deserialize_item = mount_deserialize_item,
2158
f50e0a01 2159 .active_state = mount_active_state,
10a94420 2160 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 2161
52a12341
YW
2162 .will_restart = unit_will_restart_default,
2163
f2f725e5 2164 .may_gc = mount_may_gc,
39c79477 2165 .is_extrinsic = mount_is_extrinsic,
701cc384 2166
e537352b 2167 .sigchld_event = mount_sigchld_event,
e537352b 2168
fdf20a31 2169 .reset_failed = mount_reset_failed,
291d565a
LP
2170
2171 .control_pid = mount_control_pid,
5632e374 2172
74c964d3
LP
2173 .bus_set_property = bus_mount_set_property,
2174 .bus_commit_properties = bus_mount_commit_properties,
4139c1b2 2175
68db7a3b
ZJS
2176 .get_timeout = mount_get_timeout,
2177
04eb582a 2178 .enumerate_perpetual = mount_enumerate_perpetual,
f50e0a01 2179 .enumerate = mount_enumerate,
c6918296
MS
2180 .shutdown = mount_shutdown,
2181
2182 .status_message_formats = {
2183 .starting_stopping = {
2184 [0] = "Mounting %s...",
2185 [1] = "Unmounting %s...",
2186 },
2187 .finished_start_job = {
2188 [JOB_DONE] = "Mounted %s.",
2189 [JOB_FAILED] = "Failed to mount %s.",
c6918296
MS
2190 [JOB_TIMEOUT] = "Timed out mounting %s.",
2191 },
2192 .finished_stop_job = {
2193 [JOB_DONE] = "Unmounted %s.",
2194 [JOB_FAILED] = "Failed unmounting %s.",
2195 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2196 },
2197 },
5cb5a6ff 2198};