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