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