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