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