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