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