]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/mount.c
test: merge unit file related tests into TEST-23-UNIT-FILE
[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
ef25552e 888static int mount_spawn(Mount *m, ExecCommand *c, pid_t *ret_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);
ef25552e 902 assert(ret_pid);
a16e1123 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 929
ef25552e 930 *ret_pid = pid;
a16e1123 931 return 0;
a16e1123
LP
932}
933
9d2f5178 934static void mount_enter_dead(Mount *m, MountResult f) {
e537352b
LP
935 assert(m);
936
a0fef983 937 if (m->result == MOUNT_SUCCESS)
9d2f5178 938 m->result = f;
e537352b 939
aac99f30 940 unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
4c425434
LP
941 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_stop);
942
29206d46
LP
943 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
944
28135da3 945 m->exec_runtime = exec_runtime_destroy(m->exec_runtime);
613b411c 946
bb0c0d6f 947 unit_destroy_runtime_data(UNIT(m), &m->exec_context);
e66cf1a3 948
00d9ef85
LP
949 unit_unref_uid_gid(UNIT(m), true);
950
49267b1b
YW
951 /* Any dependencies based on /proc/self/mountinfo are now stale. Let's re-generate dependencies from
952 * .mount unit. */
953 (void) mount_add_non_exec_dependencies(m);
e537352b
LP
954}
955
9d2f5178 956static void mount_enter_mounted(Mount *m, MountResult f) {
80876c20
LP
957 assert(m);
958
a0fef983 959 if (m->result == MOUNT_SUCCESS)
9d2f5178 960 m->result = f;
80876c20
LP
961
962 mount_set_state(m, MOUNT_MOUNTED);
963}
964
22af0e58
LP
965static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
966 assert(m);
967
968 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
969 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
970 * ultimately we just mirror the kernel's internal state on this. */
971
972 if (m->from_proc_self_mountinfo)
973 mount_enter_mounted(m, f);
974 else
975 mount_enter_dead(m, f);
976}
977
978static int state_to_kill_operation(MountState state) {
979 switch (state) {
980
981 case MOUNT_REMOUNTING_SIGTERM:
a232ebcc
ZJS
982 return KILL_RESTART;
983
22af0e58
LP
984 case MOUNT_UNMOUNTING_SIGTERM:
985 return KILL_TERMINATE;
986
987 case MOUNT_REMOUNTING_SIGKILL:
988 case MOUNT_UNMOUNTING_SIGKILL:
989 return KILL_KILL;
990
991 default:
992 return _KILL_OPERATION_INVALID;
993 }
994}
995
9d2f5178 996static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
e537352b
LP
997 int r;
998
999 assert(m);
1000
a0fef983 1001 if (m->result == MOUNT_SUCCESS)
9d2f5178 1002 m->result = f;
e537352b 1003
cd2086fe
LP
1004 r = unit_kill_context(
1005 UNIT(m),
1006 &m->kill_context,
22af0e58 1007 state_to_kill_operation(state),
cd2086fe
LP
1008 -1,
1009 m->control_pid,
1010 false);
1011 if (r < 0)
1012 goto fail;
e537352b 1013
cd2086fe 1014 if (r > 0) {
36c16a7c 1015 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 1016 if (r < 0)
80876c20 1017 goto fail;
e537352b 1018
80876c20 1019 mount_set_state(m, state);
22af0e58 1020 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 1021 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
22af0e58 1022 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
9d2f5178 1023 mount_enter_mounted(m, MOUNT_SUCCESS);
22af0e58 1024 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 1025 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
80876c20 1026 else
22af0e58 1027 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1028
1029 return;
1030
1031fail:
f2341e0a 1032 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
22af0e58 1033 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
5261ba90
TT
1034}
1035
9d2f5178 1036static void mount_enter_unmounting(Mount *m) {
e537352b
LP
1037 int r;
1038
1039 assert(m);
1040
7d54a03a
LP
1041 /* Start counting our attempts */
1042 if (!IN_SET(m->state,
1043 MOUNT_UNMOUNTING,
1044 MOUNT_UNMOUNTING_SIGTERM,
1045 MOUNT_UNMOUNTING_SIGKILL))
1046 m->n_retry_umount = 0;
1047
a16e1123
LP
1048 m->control_command_id = MOUNT_EXEC_UNMOUNT;
1049 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b 1050
83897d54 1051 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
e520950a
BR
1052 if (r >= 0 && m->lazy_unmount)
1053 r = exec_command_append(m->control_command, "-l", NULL);
4f8d40a9
BR
1054 if (r >= 0 && m->force_unmount)
1055 r = exec_command_append(m->control_command, "-f", NULL);
7d54a03a 1056 if (r < 0)
e537352b
LP
1057 goto fail;
1058
a16e1123 1059 mount_unwatch_control_pid(m);
5e94833f 1060
7d54a03a
LP
1061 r = mount_spawn(m, m->control_command, &m->control_pid);
1062 if (r < 0)
e537352b
LP
1063 goto fail;
1064
1065 mount_set_state(m, MOUNT_UNMOUNTING);
1066
1067 return;
1068
1069fail:
f2341e0a 1070 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
22af0e58 1071 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1072}
1073
8d567588 1074static void mount_enter_mounting(Mount *m) {
e537352b 1075 int r;
cb39ed3f 1076 MountParameters *p;
218cfe23 1077 bool source_is_dir = true;
e537352b
LP
1078
1079 assert(m);
1080
25cd4964 1081 r = unit_fail_if_noncanonical(UNIT(m), m->where);
f2341e0a
LP
1082 if (r < 0)
1083 goto fail;
1084
218cfe23
DT
1085 p = get_mount_parameters_fragment(m);
1086 if (p && mount_is_bind(p)) {
1087 r = is_dir(p->what, /* follow = */ true);
1088 if (r < 0 && r != -ENOENT)
1089 log_unit_info_errno(UNIT(m), r, "Failed to determine type of bind mount source '%s', ignoring: %m", p->what);
1090 else if (r == 0)
1091 source_is_dir = false;
1092 }
3e5235b0 1093
218cfe23
DT
1094 if (source_is_dir)
1095 (void) mkdir_p_label(m->where, m->directory_mode);
1096 else
1097 (void) touch_file(m->where, /* parents = */ true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
1098
1099 if (source_is_dir)
1100 unit_warn_if_dir_nonempty(UNIT(m), m->where);
4c425434 1101 unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_start);
a4634b21
LP
1102
1103 m->control_command_id = MOUNT_EXEC_MOUNT;
1104 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
1105
cb39ed3f 1106 /* Create the source directory for bind-mounts if needed */
5dc60faa
AP
1107 if (p && mount_is_bind(p)) {
1108 r = mkdir_p_label(p->what, m->directory_mode);
237ecfee 1109 /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is
574febda
YW
1110 * totally OK, in case the user wants us to overmount a non-directory inode. Also -EROFS can be
1111 * returned on read-only filesystem. Moreover, -EACCES (and also maybe -EPERM?) may be returned
1112 * when the path is on NFS. See issue #24120. All such errors will be logged in the debug level. */
e5e6b7c2 1113 if (r < 0 && r != -EEXIST)
574febda
YW
1114 log_unit_full_errno(UNIT(m),
1115 (r == -EROFS || ERRNO_IS_PRIVILEGE(r)) ? LOG_DEBUG : LOG_WARNING,
1116 r, "Failed to make bind mount source '%s', ignoring: %m", p->what);
5dc60faa 1117 }
5261ba90 1118
b294b79f 1119 if (p) {
17a1c597
ZJS
1120 _cleanup_free_ char *opts = NULL;
1121
ff0c31bc 1122 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, NULL, &opts);
17a1c597
ZJS
1123 if (r < 0)
1124 goto fail;
1125
b294b79f 1126 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
e86b3761
ZJS
1127 if (r >= 0 && m->sloppy_options)
1128 r = exec_command_append(m->control_command, "-s", NULL);
c600357b
MH
1129 if (r >= 0 && m->read_write_only)
1130 r = exec_command_append(m->control_command, "-w", NULL);
b294b79f
MO
1131 if (r >= 0 && p->fstype)
1132 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
0c47569a 1133 if (r >= 0 && !isempty(opts))
17a1c597 1134 r = exec_command_append(m->control_command, "-o", opts, NULL);
e86b3761 1135 } else
e537352b 1136 r = -ENOENT;
e537352b
LP
1137 if (r < 0)
1138 goto fail;
1139
a16e1123 1140 mount_unwatch_control_pid(m);
5e94833f 1141
257f1d8e
LP
1142 r = mount_spawn(m, m->control_command, &m->control_pid);
1143 if (r < 0)
e537352b
LP
1144 goto fail;
1145
1146 mount_set_state(m, MOUNT_MOUNTING);
1147
1148 return;
1149
1150fail:
f2341e0a 1151 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
22af0e58 1152 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1153}
1154
850b7410
LP
1155static void mount_set_reload_result(Mount *m, MountResult result) {
1156 assert(m);
1157
1158 /* Only store the first error we encounter */
1159 if (m->reload_result != MOUNT_SUCCESS)
1160 return;
1161
1162 m->reload_result = result;
1163}
1164
9d2f5178 1165static void mount_enter_remounting(Mount *m) {
e537352b 1166 int r;
b294b79f 1167 MountParameters *p;
e537352b
LP
1168
1169 assert(m);
1170
850b7410
LP
1171 /* Reset reload result when we are about to start a new remount operation */
1172 m->reload_result = MOUNT_SUCCESS;
1173
a16e1123
LP
1174 m->control_command_id = MOUNT_EXEC_REMOUNT;
1175 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b 1176
b294b79f
MO
1177 p = get_mount_parameters_fragment(m);
1178 if (p) {
e537352b
LP
1179 const char *o;
1180
b294b79f
MO
1181 if (p->options)
1182 o = strjoina("remount,", p->options);
718db961 1183 else
e537352b
LP
1184 o = "remount";
1185
f00929ad 1186 r = exec_command_set(m->control_command, MOUNT_PATH,
b294b79f 1187 p->what, m->where,
e86b3761 1188 "-o", o, NULL);
e86b3761
ZJS
1189 if (r >= 0 && m->sloppy_options)
1190 r = exec_command_append(m->control_command, "-s", NULL);
c600357b
MH
1191 if (r >= 0 && m->read_write_only)
1192 r = exec_command_append(m->control_command, "-w", NULL);
b294b79f
MO
1193 if (r >= 0 && p->fstype)
1194 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
6b1dc2bd 1195 } else
e537352b 1196 r = -ENOENT;
60b912f6 1197 if (r < 0)
e537352b 1198 goto fail;
e537352b 1199
a16e1123 1200 mount_unwatch_control_pid(m);
5e94833f 1201
718db961
LP
1202 r = mount_spawn(m, m->control_command, &m->control_pid);
1203 if (r < 0)
e537352b
LP
1204 goto fail;
1205
1206 mount_set_state(m, MOUNT_REMOUNTING);
1207
1208 return;
1209
1210fail:
f2341e0a 1211 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
850b7410 1212 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
22af0e58 1213 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1214}
1215
7eba1463
LP
1216static void mount_cycle_clear(Mount *m) {
1217 assert(m);
1218
1219 /* Clear all state we shall forget for this new cycle */
1220
1221 m->result = MOUNT_SUCCESS;
1222 m->reload_result = MOUNT_SUCCESS;
1223 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
1224 UNIT(m)->reset_accounting = true;
1225}
1226
e537352b
LP
1227static int mount_start(Unit *u) {
1228 Mount *m = MOUNT(u);
07299350 1229 int r;
e537352b
LP
1230
1231 assert(m);
1232
1233 /* We cannot fulfill this request right now, try again later
1234 * please! */
f2aed307
LP
1235 if (IN_SET(m->state,
1236 MOUNT_UNMOUNTING,
1237 MOUNT_UNMOUNTING_SIGTERM,
17e9d53d
YW
1238 MOUNT_UNMOUNTING_SIGKILL,
1239 MOUNT_CLEANING))
e537352b
LP
1240 return -EAGAIN;
1241
1242 /* Already on it! */
db39a627 1243 if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE))
e537352b
LP
1244 return 0;
1245
f2aed307 1246 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
e537352b 1247
4b58153d
LP
1248 r = unit_acquire_invocation_id(u);
1249 if (r < 0)
1250 return r;
1251
7eba1463 1252 mount_cycle_clear(m);
8d567588 1253 mount_enter_mounting(m);
7eba1463 1254
82a2b6bb 1255 return 1;
e537352b
LP
1256}
1257
1258static int mount_stop(Unit *u) {
1259 Mount *m = MOUNT(u);
1260
1261 assert(m);
1262
22af0e58
LP
1263 switch (m->state) {
1264
1265 case MOUNT_UNMOUNTING:
1266 case MOUNT_UNMOUNTING_SIGKILL:
1267 case MOUNT_UNMOUNTING_SIGTERM:
1268 /* Already on it */
e537352b
LP
1269 return 0;
1270
22af0e58
LP
1271 case MOUNT_MOUNTING:
1272 case MOUNT_MOUNTING_DONE:
1273 case MOUNT_REMOUNTING:
1274 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1275 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1276 return 0;
e537352b 1277
22af0e58
LP
1278 case MOUNT_REMOUNTING_SIGTERM:
1279 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1280 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1281 return 0;
1282
1283 case MOUNT_REMOUNTING_SIGKILL:
1284 /* as above */
1285 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1286 return 0;
1287
1288 case MOUNT_MOUNTED:
1289 mount_enter_unmounting(m);
1290 return 1;
1291
17e9d53d
YW
1292 case MOUNT_CLEANING:
1293 /* If we are currently cleaning, then abort it, brutally. */
1294 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
1295 return 0;
1296
22af0e58 1297 default:
04499a70 1298 assert_not_reached();
22af0e58 1299 }
e537352b
LP
1300}
1301
1302static int mount_reload(Unit *u) {
1303 Mount *m = MOUNT(u);
1304
1305 assert(m);
e537352b
LP
1306 assert(m->state == MOUNT_MOUNTED);
1307
9d2f5178 1308 mount_enter_remounting(m);
22af0e58 1309
2d018ae2 1310 return 1;
e537352b
LP
1311}
1312
a16e1123
LP
1313static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1314 Mount *m = MOUNT(u);
1315
1316 assert(m);
1317 assert(f);
1318 assert(fds);
1319
d68c645b
LP
1320 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1321 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1322 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
2c09fb81 1323 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
a16e1123
LP
1324
1325 if (m->control_pid > 0)
d68c645b 1326 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
a16e1123
LP
1327
1328 if (m->control_command_id >= 0)
d68c645b 1329 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
a16e1123
LP
1330
1331 return 0;
1332}
1333
1334static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1335 Mount *m = MOUNT(u);
2c09fb81 1336 int r;
a16e1123 1337
c73f413d 1338 assert(m);
a16e1123
LP
1339 assert(u);
1340 assert(key);
1341 assert(value);
1342 assert(fds);
1343
1344 if (streq(key, "state")) {
1345 MountState state;
1346
7fb1d980
YW
1347 state = mount_state_from_string(value);
1348 if (state < 0)
1349 log_unit_debug_errno(u, state, "Failed to parse state value: %s", value);
a16e1123
LP
1350 else
1351 m->deserialized_state = state;
2c09fb81 1352
9d2f5178
LP
1353 } else if (streq(key, "result")) {
1354 MountResult f;
a16e1123 1355
9d2f5178
LP
1356 f = mount_result_from_string(value);
1357 if (f < 0)
7fb1d980 1358 log_unit_debug_errno(u, f, "Failed to parse result value: %s", value);
9d2f5178
LP
1359 else if (f != MOUNT_SUCCESS)
1360 m->result = f;
1361
1362 } else if (streq(key, "reload-result")) {
1363 MountResult f;
1364
1365 f = mount_result_from_string(value);
1366 if (f < 0)
7fb1d980 1367 log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value);
9d2f5178
LP
1368 else if (f != MOUNT_SUCCESS)
1369 m->reload_result = f;
a16e1123 1370
2c09fb81
LP
1371 } else if (streq(key, "n-retry-umount")) {
1372
1373 r = safe_atou(value, &m->n_retry_umount);
1374 if (r < 0)
7fb1d980 1375 log_unit_debug_errno(u, r, "Failed to parse n-retry-umount value: %s", value);
2c09fb81 1376
a16e1123 1377 } else if (streq(key, "control-pid")) {
a16e1123 1378
7fb1d980
YW
1379 r = parse_pid(value, &m->control_pid);
1380 if (r < 0)
1381 log_unit_debug_errno(u, r, "Failed to parse control-pid value: %s", value);
3f459cd9 1382
a16e1123
LP
1383 } else if (streq(key, "control-command")) {
1384 MountExecCommand id;
1385
f2341e0a
LP
1386 id = mount_exec_command_from_string(value);
1387 if (id < 0)
7fb1d980 1388 log_unit_debug_errno(u, id, "Failed to parse exec-command value: %s", value);
a16e1123
LP
1389 else {
1390 m->control_command_id = id;
1391 m->control_command = m->exec_command + id;
1392 }
a16e1123 1393 } else
f2341e0a 1394 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
1395
1396 return 0;
1397}
1398
44a6b1b6 1399_pure_ static UnitActiveState mount_active_state(Unit *u) {
e537352b
LP
1400 assert(u);
1401
1402 return state_translation_table[MOUNT(u)->state];
1403}
1404
44a6b1b6 1405_pure_ static const char *mount_sub_state_to_string(Unit *u) {
10a94420
LP
1406 assert(u);
1407
a16e1123 1408 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1409}
1410
f2f725e5 1411_pure_ static bool mount_may_gc(Unit *u) {
701cc384
LP
1412 Mount *m = MOUNT(u);
1413
1414 assert(m);
1415
f2f725e5
ZJS
1416 if (m->from_proc_self_mountinfo)
1417 return false;
1418
1419 return true;
701cc384
LP
1420}
1421
e537352b
LP
1422static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1423 Mount *m = MOUNT(u);
9d2f5178 1424 MountResult f;
e537352b
LP
1425
1426 assert(m);
1427 assert(pid >= 0);
1428
8c47c732
LP
1429 if (pid != m->control_pid)
1430 return;
e537352b 1431
35080486
LP
1432 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1433 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1434 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1435 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1436 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1437 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1438 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1439 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1440 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1441 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1442 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1443 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1444 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1445 * /proc/self/mountinfo changes before our mount/umount exits. */
1446 (void) mount_process_proc_self_mountinfo(u->manager);
1447
e537352b
LP
1448 m->control_pid = 0;
1449
1f0958f6 1450 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
9d2f5178
LP
1451 f = MOUNT_SUCCESS;
1452 else if (code == CLD_EXITED)
1453 f = MOUNT_FAILURE_EXIT_CODE;
1454 else if (code == CLD_KILLED)
1455 f = MOUNT_FAILURE_SIGNAL;
1456 else if (code == CLD_DUMPED)
1457 f = MOUNT_FAILURE_CORE_DUMP;
1458 else
04499a70 1459 assert_not_reached();
9d2f5178 1460
850b7410
LP
1461 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1462 mount_set_reload_result(m, f);
1463 else if (m->result == MOUNT_SUCCESS)
9d2f5178 1464 m->result = f;
8c47c732 1465
a16e1123 1466 if (m->control_command) {
6ea832a2 1467 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
9d2f5178 1468
a16e1123
LP
1469 m->control_command = NULL;
1470 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1471 }
1472
91bbd9b7 1473 unit_log_process_exit(
5cc2cd1c 1474 u,
91bbd9b7
LP
1475 "Mount process",
1476 mount_exec_command_to_string(m->control_command_id),
5cc2cd1c 1477 f == MOUNT_SUCCESS,
91bbd9b7 1478 code, status);
e537352b 1479
006aabbd
AJ
1480 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1481 * before we process the SIGCHLD for the mount command. */
e537352b
LP
1482
1483 switch (m->state) {
1484
1485 case MOUNT_MOUNTING:
006aabbd 1486 /* Our mount point has not appeared in mountinfo. Something went wrong. */
e537352b 1487
006aabbd
AJ
1488 if (f == MOUNT_SUCCESS) {
1489 /* Either /bin/mount has an unexpected definition of success,
1490 * or someone raced us and we lost. */
1491 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1492 f = MOUNT_FAILURE_PROTOCOL;
1493 }
1494 mount_enter_dead(m, f);
1495 break;
1496
1497 case MOUNT_MOUNTING_DONE:
1498 mount_enter_mounted(m, f);
e537352b
LP
1499 break;
1500
e2f3b44c 1501 case MOUNT_REMOUNTING:
e2f3b44c 1502 case MOUNT_REMOUNTING_SIGTERM:
22af0e58
LP
1503 case MOUNT_REMOUNTING_SIGKILL:
1504 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e2f3b44c
LP
1505 break;
1506
e537352b 1507 case MOUNT_UNMOUNTING:
e537352b 1508
3cc96856 1509 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
22af0e58
LP
1510
1511 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
57018361
AJ
1512 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1513 * but we will stop as soon as any one umount times out. */
22af0e58
LP
1514
1515 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1516 log_unit_debug(u, "Mount still present, trying again.");
1517 m->n_retry_umount++;
1518 mount_enter_unmounting(m);
1519 } else {
006aabbd 1520 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
22af0e58
LP
1521 mount_enter_mounted(m, f);
1522 }
1523 } else
3cc96856 1524 mount_enter_dead_or_mounted(m, f);
22af0e58 1525
e537352b 1526 break;
57018361
AJ
1527
1528 case MOUNT_UNMOUNTING_SIGKILL:
1529 case MOUNT_UNMOUNTING_SIGTERM:
1530 mount_enter_dead_or_mounted(m, f);
1531 break;
e537352b 1532
17e9d53d
YW
1533 case MOUNT_CLEANING:
1534 if (m->clean_result == MOUNT_SUCCESS)
1535 m->clean_result = f;
1536
1537 mount_enter_dead(m, MOUNT_SUCCESS);
1538 break;
1539
e537352b 1540 default:
04499a70 1541 assert_not_reached();
e537352b 1542 }
c4e2ceae
LP
1543
1544 /* Notify clients about changed exit status */
1545 unit_add_to_dbus_queue(u);
e537352b
LP
1546}
1547
718db961
LP
1548static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1549 Mount *m = MOUNT(userdata);
e537352b
LP
1550
1551 assert(m);
718db961 1552 assert(m->timer_event_source == source);
e537352b
LP
1553
1554 switch (m->state) {
1555
1556 case MOUNT_MOUNTING:
1557 case MOUNT_MOUNTING_DONE:
22af0e58
LP
1558 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1559 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1560 break;
1561
1562 case MOUNT_REMOUNTING:
79aafbd1 1563 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
22af0e58
LP
1564 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1565 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
e537352b
LP
1566 break;
1567
22af0e58
LP
1568 case MOUNT_REMOUNTING_SIGTERM:
1569 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
e537352b 1570
4819ff03 1571 if (m->kill_context.send_sigkill) {
22af0e58
LP
1572 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1573 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
ba035df2 1574 } else {
22af0e58
LP
1575 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1576 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
ba035df2 1577 }
e537352b
LP
1578 break;
1579
22af0e58
LP
1580 case MOUNT_REMOUNTING_SIGKILL:
1581 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1582
22af0e58
LP
1583 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1584 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1585 break;
1586
1587 case MOUNT_UNMOUNTING:
79aafbd1 1588 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
22af0e58 1589 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1590 break;
1591
1592 case MOUNT_UNMOUNTING_SIGTERM:
4819ff03 1593 if (m->kill_context.send_sigkill) {
22af0e58 1594 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
9d2f5178 1595 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
ba035df2 1596 } else {
22af0e58
LP
1597 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1598 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1599 }
e537352b
LP
1600 break;
1601
e537352b 1602 case MOUNT_UNMOUNTING_SIGKILL:
22af0e58
LP
1603 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1604 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1605 break;
1606
17e9d53d
YW
1607 case MOUNT_CLEANING:
1608 log_unit_warning(UNIT(m), "Cleaning timed out. killing.");
1609
1610 if (m->clean_result == MOUNT_SUCCESS)
1611 m->clean_result = MOUNT_FAILURE_TIMEOUT;
1612
1613 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0);
1614 break;
1615
e537352b 1616 default:
04499a70 1617 assert_not_reached();
e537352b 1618 }
718db961
LP
1619
1620 return 0;
e537352b
LP
1621}
1622
03b8cfed 1623static int mount_setup_new_unit(
839ee058
LP
1624 Manager *m,
1625 const char *name,
03b8cfed
FB
1626 const char *what,
1627 const char *where,
1628 const char *options,
1629 const char *fstype,
ec88d1ea 1630 MountProcFlags *ret_flags,
839ee058 1631 Unit **ret) {
03b8cfed 1632
839ee058 1633 _cleanup_(unit_freep) Unit *u = NULL;
bbee24bc 1634 int r;
03b8cfed 1635
839ee058
LP
1636 assert(m);
1637 assert(name);
ec88d1ea 1638 assert(ret_flags);
839ee058
LP
1639 assert(ret);
1640
1641 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1642 if (r < 0)
1643 return r;
03b8cfed 1644
e10fe042
LP
1645 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1646 if (r < 0)
1647 return r;
03b8cfed 1648
e10fe042
LP
1649 r = free_and_strdup(&MOUNT(u)->where, where);
1650 if (r < 0)
1651 return r;
03b8cfed 1652
9ddaa3e4 1653 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1654 if (r < 0)
1655 return r;
03b8cfed 1656
09f4d843
ZJS
1657 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the
1658 * time we load the unit file for it (and thus add in extra deps right after) we know what source to
1659 * attributes the deps to. */
6d7e89b0
LP
1660 MOUNT(u)->from_proc_self_mountinfo = true;
1661
b9e6d23d
YW
1662 r = mount_add_non_exec_dependencies(MOUNT(u));
1663 if (r < 0)
1664 return r;
1665
09f4d843
ZJS
1666 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything
1667 * else is loaded in now. */
cfcd4318 1668 unit_add_to_load_queue(u);
26e35b16 1669
ec88d1ea 1670 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
839ee058 1671 *ret = TAKE_PTR(u);
03b8cfed
FB
1672 return 0;
1673}
1674
1675static int mount_setup_existing_unit(
1676 Unit *u,
1677 const char *what,
1678 const char *where,
1679 const char *options,
1680 const char *fstype,
ec88d1ea 1681 MountProcFlags *ret_flags) {
03b8cfed 1682
bbee24bc 1683 int r;
03b8cfed
FB
1684
1685 assert(u);
4bb68f2f 1686 assert(ret_flags);
03b8cfed
FB
1687
1688 if (!MOUNT(u)->where) {
1689 MOUNT(u)->where = strdup(where);
1690 if (!MOUNT(u)->where)
1691 return -ENOMEM;
1692 }
1693
4bb68f2f
LP
1694 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1695 * for the current unit. Note that the flags field is reset on each iteration of reading
1696 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1697 * iteration and thus worthy of taking into account. */
1698 MountProcFlags flags =
1699 MOUNT(u)->proc_flags | MOUNT_PROC_IS_MOUNTED;
1700
9ddaa3e4 1701 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
bbee24bc
LP
1702 if (r < 0)
1703 return r;
ec88d1ea
LP
1704 if (r > 0)
1705 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed 1706
4bb68f2f
LP
1707 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1708 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1709 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1710 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1711 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1712 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1713 * actually seeing it established for the first time. */
1714 if (!MOUNT(u)->from_proc_self_mountinfo || MOUNT(u)->state == MOUNT_MOUNTING)
ec88d1ea 1715 flags |= MOUNT_PROC_JUST_MOUNTED;
d253a45e
YW
1716
1717 MOUNT(u)->from_proc_self_mountinfo = true;
03b8cfed 1718
f8064c4f
LP
1719 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1720 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1721 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
03b8cfed
FB
1722 u->load_state = UNIT_LOADED;
1723 u->load_error = 0;
1724
ec88d1ea 1725 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed
FB
1726 }
1727
ec88d1ea 1728 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
a3742204
LP
1729 /* If things changed, then make sure that all deps are regenerated. Let's
1730 * first remove all automatic deps, and then add in the new ones. */
bf7eedbf 1731 r = mount_add_non_exec_dependencies(MOUNT(u));
a3742204
LP
1732 if (r < 0)
1733 return r;
1734 }
03b8cfed 1735
ec88d1ea 1736 *ret_flags = flags;
03b8cfed
FB
1737 return 0;
1738}
1739
628c89cc 1740static int mount_setup_unit(
e537352b
LP
1741 Manager *m,
1742 const char *what,
1743 const char *where,
1744 const char *options,
1745 const char *fstype,
e537352b 1746 bool set_flags) {
057d9ab8 1747
03b8cfed 1748 _cleanup_free_ char *e = NULL;
ec88d1ea 1749 MountProcFlags flags;
057d9ab8
LP
1750 Unit *u;
1751 int r;
b08d03ff 1752
f50e0a01 1753 assert(m);
b08d03ff
LP
1754 assert(what);
1755 assert(where);
e537352b
LP
1756 assert(options);
1757 assert(fstype);
1758
e537352b
LP
1759 /* Ignore API mount points. They should never be referenced in
1760 * dependencies ever. */
33ff02c9 1761 if (mount_point_is_api(where) || mount_point_ignore(where))
57f2a956 1762 return 0;
b08d03ff 1763
8d567588
LP
1764 if (streq(fstype, "autofs"))
1765 return 0;
1766
4e85aff4
LP
1767 /* probably some kind of swap, ignore */
1768 if (!is_path(where))
b08d03ff
LP
1769 return 0;
1770
7410616c 1771 r = unit_name_from_path(where, ".mount", &e);
03e52e8c 1772 if (r < 0)
2c905207 1773 return log_struct_errno(
03e52e8c 1774 LOG_WARNING, r,
2c905207
LP
1775 "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR,
1776 "MOUNT_POINT=%s", where,
03e52e8c
YW
1777 LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m",
1778 where));
b08d03ff 1779
7d17cfbc 1780 u = manager_get_unit(m, e);
839ee058 1781 if (u)
03b8cfed 1782 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
839ee058 1783 else
09f4d843
ZJS
1784 /* First time we see this mount point meaning that it's not been initiated by a mount unit
1785 * but rather by the sysadmin having called mount(8) directly. */
839ee058 1786 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
03b8cfed 1787 if (r < 0)
2c905207 1788 return log_warning_errno(r, "Failed to set up mount unit for '%s': %m", where);
ff5f34d0 1789
ec88d1ea
LP
1790 /* If the mount changed properties or state, let's notify our clients */
1791 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
ff5f34d0 1792 unit_add_to_dbus_queue(u);
c1e1601e 1793
ec88d1ea
LP
1794 if (set_flags)
1795 MOUNT(u)->proc_flags = flags;
1796
b08d03ff 1797 return 0;
b08d03ff
LP
1798}
1799
ef734fd6 1800static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
13dcfe46
ZJS
1801 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1802 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
ba0d56f5 1803 int r;
b08d03ff
LP
1804
1805 assert(m);
1806
e2857b3d 1807 r = libmount_parse(NULL, NULL, &table, &iter);
5cca8def 1808 if (r < 0)
628c89cc 1809 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
e537352b 1810
5cca8def 1811 for (;;) {
95b862b0 1812 struct libmnt_fs *fs;
8d3ae2bd 1813 const char *device, *path, *options, *fstype;
b08d03ff 1814
13dcfe46
ZJS
1815 r = mnt_table_next_fs(table, iter, &fs);
1816 if (r == 1)
5cca8def 1817 break;
13dcfe46
ZJS
1818 if (r < 0)
1819 return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
5cca8def 1820
8d3ae2bd
CL
1821 device = mnt_fs_get_source(fs);
1822 path = mnt_fs_get_target(fs);
1823 options = mnt_fs_get_options(fs);
1824 fstype = mnt_fs_get_fstype(fs);
a2e0f3d3 1825
c0a7f8d3
DM
1826 if (!device || !path)
1827 continue;
1828
9d1b2b22 1829 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
628c89cc 1830
9d1b2b22 1831 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
b08d03ff
LP
1832 }
1833
ba0d56f5 1834 return 0;
e537352b
LP
1835}
1836
1837static void mount_shutdown(Manager *m) {
1838 assert(m);
1839
5dcadb4c 1840 m->mount_event_source = sd_event_source_disable_unref(m->mount_event_source);
718db961 1841
d379d442
KZ
1842 mnt_unref_monitor(m->mount_monitor);
1843 m->mount_monitor = NULL;
b08d03ff
LP
1844}
1845
7a7821c8 1846static int mount_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1847 Mount *m = MOUNT(u);
7a7821c8 1848 usec_t t;
68db7a3b
ZJS
1849 int r;
1850
c73f413d
FS
1851 assert(m);
1852 assert(u);
1853
68db7a3b
ZJS
1854 if (!m->timer_event_source)
1855 return 0;
1856
7a7821c8 1857 r = sd_event_source_get_time(m->timer_event_source, &t);
68db7a3b
ZJS
1858 if (r < 0)
1859 return r;
7a7821c8
LP
1860 if (t == USEC_INFINITY)
1861 return 0;
68db7a3b 1862
7a7821c8 1863 *timeout = t;
68db7a3b
ZJS
1864 return 1;
1865}
1866
04eb582a 1867static void mount_enumerate_perpetual(Manager *m) {
11222d0f
LP
1868 Unit *u;
1869 int r;
1870
1871 assert(m);
1872
1873 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1874 * unconditionally synthesize it here and mark it as perpetual. */
1875
1876 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1877 if (!u) {
a581e45a 1878 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
04eb582a
LP
1879 if (r < 0) {
1880 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1881 return;
1882 }
11222d0f
LP
1883 }
1884
1885 u->perpetual = true;
1886 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1887
1888 unit_add_to_load_queue(u);
1889 unit_add_to_dbus_queue(u);
1890}
1891
1892static bool mount_is_mounted(Mount *m) {
1893 assert(m);
1894
ec88d1ea 1895 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
11222d0f
LP
1896}
1897
edc027b4 1898static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) {
99534007 1899 Manager *m = ASSERT_PTR(userdata);
c29e6a95 1900 Job *j;
edc027b4 1901
c29e6a95
MS
1902 /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */
1903 HASHMAP_FOREACH(j, m->jobs) {
1904 if (j->unit->type != UNIT_MOUNT)
1905 continue;
1906
1907 job_add_to_run_queue(j);
1908 }
1909
b0c4b282
LP
1910 /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so
1911 * let's make sure we dispatch them in the next iteration. */
1912 manager_trigger_run_queue(m);
edc027b4
MS
1913
1914 return 0;
1915}
1916
ba64af90 1917static void mount_enumerate(Manager *m) {
b08d03ff 1918 int r;
d379d442 1919
b08d03ff
LP
1920 assert(m);
1921
8d3ae2bd
CL
1922 mnt_init_debug(0);
1923
d379d442 1924 if (!m->mount_monitor) {
24a4542c 1925 unsigned mount_rate_limit_burst = 5;
d379d442 1926 int fd;
ef734fd6 1927
d379d442
KZ
1928 m->mount_monitor = mnt_new_monitor();
1929 if (!m->mount_monitor) {
ba64af90 1930 log_oom();
718db961 1931 goto fail;
d379d442 1932 }
29083707 1933
d379d442 1934 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
ba64af90
LP
1935 if (r < 0) {
1936 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
29083707 1937 goto fail;
ba64af90
LP
1938 }
1939
d379d442 1940 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
ba64af90
LP
1941 if (r < 0) {
1942 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f7c1ad4f 1943 goto fail;
ba64af90 1944 }
90598531 1945
d379d442
KZ
1946 /* mnt_unref_monitor() will close the fd */
1947 fd = r = mnt_monitor_get_fd(m->mount_monitor);
ba64af90
LP
1948 if (r < 0) {
1949 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f7c1ad4f 1950 goto fail;
ba64af90 1951 }
befb6d54 1952
d379d442 1953 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
ba64af90
LP
1954 if (r < 0) {
1955 log_error_errno(r, "Failed to watch mount file descriptor: %m");
befb6d54 1956 goto fail;
ba64af90 1957 }
befb6d54 1958
83231637 1959 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1960 if (r < 0) {
1961 log_error_errno(r, "Failed to adjust mount watch priority: %m");
befb6d54 1962 goto fail;
ba64af90 1963 }
7dfbe2e3 1964
24a4542c
LB
1965 /* Let users override the default (5 in 1s), as it stalls the boot sequence on busy systems. */
1966 const char *e = secure_getenv("SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST");
1967 if (e) {
1968 r = safe_atou(e, &mount_rate_limit_burst);
1969 if (r < 0)
1970 log_debug("Invalid value in $SYSTEMD_DEFAULT_MOUNT_RATE_LIMIT_BURST, ignoring: %s", e);
1971 }
1972
1973 r = sd_event_source_set_ratelimit(m->mount_event_source, 1 * USEC_PER_SEC, mount_rate_limit_burst);
d586f642
MS
1974 if (r < 0) {
1975 log_error_errno(r, "Failed to enable rate limit for mount events: %m");
1976 goto fail;
1977 }
1978
edc027b4
MS
1979 r = sd_event_source_set_ratelimit_expire_callback(m->mount_event_source, mount_on_ratelimit_expire);
1980 if (r < 0) {
1981 log_error_errno(r, "Failed to enable rate limit for mount events: %m");
1982 goto fail;
1983 }
1984
d379d442 1985 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
befb6d54
CL
1986 }
1987
e62d8c39
ZJS
1988 r = mount_load_proc_self_mountinfo(m, false);
1989 if (r < 0)
b08d03ff
LP
1990 goto fail;
1991
ba64af90 1992 return;
b08d03ff
LP
1993
1994fail:
1995 mount_shutdown(m);
5cb5a6ff
LP
1996}
1997
fcd8e119
LP
1998static int drain_libmount(Manager *m) {
1999 bool rescan = false;
2000 int r;
2001
2002 assert(m);
2003
2004 /* Drain all events and verify that the event is valid.
2005 *
2006 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
2007 * may generate event which is irrelevant for us.
2008 *
2009 * error: r < 0; valid: r == 0, false positive: r == 1 */
2010 do {
2011 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
2012 if (r < 0)
2013 return log_error_errno(r, "Failed to drain libmount events: %m");
2014 if (r == 0)
2015 rescan = true;
2016 } while (r == 0);
2017
2018 return rescan;
2019}
2020
35080486 2021static int mount_process_proc_self_mountinfo(Manager *m) {
6688b72d 2022 _cleanup_set_free_ Set *around = NULL, *gone = NULL;
ec8126d7 2023 const char *what;
ef734fd6
LP
2024 int r;
2025
2026 assert(m);
ef734fd6 2027
fcd8e119
LP
2028 r = drain_libmount(m);
2029 if (r <= 0)
2030 return r;
ef734fd6 2031
4f0eedb7
ZJS
2032 r = mount_load_proc_self_mountinfo(m, true);
2033 if (r < 0) {
e537352b 2034 /* Reset flags, just in case, for later calls */
ec88d1ea
LP
2035 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
2036 MOUNT(u)->proc_flags = 0;
e537352b 2037
ec8126d7 2038 return 0;
ef734fd6
LP
2039 }
2040
2041 manager_dispatch_load_queue(m);
2042
595ed347
MS
2043 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
2044 Mount *mount = MOUNT(u);
ef734fd6 2045
11222d0f 2046 if (!mount_is_mounted(mount)) {
e537352b 2047
394763f6
LP
2048 /* A mount point is not around right now. It
2049 * might be gone, or might never have
2050 * existed. */
2051
2052 if (mount->from_proc_self_mountinfo &&
6688b72d 2053 mount->parameters_proc_self_mountinfo.what)
394763f6 2054 /* Remember that this device might just have disappeared */
6688b72d 2055 if (set_put_strdup_full(&gone, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 2056 log_oom(); /* we don't care too much about OOM here... */
fcd8b266 2057
ef734fd6 2058 mount->from_proc_self_mountinfo = false;
9ddaa3e4 2059 assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
e537352b
LP
2060
2061 switch (mount->state) {
2062
2063 case MOUNT_MOUNTED:
7eba1463 2064 /* This has just been unmounted by somebody else, follow the state change. */
9d2f5178 2065 mount_enter_dead(mount, MOUNT_SUCCESS);
e537352b
LP
2066 break;
2067
2fa0bd7d
YW
2068 case MOUNT_MOUNTING_DONE:
2069 /* The mount command may add the corresponding proc mountinfo entry and
2070 * then remove it because of an internal error. E.g., fuse.sshfs seems
2071 * to do that when the connection fails. See #17617. To handle such the
2072 * case, let's once set the state back to mounting. Then, the unit can
2073 * correctly enter the failed state later in mount_sigchld(). */
2074 mount_set_state(mount, MOUNT_MOUNTING);
2075 break;
2076
e537352b 2077 default:
e537352b 2078 break;
e537352b
LP
2079 }
2080
ec88d1ea 2081 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
e537352b 2082
fcd8b266 2083 /* A mount point was added or changed */
e537352b
LP
2084
2085 switch (mount->state) {
2086
2087 case MOUNT_DEAD:
fdf20a31 2088 case MOUNT_FAILED:
4b58153d
LP
2089
2090 /* This has just been mounted by somebody else, follow the state change, but let's
2091 * generate a new invocation ID for this implicitly and automatically. */
7eba1463
LP
2092 (void) unit_acquire_invocation_id(u);
2093 mount_cycle_clear(mount);
9d2f5178 2094 mount_enter_mounted(mount, MOUNT_SUCCESS);
e537352b
LP
2095 break;
2096
2097 case MOUNT_MOUNTING:
5bcb0f2b 2098 mount_set_state(mount, MOUNT_MOUNTING_DONE);
e537352b
LP
2099 break;
2100
2101 default:
2102 /* Nothing really changed, but let's
2103 * issue an notification call
2104 * nonetheless, in case somebody is
2105 * waiting for this. (e.g. file system
2106 * ro/rw remounts.) */
2107 mount_set_state(mount, mount->state);
2108 break;
2109 }
394763f6 2110 }
fcd8b266 2111
11222d0f 2112 if (mount_is_mounted(mount) &&
394763f6 2113 mount->from_proc_self_mountinfo &&
6688b72d 2114 mount->parameters_proc_self_mountinfo.what)
b6418dc9 2115 /* Track devices currently used */
6688b72d 2116 if (set_put_strdup_full(&around, &path_hash_ops_free, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 2117 log_oom();
e537352b
LP
2118
2119 /* Reset the flags for later calls */
ec88d1ea 2120 mount->proc_flags = 0;
e537352b 2121 }
718db961 2122
90e74a66 2123 SET_FOREACH(what, gone) {
fcd8b266
LP
2124 if (set_contains(around, what))
2125 continue;
2126
2127 /* Let the device units know that the device is no longer mounted */
b1ba0ce8 2128 device_found_node(m, what, DEVICE_NOT_FOUND, DEVICE_FOUND_MOUNT);
fcd8b266 2129 }
ec8126d7
ZJS
2130
2131 return 0;
e537352b
LP
2132}
2133
35080486 2134static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
99534007 2135 Manager *m = ASSERT_PTR(userdata);
35080486 2136
35080486
LP
2137 assert(revents & EPOLLIN);
2138
2139 return mount_process_proc_self_mountinfo(m);
2140}
2141
fdf20a31 2142static void mount_reset_failed(Unit *u) {
5632e374
LP
2143 Mount *m = MOUNT(u);
2144
2145 assert(m);
2146
fdf20a31 2147 if (m->state == MOUNT_FAILED)
5632e374
LP
2148 mount_set_state(m, MOUNT_DEAD);
2149
9d2f5178
LP
2150 m->result = MOUNT_SUCCESS;
2151 m->reload_result = MOUNT_SUCCESS;
17e9d53d 2152 m->clean_result = MOUNT_SUCCESS;
5632e374
LP
2153}
2154
a721cd00 2155static int mount_kill(Unit *u, KillWho who, int signo, int code, int value, sd_bus_error *error) {
22af0e58
LP
2156 Mount *m = MOUNT(u);
2157
2158 assert(m);
2159
a721cd00 2160 return unit_kill_common(u, who, signo, code, value, -1, m->control_pid, error);
8a0867d6
LP
2161}
2162
291d565a
LP
2163static int mount_control_pid(Unit *u) {
2164 Mount *m = MOUNT(u);
2165
2166 assert(m);
2167
2168 return m->control_pid;
2169}
2170
17e9d53d
YW
2171static int mount_clean(Unit *u, ExecCleanMask mask) {
2172 _cleanup_strv_free_ char **l = NULL;
2173 Mount *m = MOUNT(u);
2174 int r;
2175
2176 assert(m);
2177 assert(mask != 0);
2178
2179 if (m->state != MOUNT_DEAD)
2180 return -EBUSY;
2181
2182 r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l);
2183 if (r < 0)
2184 return r;
2185
2186 if (strv_isempty(l))
2187 return -EUNATCH;
2188
2189 mount_unwatch_control_pid(m);
2190 m->clean_result = MOUNT_SUCCESS;
2191 m->control_command = NULL;
2192 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
2193
2194 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->exec_context.timeout_clean_usec));
2195 if (r < 0)
2196 goto fail;
2197
2198 r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
2199 if (r < 0)
2200 goto fail;
2201
2202 mount_set_state(m, MOUNT_CLEANING);
2203
2204 return 0;
2205
2206fail:
2207 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
2208 m->clean_result = MOUNT_FAILURE_RESOURCES;
5dcadb4c 2209 m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source);
17e9d53d
YW
2210 return r;
2211}
2212
2213static int mount_can_clean(Unit *u, ExecCleanMask *ret) {
2214 Mount *m = MOUNT(u);
2215
2216 assert(m);
2217
2218 return exec_context_get_clean_mask(&m->exec_context, ret);
2219}
2220
705578c3 2221static int mount_can_start(Unit *u) {
9727f242
DDM
2222 Mount *m = MOUNT(u);
2223 int r;
2224
2225 assert(m);
2226
2227 r = unit_test_start_limit(u);
2228 if (r < 0) {
2229 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
2230 return r;
2231 }
2232
705578c3 2233 return 1;
9727f242
DDM
2234}
2235
a16e1123 2236static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
48d83e33 2237 [MOUNT_EXEC_MOUNT] = "ExecMount",
a16e1123
LP
2238 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
2239 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
2240};
2241
2242DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
2243
9d2f5178 2244static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
48d83e33
ZJS
2245 [MOUNT_SUCCESS] = "success",
2246 [MOUNT_FAILURE_RESOURCES] = "resources",
2247 [MOUNT_FAILURE_TIMEOUT] = "timeout",
2248 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
2249 [MOUNT_FAILURE_SIGNAL] = "signal",
2250 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
07299350 2251 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
48d83e33 2252 [MOUNT_FAILURE_PROTOCOL] = "protocol",
9d2f5178
LP
2253};
2254
2255DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
2256
87f0e418 2257const UnitVTable mount_vtable = {
7d17cfbc 2258 .object_size = sizeof(Mount),
718db961
LP
2259 .exec_context_offset = offsetof(Mount, exec_context),
2260 .cgroup_context_offset = offsetof(Mount, cgroup_context),
2261 .kill_context_offset = offsetof(Mount, kill_context),
613b411c 2262 .exec_runtime_offset = offsetof(Mount, exec_runtime),
3ef63c31 2263
f975e971
LP
2264 .sections =
2265 "Unit\0"
2266 "Mount\0"
2267 "Install\0",
4ad49000 2268 .private_section = "Mount",
71645aca 2269
c80a9a33
LP
2270 .can_transient = true,
2271 .can_fail = true,
755021d4 2272 .exclude_from_switch_root_serialization = true,
c80a9a33 2273
e537352b
LP
2274 .init = mount_init,
2275 .load = mount_load,
034c6ed7 2276 .done = mount_done,
e537352b 2277
f50e0a01 2278 .coldplug = mount_coldplug,
01400460 2279 .catchup = mount_catchup,
f50e0a01 2280
034c6ed7 2281 .dump = mount_dump,
5cb5a6ff 2282
e537352b
LP
2283 .start = mount_start,
2284 .stop = mount_stop,
2285 .reload = mount_reload,
2286
8a0867d6 2287 .kill = mount_kill,
17e9d53d
YW
2288 .clean = mount_clean,
2289 .can_clean = mount_can_clean,
8a0867d6 2290
a16e1123
LP
2291 .serialize = mount_serialize,
2292 .deserialize_item = mount_deserialize_item,
2293
f50e0a01 2294 .active_state = mount_active_state,
10a94420 2295 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 2296
52a12341
YW
2297 .will_restart = unit_will_restart_default,
2298
f2f725e5 2299 .may_gc = mount_may_gc,
39c79477 2300 .is_extrinsic = mount_is_extrinsic,
701cc384 2301
e537352b 2302 .sigchld_event = mount_sigchld_event,
e537352b 2303
fdf20a31 2304 .reset_failed = mount_reset_failed,
291d565a
LP
2305
2306 .control_pid = mount_control_pid,
5632e374 2307
74c964d3
LP
2308 .bus_set_property = bus_mount_set_property,
2309 .bus_commit_properties = bus_mount_commit_properties,
4139c1b2 2310
68db7a3b
ZJS
2311 .get_timeout = mount_get_timeout,
2312
04eb582a 2313 .enumerate_perpetual = mount_enumerate_perpetual,
f50e0a01 2314 .enumerate = mount_enumerate,
c6918296
MS
2315 .shutdown = mount_shutdown,
2316
2317 .status_message_formats = {
2318 .starting_stopping = {
2319 [0] = "Mounting %s...",
2320 [1] = "Unmounting %s...",
2321 },
2322 .finished_start_job = {
2323 [JOB_DONE] = "Mounted %s.",
2324 [JOB_FAILED] = "Failed to mount %s.",
c6918296
MS
2325 [JOB_TIMEOUT] = "Timed out mounting %s.",
2326 },
2327 .finished_stop_job = {
2328 [JOB_DONE] = "Unmounted %s.",
2329 [JOB_FAILED] = "Failed unmounting %s.",
2330 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2331 },
2332 },
9727f242 2333
705578c3 2334 .can_start = mount_can_start,
5cb5a6ff 2335};