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