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