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