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