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