]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/mount.c
core: also log about left-over processes during unit stop
[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 (void) mkdir_p_label(p->what, m->directory_mode);
1022
1023 if (p) {
1024 _cleanup_free_ char *opts = NULL;
1025
1026 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
1027 if (r < 0)
1028 goto fail;
1029
1030 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
1031 if (r >= 0 && m->sloppy_options)
1032 r = exec_command_append(m->control_command, "-s", NULL);
1033 if (r >= 0 && m->read_write_only)
1034 r = exec_command_append(m->control_command, "-w", NULL);
1035 if (r >= 0 && p->fstype)
1036 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
1037 if (r >= 0 && !isempty(opts))
1038 r = exec_command_append(m->control_command, "-o", opts, NULL);
1039 } else
1040 r = -ENOENT;
1041 if (r < 0)
1042 goto fail;
1043
1044 mount_unwatch_control_pid(m);
1045
1046 r = mount_spawn(m, m->control_command, &m->control_pid);
1047 if (r < 0)
1048 goto fail;
1049
1050 mount_set_state(m, MOUNT_MOUNTING);
1051
1052 return;
1053
1054 fail:
1055 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
1056 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
1057 }
1058
1059 static void mount_set_reload_result(Mount *m, MountResult result) {
1060 assert(m);
1061
1062 /* Only store the first error we encounter */
1063 if (m->reload_result != MOUNT_SUCCESS)
1064 return;
1065
1066 m->reload_result = result;
1067 }
1068
1069 static void mount_enter_remounting(Mount *m) {
1070 int r;
1071 MountParameters *p;
1072
1073 assert(m);
1074
1075 /* Reset reload result when we are about to start a new remount operation */
1076 m->reload_result = MOUNT_SUCCESS;
1077
1078 m->control_command_id = MOUNT_EXEC_REMOUNT;
1079 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1080
1081 p = get_mount_parameters_fragment(m);
1082 if (p) {
1083 const char *o;
1084
1085 if (p->options)
1086 o = strjoina("remount,", p->options);
1087 else
1088 o = "remount";
1089
1090 r = exec_command_set(m->control_command, MOUNT_PATH,
1091 p->what, m->where,
1092 "-o", o, NULL);
1093 if (r >= 0 && m->sloppy_options)
1094 r = exec_command_append(m->control_command, "-s", NULL);
1095 if (r >= 0 && m->read_write_only)
1096 r = exec_command_append(m->control_command, "-w", NULL);
1097 if (r >= 0 && p->fstype)
1098 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
1099 } else
1100 r = -ENOENT;
1101 if (r < 0)
1102 goto fail;
1103
1104 mount_unwatch_control_pid(m);
1105
1106 r = mount_spawn(m, m->control_command, &m->control_pid);
1107 if (r < 0)
1108 goto fail;
1109
1110 mount_set_state(m, MOUNT_REMOUNTING);
1111
1112 return;
1113
1114 fail:
1115 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
1116 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
1117 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1118 }
1119
1120 static void mount_cycle_clear(Mount *m) {
1121 assert(m);
1122
1123 /* Clear all state we shall forget for this new cycle */
1124
1125 m->result = MOUNT_SUCCESS;
1126 m->reload_result = MOUNT_SUCCESS;
1127 exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
1128 UNIT(m)->reset_accounting = true;
1129 }
1130
1131 static int mount_start(Unit *u) {
1132 Mount *m = MOUNT(u);
1133 int r;
1134
1135 assert(m);
1136
1137 /* We cannot fulfill this request right now, try again later
1138 * please! */
1139 if (IN_SET(m->state,
1140 MOUNT_UNMOUNTING,
1141 MOUNT_UNMOUNTING_SIGTERM,
1142 MOUNT_UNMOUNTING_SIGKILL,
1143 MOUNT_CLEANING))
1144 return -EAGAIN;
1145
1146 /* Already on it! */
1147 if (m->state == MOUNT_MOUNTING)
1148 return 0;
1149
1150 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
1151
1152 r = unit_test_start_limit(u);
1153 if (r < 0) {
1154 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1155 return r;
1156 }
1157
1158 r = unit_acquire_invocation_id(u);
1159 if (r < 0)
1160 return r;
1161
1162 mount_cycle_clear(m);
1163 mount_enter_mounting(m);
1164
1165 return 1;
1166 }
1167
1168 static int mount_stop(Unit *u) {
1169 Mount *m = MOUNT(u);
1170
1171 assert(m);
1172
1173 switch (m->state) {
1174
1175 case MOUNT_UNMOUNTING:
1176 case MOUNT_UNMOUNTING_SIGKILL:
1177 case MOUNT_UNMOUNTING_SIGTERM:
1178 /* Already on it */
1179 return 0;
1180
1181 case MOUNT_MOUNTING:
1182 case MOUNT_MOUNTING_DONE:
1183 case MOUNT_REMOUNTING:
1184 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1185 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1186 return 0;
1187
1188 case MOUNT_REMOUNTING_SIGTERM:
1189 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1190 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1191 return 0;
1192
1193 case MOUNT_REMOUNTING_SIGKILL:
1194 /* as above */
1195 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1196 return 0;
1197
1198 case MOUNT_MOUNTED:
1199 mount_enter_unmounting(m);
1200 return 1;
1201
1202 case MOUNT_CLEANING:
1203 /* If we are currently cleaning, then abort it, brutally. */
1204 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
1205 return 0;
1206
1207 default:
1208 assert_not_reached("Unexpected state.");
1209 }
1210 }
1211
1212 static int mount_reload(Unit *u) {
1213 Mount *m = MOUNT(u);
1214
1215 assert(m);
1216 assert(m->state == MOUNT_MOUNTED);
1217
1218 mount_enter_remounting(m);
1219
1220 return 1;
1221 }
1222
1223 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1224 Mount *m = MOUNT(u);
1225
1226 assert(m);
1227 assert(f);
1228 assert(fds);
1229
1230 (void) serialize_item(f, "state", mount_state_to_string(m->state));
1231 (void) serialize_item(f, "result", mount_result_to_string(m->result));
1232 (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result));
1233 (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount);
1234
1235 if (m->control_pid > 0)
1236 (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid);
1237
1238 if (m->control_command_id >= 0)
1239 (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id));
1240
1241 return 0;
1242 }
1243
1244 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1245 Mount *m = MOUNT(u);
1246 int r;
1247
1248 assert(u);
1249 assert(key);
1250 assert(value);
1251 assert(fds);
1252
1253 if (streq(key, "state")) {
1254 MountState state;
1255
1256 if ((state = mount_state_from_string(value)) < 0)
1257 log_unit_debug(u, "Failed to parse state value: %s", value);
1258 else
1259 m->deserialized_state = state;
1260
1261 } else if (streq(key, "result")) {
1262 MountResult f;
1263
1264 f = mount_result_from_string(value);
1265 if (f < 0)
1266 log_unit_debug(u, "Failed to parse result value: %s", value);
1267 else if (f != MOUNT_SUCCESS)
1268 m->result = f;
1269
1270 } else if (streq(key, "reload-result")) {
1271 MountResult f;
1272
1273 f = mount_result_from_string(value);
1274 if (f < 0)
1275 log_unit_debug(u, "Failed to parse reload result value: %s", value);
1276 else if (f != MOUNT_SUCCESS)
1277 m->reload_result = f;
1278
1279 } else if (streq(key, "n-retry-umount")) {
1280
1281 r = safe_atou(value, &m->n_retry_umount);
1282 if (r < 0)
1283 log_unit_debug(u, "Failed to parse n-retry-umount value: %s", value);
1284
1285 } else if (streq(key, "control-pid")) {
1286
1287 if (parse_pid(value, &m->control_pid) < 0)
1288 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
1289
1290 } else if (streq(key, "control-command")) {
1291 MountExecCommand id;
1292
1293 id = mount_exec_command_from_string(value);
1294 if (id < 0)
1295 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
1296 else {
1297 m->control_command_id = id;
1298 m->control_command = m->exec_command + id;
1299 }
1300 } else
1301 log_unit_debug(u, "Unknown serialization key: %s", key);
1302
1303 return 0;
1304 }
1305
1306 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1307 assert(u);
1308
1309 return state_translation_table[MOUNT(u)->state];
1310 }
1311
1312 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1313 assert(u);
1314
1315 return mount_state_to_string(MOUNT(u)->state);
1316 }
1317
1318 _pure_ static bool mount_may_gc(Unit *u) {
1319 Mount *m = MOUNT(u);
1320
1321 assert(m);
1322
1323 if (m->from_proc_self_mountinfo)
1324 return false;
1325
1326 return true;
1327 }
1328
1329 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1330 Mount *m = MOUNT(u);
1331 MountResult f;
1332
1333 assert(m);
1334 assert(pid >= 0);
1335
1336 if (pid != m->control_pid)
1337 return;
1338
1339 /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
1340 * they established/remove a mount. This is important when mounting, but even more so when unmounting
1341 * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
1342 * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
1343 * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
1344 * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
1345 * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
1346 * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
1347 * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
1348 * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
1349 * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
1350 * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
1351 * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
1352 * /proc/self/mountinfo changes before our mount/umount exits. */
1353 (void) mount_process_proc_self_mountinfo(u->manager);
1354
1355 m->control_pid = 0;
1356
1357 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1358 f = MOUNT_SUCCESS;
1359 else if (code == CLD_EXITED)
1360 f = MOUNT_FAILURE_EXIT_CODE;
1361 else if (code == CLD_KILLED)
1362 f = MOUNT_FAILURE_SIGNAL;
1363 else if (code == CLD_DUMPED)
1364 f = MOUNT_FAILURE_CORE_DUMP;
1365 else
1366 assert_not_reached("Unknown code");
1367
1368 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1369 mount_set_reload_result(m, f);
1370 else if (m->result == MOUNT_SUCCESS)
1371 m->result = f;
1372
1373 if (m->control_command) {
1374 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1375
1376 m->control_command = NULL;
1377 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1378 }
1379
1380 unit_log_process_exit(
1381 u,
1382 "Mount process",
1383 mount_exec_command_to_string(m->control_command_id),
1384 f == MOUNT_SUCCESS,
1385 code, status);
1386
1387 /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded
1388 * before we process the SIGCHLD for the mount command. */
1389
1390 switch (m->state) {
1391
1392 case MOUNT_MOUNTING:
1393 /* Our mount point has not appeared in mountinfo. Something went wrong. */
1394
1395 if (f == MOUNT_SUCCESS) {
1396 /* Either /bin/mount has an unexpected definition of success,
1397 * or someone raced us and we lost. */
1398 log_unit_warning(UNIT(m), "Mount process finished, but there is no mount.");
1399 f = MOUNT_FAILURE_PROTOCOL;
1400 }
1401 mount_enter_dead(m, f);
1402 break;
1403
1404 case MOUNT_MOUNTING_DONE:
1405 mount_enter_mounted(m, f);
1406 break;
1407
1408 case MOUNT_REMOUNTING:
1409 case MOUNT_REMOUNTING_SIGTERM:
1410 case MOUNT_REMOUNTING_SIGKILL:
1411 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1412 break;
1413
1414 case MOUNT_UNMOUNTING:
1415
1416 if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) {
1417
1418 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1419 * stacked on top of each other. We might exceed the timeout specified by the user overall,
1420 * but we will stop as soon as any one umount times out. */
1421
1422 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1423 log_unit_debug(u, "Mount still present, trying again.");
1424 m->n_retry_umount++;
1425 mount_enter_unmounting(m);
1426 } else {
1427 log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
1428 mount_enter_mounted(m, f);
1429 }
1430 } else
1431 mount_enter_dead_or_mounted(m, f);
1432
1433 break;
1434
1435 case MOUNT_UNMOUNTING_SIGKILL:
1436 case MOUNT_UNMOUNTING_SIGTERM:
1437 mount_enter_dead_or_mounted(m, f);
1438 break;
1439
1440 case MOUNT_CLEANING:
1441 if (m->clean_result == MOUNT_SUCCESS)
1442 m->clean_result = f;
1443
1444 mount_enter_dead(m, MOUNT_SUCCESS);
1445 break;
1446
1447 default:
1448 assert_not_reached("Uh, control process died at wrong time.");
1449 }
1450
1451 /* Notify clients about changed exit status */
1452 unit_add_to_dbus_queue(u);
1453 }
1454
1455 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1456 Mount *m = MOUNT(userdata);
1457
1458 assert(m);
1459 assert(m->timer_event_source == source);
1460
1461 switch (m->state) {
1462
1463 case MOUNT_MOUNTING:
1464 case MOUNT_MOUNTING_DONE:
1465 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1466 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1467 break;
1468
1469 case MOUNT_REMOUNTING:
1470 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
1471 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1472 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
1473 break;
1474
1475 case MOUNT_REMOUNTING_SIGTERM:
1476 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1477
1478 if (m->kill_context.send_sigkill) {
1479 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1480 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
1481 } else {
1482 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1483 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1484 }
1485 break;
1486
1487 case MOUNT_REMOUNTING_SIGKILL:
1488 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1489
1490 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1491 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1492 break;
1493
1494 case MOUNT_UNMOUNTING:
1495 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
1496 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1497 break;
1498
1499 case MOUNT_UNMOUNTING_SIGTERM:
1500 if (m->kill_context.send_sigkill) {
1501 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
1502 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1503 } else {
1504 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1505 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
1506 }
1507 break;
1508
1509 case MOUNT_UNMOUNTING_SIGKILL:
1510 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1511 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
1512 break;
1513
1514 case MOUNT_CLEANING:
1515 log_unit_warning(UNIT(m), "Cleaning timed out. killing.");
1516
1517 if (m->clean_result == MOUNT_SUCCESS)
1518 m->clean_result = MOUNT_FAILURE_TIMEOUT;
1519
1520 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0);
1521 break;
1522
1523 default:
1524 assert_not_reached("Timeout at wrong time.");
1525 }
1526
1527 return 0;
1528 }
1529
1530 static int mount_setup_new_unit(
1531 Manager *m,
1532 const char *name,
1533 const char *what,
1534 const char *where,
1535 const char *options,
1536 const char *fstype,
1537 MountProcFlags *ret_flags,
1538 Unit **ret) {
1539
1540 _cleanup_(unit_freep) Unit *u = NULL;
1541 int r;
1542
1543 assert(m);
1544 assert(name);
1545 assert(ret_flags);
1546 assert(ret);
1547
1548 r = unit_new_for_name(m, sizeof(Mount), name, &u);
1549 if (r < 0)
1550 return r;
1551
1552 r = free_and_strdup(&u->source_path, "/proc/self/mountinfo");
1553 if (r < 0)
1554 return r;
1555
1556 r = free_and_strdup(&MOUNT(u)->where, where);
1557 if (r < 0)
1558 return r;
1559
1560 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
1561 if (r < 0)
1562 return r;
1563
1564 /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load
1565 * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps
1566 * to.*/
1567 MOUNT(u)->from_proc_self_mountinfo = true;
1568
1569 /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is
1570 * loaded in now. */
1571 unit_add_to_load_queue(u);
1572
1573 *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED;
1574 *ret = TAKE_PTR(u);
1575 return 0;
1576 }
1577
1578 static int mount_setup_existing_unit(
1579 Unit *u,
1580 const char *what,
1581 const char *where,
1582 const char *options,
1583 const char *fstype,
1584 MountProcFlags *ret_flags) {
1585
1586 int r;
1587
1588 assert(u);
1589 assert(ret_flags);
1590
1591 if (!MOUNT(u)->where) {
1592 MOUNT(u)->where = strdup(where);
1593 if (!MOUNT(u)->where)
1594 return -ENOMEM;
1595 }
1596
1597 /* In case we have multiple mounts established on the same mount point, let's merge flags set already
1598 * for the current unit. Note that the flags field is reset on each iteration of reading
1599 * /proc/self/mountinfo, hence we know for sure anything already set here is from the current
1600 * iteration and thus worthy of taking into account. */
1601 MountProcFlags flags =
1602 MOUNT(u)->proc_flags | MOUNT_PROC_IS_MOUNTED;
1603
1604 r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype);
1605 if (r < 0)
1606 return r;
1607 if (r > 0)
1608 flags |= MOUNT_PROC_JUST_CHANGED;
1609
1610 /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in
1611 * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the
1612 * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then
1613 * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems
1614 * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is
1615 * reached when we wait for the mount to appear we hence can assume that if we are in it, we are
1616 * actually seeing it established for the first time. */
1617 if (!MOUNT(u)->from_proc_self_mountinfo || MOUNT(u)->state == MOUNT_MOUNTING)
1618 flags |= MOUNT_PROC_JUST_MOUNTED;
1619
1620 MOUNT(u)->from_proc_self_mountinfo = true;
1621
1622 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
1623 /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in
1624 * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */
1625 u->load_state = UNIT_LOADED;
1626 u->load_error = 0;
1627
1628 flags |= MOUNT_PROC_JUST_CHANGED;
1629 }
1630
1631 if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) {
1632 /* If things changed, then make sure that all deps are regenerated. Let's
1633 * first remove all automatic deps, and then add in the new ones. */
1634
1635 unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1636
1637 r = mount_add_non_exec_dependencies(MOUNT(u));
1638 if (r < 0)
1639 return r;
1640 }
1641
1642 *ret_flags = flags;
1643 return 0;
1644 }
1645
1646 static int mount_setup_unit(
1647 Manager *m,
1648 const char *what,
1649 const char *where,
1650 const char *options,
1651 const char *fstype,
1652 bool set_flags) {
1653
1654 _cleanup_free_ char *e = NULL;
1655 MountProcFlags flags;
1656 Unit *u;
1657 int r;
1658
1659 assert(m);
1660 assert(what);
1661 assert(where);
1662 assert(options);
1663 assert(fstype);
1664
1665 /* Ignore API mount points. They should never be referenced in
1666 * dependencies ever. */
1667 if (mount_point_is_api(where) || mount_point_ignore(where))
1668 return 0;
1669
1670 if (streq(fstype, "autofs"))
1671 return 0;
1672
1673 /* probably some kind of swap, ignore */
1674 if (!is_path(where))
1675 return 0;
1676
1677 r = unit_name_from_path(where, ".mount", &e);
1678 if (r < 0)
1679 return log_error_errno(r, "Failed to generate unit name from path '%s': %m", where);
1680
1681 u = manager_get_unit(m, e);
1682 if (u)
1683 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
1684 else
1685 /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather
1686 * by the sysadmin having called mount(8) directly. */
1687 r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u);
1688 if (r < 0)
1689 return log_warning_errno(r, "Failed to set up mount unit: %m");
1690
1691 /* If the mount changed properties or state, let's notify our clients */
1692 if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED))
1693 unit_add_to_dbus_queue(u);
1694
1695 if (set_flags)
1696 MOUNT(u)->proc_flags = flags;
1697
1698 return 0;
1699 }
1700
1701 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1702 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
1703 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
1704 int r;
1705
1706 assert(m);
1707
1708 r = libmount_parse(NULL, NULL, &table, &iter);
1709 if (r < 0)
1710 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
1711
1712 for (;;) {
1713 struct libmnt_fs *fs;
1714 const char *device, *path, *options, *fstype;
1715
1716 r = mnt_table_next_fs(table, iter, &fs);
1717 if (r == 1)
1718 break;
1719 if (r < 0)
1720 return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
1721
1722 device = mnt_fs_get_source(fs);
1723 path = mnt_fs_get_target(fs);
1724 options = mnt_fs_get_options(fs);
1725 fstype = mnt_fs_get_fstype(fs);
1726
1727 if (!device || !path)
1728 continue;
1729
1730 device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT);
1731
1732 (void) mount_setup_unit(m, device, path, options, fstype, set_flags);
1733 }
1734
1735 return 0;
1736 }
1737
1738 static void mount_shutdown(Manager *m) {
1739 assert(m);
1740
1741 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1742
1743 mnt_unref_monitor(m->mount_monitor);
1744 m->mount_monitor = NULL;
1745 }
1746
1747 static int mount_get_timeout(Unit *u, usec_t *timeout) {
1748 Mount *m = MOUNT(u);
1749 usec_t t;
1750 int r;
1751
1752 if (!m->timer_event_source)
1753 return 0;
1754
1755 r = sd_event_source_get_time(m->timer_event_source, &t);
1756 if (r < 0)
1757 return r;
1758 if (t == USEC_INFINITY)
1759 return 0;
1760
1761 *timeout = t;
1762 return 1;
1763 }
1764
1765 static void mount_enumerate_perpetual(Manager *m) {
1766 Unit *u;
1767 int r;
1768
1769 assert(m);
1770
1771 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1772 * unconditionally synthesize it here and mark it as perpetual. */
1773
1774 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1775 if (!u) {
1776 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
1777 if (r < 0) {
1778 log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1779 return;
1780 }
1781 }
1782
1783 u->perpetual = true;
1784 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1785
1786 unit_add_to_load_queue(u);
1787 unit_add_to_dbus_queue(u);
1788 }
1789
1790 static bool mount_is_mounted(Mount *m) {
1791 assert(m);
1792
1793 return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED);
1794 }
1795
1796 static void mount_enumerate(Manager *m) {
1797 int r;
1798
1799 assert(m);
1800
1801 mnt_init_debug(0);
1802
1803 if (!m->mount_monitor) {
1804 int fd;
1805
1806 m->mount_monitor = mnt_new_monitor();
1807 if (!m->mount_monitor) {
1808 log_oom();
1809 goto fail;
1810 }
1811
1812 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
1813 if (r < 0) {
1814 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
1815 goto fail;
1816 }
1817
1818 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
1819 if (r < 0) {
1820 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
1821 goto fail;
1822 }
1823
1824 /* mnt_unref_monitor() will close the fd */
1825 fd = r = mnt_monitor_get_fd(m->mount_monitor);
1826 if (r < 0) {
1827 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
1828 goto fail;
1829 }
1830
1831 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
1832 if (r < 0) {
1833 log_error_errno(r, "Failed to watch mount file descriptor: %m");
1834 goto fail;
1835 }
1836
1837 r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10);
1838 if (r < 0) {
1839 log_error_errno(r, "Failed to adjust mount watch priority: %m");
1840 goto fail;
1841 }
1842
1843 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
1844 }
1845
1846 r = mount_load_proc_self_mountinfo(m, false);
1847 if (r < 0)
1848 goto fail;
1849
1850 return;
1851
1852 fail:
1853 mount_shutdown(m);
1854 }
1855
1856 static int drain_libmount(Manager *m) {
1857 bool rescan = false;
1858 int r;
1859
1860 assert(m);
1861
1862 /* Drain all events and verify that the event is valid.
1863 *
1864 * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir
1865 * may generate event which is irrelevant for us.
1866 *
1867 * error: r < 0; valid: r == 0, false positive: r == 1 */
1868 do {
1869 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1870 if (r < 0)
1871 return log_error_errno(r, "Failed to drain libmount events: %m");
1872 if (r == 0)
1873 rescan = true;
1874 } while (r == 0);
1875
1876 return rescan;
1877 }
1878
1879 static int mount_process_proc_self_mountinfo(Manager *m) {
1880 _cleanup_set_free_free_ Set *around = NULL, *gone = NULL;
1881 const char *what;
1882 Iterator i;
1883 Unit *u;
1884 int r;
1885
1886 assert(m);
1887
1888 r = drain_libmount(m);
1889 if (r <= 0)
1890 return r;
1891
1892 r = mount_load_proc_self_mountinfo(m, true);
1893 if (r < 0) {
1894 /* Reset flags, just in case, for later calls */
1895 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT])
1896 MOUNT(u)->proc_flags = 0;
1897
1898 return 0;
1899 }
1900
1901 manager_dispatch_load_queue(m);
1902
1903 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1904 Mount *mount = MOUNT(u);
1905
1906 if (!mount_is_mounted(mount)) {
1907
1908 /* A mount point is not around right now. It
1909 * might be gone, or might never have
1910 * existed. */
1911
1912 if (mount->from_proc_self_mountinfo &&
1913 mount->parameters_proc_self_mountinfo.what) {
1914
1915 /* Remember that this device might just have disappeared */
1916 if (set_ensure_allocated(&gone, &path_hash_ops) < 0 ||
1917 set_put_strdup(&gone, mount->parameters_proc_self_mountinfo.what) < 0)
1918 log_oom(); /* we don't care too much about OOM here... */
1919 }
1920
1921 mount->from_proc_self_mountinfo = false;
1922 assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0);
1923
1924 switch (mount->state) {
1925
1926 case MOUNT_MOUNTED:
1927 /* This has just been unmounted by somebody else, follow the state change. */
1928 mount_enter_dead(mount, MOUNT_SUCCESS);
1929 break;
1930
1931 default:
1932 break;
1933 }
1934
1935 } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) {
1936
1937 /* A mount point was added or changed */
1938
1939 switch (mount->state) {
1940
1941 case MOUNT_DEAD:
1942 case MOUNT_FAILED:
1943
1944 /* This has just been mounted by somebody else, follow the state change, but let's
1945 * generate a new invocation ID for this implicitly and automatically. */
1946 (void) unit_acquire_invocation_id(u);
1947 mount_cycle_clear(mount);
1948 mount_enter_mounted(mount, MOUNT_SUCCESS);
1949 break;
1950
1951 case MOUNT_MOUNTING:
1952 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1953 break;
1954
1955 default:
1956 /* Nothing really changed, but let's
1957 * issue an notification call
1958 * nonetheless, in case somebody is
1959 * waiting for this. (e.g. file system
1960 * ro/rw remounts.) */
1961 mount_set_state(mount, mount->state);
1962 break;
1963 }
1964 }
1965
1966 if (mount_is_mounted(mount) &&
1967 mount->from_proc_self_mountinfo &&
1968 mount->parameters_proc_self_mountinfo.what) {
1969 /* Track devices currently used */
1970
1971 if (set_ensure_allocated(&around, &path_hash_ops) < 0 ||
1972 set_put_strdup(&around, mount->parameters_proc_self_mountinfo.what) < 0)
1973 log_oom();
1974 }
1975
1976 /* Reset the flags for later calls */
1977 mount->proc_flags = 0;
1978 }
1979
1980 SET_FOREACH(what, gone, i) {
1981 if (set_contains(around, what))
1982 continue;
1983
1984 /* Let the device units know that the device is no longer mounted */
1985 device_found_node(m, what, 0, DEVICE_FOUND_MOUNT);
1986 }
1987
1988 return 0;
1989 }
1990
1991 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1992 Manager *m = userdata;
1993
1994 assert(m);
1995 assert(revents & EPOLLIN);
1996
1997 return mount_process_proc_self_mountinfo(m);
1998 }
1999
2000 static void mount_reset_failed(Unit *u) {
2001 Mount *m = MOUNT(u);
2002
2003 assert(m);
2004
2005 if (m->state == MOUNT_FAILED)
2006 mount_set_state(m, MOUNT_DEAD);
2007
2008 m->result = MOUNT_SUCCESS;
2009 m->reload_result = MOUNT_SUCCESS;
2010 m->clean_result = MOUNT_SUCCESS;
2011 }
2012
2013 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
2014 Mount *m = MOUNT(u);
2015
2016 assert(m);
2017
2018 return unit_kill_common(u, who, signo, -1, m->control_pid, error);
2019 }
2020
2021 static int mount_control_pid(Unit *u) {
2022 Mount *m = MOUNT(u);
2023
2024 assert(m);
2025
2026 return m->control_pid;
2027 }
2028
2029 static int mount_clean(Unit *u, ExecCleanMask mask) {
2030 _cleanup_strv_free_ char **l = NULL;
2031 Mount *m = MOUNT(u);
2032 int r;
2033
2034 assert(m);
2035 assert(mask != 0);
2036
2037 if (m->state != MOUNT_DEAD)
2038 return -EBUSY;
2039
2040 r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l);
2041 if (r < 0)
2042 return r;
2043
2044 if (strv_isempty(l))
2045 return -EUNATCH;
2046
2047 mount_unwatch_control_pid(m);
2048 m->clean_result = MOUNT_SUCCESS;
2049 m->control_command = NULL;
2050 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
2051
2052 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->exec_context.timeout_clean_usec));
2053 if (r < 0)
2054 goto fail;
2055
2056 r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
2057 if (r < 0)
2058 goto fail;
2059
2060 mount_set_state(m, MOUNT_CLEANING);
2061
2062 return 0;
2063
2064 fail:
2065 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
2066 m->clean_result = MOUNT_FAILURE_RESOURCES;
2067 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
2068 return r;
2069 }
2070
2071 static int mount_can_clean(Unit *u, ExecCleanMask *ret) {
2072 Mount *m = MOUNT(u);
2073
2074 assert(m);
2075
2076 return exec_context_get_clean_mask(&m->exec_context, ret);
2077 }
2078
2079 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
2080 [MOUNT_EXEC_MOUNT] = "ExecMount",
2081 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
2082 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
2083 };
2084
2085 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
2086
2087 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
2088 [MOUNT_SUCCESS] = "success",
2089 [MOUNT_FAILURE_RESOURCES] = "resources",
2090 [MOUNT_FAILURE_TIMEOUT] = "timeout",
2091 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
2092 [MOUNT_FAILURE_SIGNAL] = "signal",
2093 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
2094 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
2095 [MOUNT_FAILURE_PROTOCOL] = "protocol",
2096 };
2097
2098 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
2099
2100 const UnitVTable mount_vtable = {
2101 .object_size = sizeof(Mount),
2102 .exec_context_offset = offsetof(Mount, exec_context),
2103 .cgroup_context_offset = offsetof(Mount, cgroup_context),
2104 .kill_context_offset = offsetof(Mount, kill_context),
2105 .exec_runtime_offset = offsetof(Mount, exec_runtime),
2106 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
2107
2108 .sections =
2109 "Unit\0"
2110 "Mount\0"
2111 "Install\0",
2112 .private_section = "Mount",
2113
2114 .can_transient = true,
2115 .can_fail = true,
2116
2117 .init = mount_init,
2118 .load = mount_load,
2119 .done = mount_done,
2120
2121 .coldplug = mount_coldplug,
2122
2123 .dump = mount_dump,
2124
2125 .start = mount_start,
2126 .stop = mount_stop,
2127 .reload = mount_reload,
2128
2129 .kill = mount_kill,
2130 .clean = mount_clean,
2131 .can_clean = mount_can_clean,
2132
2133 .serialize = mount_serialize,
2134 .deserialize_item = mount_deserialize_item,
2135
2136 .active_state = mount_active_state,
2137 .sub_state_to_string = mount_sub_state_to_string,
2138
2139 .will_restart = unit_will_restart_default,
2140
2141 .may_gc = mount_may_gc,
2142
2143 .sigchld_event = mount_sigchld_event,
2144
2145 .reset_failed = mount_reset_failed,
2146
2147 .control_pid = mount_control_pid,
2148
2149 .bus_set_property = bus_mount_set_property,
2150 .bus_commit_properties = bus_mount_commit_properties,
2151
2152 .get_timeout = mount_get_timeout,
2153
2154 .enumerate_perpetual = mount_enumerate_perpetual,
2155 .enumerate = mount_enumerate,
2156 .shutdown = mount_shutdown,
2157
2158 .status_message_formats = {
2159 .starting_stopping = {
2160 [0] = "Mounting %s...",
2161 [1] = "Unmounting %s...",
2162 },
2163 .finished_start_job = {
2164 [JOB_DONE] = "Mounted %s.",
2165 [JOB_FAILED] = "Failed to mount %s.",
2166 [JOB_TIMEOUT] = "Timed out mounting %s.",
2167 },
2168 .finished_stop_job = {
2169 [JOB_DONE] = "Unmounted %s.",
2170 [JOB_FAILED] = "Failed unmounting %s.",
2171 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2172 },
2173 },
2174 };