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