]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/mount.c
Merge pull request #7186 from poettering/track-deps
[thirdparty/systemd.git] / src / core / mount.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <sys/epoll.h>
24
25 #include "sd-messages.h"
26
27 #include "alloc-util.h"
28 #include "dbus-mount.h"
29 #include "escape.h"
30 #include "exit-status.h"
31 #include "format-util.h"
32 #include "fstab-util.h"
33 #include "log.h"
34 #include "manager.h"
35 #include "mkdir.h"
36 #include "mount-setup.h"
37 #include "mount-util.h"
38 #include "mount.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "special.h"
43 #include "string-table.h"
44 #include "string-util.h"
45 #include "strv.h"
46 #include "unit-name.h"
47 #include "unit.h"
48
49 #define RETRY_UMOUNT_MAX 32
50
51 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
52 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
53
54 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
55 [MOUNT_DEAD] = UNIT_INACTIVE,
56 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
57 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
58 [MOUNT_MOUNTED] = UNIT_ACTIVE,
59 [MOUNT_REMOUNTING] = UNIT_RELOADING,
60 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
61 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
62 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
63 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
64 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
65 [MOUNT_FAILED] = UNIT_FAILED
66 };
67
68 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
69 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
70
71 static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
72 return IN_SET(state,
73 MOUNT_MOUNTING,
74 MOUNT_MOUNTING_DONE,
75 MOUNT_REMOUNTING,
76 MOUNT_REMOUNTING_SIGTERM,
77 MOUNT_REMOUNTING_SIGKILL,
78 MOUNT_UNMOUNTING,
79 MOUNT_UNMOUNTING_SIGTERM,
80 MOUNT_UNMOUNTING_SIGKILL);
81 }
82
83 static bool mount_needs_network(const char *options, const char *fstype) {
84 if (fstab_test_option(options, "_netdev\0"))
85 return true;
86
87 if (fstype && fstype_is_network(fstype))
88 return true;
89
90 return false;
91 }
92
93 static bool mount_is_network(const MountParameters *p) {
94 assert(p);
95
96 return mount_needs_network(p->options, p->fstype);
97 }
98
99 static bool mount_is_loop(const MountParameters *p) {
100 assert(p);
101
102 if (fstab_test_option(p->options, "loop\0"))
103 return true;
104
105 return false;
106 }
107
108 static bool mount_is_bind(const MountParameters *p) {
109 assert(p);
110
111 if (fstab_test_option(p->options, "bind\0" "rbind\0"))
112 return true;
113
114 if (p->fstype && STR_IN_SET(p->fstype, "bind", "rbind"))
115 return true;
116
117 return false;
118 }
119
120 static bool mount_is_auto(const MountParameters *p) {
121 assert(p);
122
123 return !fstab_test_option(p->options, "noauto\0");
124 }
125
126 static bool mount_is_automount(const MountParameters *p) {
127 assert(p);
128
129 return fstab_test_option(p->options,
130 "comment=systemd.automount\0"
131 "x-systemd.automount\0");
132 }
133
134 static bool mount_is_bound_to_device(const Mount *m) {
135 const MountParameters *p;
136
137 if (m->from_fragment)
138 return true;
139
140 p = &m->parameters_proc_self_mountinfo;
141 return fstab_test_option(p->options, "x-systemd.device-bound\0");
142 }
143
144 static bool needs_quota(const MountParameters *p) {
145 assert(p);
146
147 /* Quotas are not enabled on network filesystems,
148 * but we want them, for example, on storage connected via iscsi */
149 if (p->fstype && fstype_is_network(p->fstype))
150 return false;
151
152 if (mount_is_bind(p))
153 return false;
154
155 return fstab_test_option(p->options,
156 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
157 }
158
159 static void mount_init(Unit *u) {
160 Mount *m = MOUNT(u);
161
162 assert(u);
163 assert(u->load_state == UNIT_STUB);
164
165 m->timeout_usec = u->manager->default_timeout_start_usec;
166 m->directory_mode = 0755;
167
168 /* We need to make sure that /usr/bin/mount is always called
169 * in the same process group as us, so that the autofs kernel
170 * side doesn't send us another mount request while we are
171 * already trying to comply its last one. */
172 m->exec_context.same_pgrp = true;
173
174 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
175
176 u->ignore_on_isolate = true;
177 }
178
179 static int mount_arm_timer(Mount *m, usec_t usec) {
180 int r;
181
182 assert(m);
183
184 if (m->timer_event_source) {
185 r = sd_event_source_set_time(m->timer_event_source, usec);
186 if (r < 0)
187 return r;
188
189 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
190 }
191
192 if (usec == USEC_INFINITY)
193 return 0;
194
195 r = sd_event_add_time(
196 UNIT(m)->manager->event,
197 &m->timer_event_source,
198 CLOCK_MONOTONIC,
199 usec, 0,
200 mount_dispatch_timer, m);
201 if (r < 0)
202 return r;
203
204 (void) sd_event_source_set_description(m->timer_event_source, "mount-timer");
205
206 return 0;
207 }
208
209 static void mount_unwatch_control_pid(Mount *m) {
210 assert(m);
211
212 if (m->control_pid <= 0)
213 return;
214
215 unit_unwatch_pid(UNIT(m), m->control_pid);
216 m->control_pid = 0;
217 }
218
219 static void mount_parameters_done(MountParameters *p) {
220 assert(p);
221
222 free(p->what);
223 free(p->options);
224 free(p->fstype);
225
226 p->what = p->options = p->fstype = NULL;
227 }
228
229 static void mount_done(Unit *u) {
230 Mount *m = MOUNT(u);
231
232 assert(m);
233
234 m->where = mfree(m->where);
235
236 mount_parameters_done(&m->parameters_proc_self_mountinfo);
237 mount_parameters_done(&m->parameters_fragment);
238
239 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
240 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
241 m->control_command = NULL;
242
243 dynamic_creds_unref(&m->dynamic_creds);
244
245 mount_unwatch_control_pid(m);
246
247 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
248 }
249
250 _pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
251 assert(m);
252
253 if (m->from_fragment)
254 return &m->parameters_fragment;
255
256 return NULL;
257 }
258
259 _pure_ static MountParameters* get_mount_parameters(Mount *m) {
260 assert(m);
261
262 if (m->from_proc_self_mountinfo)
263 return &m->parameters_proc_self_mountinfo;
264
265 return get_mount_parameters_fragment(m);
266 }
267
268 static int mount_add_mount_dependencies(Mount *m) {
269 MountParameters *pm;
270 Unit *other;
271 Iterator i;
272 Set *s;
273 int r;
274
275 assert(m);
276
277 if (!path_equal(m->where, "/")) {
278 _cleanup_free_ char *parent = NULL;
279
280 /* Adds in links to other mount points that might lie further up in the hierarchy */
281
282 parent = dirname_malloc(m->where);
283 if (!parent)
284 return -ENOMEM;
285
286 r = unit_require_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT);
287 if (r < 0)
288 return r;
289 }
290
291 /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount
292 * or a loop mount) to be available. */
293 pm = get_mount_parameters_fragment(m);
294 if (pm && pm->what &&
295 path_is_absolute(pm->what) &&
296 (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) {
297
298 r = unit_require_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE);
299 if (r < 0)
300 return r;
301 }
302
303 /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */
304 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
305 SET_FOREACH(other, s, i) {
306
307 if (other->load_state != UNIT_LOADED)
308 continue;
309
310 if (other == UNIT(m))
311 continue;
312
313 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true, UNIT_DEPENDENCY_PATH);
314 if (r < 0)
315 return r;
316
317 if (UNIT(m)->fragment_path) {
318 /* If we have fragment configuration, then make this dependency required */
319 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true, UNIT_DEPENDENCY_PATH);
320 if (r < 0)
321 return r;
322 }
323 }
324
325 return 0;
326 }
327
328 static int mount_add_device_dependencies(Mount *m) {
329 bool device_wants_mount = false;
330 UnitDependencyMask mask;
331 MountParameters *p;
332 UnitDependency dep;
333 int r;
334
335 assert(m);
336
337 p = get_mount_parameters(m);
338 if (!p)
339 return 0;
340
341 if (!p->what)
342 return 0;
343
344 if (mount_is_bind(p))
345 return 0;
346
347 if (!is_device_path(p->what))
348 return 0;
349
350 /* /dev/root is a really weird thing, it's not a real device,
351 * but just a path the kernel exports for the root file system
352 * specified on the kernel command line. Ignore it here. */
353 if (path_equal(p->what, "/dev/root"))
354 return 0;
355
356 if (path_equal(m->where, "/"))
357 return 0;
358
359 if (mount_is_auto(p) && !mount_is_automount(p) && MANAGER_IS_SYSTEM(UNIT(m)->manager))
360 device_wants_mount = true;
361
362 /* Mount units from /proc/self/mountinfo are not bound to devices
363 * by default since they're subject to races when devices are
364 * unplugged. But the user can still force this dep with an
365 * appropriate option (or udev property) so the mount units are
366 * automatically stopped when the device disappears suddenly. */
367 dep = mount_is_bound_to_device(m) ? UNIT_BINDS_TO : UNIT_REQUIRES;
368
369 mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT;
370
371 r = unit_add_node_dependency(UNIT(m), p->what, device_wants_mount, dep, mask);
372 if (r < 0)
373 return r;
374
375 return 0;
376 }
377
378 static int mount_add_quota_dependencies(Mount *m) {
379 UnitDependencyMask mask;
380 MountParameters *p;
381 int r;
382
383 assert(m);
384
385 if (!MANAGER_IS_SYSTEM(UNIT(m)->manager))
386 return 0;
387
388 p = get_mount_parameters_fragment(m);
389 if (!p)
390 return 0;
391
392 if (!needs_quota(p))
393 return 0;
394
395 mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT;
396
397 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true, mask);
398 if (r < 0)
399 return r;
400
401 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true, mask);
402 if (r < 0)
403 return r;
404
405 return 0;
406 }
407
408 static bool mount_is_extrinsic(Mount *m) {
409 MountParameters *p;
410 assert(m);
411
412 /* Returns true for all units that are "magic" and should be excluded from the usual start-up and shutdown
413 * dependencies. We call them "extrinsic" here, as they are generally mounted outside of the systemd dependency
414 * logic. We shouldn't attempt to manage them ourselves but it's fine if the user operates on them with us. */
415
416 if (!MANAGER_IS_SYSTEM(UNIT(m)->manager)) /* We only automatically manage mounts if we are in system mode */
417 return true;
418
419 if (PATH_IN_SET(m->where, /* Don't bother with the OS data itself */
420 "/",
421 "/usr"))
422 return true;
423
424 if (PATH_STARTSWITH_SET(m->where,
425 "/run/initramfs", /* This should stay around from before we boot until after we shutdown */
426 "/proc", /* All of this is API VFS */
427 "/sys", /* … dito … */
428 "/dev")) /* … dito … */
429 return true;
430
431 /* If this is an initrd mount, and we are not in the initrd, then leave this around forever, too. */
432 p = get_mount_parameters(m);
433 if (p && fstab_test_option(p->options, "x-initrd.mount\0") && !in_initrd())
434 return true;
435
436 return false;
437 }
438
439 static int mount_add_default_dependencies(Mount *m) {
440 UnitDependencyMask mask;
441 int r;
442 MountParameters *p;
443 const char *after;
444
445 assert(m);
446
447 if (!UNIT(m)->default_dependencies)
448 return 0;
449
450 /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are guaranteed to stay
451 * mounted the whole time, since our system is on it. Also, don't bother with anything mounted below virtual
452 * file systems, it's also going to be virtual, and hence not worth the effort. */
453 if (mount_is_extrinsic(m))
454 return 0;
455
456 p = get_mount_parameters(m);
457 if (!p)
458 return 0;
459
460 mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_DEFAULT;
461
462 if (mount_is_network(p)) {
463 /* We order ourselves after network.target. This is
464 * primarily useful at shutdown: services that take
465 * down the network should order themselves before
466 * network.target, so that they are shut down only
467 * after this mount unit is stopped. */
468
469 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, NULL, true, mask);
470 if (r < 0)
471 return r;
472
473 /* We pull in network-online.target, and order
474 * ourselves after it. This is useful at start-up to
475 * actively pull in tools that want to be started
476 * before we start mounting network file systems, and
477 * whose purpose it is to delay this until the network
478 * is "up". */
479
480 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, NULL, true, mask);
481 if (r < 0)
482 return r;
483
484 after = SPECIAL_REMOTE_FS_PRE_TARGET;
485 } else
486 after = SPECIAL_LOCAL_FS_PRE_TARGET;
487
488 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true, mask);
489 if (r < 0)
490 return r;
491
492 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true, mask);
493 if (r < 0)
494 return r;
495
496 /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */
497 if (streq_ptr(p->fstype, "tmpfs")) {
498 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, NULL, true, mask);
499 if (r < 0)
500 return r;
501 }
502
503 return 0;
504 }
505
506 static int mount_verify(Mount *m) {
507 _cleanup_free_ char *e = NULL;
508 MountParameters *p;
509 int r;
510
511 assert(m);
512
513 if (UNIT(m)->load_state != UNIT_LOADED)
514 return 0;
515
516 if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual)
517 return -ENOENT;
518
519 r = unit_name_from_path(m->where, ".mount", &e);
520 if (r < 0)
521 return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
522
523 if (!unit_has_name(UNIT(m), e)) {
524 log_unit_error(UNIT(m), "Where= setting doesn't match unit name. Refusing.");
525 return -EINVAL;
526 }
527
528 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
529 log_unit_error(UNIT(m), "Cannot create mount unit for API file system %s. Refusing.", m->where);
530 return -EINVAL;
531 }
532
533 p = get_mount_parameters_fragment(m);
534 if (p && !p->what) {
535 log_unit_error(UNIT(m), "What= setting is missing. Refusing.");
536 return -EBADMSG;
537 }
538
539 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
540 log_unit_error(UNIT(m), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
541 return -EINVAL;
542 }
543
544 return 0;
545 }
546
547 static int mount_add_extras(Mount *m) {
548 Unit *u = UNIT(m);
549 int r;
550
551 assert(m);
552
553 if (u->fragment_path)
554 m->from_fragment = true;
555
556 if (!m->where) {
557 r = unit_name_to_path(u->id, &m->where);
558 if (r < 0)
559 return r;
560 }
561
562 path_kill_slashes(m->where);
563
564 if (!u->description) {
565 r = unit_set_description(u, m->where);
566 if (r < 0)
567 return r;
568 }
569
570 r = mount_add_device_dependencies(m);
571 if (r < 0)
572 return r;
573
574 r = mount_add_mount_dependencies(m);
575 if (r < 0)
576 return r;
577
578 r = mount_add_quota_dependencies(m);
579 if (r < 0)
580 return r;
581
582 r = unit_patch_contexts(u);
583 if (r < 0)
584 return r;
585
586 r = unit_add_exec_dependencies(u, &m->exec_context);
587 if (r < 0)
588 return r;
589
590 r = unit_set_default_slice(u);
591 if (r < 0)
592 return r;
593
594 r = mount_add_default_dependencies(m);
595 if (r < 0)
596 return r;
597
598 return 0;
599 }
600
601 static int mount_load_root_mount(Unit *u) {
602 assert(u);
603
604 if (!unit_has_name(u, SPECIAL_ROOT_MOUNT))
605 return 0;
606
607 u->perpetual = true;
608 u->default_dependencies = false;
609
610 /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */
611 MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL;
612 MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL;
613
614 if (!u->description)
615 u->description = strdup("Root Mount");
616
617 return 1;
618 }
619
620 static int mount_load(Unit *u) {
621 Mount *m = MOUNT(u);
622 int r;
623
624 assert(u);
625 assert(u->load_state == UNIT_STUB);
626
627 r = mount_load_root_mount(u);
628 if (r < 0)
629 return r;
630
631 if (m->from_proc_self_mountinfo || u->perpetual)
632 r = unit_load_fragment_and_dropin_optional(u);
633 else
634 r = unit_load_fragment_and_dropin(u);
635 if (r < 0)
636 return r;
637
638 /* This is a new unit? Then let's add in some extras */
639 if (u->load_state == UNIT_LOADED) {
640 r = mount_add_extras(m);
641 if (r < 0)
642 return r;
643 }
644
645 return mount_verify(m);
646 }
647
648 static void mount_set_state(Mount *m, MountState state) {
649 MountState old_state;
650 assert(m);
651
652 old_state = m->state;
653 m->state = state;
654
655 if (!MOUNT_STATE_WITH_PROCESS(state)) {
656 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
657 mount_unwatch_control_pid(m);
658 m->control_command = NULL;
659 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
660 }
661
662 if (state != old_state)
663 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
664
665 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
666 }
667
668 static int mount_coldplug(Unit *u) {
669 Mount *m = MOUNT(u);
670 MountState new_state = MOUNT_DEAD;
671 int r;
672
673 assert(m);
674 assert(m->state == MOUNT_DEAD);
675
676 if (m->deserialized_state != m->state)
677 new_state = m->deserialized_state;
678 else if (m->from_proc_self_mountinfo)
679 new_state = MOUNT_MOUNTED;
680
681 if (new_state == m->state)
682 return 0;
683
684 if (m->control_pid > 0 &&
685 pid_is_unwaited(m->control_pid) &&
686 MOUNT_STATE_WITH_PROCESS(new_state)) {
687
688 r = unit_watch_pid(UNIT(m), m->control_pid);
689 if (r < 0)
690 return r;
691
692 r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
693 if (r < 0)
694 return r;
695 }
696
697 if (!IN_SET(new_state, MOUNT_DEAD, MOUNT_FAILED))
698 (void) unit_setup_dynamic_creds(u);
699
700 mount_set_state(m, new_state);
701 return 0;
702 }
703
704 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
705 Mount *m = MOUNT(u);
706 MountParameters *p;
707
708 assert(m);
709 assert(f);
710
711 p = get_mount_parameters(m);
712
713 fprintf(f,
714 "%sMount State: %s\n"
715 "%sResult: %s\n"
716 "%sWhere: %s\n"
717 "%sWhat: %s\n"
718 "%sFile System Type: %s\n"
719 "%sOptions: %s\n"
720 "%sFrom /proc/self/mountinfo: %s\n"
721 "%sFrom fragment: %s\n"
722 "%sExtrinsic: %s\n"
723 "%sDirectoryMode: %04o\n"
724 "%sSloppyOptions: %s\n"
725 "%sLazyUnmount: %s\n"
726 "%sForceUnmount: %s\n",
727 prefix, mount_state_to_string(m->state),
728 prefix, mount_result_to_string(m->result),
729 prefix, m->where,
730 prefix, p ? strna(p->what) : "n/a",
731 prefix, p ? strna(p->fstype) : "n/a",
732 prefix, p ? strna(p->options) : "n/a",
733 prefix, yes_no(m->from_proc_self_mountinfo),
734 prefix, yes_no(m->from_fragment),
735 prefix, yes_no(mount_is_extrinsic(m)),
736 prefix, m->directory_mode,
737 prefix, yes_no(m->sloppy_options),
738 prefix, yes_no(m->lazy_unmount),
739 prefix, yes_no(m->force_unmount));
740
741 if (m->control_pid > 0)
742 fprintf(f,
743 "%sControl PID: "PID_FMT"\n",
744 prefix, m->control_pid);
745
746 exec_context_dump(&m->exec_context, f, prefix);
747 kill_context_dump(&m->kill_context, f, prefix);
748 cgroup_context_dump(&m->cgroup_context, f, prefix);
749 }
750
751 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
752 pid_t pid;
753 int r;
754 ExecParameters exec_params = {
755 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
756 .stdin_fd = -1,
757 .stdout_fd = -1,
758 .stderr_fd = -1,
759 };
760
761 assert(m);
762 assert(c);
763 assert(_pid);
764
765 (void) unit_realize_cgroup(UNIT(m));
766 if (m->reset_accounting) {
767 (void) unit_reset_cpu_accounting(UNIT(m));
768 (void) unit_reset_ip_accounting(UNIT(m));
769 m->reset_accounting = false;
770 }
771
772 r = unit_setup_exec_runtime(UNIT(m));
773 if (r < 0)
774 return r;
775
776 r = unit_setup_dynamic_creds(UNIT(m));
777 if (r < 0)
778 return r;
779
780 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
781 if (r < 0)
782 return r;
783
784 manager_set_exec_params(UNIT(m)->manager, &exec_params);
785 unit_set_exec_params(UNIT(m), &exec_params);
786
787 r = exec_spawn(UNIT(m),
788 c,
789 &m->exec_context,
790 &exec_params,
791 m->exec_runtime,
792 &m->dynamic_creds,
793 &pid);
794 if (r < 0)
795 return r;
796
797 r = unit_watch_pid(UNIT(m), pid);
798 if (r < 0)
799 /* FIXME: we need to do something here */
800 return r;
801
802 *_pid = pid;
803
804 return 0;
805 }
806
807 static void mount_enter_dead(Mount *m, MountResult f) {
808 assert(m);
809
810 if (m->result == MOUNT_SUCCESS)
811 m->result = f;
812
813 if (m->result != MOUNT_SUCCESS)
814 log_unit_warning(UNIT(m), "Failed with result '%s'.", mount_result_to_string(m->result));
815
816 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
817
818 exec_runtime_destroy(m->exec_runtime);
819 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
820
821 exec_context_destroy_runtime_directory(&m->exec_context, UNIT(m)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
822
823 unit_unref_uid_gid(UNIT(m), true);
824
825 dynamic_creds_destroy(&m->dynamic_creds);
826 }
827
828 static void mount_enter_mounted(Mount *m, MountResult f) {
829 assert(m);
830
831 if (m->result == MOUNT_SUCCESS)
832 m->result = f;
833
834 mount_set_state(m, MOUNT_MOUNTED);
835 }
836
837 static void mount_enter_dead_or_mounted(Mount *m, MountResult f) {
838 assert(m);
839
840 /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this
841 * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all
842 * ultimately we just mirror the kernel's internal state on this. */
843
844 if (m->from_proc_self_mountinfo)
845 mount_enter_mounted(m, f);
846 else
847 mount_enter_dead(m, f);
848 }
849
850 static int state_to_kill_operation(MountState state) {
851 switch (state) {
852
853 case MOUNT_REMOUNTING_SIGTERM:
854 case MOUNT_UNMOUNTING_SIGTERM:
855 return KILL_TERMINATE;
856
857 case MOUNT_REMOUNTING_SIGKILL:
858 case MOUNT_UNMOUNTING_SIGKILL:
859 return KILL_KILL;
860
861 default:
862 return _KILL_OPERATION_INVALID;
863 }
864 }
865
866 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
867 int r;
868
869 assert(m);
870
871 if (m->result == MOUNT_SUCCESS)
872 m->result = f;
873
874 r = unit_kill_context(
875 UNIT(m),
876 &m->kill_context,
877 state_to_kill_operation(state),
878 -1,
879 m->control_pid,
880 false);
881 if (r < 0)
882 goto fail;
883
884 if (r > 0) {
885 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
886 if (r < 0)
887 goto fail;
888
889 mount_set_state(m, state);
890 } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill)
891 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
892 else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
893 mount_enter_mounted(m, MOUNT_SUCCESS);
894 else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill)
895 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
896 else
897 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
898
899 return;
900
901 fail:
902 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
903 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
904 }
905
906 static void mount_enter_unmounting(Mount *m) {
907 int r;
908
909 assert(m);
910
911 /* Start counting our attempts */
912 if (!IN_SET(m->state,
913 MOUNT_UNMOUNTING,
914 MOUNT_UNMOUNTING_SIGTERM,
915 MOUNT_UNMOUNTING_SIGKILL))
916 m->n_retry_umount = 0;
917
918 m->control_command_id = MOUNT_EXEC_UNMOUNT;
919 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
920
921 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL);
922 if (r >= 0 && m->lazy_unmount)
923 r = exec_command_append(m->control_command, "-l", NULL);
924 if (r >= 0 && m->force_unmount)
925 r = exec_command_append(m->control_command, "-f", NULL);
926 if (r < 0)
927 goto fail;
928
929 mount_unwatch_control_pid(m);
930
931 r = mount_spawn(m, m->control_command, &m->control_pid);
932 if (r < 0)
933 goto fail;
934
935 mount_set_state(m, MOUNT_UNMOUNTING);
936
937 return;
938
939 fail:
940 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
941 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
942 }
943
944 static void mount_enter_mounting(Mount *m) {
945 int r;
946 MountParameters *p;
947
948 assert(m);
949
950 m->control_command_id = MOUNT_EXEC_MOUNT;
951 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
952
953 r = unit_fail_if_symlink(UNIT(m), m->where);
954 if (r < 0)
955 goto fail;
956
957 (void) mkdir_p_label(m->where, m->directory_mode);
958
959 unit_warn_if_dir_nonempty(UNIT(m), m->where);
960
961 /* Create the source directory for bind-mounts if needed */
962 p = get_mount_parameters_fragment(m);
963 if (p && mount_is_bind(p))
964 (void) mkdir_p_label(p->what, m->directory_mode);
965
966 if (p) {
967 _cleanup_free_ char *opts = NULL;
968
969 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
970 if (r < 0)
971 goto fail;
972
973 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
974 if (r >= 0 && m->sloppy_options)
975 r = exec_command_append(m->control_command, "-s", NULL);
976 if (r >= 0 && p->fstype)
977 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
978 if (r >= 0 && !isempty(opts))
979 r = exec_command_append(m->control_command, "-o", opts, NULL);
980 } else
981 r = -ENOENT;
982 if (r < 0)
983 goto fail;
984
985 mount_unwatch_control_pid(m);
986
987 r = mount_spawn(m, m->control_command, &m->control_pid);
988 if (r < 0)
989 goto fail;
990
991 mount_set_state(m, MOUNT_MOUNTING);
992
993 return;
994
995 fail:
996 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
997 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES);
998 }
999
1000 static void mount_set_reload_result(Mount *m, MountResult result) {
1001 assert(m);
1002
1003 /* Only store the first error we encounter */
1004 if (m->reload_result != MOUNT_SUCCESS)
1005 return;
1006
1007 m->reload_result = result;
1008 }
1009
1010 static void mount_enter_remounting(Mount *m) {
1011 int r;
1012 MountParameters *p;
1013
1014 assert(m);
1015
1016 /* Reset reload result when we are about to start a new remount operation */
1017 m->reload_result = MOUNT_SUCCESS;
1018
1019 m->control_command_id = MOUNT_EXEC_REMOUNT;
1020 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
1021
1022 p = get_mount_parameters_fragment(m);
1023 if (p) {
1024 const char *o;
1025
1026 if (p->options)
1027 o = strjoina("remount,", p->options);
1028 else
1029 o = "remount";
1030
1031 r = exec_command_set(m->control_command, MOUNT_PATH,
1032 p->what, m->where,
1033 "-o", o, NULL);
1034 if (r >= 0 && m->sloppy_options)
1035 r = exec_command_append(m->control_command, "-s", NULL);
1036 if (r >= 0 && p->fstype)
1037 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
1038 } else
1039 r = -ENOENT;
1040 if (r < 0)
1041 goto fail;
1042
1043 mount_unwatch_control_pid(m);
1044
1045 r = mount_spawn(m, m->control_command, &m->control_pid);
1046 if (r < 0)
1047 goto fail;
1048
1049 mount_set_state(m, MOUNT_REMOUNTING);
1050
1051 return;
1052
1053 fail:
1054 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
1055 mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES);
1056 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1057 }
1058
1059 static int mount_start(Unit *u) {
1060 Mount *m = MOUNT(u);
1061 int r;
1062
1063 assert(m);
1064
1065 /* We cannot fulfill this request right now, try again later
1066 * please! */
1067 if (IN_SET(m->state,
1068 MOUNT_UNMOUNTING,
1069 MOUNT_UNMOUNTING_SIGTERM,
1070 MOUNT_UNMOUNTING_SIGKILL))
1071 return -EAGAIN;
1072
1073 /* Already on it! */
1074 if (m->state == MOUNT_MOUNTING)
1075 return 0;
1076
1077 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
1078
1079 r = unit_start_limit_test(u);
1080 if (r < 0) {
1081 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1082 return r;
1083 }
1084
1085 r = unit_acquire_invocation_id(u);
1086 if (r < 0)
1087 return r;
1088
1089 m->result = MOUNT_SUCCESS;
1090 m->reload_result = MOUNT_SUCCESS;
1091 m->reset_accounting = true;
1092
1093 mount_enter_mounting(m);
1094 return 1;
1095 }
1096
1097 static int mount_stop(Unit *u) {
1098 Mount *m = MOUNT(u);
1099
1100 assert(m);
1101
1102 switch (m->state) {
1103
1104 case MOUNT_UNMOUNTING:
1105 case MOUNT_UNMOUNTING_SIGKILL:
1106 case MOUNT_UNMOUNTING_SIGTERM:
1107 /* Already on it */
1108 return 0;
1109
1110 case MOUNT_MOUNTING:
1111 case MOUNT_MOUNTING_DONE:
1112 case MOUNT_REMOUNTING:
1113 /* If we are still waiting for /bin/mount, we go directly into kill mode. */
1114 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS);
1115 return 0;
1116
1117 case MOUNT_REMOUNTING_SIGTERM:
1118 /* If we are already waiting for a hung remount, convert this to the matching unmounting state */
1119 mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM);
1120 return 0;
1121
1122 case MOUNT_REMOUNTING_SIGKILL:
1123 /* as above */
1124 mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL);
1125 return 0;
1126
1127 case MOUNT_MOUNTED:
1128 mount_enter_unmounting(m);
1129 return 1;
1130
1131 default:
1132 assert_not_reached("Unexpected state.");
1133 }
1134 }
1135
1136 static int mount_reload(Unit *u) {
1137 Mount *m = MOUNT(u);
1138
1139 assert(m);
1140
1141 if (m->state == MOUNT_MOUNTING_DONE) /* not yet ready to reload, try again */
1142 return -EAGAIN;
1143
1144 assert(m->state == MOUNT_MOUNTED);
1145
1146 mount_enter_remounting(m);
1147
1148 return 1;
1149 }
1150
1151 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1152 Mount *m = MOUNT(u);
1153
1154 assert(m);
1155 assert(f);
1156 assert(fds);
1157
1158 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1159 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1160 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1161
1162 if (m->control_pid > 0)
1163 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1164
1165 if (m->control_command_id >= 0)
1166 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1167
1168 return 0;
1169 }
1170
1171 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1172 Mount *m = MOUNT(u);
1173
1174 assert(u);
1175 assert(key);
1176 assert(value);
1177 assert(fds);
1178
1179 if (streq(key, "state")) {
1180 MountState state;
1181
1182 if ((state = mount_state_from_string(value)) < 0)
1183 log_unit_debug(u, "Failed to parse state value: %s", value);
1184 else
1185 m->deserialized_state = state;
1186 } else if (streq(key, "result")) {
1187 MountResult f;
1188
1189 f = mount_result_from_string(value);
1190 if (f < 0)
1191 log_unit_debug(u, "Failed to parse result value: %s", value);
1192 else if (f != MOUNT_SUCCESS)
1193 m->result = f;
1194
1195 } else if (streq(key, "reload-result")) {
1196 MountResult f;
1197
1198 f = mount_result_from_string(value);
1199 if (f < 0)
1200 log_unit_debug(u, "Failed to parse reload result value: %s", value);
1201 else if (f != MOUNT_SUCCESS)
1202 m->reload_result = f;
1203
1204 } else if (streq(key, "control-pid")) {
1205 pid_t pid;
1206
1207 if (parse_pid(value, &pid) < 0)
1208 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
1209 else
1210 m->control_pid = pid;
1211 } else if (streq(key, "control-command")) {
1212 MountExecCommand id;
1213
1214 id = mount_exec_command_from_string(value);
1215 if (id < 0)
1216 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
1217 else {
1218 m->control_command_id = id;
1219 m->control_command = m->exec_command + id;
1220 }
1221 } else
1222 log_unit_debug(u, "Unknown serialization key: %s", key);
1223
1224 return 0;
1225 }
1226
1227 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1228 assert(u);
1229
1230 return state_translation_table[MOUNT(u)->state];
1231 }
1232
1233 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1234 assert(u);
1235
1236 return mount_state_to_string(MOUNT(u)->state);
1237 }
1238
1239 _pure_ static bool mount_check_gc(Unit *u) {
1240 Mount *m = MOUNT(u);
1241
1242 assert(m);
1243
1244 return m->from_proc_self_mountinfo;
1245 }
1246
1247 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1248 Mount *m = MOUNT(u);
1249 MountResult f;
1250
1251 assert(m);
1252 assert(pid >= 0);
1253
1254 if (pid != m->control_pid)
1255 return;
1256
1257 m->control_pid = 0;
1258
1259 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1260 f = MOUNT_SUCCESS;
1261 else if (code == CLD_EXITED)
1262 f = MOUNT_FAILURE_EXIT_CODE;
1263 else if (code == CLD_KILLED)
1264 f = MOUNT_FAILURE_SIGNAL;
1265 else if (code == CLD_DUMPED)
1266 f = MOUNT_FAILURE_CORE_DUMP;
1267 else
1268 assert_not_reached("Unknown code");
1269
1270 if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM))
1271 mount_set_reload_result(m, f);
1272 else if (m->result == MOUNT_SUCCESS)
1273 m->result = f;
1274
1275 if (m->control_command) {
1276 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1277
1278 m->control_command = NULL;
1279 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1280 }
1281
1282 log_unit_full(u, f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
1283 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code), status);
1284
1285 /* Note that mount(8) returning and the kernel sending us a mount table change event might happen
1286 * out-of-order. If an operation succeed we assume the kernel will follow soon too and already change into the
1287 * resulting state. If it fails we check if the kernel still knows about the mount. and change state
1288 * accordingly. */
1289
1290 switch (m->state) {
1291
1292 case MOUNT_MOUNTING:
1293 case MOUNT_MOUNTING_DONE:
1294
1295 if (f == MOUNT_SUCCESS || m->from_proc_self_mountinfo)
1296 /* If /bin/mount returned success, or if we see the mount point in /proc/self/mountinfo we are
1297 * happy. If we see the first condition first, we should see the second condition
1298 * immediately after – or /bin/mount lies to us and is broken. */
1299 mount_enter_mounted(m, f);
1300 else
1301 mount_enter_dead(m, f);
1302 break;
1303
1304 case MOUNT_REMOUNTING:
1305 case MOUNT_REMOUNTING_SIGTERM:
1306 case MOUNT_REMOUNTING_SIGKILL:
1307 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1308 break;
1309
1310 case MOUNT_UNMOUNTING:
1311 case MOUNT_UNMOUNTING_SIGKILL:
1312 case MOUNT_UNMOUNTING_SIGTERM:
1313
1314 if (m->from_proc_self_mountinfo) {
1315
1316 /* Still a mount point? If so, let's try again. Most likely there were multiple mount points
1317 * stacked on top of each other. Note that due to the io event priority logic we can be sure
1318 * the new mountinfo is loaded before we process the SIGCHLD for the mount command. */
1319
1320 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1321 log_unit_debug(u, "Mount still present, trying again.");
1322 m->n_retry_umount++;
1323 mount_enter_unmounting(m);
1324 } else {
1325 log_unit_debug(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
1326 mount_enter_mounted(m, f);
1327 }
1328 } else
1329 mount_enter_dead(m, f);
1330
1331 break;
1332
1333 default:
1334 assert_not_reached("Uh, control process died at wrong time.");
1335 }
1336
1337 /* Notify clients about changed exit status */
1338 unit_add_to_dbus_queue(u);
1339 }
1340
1341 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1342 Mount *m = MOUNT(userdata);
1343
1344 assert(m);
1345 assert(m->timer_event_source == source);
1346
1347 switch (m->state) {
1348
1349 case MOUNT_MOUNTING:
1350 case MOUNT_MOUNTING_DONE:
1351 log_unit_warning(UNIT(m), "Mounting timed out. Terminating.");
1352 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1353 break;
1354
1355 case MOUNT_REMOUNTING:
1356 log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process.");
1357 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1358 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS);
1359 break;
1360
1361 case MOUNT_REMOUNTING_SIGTERM:
1362 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1363
1364 if (m->kill_context.send_sigkill) {
1365 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1366 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
1367 } else {
1368 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1369 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1370 }
1371 break;
1372
1373 case MOUNT_REMOUNTING_SIGKILL:
1374 mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT);
1375
1376 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1377 mount_enter_dead_or_mounted(m, MOUNT_SUCCESS);
1378 break;
1379
1380 case MOUNT_UNMOUNTING:
1381 log_unit_warning(UNIT(m), "Unmounting timed out. Terminating.");
1382 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1383 break;
1384
1385 case MOUNT_UNMOUNTING_SIGTERM:
1386 if (m->kill_context.send_sigkill) {
1387 log_unit_warning(UNIT(m), "Mount process timed out. Killing.");
1388 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1389 } else {
1390 log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring.");
1391 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
1392 }
1393 break;
1394
1395 case MOUNT_UNMOUNTING_SIGKILL:
1396 log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring.");
1397 mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT);
1398 break;
1399
1400 default:
1401 assert_not_reached("Timeout at wrong time.");
1402 }
1403
1404 return 0;
1405 }
1406
1407 typedef struct {
1408 bool is_mounted;
1409 bool just_mounted;
1410 bool just_changed;
1411 } MountSetupFlags;
1412
1413 static int mount_setup_new_unit(
1414 Unit *u,
1415 const char *what,
1416 const char *where,
1417 const char *options,
1418 const char *fstype,
1419 MountSetupFlags *flags) {
1420
1421 MountParameters *p;
1422
1423 assert(u);
1424 assert(flags);
1425
1426 u->source_path = strdup("/proc/self/mountinfo");
1427 MOUNT(u)->where = strdup(where);
1428 if (!u->source_path || !MOUNT(u)->where)
1429 return -ENOMEM;
1430
1431 /* Make sure to initialize those fields before mount_is_extrinsic(). */
1432 MOUNT(u)->from_proc_self_mountinfo = true;
1433 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1434
1435 p->what = strdup(what);
1436 p->options = strdup(options);
1437 p->fstype = strdup(fstype);
1438 if (!p->what || !p->options || !p->fstype)
1439 return -ENOMEM;
1440
1441 if (!mount_is_extrinsic(MOUNT(u))) {
1442 const char *target;
1443 int r;
1444
1445 target = mount_is_network(p) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1446 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1447 if (r < 0)
1448 return r;
1449
1450 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1451 if (r < 0)
1452 return r;
1453 }
1454
1455 unit_add_to_load_queue(u);
1456 flags->is_mounted = true;
1457 flags->just_mounted = true;
1458 flags->just_changed = true;
1459
1460 return 0;
1461 }
1462
1463 static int mount_setup_existing_unit(
1464 Unit *u,
1465 const char *what,
1466 const char *where,
1467 const char *options,
1468 const char *fstype,
1469 MountSetupFlags *flags) {
1470
1471 MountParameters *p;
1472 bool load_extras = false;
1473 int r1, r2, r3;
1474
1475 assert(u);
1476 assert(flags);
1477
1478 if (!MOUNT(u)->where) {
1479 MOUNT(u)->where = strdup(where);
1480 if (!MOUNT(u)->where)
1481 return -ENOMEM;
1482 }
1483
1484 /* Make sure to initialize those fields before mount_is_extrinsic(). */
1485 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1486
1487 r1 = free_and_strdup(&p->what, what);
1488 r2 = free_and_strdup(&p->options, options);
1489 r3 = free_and_strdup(&p->fstype, fstype);
1490 if (r1 < 0 || r2 < 0 || r3 < 0)
1491 return -ENOMEM;
1492
1493 flags->just_changed = r1 > 0 || r2 > 0 || r3 > 0;
1494 flags->is_mounted = true;
1495 flags->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1496
1497 MOUNT(u)->from_proc_self_mountinfo = true;
1498
1499 if (!mount_is_extrinsic(MOUNT(u)) && mount_is_network(p)) {
1500 /* _netdev option may have shown up late, or on a
1501 * remount. Add remote-fs dependencies, even though
1502 * local-fs ones may already be there.
1503 *
1504 * Note: due to a current limitation (we don't track
1505 * in the dependency "Set*" objects who created a
1506 * dependency), we can only add deps, never lose them,
1507 * until the next full daemon-reload. */
1508 unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_REMOTE_FS_TARGET, NULL, true, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT);
1509 load_extras = true;
1510 }
1511
1512 if (u->load_state == UNIT_NOT_FOUND) {
1513 u->load_state = UNIT_LOADED;
1514 u->load_error = 0;
1515
1516 /* Load in the extras later on, after we
1517 * finished initialization of the unit */
1518
1519 /* FIXME: since we're going to load the unit later on, why setting load_extras=true ? */
1520 load_extras = true;
1521 flags->just_changed = true;
1522 }
1523
1524 if (load_extras)
1525 return mount_add_extras(MOUNT(u));
1526
1527 return 0;
1528 }
1529
1530 static int mount_setup_unit(
1531 Manager *m,
1532 const char *what,
1533 const char *where,
1534 const char *options,
1535 const char *fstype,
1536 bool set_flags) {
1537
1538 _cleanup_free_ char *e = NULL;
1539 MountSetupFlags flags;
1540 Unit *u;
1541 int r;
1542
1543 assert(m);
1544 assert(what);
1545 assert(where);
1546 assert(options);
1547 assert(fstype);
1548
1549 /* Ignore API mount points. They should never be referenced in
1550 * dependencies ever. */
1551 if (mount_point_is_api(where) || mount_point_ignore(where))
1552 return 0;
1553
1554 if (streq(fstype, "autofs"))
1555 return 0;
1556
1557 /* probably some kind of swap, ignore */
1558 if (!is_path(where))
1559 return 0;
1560
1561 r = unit_name_from_path(where, ".mount", &e);
1562 if (r < 0)
1563 return r;
1564
1565 u = manager_get_unit(m, e);
1566 if (!u) {
1567 /* First time we see this mount point meaning that it's
1568 * not been initiated by a mount unit but rather by the
1569 * sysadmin having called mount(8) directly. */
1570 r = unit_new_for_name(m, sizeof(Mount), e, &u);
1571 if (r < 0)
1572 goto fail;
1573
1574 r = mount_setup_new_unit(u, what, where, options, fstype, &flags);
1575 if (r < 0)
1576 unit_free(u);
1577 } else
1578 r = mount_setup_existing_unit(u, what, where, options, fstype, &flags);
1579
1580 if (r < 0)
1581 goto fail;
1582
1583 if (set_flags) {
1584 MOUNT(u)->is_mounted = flags.is_mounted;
1585 MOUNT(u)->just_mounted = flags.just_mounted;
1586 MOUNT(u)->just_changed = flags.just_changed;
1587 }
1588
1589 if (flags.just_changed)
1590 unit_add_to_dbus_queue(u);
1591
1592 return 0;
1593 fail:
1594 log_warning_errno(r, "Failed to set up mount unit: %m");
1595 return r;
1596 }
1597
1598 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1599 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
1600 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
1601 int r = 0;
1602
1603 assert(m);
1604
1605 t = mnt_new_table();
1606 if (!t)
1607 return log_oom();
1608
1609 i = mnt_new_iter(MNT_ITER_FORWARD);
1610 if (!i)
1611 return log_oom();
1612
1613 r = mnt_table_parse_mtab(t, NULL);
1614 if (r < 0)
1615 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
1616
1617 r = 0;
1618 for (;;) {
1619 const char *device, *path, *options, *fstype;
1620 _cleanup_free_ char *d = NULL, *p = NULL;
1621 struct libmnt_fs *fs;
1622 int k;
1623
1624 k = mnt_table_next_fs(t, i, &fs);
1625 if (k == 1)
1626 break;
1627 if (k < 0)
1628 return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
1629
1630 device = mnt_fs_get_source(fs);
1631 path = mnt_fs_get_target(fs);
1632 options = mnt_fs_get_options(fs);
1633 fstype = mnt_fs_get_fstype(fs);
1634
1635 if (!device || !path)
1636 continue;
1637
1638 if (cunescape(device, UNESCAPE_RELAX, &d) < 0)
1639 return log_oom();
1640
1641 if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
1642 return log_oom();
1643
1644 (void) device_found_node(m, d, true, DEVICE_FOUND_MOUNT, set_flags);
1645
1646 k = mount_setup_unit(m, d, p, options, fstype, set_flags);
1647 if (r == 0 && k < 0)
1648 r = k;
1649 }
1650
1651 return r;
1652 }
1653
1654 static void mount_shutdown(Manager *m) {
1655 assert(m);
1656
1657 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1658
1659 mnt_unref_monitor(m->mount_monitor);
1660 m->mount_monitor = NULL;
1661 }
1662
1663 static int mount_get_timeout(Unit *u, usec_t *timeout) {
1664 Mount *m = MOUNT(u);
1665 usec_t t;
1666 int r;
1667
1668 if (!m->timer_event_source)
1669 return 0;
1670
1671 r = sd_event_source_get_time(m->timer_event_source, &t);
1672 if (r < 0)
1673 return r;
1674 if (t == USEC_INFINITY)
1675 return 0;
1676
1677 *timeout = t;
1678 return 1;
1679 }
1680
1681 static int synthesize_root_mount(Manager *m) {
1682 Unit *u;
1683 int r;
1684
1685 assert(m);
1686
1687 /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's
1688 * unconditionally synthesize it here and mark it as perpetual. */
1689
1690 u = manager_get_unit(m, SPECIAL_ROOT_MOUNT);
1691 if (!u) {
1692 r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u);
1693 if (r < 0)
1694 return log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m");
1695 }
1696
1697 u->perpetual = true;
1698 MOUNT(u)->deserialized_state = MOUNT_MOUNTED;
1699
1700 unit_add_to_load_queue(u);
1701 unit_add_to_dbus_queue(u);
1702
1703 return 0;
1704 }
1705
1706 static bool mount_is_mounted(Mount *m) {
1707 assert(m);
1708
1709 return UNIT(m)->perpetual || m->is_mounted;
1710 }
1711
1712 static void mount_enumerate(Manager *m) {
1713 int r;
1714
1715 assert(m);
1716
1717 r = synthesize_root_mount(m);
1718 if (r < 0)
1719 goto fail;
1720
1721 mnt_init_debug(0);
1722
1723 if (!m->mount_monitor) {
1724 int fd;
1725
1726 m->mount_monitor = mnt_new_monitor();
1727 if (!m->mount_monitor) {
1728 log_oom();
1729 goto fail;
1730 }
1731
1732 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
1733 if (r < 0) {
1734 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
1735 goto fail;
1736 }
1737
1738 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
1739 if (r < 0) {
1740 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
1741 goto fail;
1742 }
1743
1744 /* mnt_unref_monitor() will close the fd */
1745 fd = r = mnt_monitor_get_fd(m->mount_monitor);
1746 if (r < 0) {
1747 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
1748 goto fail;
1749 }
1750
1751 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
1752 if (r < 0) {
1753 log_error_errno(r, "Failed to watch mount file descriptor: %m");
1754 goto fail;
1755 }
1756
1757 r = sd_event_source_set_priority(m->mount_event_source, -10);
1758 if (r < 0) {
1759 log_error_errno(r, "Failed to adjust mount watch priority: %m");
1760 goto fail;
1761 }
1762
1763 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
1764 }
1765
1766 r = mount_load_proc_self_mountinfo(m, false);
1767 if (r < 0)
1768 goto fail;
1769
1770 return;
1771
1772 fail:
1773 mount_shutdown(m);
1774 }
1775
1776 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1777 _cleanup_set_free_ Set *around = NULL, *gone = NULL;
1778 Manager *m = userdata;
1779 const char *what;
1780 Iterator i;
1781 Unit *u;
1782 int r;
1783
1784 assert(m);
1785 assert(revents & EPOLLIN);
1786
1787 if (fd == mnt_monitor_get_fd(m->mount_monitor)) {
1788 bool rescan = false;
1789
1790 /* Drain all events and verify that the event is valid.
1791 *
1792 * Note that libmount also monitors /run/mount mkdir if the
1793 * directory does not exist yet. The mkdir may generate event
1794 * which is irrelevant for us.
1795 *
1796 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1797 do {
1798 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1799 if (r == 0)
1800 rescan = true;
1801 else if (r < 0)
1802 return log_error_errno(r, "Failed to drain libmount events");
1803 } while (r == 0);
1804
1805 log_debug("libmount event [rescan: %s]", yes_no(rescan));
1806 if (!rescan)
1807 return 0;
1808 }
1809
1810 r = mount_load_proc_self_mountinfo(m, true);
1811 if (r < 0) {
1812 /* Reset flags, just in case, for later calls */
1813 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1814 Mount *mount = MOUNT(u);
1815
1816 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1817 }
1818
1819 return 0;
1820 }
1821
1822 manager_dispatch_load_queue(m);
1823
1824 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1825 Mount *mount = MOUNT(u);
1826
1827 if (!mount_is_mounted(mount)) {
1828
1829 /* A mount point is not around right now. It
1830 * might be gone, or might never have
1831 * existed. */
1832
1833 if (mount->from_proc_self_mountinfo &&
1834 mount->parameters_proc_self_mountinfo.what) {
1835
1836 /* Remember that this device might just have disappeared */
1837 if (set_ensure_allocated(&gone, &string_hash_ops) < 0 ||
1838 set_put(gone, mount->parameters_proc_self_mountinfo.what) < 0)
1839 log_oom(); /* we don't care too much about OOM here... */
1840 }
1841
1842 mount->from_proc_self_mountinfo = false;
1843
1844 switch (mount->state) {
1845
1846 case MOUNT_MOUNTED:
1847 /* This has just been unmounted by
1848 * somebody else, follow the state
1849 * change. */
1850 mount->result = MOUNT_SUCCESS; /* make sure we forget any earlier umount failures */
1851 mount_enter_dead(mount, MOUNT_SUCCESS);
1852 break;
1853
1854 default:
1855 break;
1856 }
1857
1858 } else if (mount->just_mounted || mount->just_changed) {
1859
1860 /* A mount point was added or changed */
1861
1862 switch (mount->state) {
1863
1864 case MOUNT_DEAD:
1865 case MOUNT_FAILED:
1866
1867 /* This has just been mounted by somebody else, follow the state change, but let's
1868 * generate a new invocation ID for this implicitly and automatically. */
1869 (void) unit_acquire_invocation_id(UNIT(mount));
1870 mount_enter_mounted(mount, MOUNT_SUCCESS);
1871 break;
1872
1873 case MOUNT_MOUNTING:
1874 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1875 break;
1876
1877 default:
1878 /* Nothing really changed, but let's
1879 * issue an notification call
1880 * nonetheless, in case somebody is
1881 * waiting for this. (e.g. file system
1882 * ro/rw remounts.) */
1883 mount_set_state(mount, mount->state);
1884 break;
1885 }
1886 }
1887
1888 if (mount_is_mounted(mount) &&
1889 mount->from_proc_self_mountinfo &&
1890 mount->parameters_proc_self_mountinfo.what) {
1891
1892 if (set_ensure_allocated(&around, &string_hash_ops) < 0 ||
1893 set_put(around, mount->parameters_proc_self_mountinfo.what) < 0)
1894 log_oom();
1895 }
1896
1897 /* Reset the flags for later calls */
1898 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1899 }
1900
1901 SET_FOREACH(what, gone, i) {
1902 if (set_contains(around, what))
1903 continue;
1904
1905 /* Let the device units know that the device is no longer mounted */
1906 (void) device_found_node(m, what, false, DEVICE_FOUND_MOUNT, true);
1907 }
1908
1909 return 0;
1910 }
1911
1912 static void mount_reset_failed(Unit *u) {
1913 Mount *m = MOUNT(u);
1914
1915 assert(m);
1916
1917 if (m->state == MOUNT_FAILED)
1918 mount_set_state(m, MOUNT_DEAD);
1919
1920 m->result = MOUNT_SUCCESS;
1921 m->reload_result = MOUNT_SUCCESS;
1922 }
1923
1924 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1925 Mount *m = MOUNT(u);
1926
1927 assert(m);
1928
1929 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1930 }
1931
1932 static int mount_control_pid(Unit *u) {
1933 Mount *m = MOUNT(u);
1934
1935 assert(m);
1936
1937 return m->control_pid;
1938 }
1939
1940 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1941 [MOUNT_EXEC_MOUNT] = "ExecMount",
1942 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1943 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1944 };
1945
1946 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1947
1948 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1949 [MOUNT_SUCCESS] = "success",
1950 [MOUNT_FAILURE_RESOURCES] = "resources",
1951 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1952 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1953 [MOUNT_FAILURE_SIGNAL] = "signal",
1954 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
1955 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1956 };
1957
1958 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1959
1960 const UnitVTable mount_vtable = {
1961 .object_size = sizeof(Mount),
1962 .exec_context_offset = offsetof(Mount, exec_context),
1963 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1964 .kill_context_offset = offsetof(Mount, kill_context),
1965 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1966 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
1967
1968 .sections =
1969 "Unit\0"
1970 "Mount\0"
1971 "Install\0",
1972 .private_section = "Mount",
1973
1974 .init = mount_init,
1975 .load = mount_load,
1976 .done = mount_done,
1977
1978 .coldplug = mount_coldplug,
1979
1980 .dump = mount_dump,
1981
1982 .start = mount_start,
1983 .stop = mount_stop,
1984 .reload = mount_reload,
1985
1986 .kill = mount_kill,
1987
1988 .serialize = mount_serialize,
1989 .deserialize_item = mount_deserialize_item,
1990
1991 .active_state = mount_active_state,
1992 .sub_state_to_string = mount_sub_state_to_string,
1993
1994 .check_gc = mount_check_gc,
1995
1996 .sigchld_event = mount_sigchld_event,
1997
1998 .reset_failed = mount_reset_failed,
1999
2000 .control_pid = mount_control_pid,
2001
2002 .bus_vtable = bus_mount_vtable,
2003 .bus_set_property = bus_mount_set_property,
2004 .bus_commit_properties = bus_mount_commit_properties,
2005
2006 .get_timeout = mount_get_timeout,
2007
2008 .can_transient = true,
2009
2010 .enumerate = mount_enumerate,
2011 .shutdown = mount_shutdown,
2012
2013 .status_message_formats = {
2014 .starting_stopping = {
2015 [0] = "Mounting %s...",
2016 [1] = "Unmounting %s...",
2017 },
2018 .finished_start_job = {
2019 [JOB_DONE] = "Mounted %s.",
2020 [JOB_FAILED] = "Failed to mount %s.",
2021 [JOB_TIMEOUT] = "Timed out mounting %s.",
2022 },
2023 .finished_stop_job = {
2024 [JOB_DONE] = "Unmounted %s.",
2025 [JOB_FAILED] = "Failed unmounting %s.",
2026 [JOB_TIMEOUT] = "Timed out unmounting %s.",
2027 },
2028 },
2029 };