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