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