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