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