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