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