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