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