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