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