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