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