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