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