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