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