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