]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/mount.c
core: add comment explaining ECOMM return value of unit_start()
[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
LP
706
707 r = unit_watch_pid(UNIT(m), m->control_pid);
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
4ad49000
LP
813 r = unit_watch_pid(UNIT(m), pid);
814 if (r < 0)
a16e1123 815 /* FIXME: we need to do something here */
36c16a7c 816 return r;
a16e1123
LP
817
818 *_pid = pid;
819
820 return 0;
a16e1123
LP
821}
822
9d2f5178 823static void mount_enter_dead(Mount *m, MountResult f) {
e537352b
LP
824 assert(m);
825
a0fef983 826 if (m->result == MOUNT_SUCCESS)
9d2f5178 827 m->result = f;
e537352b 828
aac99f30 829 unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result));
29206d46
LP
830 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
831
e8a565cb 832 m->exec_runtime = exec_runtime_unref(m->exec_runtime, true);
613b411c 833
3536f49e 834 exec_context_destroy_runtime_directory(&m->exec_context, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
e66cf1a3 835
00d9ef85
LP
836 unit_unref_uid_gid(UNIT(m), true);
837
29206d46 838 dynamic_creds_destroy(&m->dynamic_creds);
c52c2dc6
ZJS
839
840 /* Any dependencies based on /proc/self/mountinfo are now stale */
841 unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
e537352b
LP
842}
843
9d2f5178 844static void mount_enter_mounted(Mount *m, MountResult f) {
80876c20
LP
845 assert(m);
846
a0fef983 847 if (m->result == MOUNT_SUCCESS)
9d2f5178 848 m->result = f;
80876c20
LP
849
850 mount_set_state(m, MOUNT_MOUNTED);
851}
852
22af0e58
LP
853static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
854 assert(m);
855
856 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
857 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
858 * ultimately we just mirror the kernel's internal state on this. */
859
860 if (m->from_proc_self_mountinfo)
861 mount_enter_mounted(m, f);
862 else
863 mount_enter_dead(m, f);
864}
865
866static int state_to_kill_operation(MountState state) {
867 switch (state) {
868
869 case MOUNT_REMOUNTING_SIGTERM:
870 case MOUNT_UNMOUNTING_SIGTERM:
871 return KILL_TERMINATE;
872
873 case MOUNT_REMOUNTING_SIGKILL:
874 case MOUNT_UNMOUNTING_SIGKILL:
875 return KILL_KILL;
876
877 default:
878 return _KILL_OPERATION_INVALID;
879 }
880}
881
9d2f5178 882static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
e537352b
LP
883 int r;
884
885 assert(m);
886
a0fef983 887 if (m->result == MOUNT_SUCCESS)
9d2f5178 888 m->result = f;
e537352b 889
cd2086fe
LP
890 r = unit_kill_context(
891 UNIT(m),
892 &m->kill_context,
22af0e58 893 state_to_kill_operation(state),
cd2086fe
LP
894 -1,
895 m->control_pid,
896 false);
897 if (r < 0)
898 goto fail;
e537352b 899
cd2086fe 900 if (r > 0) {
36c16a7c 901 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
36697dc0 902 if (r < 0)
80876c20 903 goto fail;
e537352b 904
80876c20 905 mount_set_state(m, state);
22af0e58 906 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 907 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
22af0e58 908 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
9d2f5178 909 mount_enter_mounted(m, MOUNT_SUCCESS);
22af0e58 910 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
ac84d1fb 911 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
80876c20 912 else
22af0e58 913 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
914
915 return;
916
917fail:
f2341e0a 918 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
22af0e58 919 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
5261ba90
TT
920}
921
9d2f5178 922static void mount_enter_unmounting(Mount *m) {
e537352b
LP
923 int r;
924
925 assert(m);
926
7d54a03a
LP
927 /* Start counting our attempts */
928 if (!IN_SET(m->state,
929 MOUNT_UNMOUNTING,
930 MOUNT_UNMOUNTING_SIGTERM,
931 MOUNT_UNMOUNTING_SIGKILL))
932 m->n_retry_umount = 0;
933
a16e1123
LP
934 m->control_command_id = MOUNT_EXEC_UNMOUNT;
935 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b 936
83897d54 937 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
e520950a
BR
938 if (r >= 0 && m->lazy_unmount)
939 r = exec_command_append(m->control_command, "-l", NULL);
4f8d40a9
BR
940 if (r >= 0 && m->force_unmount)
941 r = exec_command_append(m->control_command, "-f", NULL);
7d54a03a 942 if (r < 0)
e537352b
LP
943 goto fail;
944
a16e1123 945 mount_unwatch_control_pid(m);
5e94833f 946
7d54a03a
LP
947 r = mount_spawn(m, m->control_command, &m->control_pid);
948 if (r < 0)
e537352b
LP
949 goto fail;
950
951 mount_set_state(m, MOUNT_UNMOUNTING);
952
953 return;
954
955fail:
f2341e0a 956 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
22af0e58 957 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
958}
959
8d567588 960static void mount_enter_mounting(Mount *m) {
e537352b 961 int r;
cb39ed3f 962 MountParameters *p;
e537352b
LP
963
964 assert(m);
965
25cd4964 966 r = unit_fail_if_noncanonical(UNIT(m), m->where);
f2341e0a
LP
967 if (r < 0)
968 goto fail;
969
970 (void) mkdir_p_label(m->where, m->directory_mode);
3e5235b0 971
f2341e0a 972 unit_warn_if_dir_nonempty(UNIT(m), m->where);
a4634b21
LP
973 unit_warn_leftover_processes(UNIT(m));
974
975 m->control_command_id = MOUNT_EXEC_MOUNT;
976 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
977
cb39ed3f 978 /* Create the source directory for bind-mounts if needed */
6b1dc2bd 979 p = get_mount_parameters_fragment(m);
cb39ed3f 980 if (p && mount_is_bind(p))
f2341e0a 981 (void) mkdir_p_label(p->what, m->directory_mode);
5261ba90 982
b294b79f 983 if (p) {
17a1c597
ZJS
984 _cleanup_free_ char *opts = NULL;
985
b294b79f 986 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
17a1c597
ZJS
987 if (r < 0)
988 goto fail;
989
b294b79f 990 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
e86b3761
ZJS
991 if (r >= 0 && m->sloppy_options)
992 r = exec_command_append(m->control_command, "-s", NULL);
b294b79f
MO
993 if (r >= 0 && p->fstype)
994 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
0c47569a 995 if (r >= 0 && !isempty(opts))
17a1c597 996 r = exec_command_append(m->control_command, "-o", opts, NULL);
e86b3761 997 } else
e537352b 998 r = -ENOENT;
e537352b
LP
999 if (r < 0)
1000 goto fail;
1001
a16e1123 1002 mount_unwatch_control_pid(m);
5e94833f 1003
257f1d8e
LP
1004 r = mount_spawn(m, m->control_command, &m->control_pid);
1005 if (r < 0)
e537352b
LP
1006 goto fail;
1007
1008 mount_set_state(m, MOUNT_MOUNTING);
1009
1010 return;
1011
1012fail:
f2341e0a 1013 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
22af0e58 1014 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
e537352b
LP
1015}
1016
850b7410
LP
1017static void mount_set_reload_result(Mount *m, MountResult result) {
1018 assert(m);
1019
1020 /* Only store the first error we encounter */
1021 if (m->reload_result != MOUNT_SUCCESS)
1022 return;
1023
1024 m->reload_result = result;
1025}
1026
9d2f5178 1027static void mount_enter_remounting(Mount *m) {
e537352b 1028 int r;
b294b79f 1029 MountParameters *p;
e537352b
LP
1030
1031 assert(m);
1032
850b7410
LP
1033 /* Reset reload result when we are about to start a new remount operation */
1034 m->reload_result = MOUNT_SUCCESS;
1035
a16e1123
LP
1036 m->control_command_id = MOUNT_EXEC_REMOUNT;
1037 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b 1038
b294b79f
MO
1039 p = get_mount_parameters_fragment(m);
1040 if (p) {
e537352b
LP
1041 const char *o;
1042
b294b79f
MO
1043 if (p->options)
1044 o = strjoina("remount,", p->options);
718db961 1045 else
e537352b
LP
1046 o = "remount";
1047
f00929ad 1048 r = exec_command_set(m->control_command, MOUNT_PATH,
b294b79f 1049 p->what, m->where,
e86b3761 1050 "-o", o, NULL);
e86b3761
ZJS
1051 if (r >= 0 && m->sloppy_options)
1052 r = exec_command_append(m->control_command, "-s", NULL);
b294b79f
MO
1053 if (r >= 0 && p->fstype)
1054 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
6b1dc2bd 1055 } else
e537352b 1056 r = -ENOENT;
60b912f6 1057 if (r < 0)
e537352b 1058 goto fail;
e537352b 1059
a16e1123 1060 mount_unwatch_control_pid(m);
5e94833f 1061
718db961
LP
1062 r = mount_spawn(m, m->control_command, &m->control_pid);
1063 if (r < 0)
e537352b
LP
1064 goto fail;
1065
1066 mount_set_state(m, MOUNT_REMOUNTING);
1067
1068 return;
1069
1070fail:
f2341e0a 1071 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
850b7410 1072 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
22af0e58 1073 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e537352b
LP
1074}
1075
7eba1463
LP
1076static void mount_cycle_clear(Mount *m) {
1077 assert(m);
1078
1079 /* Clear all state we shall forget for this new cycle */
1080
1081 m->result = MOUNT_SUCCESS;
1082 m->reload_result = MOUNT_SUCCESS;
1083 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
1084 UNIT(m)->reset_accounting = true;
1085}
1086
e537352b
LP
1087static int mount_start(Unit *u) {
1088 Mount *m = MOUNT(u);
07299350 1089 int r;
e537352b
LP
1090
1091 assert(m);
1092
1093 /* We cannot fulfill this request right now, try again later
1094 * please! */
f2aed307
LP
1095 if (IN_SET(m->state,
1096 MOUNT_UNMOUNTING,
1097 MOUNT_UNMOUNTING_SIGTERM,
22af0e58 1098 MOUNT_UNMOUNTING_SIGKILL))
e537352b
LP
1099 return -EAGAIN;
1100
1101 /* Already on it! */
60b912f6 1102 if (m->state == MOUNT_MOUNTING)
e537352b
LP
1103 return 0;
1104
f2aed307 1105 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
e537352b 1106
07299350
LP
1107 r = unit_start_limit_test(u);
1108 if (r < 0) {
1109 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1110 return r;
1111 }
1112
4b58153d
LP
1113 r = unit_acquire_invocation_id(u);
1114 if (r < 0)
1115 return r;
1116
7eba1463 1117 mount_cycle_clear(m);
8d567588 1118 mount_enter_mounting(m);
7eba1463 1119
82a2b6bb 1120 return 1;
e537352b
LP
1121}
1122
1123static int mount_stop(Unit *u) {
1124 Mount *m = MOUNT(u);
1125
1126 assert(m);
1127
22af0e58
LP
1128 switch (m->state) {
1129
1130 case MOUNT_UNMOUNTING:
1131 case MOUNT_UNMOUNTING_SIGKILL:
1132 case MOUNT_UNMOUNTING_SIGTERM:
1133 /* Already on it */
e537352b
LP
1134 return 0;
1135
22af0e58
LP
1136 case MOUNT_MOUNTING:
1137 case MOUNT_MOUNTING_DONE:
1138 case MOUNT_REMOUNTING:
1139 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1140 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1141 return 0;
e537352b 1142
22af0e58
LP
1143 case MOUNT_REMOUNTING_SIGTERM:
1144 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1145 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1146 return 0;
1147
1148 case MOUNT_REMOUNTING_SIGKILL:
1149 /* as above */
1150 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1151 return 0;
1152
1153 case MOUNT_MOUNTED:
1154 mount_enter_unmounting(m);
1155 return 1;
1156
1157 default:
1158 assert_not_reached("Unexpected state.");
1159 }
e537352b
LP
1160}
1161
1162static int mount_reload(Unit *u) {
1163 Mount *m = MOUNT(u);
1164
1165 assert(m);
e537352b
LP
1166 assert(m->state == MOUNT_MOUNTED);
1167
9d2f5178 1168 mount_enter_remounting(m);
22af0e58 1169
2d018ae2 1170 return 1;
e537352b
LP
1171}
1172
a16e1123
LP
1173static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1174 Mount *m = MOUNT(u);
1175
1176 assert(m);
1177 assert(f);
1178 assert(fds);
1179
d68c645b
LP
1180 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1181 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1182 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
2c09fb81 1183 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
a16e1123
LP
1184
1185 if (m->control_pid > 0)
d68c645b 1186 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
a16e1123
LP
1187
1188 if (m->control_command_id >= 0)
d68c645b 1189 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
a16e1123
LP
1190
1191 return 0;
1192}
1193
1194static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1195 Mount *m = MOUNT(u);
2c09fb81 1196 int r;
a16e1123
LP
1197
1198 assert(u);
1199 assert(key);
1200 assert(value);
1201 assert(fds);
1202
1203 if (streq(key, "state")) {
1204 MountState state;
1205
1206 if ((state = mount_state_from_string(value)) < 0)
f2341e0a 1207 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
1208 else
1209 m->deserialized_state = state;
2c09fb81 1210
9d2f5178
LP
1211 } else if (streq(key, "result")) {
1212 MountResult f;
a16e1123 1213
9d2f5178
LP
1214 f = mount_result_from_string(value);
1215 if (f < 0)
f2341e0a 1216 log_unit_debug(u, "Failed to parse result value: %s", value);
9d2f5178
LP
1217 else if (f != MOUNT_SUCCESS)
1218 m->result = f;
1219
1220 } else if (streq(key, "reload-result")) {
1221 MountResult f;
1222
1223 f = mount_result_from_string(value);
1224 if (f < 0)
f2341e0a 1225 log_unit_debug(u, "Failed to parse reload result value: %s", value);
9d2f5178
LP
1226 else if (f != MOUNT_SUCCESS)
1227 m->reload_result = f;
a16e1123 1228
2c09fb81
LP
1229 } else if (streq(key, "n-retry-umount")) {
1230
1231 r = safe_atou(value, &m->n_retry_umount);
1232 if (r < 0)
1233 log_unit_debug(u, "Failed to parse n-retry-umount value: %s", value);
1234
a16e1123 1235 } else if (streq(key, "control-pid")) {
a16e1123 1236
3f459cd9 1237 if (parse_pid(value, &m->control_pid) < 0)
f2341e0a 1238 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
3f459cd9 1239
a16e1123
LP
1240 } else if (streq(key, "control-command")) {
1241 MountExecCommand id;
1242
f2341e0a
LP
1243 id = mount_exec_command_from_string(value);
1244 if (id < 0)
1245 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
1246 else {
1247 m->control_command_id = id;
1248 m->control_command = m->exec_command + id;
1249 }
a16e1123 1250 } else
f2341e0a 1251 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
1252
1253 return 0;
1254}
1255
44a6b1b6 1256_pure_ static UnitActiveState mount_active_state(Unit *u) {
e537352b
LP
1257 assert(u);
1258
1259 return state_translation_table[MOUNT(u)->state];
1260}
1261
44a6b1b6 1262_pure_ static const char *mount_sub_state_to_string(Unit *u) {
10a94420
LP
1263 assert(u);
1264
a16e1123 1265 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1266}
1267
f2f725e5 1268_pure_ static bool mount_may_gc(Unit *u) {
701cc384
LP
1269 Mount *m = MOUNT(u);
1270
1271 assert(m);
1272
f2f725e5
ZJS
1273 if (m->from_proc_self_mountinfo)
1274 return false;
1275
1276 return true;
701cc384
LP
1277}
1278
e537352b
LP
1279static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1280 Mount *m = MOUNT(u);
9d2f5178 1281 MountResult f;
e537352b
LP
1282
1283 assert(m);
1284 assert(pid >= 0);
1285
8c47c732
LP
1286 if (pid != m->control_pid)
1287 return;
e537352b 1288
e537352b
LP
1289 m->control_pid = 0;
1290
1f0958f6 1291 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
9d2f5178
LP
1292 f = MOUNT_SUCCESS;
1293 else if (code == CLD_EXITED)
1294 f = MOUNT_FAILURE_EXIT_CODE;
1295 else if (code == CLD_KILLED)
1296 f = MOUNT_FAILURE_SIGNAL;
1297 else if (code == CLD_DUMPED)
1298 f = MOUNT_FAILURE_CORE_DUMP;
1299 else
1300 assert_not_reached("Unknown code");
1301
850b7410
LP
1302 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1303 mount_set_reload_result(m, f);
1304 else if (m->result == MOUNT_SUCCESS)
9d2f5178 1305 m->result = f;
8c47c732 1306
a16e1123 1307 if (m->control_command) {
6ea832a2 1308 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
9d2f5178 1309
a16e1123
LP
1310 m->control_command = NULL;
1311 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1312 }
1313
91bbd9b7
LP
1314 unit_log_process_exit(
1315 u, f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1316 "Mount process",
1317 mount_exec_command_to_string(m->control_command_id),
1318 code, status);
e537352b 1319
006aabbd
AJ
1320 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1321 * before we process the SIGCHLD for the mount command. */
e537352b
LP
1322
1323 switch (m->state) {
1324
1325 case MOUNT_MOUNTING:
006aabbd 1326 /* Our mount point has not appeared in mountinfo. Something went wrong. */
e537352b 1327
006aabbd
AJ
1328 if (f == MOUNT_SUCCESS) {
1329 /* Either /bin/mount has an unexpected definition of success,
1330 * or someone raced us and we lost. */
1331 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1332 f = MOUNT_FAILURE_PROTOCOL;
1333 }
1334 mount_enter_dead(m, f);
1335 break;
1336
1337 case MOUNT_MOUNTING_DONE:
1338 mount_enter_mounted(m, f);
e537352b
LP
1339 break;
1340
e2f3b44c 1341 case MOUNT_REMOUNTING:
e2f3b44c 1342 case MOUNT_REMOUNTING_SIGTERM:
22af0e58
LP
1343 case MOUNT_REMOUNTING_SIGKILL:
1344 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
e2f3b44c
LP
1345 break;
1346
e537352b 1347 case MOUNT_UNMOUNTING:
e537352b 1348
3cc96856 1349 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
22af0e58
LP
1350
1351 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
57018361
AJ
1352 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1353 * but we will stop as soon as any one umount times out. */
22af0e58
LP
1354
1355 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1356 log_unit_debug(u, "Mount still present, trying again.");
1357 m->n_retry_umount++;
1358 mount_enter_unmounting(m);
1359 } else {
006aabbd 1360 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
22af0e58
LP
1361 mount_enter_mounted(m, f);
1362 }
1363 } else
3cc96856 1364 mount_enter_dead_or_mounted(m, f);
22af0e58 1365
e537352b 1366 break;
57018361
AJ
1367
1368 case MOUNT_UNMOUNTING_SIGKILL:
1369 case MOUNT_UNMOUNTING_SIGTERM:
1370 mount_enter_dead_or_mounted(m, f);
1371 break;
e537352b
LP
1372
1373 default:
1374 assert_not_reached("Uh, control process died at wrong time.");
1375 }
c4e2ceae
LP
1376
1377 /* Notify clients about changed exit status */
1378 unit_add_to_dbus_queue(u);
e537352b
LP
1379}
1380
718db961
LP
1381static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1382 Mount *m = MOUNT(userdata);
e537352b
LP
1383
1384 assert(m);
718db961 1385 assert(m->timer_event_source == source);
e537352b
LP
1386
1387 switch (m->state) {
1388
1389 case MOUNT_MOUNTING:
1390 case MOUNT_MOUNTING_DONE:
22af0e58
LP
1391 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1392 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1393 break;
1394
1395 case MOUNT_REMOUNTING:
79aafbd1 1396 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
22af0e58
LP
1397 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1398 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
e537352b
LP
1399 break;
1400
22af0e58
LP
1401 case MOUNT_REMOUNTING_SIGTERM:
1402 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
e537352b 1403
4819ff03 1404 if (m->kill_context.send_sigkill) {
22af0e58
LP
1405 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1406 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
ba035df2 1407 } else {
22af0e58
LP
1408 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1409 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
ba035df2 1410 }
e537352b
LP
1411 break;
1412
22af0e58
LP
1413 case MOUNT_REMOUNTING_SIGKILL:
1414 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1415
22af0e58
LP
1416 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1417 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1418 break;
1419
1420 case MOUNT_UNMOUNTING:
79aafbd1 1421 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
22af0e58 1422 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1423 break;
1424
1425 case MOUNT_UNMOUNTING_SIGTERM:
4819ff03 1426 if (m->kill_context.send_sigkill) {
22af0e58 1427 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
9d2f5178 1428 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
ba035df2 1429 } else {
22af0e58
LP
1430 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1431 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
ba035df2 1432 }
e537352b
LP
1433 break;
1434
e537352b 1435 case MOUNT_UNMOUNTING_SIGKILL:
22af0e58
LP
1436 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1437 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
e537352b
LP
1438 break;
1439
1440 default:
1441 assert_not_reached("Timeout at wrong time.");
1442 }
718db961
LP
1443
1444 return 0;
e537352b
LP
1445}
1446
03b8cfed 1447static int mount_setup_new_unit(
839ee058
LP
1448 Manager *m,
1449 const char *name,
03b8cfed
FB
1450 const char *what,
1451 const char *where,
1452 const char *options,
1453 const char *fstype,
ec88d1ea 1454 MountProcFlags *ret_flags,
839ee058 1455 Unit **ret) {
03b8cfed 1456
839ee058 1457 _cleanup_(unit_freep) Unit *u = NULL;
bbee24bc 1458 int r;
03b8cfed 1459
839ee058
LP
1460 assert(m);
1461 assert(name);
ec88d1ea 1462 assert(ret_flags);
839ee058
LP
1463 assert(ret);
1464
1465 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1466 if (r < 0)
1467 return r;
03b8cfed 1468
e10fe042
LP
1469 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1470 if (r < 0)
1471 return r;
03b8cfed 1472
e10fe042
LP
1473 r = free_and_strdup(&MOUNT(u)->where, where);
1474 if (r < 0)
1475 return r;
03b8cfed 1476
bbee24bc
LP
1477 r = update_parameters_proc_self_mount_info(MOUNT(u), what, options, fstype);
1478 if (r < 0)
1479 return r;
03b8cfed 1480
6d7e89b0
LP
1481 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1482 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1483 * to.*/
1484 MOUNT(u)->from_proc_self_mountinfo = true;
1485
1486 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1487 * loaded in now. */
cfcd4318 1488 unit_add_to_load_queue(u);
26e35b16 1489
ec88d1ea 1490 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
839ee058 1491 *ret = TAKE_PTR(u);
03b8cfed
FB
1492 return 0;
1493}
1494
1495static int mount_setup_existing_unit(
1496 Unit *u,
1497 const char *what,
1498 const char *where,
1499 const char *options,
1500 const char *fstype,
ec88d1ea 1501 MountProcFlags *ret_flags) {
03b8cfed 1502
ec88d1ea 1503 MountProcFlags flags = MOUNT_PROC_IS_MOUNTED;
bbee24bc 1504 int r;
03b8cfed
FB
1505
1506 assert(u);
1507 assert(flags);
1508
1509 if (!MOUNT(u)->where) {
1510 MOUNT(u)->where = strdup(where);
1511 if (!MOUNT(u)->where)
1512 return -ENOMEM;
1513 }
1514
bbee24bc
LP
1515 r = update_parameters_proc_self_mount_info(MOUNT(u), what, options, fstype);
1516 if (r < 0)
1517 return r;
ec88d1ea
LP
1518 if (r > 0)
1519 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed 1520
d253a45e 1521 if (!MOUNT(u)->from_proc_self_mountinfo || FLAGS_SET(MOUNT(u)->proc_flags, MOUNT_PROC_JUST_MOUNTED))
ec88d1ea 1522 flags |= MOUNT_PROC_JUST_MOUNTED;
d253a45e
YW
1523
1524 MOUNT(u)->from_proc_self_mountinfo = true;
03b8cfed 1525
f8064c4f
LP
1526 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1527 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1528 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
03b8cfed
FB
1529 u->load_state = UNIT_LOADED;
1530 u->load_error = 0;
1531
ec88d1ea 1532 flags |= MOUNT_PROC_JUST_CHANGED;
03b8cfed
FB
1533 }
1534
ec88d1ea 1535 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
a3742204
LP
1536 /* If things changed, then make sure that all deps are regenerated. Let's
1537 * first remove all automatic deps, and then add in the new ones. */
1538
1539 unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1540
1541 r = mount_add_extras(MOUNT(u));
1542 if (r < 0)
1543 return r;
1544 }
03b8cfed 1545
ec88d1ea 1546 *ret_flags = flags;
03b8cfed
FB
1547 return 0;
1548}
1549
628c89cc 1550static int mount_setup_unit(
e537352b
LP
1551 Manager *m,
1552 const char *what,
1553 const char *where,
1554 const char *options,
1555 const char *fstype,
e537352b 1556 bool set_flags) {
057d9ab8 1557
03b8cfed 1558 _cleanup_free_ char *e = NULL;
ec88d1ea 1559 MountProcFlags flags;
057d9ab8
LP
1560 Unit *u;
1561 int r;
b08d03ff 1562
f50e0a01 1563 assert(m);
b08d03ff
LP
1564 assert(what);
1565 assert(where);
e537352b
LP
1566 assert(options);
1567 assert(fstype);
1568
e537352b
LP
1569 /* Ignore API mount points. They should never be referenced in
1570 * dependencies ever. */
33ff02c9 1571 if (mount_point_is_api(where) || mount_point_ignore(where))
57f2a956 1572 return 0;
b08d03ff 1573
8d567588
LP
1574 if (streq(fstype, "autofs"))
1575 return 0;
1576
4e85aff4
LP
1577 /* probably some kind of swap, ignore */
1578 if (!is_path(where))
b08d03ff
LP
1579 return 0;
1580
7410616c
LP
1581 r = unit_name_from_path(where, ".mount", &e);
1582 if (r < 0)
60b8c203 1583 return log_error_errno(r, "Failed to generate unit name from path '%s': %m", where);
b08d03ff 1584
7d17cfbc 1585 u = manager_get_unit(m, e);
839ee058 1586 if (u)
03b8cfed 1587 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
839ee058
LP
1588 else
1589 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1590 * by the sysadmin having called mount(8) directly. */
1591 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
03b8cfed 1592 if (r < 0)
60b8c203 1593 return log_warning_errno(r, "Failed to set up mount unit: %m");
ff5f34d0 1594
ec88d1ea
LP
1595 /* If the mount changed properties or state, let's notify our clients */
1596 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
ff5f34d0 1597 unit_add_to_dbus_queue(u);
c1e1601e 1598
ec88d1ea
LP
1599 if (set_flags)
1600 MOUNT(u)->proc_flags = flags;
1601
b08d03ff 1602 return 0;
b08d03ff
LP
1603}
1604
ef734fd6 1605static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
628c89cc
LP
1606 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
1607 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
ba0d56f5 1608 int r;
b08d03ff
LP
1609
1610 assert(m);
1611
628c89cc 1612 t = mnt_new_table();
628c89cc 1613 i = mnt_new_iter(MNT_ITER_FORWARD);
95b862b0 1614 if (!t || !i)
628c89cc
LP
1615 return log_oom();
1616
1617 r = mnt_table_parse_mtab(t, NULL);
5cca8def 1618 if (r < 0)
628c89cc 1619 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
e537352b 1620
5cca8def 1621 for (;;) {
95b862b0 1622 struct libmnt_fs *fs;
8d3ae2bd 1623 const char *device, *path, *options, *fstype;
527b7a42 1624 _cleanup_free_ char *d = NULL, *p = NULL;
8d3ae2bd 1625 int k;
b08d03ff 1626
628c89cc 1627 k = mnt_table_next_fs(t, i, &fs);
5cca8def
ZJS
1628 if (k == 1)
1629 break;
628c89cc
LP
1630 if (k < 0)
1631 return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
5cca8def 1632
8d3ae2bd
CL
1633 device = mnt_fs_get_source(fs);
1634 path = mnt_fs_get_target(fs);
1635 options = mnt_fs_get_options(fs);
1636 fstype = mnt_fs_get_fstype(fs);
a2e0f3d3 1637
c0a7f8d3
DM
1638 if (!device || !path)
1639 continue;
1640
527b7a42 1641 if (cunescape(device, UNESCAPE_RELAX, &d) < 0)
628c89cc
LP
1642 return log_oom();
1643
527b7a42 1644 if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
a57f7e2c 1645 return log_oom();
b08d03ff 1646
485ae697 1647 device_found_node(m, d, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
628c89cc 1648
ba0d56f5 1649 (void) mount_setup_unit(m, d, p, options, fstype, set_flags);
b08d03ff
LP
1650 }
1651
ba0d56f5 1652 return 0;
e537352b
LP
1653}
1654
1655static void mount_shutdown(Manager *m) {
1656 assert(m);
1657
718db961
LP
1658 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1659
d379d442
KZ
1660 mnt_unref_monitor(m->mount_monitor);
1661 m->mount_monitor = NULL;
b08d03ff
LP
1662}
1663
7a7821c8 1664static int mount_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1665 Mount *m = MOUNT(u);
7a7821c8 1666 usec_t t;
68db7a3b
ZJS
1667 int r;
1668
1669 if (!m->timer_event_source)
1670 return 0;
1671
7a7821c8 1672 r = sd_event_source_get_time(m->timer_event_source, &t);
68db7a3b
ZJS
1673 if (r < 0)
1674 return r;
7a7821c8
LP
1675 if (t == USEC_INFINITY)
1676 return 0;
68db7a3b 1677
7a7821c8 1678 *timeout = t;
68db7a3b
ZJS
1679 return 1;
1680}
1681
04eb582a 1682static void mount_enumerate_perpetual(Manager *m) {
11222d0f
LP
1683 Unit *u;
1684 int r;
1685
1686 assert(m);
1687
1688 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1689 * unconditionally synthesize it here and mark it as perpetual. */
1690
1691 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1692 if (!u) {
a581e45a 1693 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
04eb582a
LP
1694 if (r < 0) {
1695 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1696 return;
1697 }
11222d0f
LP
1698 }
1699
1700 u->perpetual = true;
1701 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1702
1703 unit_add_to_load_queue(u);
1704 unit_add_to_dbus_queue(u);
1705}
1706
1707static bool mount_is_mounted(Mount *m) {
1708 assert(m);
1709
ec88d1ea 1710 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
11222d0f
LP
1711}
1712
ba64af90 1713static void mount_enumerate(Manager *m) {
b08d03ff 1714 int r;
d379d442 1715
b08d03ff
LP
1716 assert(m);
1717
8d3ae2bd
CL
1718 mnt_init_debug(0);
1719
d379d442
KZ
1720 if (!m->mount_monitor) {
1721 int fd;
ef734fd6 1722
d379d442
KZ
1723 m->mount_monitor = mnt_new_monitor();
1724 if (!m->mount_monitor) {
ba64af90 1725 log_oom();
718db961 1726 goto fail;
d379d442 1727 }
29083707 1728
d379d442 1729 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
ba64af90
LP
1730 if (r < 0) {
1731 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
29083707 1732 goto fail;
ba64af90
LP
1733 }
1734
d379d442 1735 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
ba64af90
LP
1736 if (r < 0) {
1737 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f7c1ad4f 1738 goto fail;
ba64af90 1739 }
90598531 1740
d379d442
KZ
1741 /* mnt_unref_monitor() will close the fd */
1742 fd = r = mnt_monitor_get_fd(m->mount_monitor);
ba64af90
LP
1743 if (r < 0) {
1744 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f7c1ad4f 1745 goto fail;
ba64af90 1746 }
befb6d54 1747
d379d442 1748 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
ba64af90
LP
1749 if (r < 0) {
1750 log_error_errno(r, "Failed to watch mount file descriptor: %m");
befb6d54 1751 goto fail;
ba64af90 1752 }
befb6d54 1753
83231637 1754 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1755 if (r < 0) {
1756 log_error_errno(r, "Failed to adjust mount watch priority: %m");
befb6d54 1757 goto fail;
ba64af90 1758 }
7dfbe2e3 1759
d379d442 1760 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
befb6d54
CL
1761 }
1762
e62d8c39
ZJS
1763 r = mount_load_proc_self_mountinfo(m, false);
1764 if (r < 0)
b08d03ff
LP
1765 goto fail;
1766
ba64af90 1767 return;
b08d03ff
LP
1768
1769fail:
1770 mount_shutdown(m);
5cb5a6ff
LP
1771}
1772
718db961 1773static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
ec8126d7 1774 _cleanup_set_free_free_ Set *around = NULL, *gone = NULL;
718db961 1775 Manager *m = userdata;
ec8126d7
ZJS
1776 const char *what;
1777 Iterator i;
1778 Unit *u;
ef734fd6
LP
1779 int r;
1780
1781 assert(m);
d379d442 1782 assert(revents & EPOLLIN);
ef734fd6 1783
d379d442 1784 if (fd == mnt_monitor_get_fd(m->mount_monitor)) {
df63dda6 1785 bool rescan = false;
befb6d54 1786
d379d442
KZ
1787 /* Drain all events and verify that the event is valid.
1788 *
1789 * Note that libmount also monitors /run/mount mkdir if the
1790 * directory does not exist yet. The mkdir may generate event
1791 * which is irrelevant for us.
1792 *
1793 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1794 do {
1795 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1796 if (r == 0)
1797 rescan = true;
1798 else if (r < 0)
5e1ee764 1799 return log_error_errno(r, "Failed to drain libmount events: %m");
d379d442
KZ
1800 } while (r == 0);
1801
1802 log_debug("libmount event [rescan: %s]", yes_no(rescan));
befb6d54
CL
1803 if (!rescan)
1804 return 0;
1805 }
ef734fd6 1806
4f0eedb7
ZJS
1807 r = mount_load_proc_self_mountinfo(m, true);
1808 if (r < 0) {
e537352b 1809 /* Reset flags, just in case, for later calls */
ec88d1ea
LP
1810 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
1811 MOUNT(u)->proc_flags = 0;
e537352b 1812
ec8126d7 1813 return 0;
ef734fd6
LP
1814 }
1815
1816 manager_dispatch_load_queue(m);
1817
595ed347
MS
1818 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1819 Mount *mount = MOUNT(u);
ef734fd6 1820
11222d0f 1821 if (!mount_is_mounted(mount)) {
e537352b 1822
394763f6
LP
1823 /* A mount point is not around right now. It
1824 * might be gone, or might never have
1825 * existed. */
1826
1827 if (mount->from_proc_self_mountinfo &&
1828 mount->parameters_proc_self_mountinfo.what) {
1829
1830 /* Remember that this device might just have disappeared */
548f6937 1831 if (set_ensure_allocated(&gone, &path_hash_ops) < 0 ||
b6418dc9 1832 set_put_strdup(gone, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6
LP
1833 log_oom(); /* we don't care too much about OOM here... */
1834 }
fcd8b266 1835
ef734fd6 1836 mount->from_proc_self_mountinfo = false;
c52c2dc6 1837 assert_se(update_parameters_proc_self_mount_info(mount, NULL, NULL, NULL) >= 0);
e537352b
LP
1838
1839 switch (mount->state) {
1840
1841 case MOUNT_MOUNTED:
7eba1463 1842 /* This has just been unmounted by somebody else, follow the state change. */
9d2f5178 1843 mount_enter_dead(mount, MOUNT_SUCCESS);
e537352b
LP
1844 break;
1845
1846 default:
e537352b 1847 break;
e537352b
LP
1848 }
1849
ec88d1ea 1850 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
e537352b 1851
fcd8b266 1852 /* A mount point was added or changed */
e537352b
LP
1853
1854 switch (mount->state) {
1855
1856 case MOUNT_DEAD:
fdf20a31 1857 case MOUNT_FAILED:
4b58153d
LP
1858
1859 /* This has just been mounted by somebody else, follow the state change, but let's
1860 * generate a new invocation ID for this implicitly and automatically. */
7eba1463
LP
1861 (void) unit_acquire_invocation_id(u);
1862 mount_cycle_clear(mount);
9d2f5178 1863 mount_enter_mounted(mount, MOUNT_SUCCESS);
e537352b
LP
1864 break;
1865
1866 case MOUNT_MOUNTING:
5bcb0f2b 1867 mount_set_state(mount, MOUNT_MOUNTING_DONE);
e537352b
LP
1868 break;
1869
1870 default:
1871 /* Nothing really changed, but let's
1872 * issue an notification call
1873 * nonetheless, in case somebody is
1874 * waiting for this. (e.g. file system
1875 * ro/rw remounts.) */
1876 mount_set_state(mount, mount->state);
1877 break;
1878 }
394763f6 1879 }
fcd8b266 1880
11222d0f 1881 if (mount_is_mounted(mount) &&
394763f6
LP
1882 mount->from_proc_self_mountinfo &&
1883 mount->parameters_proc_self_mountinfo.what) {
b6418dc9 1884 /* Track devices currently used */
fcd8b266 1885
548f6937 1886 if (set_ensure_allocated(&around, &path_hash_ops) < 0 ||
b6418dc9 1887 set_put_strdup(around, mount->parameters_proc_self_mountinfo.what) < 0)
394763f6 1888 log_oom();
e537352b
LP
1889 }
1890
1891 /* Reset the flags for later calls */
ec88d1ea 1892 mount->proc_flags = 0;
e537352b 1893 }
718db961 1894
fcd8b266
LP
1895 SET_FOREACH(what, gone, i) {
1896 if (set_contains(around, what))
1897 continue;
1898
1899 /* Let the device units know that the device is no longer mounted */
485ae697 1900 device_found_node(m, what, 0, DEVICE_FOUND_MOUNT);
fcd8b266 1901 }
ec8126d7
ZJS
1902
1903 return 0;
e537352b
LP
1904}
1905
fdf20a31 1906static void mount_reset_failed(Unit *u) {
5632e374
LP
1907 Mount *m = MOUNT(u);
1908
1909 assert(m);
1910
fdf20a31 1911 if (m->state == MOUNT_FAILED)
5632e374
LP
1912 mount_set_state(m, MOUNT_DEAD);
1913
9d2f5178
LP
1914 m->result = MOUNT_SUCCESS;
1915 m->reload_result = MOUNT_SUCCESS;
5632e374
LP
1916}
1917
718db961 1918static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
22af0e58
LP
1919 Mount *m = MOUNT(u);
1920
1921 assert(m);
1922
814cc562 1923 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
8a0867d6
LP
1924}
1925
291d565a
LP
1926static int mount_control_pid(Unit *u) {
1927 Mount *m = MOUNT(u);
1928
1929 assert(m);
1930
1931 return m->control_pid;
1932}
1933
a16e1123
LP
1934static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1935 [MOUNT_EXEC_MOUNT] = "ExecMount",
1936 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1937 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1938};
1939
1940DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1941
9d2f5178
LP
1942static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1943 [MOUNT_SUCCESS] = "success",
1944 [MOUNT_FAILURE_RESOURCES] = "resources",
1945 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1946 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1947 [MOUNT_FAILURE_SIGNAL] = "signal",
07299350
LP
1948 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
1949 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
006aabbd 1950 [MOUNT_FAILURE_PROTOCOL] = "protocol",
9d2f5178
LP
1951};
1952
1953DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1954
87f0e418 1955const UnitVTable mount_vtable = {
7d17cfbc 1956 .object_size = sizeof(Mount),
718db961
LP
1957 .exec_context_offset = offsetof(Mount, exec_context),
1958 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1959 .kill_context_offset = offsetof(Mount, kill_context),
613b411c 1960 .exec_runtime_offset = offsetof(Mount, exec_runtime),
29206d46 1961 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
3ef63c31 1962
f975e971
LP
1963 .sections =
1964 "Unit\0"
1965 "Mount\0"
1966 "Install\0",
4ad49000 1967 .private_section = "Mount",
71645aca 1968
e537352b
LP
1969 .init = mount_init,
1970 .load = mount_load,
034c6ed7 1971 .done = mount_done,
e537352b 1972
f50e0a01
LP
1973 .coldplug = mount_coldplug,
1974
034c6ed7 1975 .dump = mount_dump,
5cb5a6ff 1976
e537352b
LP
1977 .start = mount_start,
1978 .stop = mount_stop,
1979 .reload = mount_reload,
1980
8a0867d6
LP
1981 .kill = mount_kill,
1982
a16e1123
LP
1983 .serialize = mount_serialize,
1984 .deserialize_item = mount_deserialize_item,
1985
f50e0a01 1986 .active_state = mount_active_state,
10a94420 1987 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 1988
f2f725e5 1989 .may_gc = mount_may_gc,
701cc384 1990
e537352b 1991 .sigchld_event = mount_sigchld_event,
e537352b 1992
fdf20a31 1993 .reset_failed = mount_reset_failed,
291d565a
LP
1994
1995 .control_pid = mount_control_pid,
5632e374 1996
718db961 1997 .bus_vtable = bus_mount_vtable,
74c964d3
LP
1998 .bus_set_property = bus_mount_set_property,
1999 .bus_commit_properties = bus_mount_commit_properties,
4139c1b2 2000
68db7a3b
ZJS
2001 .get_timeout = mount_get_timeout,
2002
0e252f6b
TG
2003 .can_transient = true,
2004
04eb582a 2005 .enumerate_perpetual = mount_enumerate_perpetual,
f50e0a01 2006 .enumerate = mount_enumerate,
c6918296
MS
2007 .shutdown = mount_shutdown,
2008
2009 .status_message_formats = {
2010 .starting_stopping = {
2011 [0] = "Mounting %s...",
2012 [1] = "Unmounting %s...",
2013 },
2014 .finished_start_job = {
2015 [JOB_DONE] = "Mounted %s.",
2016 [JOB_FAILED] = "Failed to mount %s.",
c6918296
MS
2017 [JOB_TIMEOUT] = "Timed out mounting %s.",
2018 },
2019 .finished_stop_job = {
2020 [JOB_DONE] = "Unmounted %s.",
2021 [JOB_FAILED] = "Failed unmounting %s.",
2022 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2023 },
2024 },
5cb5a6ff 2025};