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