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