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