]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/mount.c
Merge pull request #4391 from keszybz/treewide-macros
[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 "%sSloppyOptions: %s\n"
682 "%sLazyUnmount: %s\n"
683 "%sForceUnmount: %s\n",
684 prefix, mount_state_to_string(m->state),
685 prefix, mount_result_to_string(m->result),
686 prefix, m->where,
687 prefix, p ? strna(p->what) : "n/a",
688 prefix, p ? strna(p->fstype) : "n/a",
689 prefix, p ? strna(p->options) : "n/a",
690 prefix, yes_no(m->from_proc_self_mountinfo),
691 prefix, yes_no(m->from_fragment),
692 prefix, m->directory_mode,
693 prefix, yes_no(m->sloppy_options),
694 prefix, yes_no(m->lazy_unmount),
695 prefix, yes_no(m->force_unmount));
696
697 if (m->control_pid > 0)
698 fprintf(f,
699 "%sControl PID: "PID_FMT"\n",
700 prefix, m->control_pid);
701
702 exec_context_dump(&m->exec_context, f, prefix);
703 kill_context_dump(&m->kill_context, f, prefix);
704 }
705
706 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
707 pid_t pid;
708 int r;
709 ExecParameters exec_params = {
710 .flags = EXEC_APPLY_PERMISSIONS|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
711 .stdin_fd = -1,
712 .stdout_fd = -1,
713 .stderr_fd = -1,
714 };
715
716 assert(m);
717 assert(c);
718 assert(_pid);
719
720 (void) unit_realize_cgroup(UNIT(m));
721 if (m->reset_cpu_usage) {
722 (void) unit_reset_cpu_usage(UNIT(m));
723 m->reset_cpu_usage = false;
724 }
725
726 r = unit_setup_exec_runtime(UNIT(m));
727 if (r < 0)
728 return r;
729
730 r = unit_setup_dynamic_creds(UNIT(m));
731 if (r < 0)
732 return r;
733
734 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
735 if (r < 0)
736 return r;
737
738 exec_params.environment = UNIT(m)->manager->environment;
739 exec_params.flags |= UNIT(m)->manager->confirm_spawn ? EXEC_CONFIRM_SPAWN : 0;
740 exec_params.cgroup_supported = UNIT(m)->manager->cgroup_supported;
741 exec_params.cgroup_path = UNIT(m)->cgroup_path;
742 exec_params.cgroup_delegate = m->cgroup_context.delegate;
743 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(m)->manager);
744
745 r = exec_spawn(UNIT(m),
746 c,
747 &m->exec_context,
748 &exec_params,
749 m->exec_runtime,
750 &m->dynamic_creds,
751 &pid);
752 if (r < 0)
753 return r;
754
755 r = unit_watch_pid(UNIT(m), pid);
756 if (r < 0)
757 /* FIXME: we need to do something here */
758 return r;
759
760 *_pid = pid;
761
762 return 0;
763 }
764
765 static void mount_enter_dead(Mount *m, MountResult f) {
766 assert(m);
767
768 if (m->result == MOUNT_SUCCESS)
769 m->result = f;
770
771 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
772
773 exec_runtime_destroy(m->exec_runtime);
774 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
775
776 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
777
778 unit_unref_uid_gid(UNIT(m), true);
779
780 dynamic_creds_destroy(&m->dynamic_creds);
781 }
782
783 static void mount_enter_mounted(Mount *m, MountResult f) {
784 assert(m);
785
786 if (m->result == MOUNT_SUCCESS)
787 m->result = f;
788
789 mount_set_state(m, MOUNT_MOUNTED);
790 }
791
792 static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
793 int r;
794
795 assert(m);
796
797 if (m->result == MOUNT_SUCCESS)
798 m->result = f;
799
800 r = unit_kill_context(
801 UNIT(m),
802 &m->kill_context,
803 (state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM) ?
804 KILL_KILL : KILL_TERMINATE,
805 -1,
806 m->control_pid,
807 false);
808 if (r < 0)
809 goto fail;
810
811 if (r > 0) {
812 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
813 if (r < 0)
814 goto fail;
815
816 mount_set_state(m, state);
817 } else if (state == MOUNT_REMOUNTING_SIGTERM)
818 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
819 else if (state == MOUNT_REMOUNTING_SIGKILL)
820 mount_enter_mounted(m, MOUNT_SUCCESS);
821 else if (state == MOUNT_MOUNTING_SIGTERM)
822 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
823 else if (state == MOUNT_UNMOUNTING_SIGTERM)
824 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
825 else
826 mount_enter_dead(m, MOUNT_SUCCESS);
827
828 return;
829
830 fail:
831 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
832
833 if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL))
834 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
835 else
836 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
837 }
838
839 static void mount_enter_unmounting(Mount *m) {
840 int r;
841
842 assert(m);
843
844 /* Start counting our attempts */
845 if (!IN_SET(m->state,
846 MOUNT_UNMOUNTING,
847 MOUNT_UNMOUNTING_SIGTERM,
848 MOUNT_UNMOUNTING_SIGKILL))
849 m->n_retry_umount = 0;
850
851 m->control_command_id = MOUNT_EXEC_UNMOUNT;
852 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
853
854 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, NULL);
855 if (r >= 0 && m->lazy_unmount)
856 r = exec_command_append(m->control_command, "-l", NULL);
857 if (r >= 0 && m->force_unmount)
858 r = exec_command_append(m->control_command, "-f", NULL);
859 if (r < 0)
860 goto fail;
861
862 mount_unwatch_control_pid(m);
863
864 r = mount_spawn(m, m->control_command, &m->control_pid);
865 if (r < 0)
866 goto fail;
867
868 mount_set_state(m, MOUNT_UNMOUNTING);
869
870 return;
871
872 fail:
873 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
874 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
875 }
876
877 static void mount_enter_mounting(Mount *m) {
878 int r;
879 MountParameters *p;
880
881 assert(m);
882
883 m->control_command_id = MOUNT_EXEC_MOUNT;
884 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
885
886 r = unit_fail_if_symlink(UNIT(m), m->where);
887 if (r < 0)
888 goto fail;
889
890 (void) mkdir_p_label(m->where, m->directory_mode);
891
892 unit_warn_if_dir_nonempty(UNIT(m), m->where);
893
894 /* Create the source directory for bind-mounts if needed */
895 p = get_mount_parameters_fragment(m);
896 if (p && mount_is_bind(p))
897 (void) mkdir_p_label(p->what, m->directory_mode);
898
899 if (p) {
900 _cleanup_free_ char *opts = NULL;
901
902 r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, &opts);
903 if (r < 0)
904 goto fail;
905
906 r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL);
907 if (r >= 0 && m->sloppy_options)
908 r = exec_command_append(m->control_command, "-s", NULL);
909 if (r >= 0 && p->fstype)
910 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
911 if (r >= 0 && !isempty(opts))
912 r = exec_command_append(m->control_command, "-o", opts, NULL);
913 } else
914 r = -ENOENT;
915
916 if (r < 0)
917 goto fail;
918
919 mount_unwatch_control_pid(m);
920
921 r = mount_spawn(m, m->control_command, &m->control_pid);
922 if (r < 0)
923 goto fail;
924
925 mount_set_state(m, MOUNT_MOUNTING);
926
927 return;
928
929 fail:
930 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
931 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
932 }
933
934 static void mount_enter_remounting(Mount *m) {
935 int r;
936 MountParameters *p;
937
938 assert(m);
939
940 m->control_command_id = MOUNT_EXEC_REMOUNT;
941 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
942
943 p = get_mount_parameters_fragment(m);
944 if (p) {
945 const char *o;
946
947 if (p->options)
948 o = strjoina("remount,", p->options);
949 else
950 o = "remount";
951
952 r = exec_command_set(m->control_command, MOUNT_PATH,
953 p->what, m->where,
954 "-o", o, NULL);
955 if (r >= 0 && m->sloppy_options)
956 r = exec_command_append(m->control_command, "-s", NULL);
957 if (r >= 0 && p->fstype)
958 r = exec_command_append(m->control_command, "-t", p->fstype, NULL);
959 } else
960 r = -ENOENT;
961
962 if (r < 0)
963 goto fail;
964
965 mount_unwatch_control_pid(m);
966
967 r = mount_spawn(m, m->control_command, &m->control_pid);
968 if (r < 0)
969 goto fail;
970
971 mount_set_state(m, MOUNT_REMOUNTING);
972
973 return;
974
975 fail:
976 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
977 m->reload_result = MOUNT_FAILURE_RESOURCES;
978 mount_enter_mounted(m, MOUNT_SUCCESS);
979 }
980
981 static int mount_start(Unit *u) {
982 Mount *m = MOUNT(u);
983 int r;
984
985 assert(m);
986
987 /* We cannot fulfill this request right now, try again later
988 * please! */
989 if (IN_SET(m->state,
990 MOUNT_UNMOUNTING,
991 MOUNT_UNMOUNTING_SIGTERM,
992 MOUNT_UNMOUNTING_SIGKILL,
993 MOUNT_MOUNTING_SIGTERM,
994 MOUNT_MOUNTING_SIGKILL))
995 return -EAGAIN;
996
997 /* Already on it! */
998 if (m->state == MOUNT_MOUNTING)
999 return 0;
1000
1001 assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
1002
1003 r = unit_start_limit_test(u);
1004 if (r < 0) {
1005 mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
1006 return r;
1007 }
1008
1009 r = unit_acquire_invocation_id(u);
1010 if (r < 0)
1011 return r;
1012
1013 m->result = MOUNT_SUCCESS;
1014 m->reload_result = MOUNT_SUCCESS;
1015 m->reset_cpu_usage = true;
1016
1017 mount_enter_mounting(m);
1018 return 1;
1019 }
1020
1021 static int mount_stop(Unit *u) {
1022 Mount *m = MOUNT(u);
1023
1024 assert(m);
1025
1026 /* Already on it */
1027 if (IN_SET(m->state,
1028 MOUNT_UNMOUNTING,
1029 MOUNT_UNMOUNTING_SIGKILL,
1030 MOUNT_UNMOUNTING_SIGTERM,
1031 MOUNT_MOUNTING_SIGTERM,
1032 MOUNT_MOUNTING_SIGKILL))
1033 return 0;
1034
1035 assert(IN_SET(m->state,
1036 MOUNT_MOUNTING,
1037 MOUNT_MOUNTING_DONE,
1038 MOUNT_MOUNTED,
1039 MOUNT_REMOUNTING,
1040 MOUNT_REMOUNTING_SIGTERM,
1041 MOUNT_REMOUNTING_SIGKILL));
1042
1043 mount_enter_unmounting(m);
1044 return 1;
1045 }
1046
1047 static int mount_reload(Unit *u) {
1048 Mount *m = MOUNT(u);
1049
1050 assert(m);
1051
1052 if (m->state == MOUNT_MOUNTING_DONE)
1053 return -EAGAIN;
1054
1055 assert(m->state == MOUNT_MOUNTED);
1056
1057 mount_enter_remounting(m);
1058 return 1;
1059 }
1060
1061 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1062 Mount *m = MOUNT(u);
1063
1064 assert(m);
1065 assert(f);
1066 assert(fds);
1067
1068 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1069 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1070 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1071
1072 if (m->control_pid > 0)
1073 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
1074
1075 if (m->control_command_id >= 0)
1076 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1077
1078 return 0;
1079 }
1080
1081 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1082 Mount *m = MOUNT(u);
1083
1084 assert(u);
1085 assert(key);
1086 assert(value);
1087 assert(fds);
1088
1089 if (streq(key, "state")) {
1090 MountState state;
1091
1092 if ((state = mount_state_from_string(value)) < 0)
1093 log_unit_debug(u, "Failed to parse state value: %s", value);
1094 else
1095 m->deserialized_state = state;
1096 } else if (streq(key, "result")) {
1097 MountResult f;
1098
1099 f = mount_result_from_string(value);
1100 if (f < 0)
1101 log_unit_debug(u, "Failed to parse result value: %s", value);
1102 else if (f != MOUNT_SUCCESS)
1103 m->result = f;
1104
1105 } else if (streq(key, "reload-result")) {
1106 MountResult f;
1107
1108 f = mount_result_from_string(value);
1109 if (f < 0)
1110 log_unit_debug(u, "Failed to parse reload result value: %s", value);
1111 else if (f != MOUNT_SUCCESS)
1112 m->reload_result = f;
1113
1114 } else if (streq(key, "control-pid")) {
1115 pid_t pid;
1116
1117 if (parse_pid(value, &pid) < 0)
1118 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
1119 else
1120 m->control_pid = pid;
1121 } else if (streq(key, "control-command")) {
1122 MountExecCommand id;
1123
1124 id = mount_exec_command_from_string(value);
1125 if (id < 0)
1126 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
1127 else {
1128 m->control_command_id = id;
1129 m->control_command = m->exec_command + id;
1130 }
1131 } else
1132 log_unit_debug(u, "Unknown serialization key: %s", key);
1133
1134 return 0;
1135 }
1136
1137 _pure_ static UnitActiveState mount_active_state(Unit *u) {
1138 assert(u);
1139
1140 return state_translation_table[MOUNT(u)->state];
1141 }
1142
1143 _pure_ static const char *mount_sub_state_to_string(Unit *u) {
1144 assert(u);
1145
1146 return mount_state_to_string(MOUNT(u)->state);
1147 }
1148
1149 _pure_ static bool mount_check_gc(Unit *u) {
1150 Mount *m = MOUNT(u);
1151
1152 assert(m);
1153
1154 return m->from_proc_self_mountinfo;
1155 }
1156
1157 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1158 Mount *m = MOUNT(u);
1159 MountResult f;
1160
1161 assert(m);
1162 assert(pid >= 0);
1163
1164 if (pid != m->control_pid)
1165 return;
1166
1167 m->control_pid = 0;
1168
1169 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1170 f = MOUNT_SUCCESS;
1171 else if (code == CLD_EXITED)
1172 f = MOUNT_FAILURE_EXIT_CODE;
1173 else if (code == CLD_KILLED)
1174 f = MOUNT_FAILURE_SIGNAL;
1175 else if (code == CLD_DUMPED)
1176 f = MOUNT_FAILURE_CORE_DUMP;
1177 else
1178 assert_not_reached("Unknown code");
1179
1180 if (m->result == MOUNT_SUCCESS)
1181 m->result = f;
1182
1183 if (m->control_command) {
1184 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1185
1186 m->control_command = NULL;
1187 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1188 }
1189
1190 log_unit_full(u, f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
1191 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code), status);
1192
1193 /* Note that mount(8) returning and the kernel sending us a
1194 * mount table change event might happen out-of-order. If an
1195 * operation succeed we assume the kernel will follow soon too
1196 * and already change into the resulting state. If it fails
1197 * we check if the kernel still knows about the mount. and
1198 * change state accordingly. */
1199
1200 switch (m->state) {
1201
1202 case MOUNT_MOUNTING:
1203 case MOUNT_MOUNTING_DONE:
1204 case MOUNT_MOUNTING_SIGKILL:
1205 case MOUNT_MOUNTING_SIGTERM:
1206
1207 if (f == MOUNT_SUCCESS || m->from_proc_self_mountinfo)
1208 /* If /bin/mount returned success, or if we see the mount point in /proc/self/mountinfo we are
1209 * happy. If we see the first condition first, we should see the the second condition
1210 * immediately after – or /bin/mount lies to us and is broken. */
1211 mount_enter_mounted(m, f);
1212 else
1213 mount_enter_dead(m, f);
1214 break;
1215
1216 case MOUNT_REMOUNTING:
1217 case MOUNT_REMOUNTING_SIGKILL:
1218 case MOUNT_REMOUNTING_SIGTERM:
1219
1220 m->reload_result = f;
1221 if (m->from_proc_self_mountinfo)
1222 mount_enter_mounted(m, MOUNT_SUCCESS);
1223 else
1224 mount_enter_dead(m, MOUNT_SUCCESS);
1225
1226 break;
1227
1228 case MOUNT_UNMOUNTING:
1229 case MOUNT_UNMOUNTING_SIGKILL:
1230 case MOUNT_UNMOUNTING_SIGTERM:
1231
1232 if (f == MOUNT_SUCCESS) {
1233
1234 if (m->from_proc_self_mountinfo) {
1235
1236 /* Still a mount point? If so, let's
1237 * try again. Most likely there were
1238 * multiple mount points stacked on
1239 * top of each other. Note that due to
1240 * the io event priority logic we can
1241 * be sure the new mountinfo is loaded
1242 * before we process the SIGCHLD for
1243 * the mount command. */
1244
1245 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
1246 log_unit_debug(u, "Mount still present, trying again.");
1247 m->n_retry_umount++;
1248 mount_enter_unmounting(m);
1249 } else {
1250 log_unit_debug(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
1251 mount_enter_mounted(m, f);
1252 }
1253 } else
1254 mount_enter_dead(m, f);
1255
1256 } else if (m->from_proc_self_mountinfo)
1257 mount_enter_mounted(m, f);
1258 else
1259 mount_enter_dead(m, f);
1260 break;
1261
1262 default:
1263 assert_not_reached("Uh, control process died at wrong time.");
1264 }
1265
1266 /* Notify clients about changed exit status */
1267 unit_add_to_dbus_queue(u);
1268 }
1269
1270 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1271 Mount *m = MOUNT(userdata);
1272
1273 assert(m);
1274 assert(m->timer_event_source == source);
1275
1276 switch (m->state) {
1277
1278 case MOUNT_MOUNTING:
1279 case MOUNT_MOUNTING_DONE:
1280 log_unit_warning(UNIT(m), "Mounting timed out. Stopping.");
1281 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1282 break;
1283
1284 case MOUNT_REMOUNTING:
1285 log_unit_warning(UNIT(m), "Remounting timed out. Stopping.");
1286 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1287 mount_enter_mounted(m, MOUNT_SUCCESS);
1288 break;
1289
1290 case MOUNT_UNMOUNTING:
1291 log_unit_warning(UNIT(m), "Unmounting timed out. Stopping.");
1292 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1293 break;
1294
1295 case MOUNT_MOUNTING_SIGTERM:
1296 if (m->kill_context.send_sigkill) {
1297 log_unit_warning(UNIT(m), "Mounting timed out. Killing.");
1298 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1299 } else {
1300 log_unit_warning(UNIT(m), "Mounting 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_REMOUNTING_SIGTERM:
1310 if (m->kill_context.send_sigkill) {
1311 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
1312 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1313 } else {
1314 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
1315
1316 if (m->from_proc_self_mountinfo)
1317 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1318 else
1319 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1320 }
1321 break;
1322
1323 case MOUNT_UNMOUNTING_SIGTERM:
1324 if (m->kill_context.send_sigkill) {
1325 log_unit_warning(UNIT(m), "Unmounting timed out. Killing.");
1326 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1327 } else {
1328 log_unit_warning(UNIT(m), "Unmounting timed out. Skipping SIGKILL. Ignoring.");
1329
1330 if (m->from_proc_self_mountinfo)
1331 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1332 else
1333 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1334 }
1335 break;
1336
1337 case MOUNT_MOUNTING_SIGKILL:
1338 case MOUNT_REMOUNTING_SIGKILL:
1339 case MOUNT_UNMOUNTING_SIGKILL:
1340 log_unit_warning(UNIT(m),"Mount process still around after SIGKILL. Ignoring.");
1341
1342 if (m->from_proc_self_mountinfo)
1343 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1344 else
1345 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1346 break;
1347
1348 default:
1349 assert_not_reached("Timeout at wrong time.");
1350 }
1351
1352 return 0;
1353 }
1354
1355 static int mount_setup_unit(
1356 Manager *m,
1357 const char *what,
1358 const char *where,
1359 const char *options,
1360 const char *fstype,
1361 bool set_flags) {
1362
1363 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
1364 bool load_extras = false;
1365 MountParameters *p;
1366 bool delete, changed = false;
1367 Unit *u;
1368 int r;
1369
1370 assert(m);
1371 assert(what);
1372 assert(where);
1373 assert(options);
1374 assert(fstype);
1375
1376 /* Ignore API mount points. They should never be referenced in
1377 * dependencies ever. */
1378 if (mount_point_is_api(where) || mount_point_ignore(where))
1379 return 0;
1380
1381 if (streq(fstype, "autofs"))
1382 return 0;
1383
1384 /* probably some kind of swap, ignore */
1385 if (!is_path(where))
1386 return 0;
1387
1388 r = unit_name_from_path(where, ".mount", &e);
1389 if (r < 0)
1390 return r;
1391
1392 u = manager_get_unit(m, e);
1393 if (!u) {
1394 delete = true;
1395
1396 u = unit_new(m, sizeof(Mount));
1397 if (!u)
1398 return log_oom();
1399
1400 r = unit_add_name(u, e);
1401 if (r < 0)
1402 goto fail;
1403
1404 MOUNT(u)->where = strdup(where);
1405 if (!MOUNT(u)->where) {
1406 r = -ENOMEM;
1407 goto fail;
1408 }
1409
1410 u->source_path = strdup("/proc/self/mountinfo");
1411 if (!u->source_path) {
1412 r = -ENOMEM;
1413 goto fail;
1414 }
1415
1416 if (MANAGER_IS_SYSTEM(m)) {
1417 const char* target;
1418
1419 target = mount_needs_network(options, fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
1420 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
1421 if (r < 0)
1422 goto fail;
1423
1424 if (should_umount(MOUNT(u))) {
1425 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1426 if (r < 0)
1427 goto fail;
1428 }
1429 }
1430
1431 unit_add_to_load_queue(u);
1432 changed = true;
1433 } else {
1434 delete = false;
1435
1436 if (!MOUNT(u)->where) {
1437 MOUNT(u)->where = strdup(where);
1438 if (!MOUNT(u)->where) {
1439 r = -ENOMEM;
1440 goto fail;
1441 }
1442 }
1443
1444 if (MANAGER_IS_SYSTEM(m) &&
1445 mount_needs_network(options, fstype)) {
1446 /* _netdev option may have shown up late, or on a
1447 * remount. Add remote-fs dependencies, even though
1448 * local-fs ones may already be there. */
1449 unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_REMOTE_FS_TARGET, NULL, true);
1450 load_extras = true;
1451 }
1452
1453 if (u->load_state == UNIT_NOT_FOUND) {
1454 u->load_state = UNIT_LOADED;
1455 u->load_error = 0;
1456
1457 /* Load in the extras later on, after we
1458 * finished initialization of the unit */
1459 load_extras = true;
1460 changed = true;
1461 }
1462 }
1463
1464 w = strdup(what);
1465 o = strdup(options);
1466 f = strdup(fstype);
1467 if (!w || !o || !f) {
1468 r = -ENOMEM;
1469 goto fail;
1470 }
1471
1472 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1473
1474 changed = changed ||
1475 !streq_ptr(p->options, options) ||
1476 !streq_ptr(p->what, what) ||
1477 !streq_ptr(p->fstype, fstype);
1478
1479 if (set_flags) {
1480 MOUNT(u)->is_mounted = true;
1481 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1482 MOUNT(u)->just_changed = changed;
1483 }
1484
1485 MOUNT(u)->from_proc_self_mountinfo = true;
1486
1487 free_and_replace(p->what, w);
1488 free_and_replace(p->options, o);
1489 free_and_replace(p->fstype, f);
1490
1491 if (load_extras) {
1492 r = mount_add_extras(MOUNT(u));
1493 if (r < 0)
1494 goto fail;
1495 }
1496
1497 if (changed)
1498 unit_add_to_dbus_queue(u);
1499
1500 return 0;
1501
1502 fail:
1503 log_warning_errno(r, "Failed to set up mount unit: %m");
1504
1505 if (delete && u)
1506 unit_free(u);
1507
1508 return r;
1509 }
1510
1511 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1512 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
1513 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
1514 int r = 0;
1515
1516 assert(m);
1517
1518 t = mnt_new_table();
1519 if (!t)
1520 return log_oom();
1521
1522 i = mnt_new_iter(MNT_ITER_FORWARD);
1523 if (!i)
1524 return log_oom();
1525
1526 r = mnt_table_parse_mtab(t, NULL);
1527 if (r < 0)
1528 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
1529
1530 r = 0;
1531 for (;;) {
1532 const char *device, *path, *options, *fstype;
1533 _cleanup_free_ char *d = NULL, *p = NULL;
1534 struct libmnt_fs *fs;
1535 int k;
1536
1537 k = mnt_table_next_fs(t, i, &fs);
1538 if (k == 1)
1539 break;
1540 if (k < 0)
1541 return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
1542
1543 device = mnt_fs_get_source(fs);
1544 path = mnt_fs_get_target(fs);
1545 options = mnt_fs_get_options(fs);
1546 fstype = mnt_fs_get_fstype(fs);
1547
1548 if (!device || !path)
1549 continue;
1550
1551 if (cunescape(device, UNESCAPE_RELAX, &d) < 0)
1552 return log_oom();
1553
1554 if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
1555 return log_oom();
1556
1557 (void) device_found_node(m, d, true, DEVICE_FOUND_MOUNT, set_flags);
1558
1559 k = mount_setup_unit(m, d, p, options, fstype, set_flags);
1560 if (r == 0 && k < 0)
1561 r = k;
1562 }
1563
1564 return r;
1565 }
1566
1567 static void mount_shutdown(Manager *m) {
1568
1569 assert(m);
1570
1571 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1572
1573 mnt_unref_monitor(m->mount_monitor);
1574 m->mount_monitor = NULL;
1575 }
1576
1577 static int mount_get_timeout(Unit *u, usec_t *timeout) {
1578 Mount *m = MOUNT(u);
1579 usec_t t;
1580 int r;
1581
1582 if (!m->timer_event_source)
1583 return 0;
1584
1585 r = sd_event_source_get_time(m->timer_event_source, &t);
1586 if (r < 0)
1587 return r;
1588 if (t == USEC_INFINITY)
1589 return 0;
1590
1591 *timeout = t;
1592 return 1;
1593 }
1594
1595 static void mount_enumerate(Manager *m) {
1596 int r;
1597
1598 assert(m);
1599
1600 mnt_init_debug(0);
1601
1602 if (!m->mount_monitor) {
1603 int fd;
1604
1605 m->mount_monitor = mnt_new_monitor();
1606 if (!m->mount_monitor) {
1607 log_oom();
1608 goto fail;
1609 }
1610
1611 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
1612 if (r < 0) {
1613 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
1614 goto fail;
1615 }
1616
1617 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
1618 if (r < 0) {
1619 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
1620 goto fail;
1621 }
1622
1623 /* mnt_unref_monitor() will close the fd */
1624 fd = r = mnt_monitor_get_fd(m->mount_monitor);
1625 if (r < 0) {
1626 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
1627 goto fail;
1628 }
1629
1630 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
1631 if (r < 0) {
1632 log_error_errno(r, "Failed to watch mount file descriptor: %m");
1633 goto fail;
1634 }
1635
1636 r = sd_event_source_set_priority(m->mount_event_source, -10);
1637 if (r < 0) {
1638 log_error_errno(r, "Failed to adjust mount watch priority: %m");
1639 goto fail;
1640 }
1641
1642 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
1643 }
1644
1645 r = mount_load_proc_self_mountinfo(m, false);
1646 if (r < 0)
1647 goto fail;
1648
1649 return;
1650
1651 fail:
1652 mount_shutdown(m);
1653 }
1654
1655 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1656 _cleanup_set_free_ Set *around = NULL, *gone = NULL;
1657 Manager *m = userdata;
1658 const char *what;
1659 Iterator i;
1660 Unit *u;
1661 int r;
1662
1663 assert(m);
1664 assert(revents & EPOLLIN);
1665
1666 if (fd == mnt_monitor_get_fd(m->mount_monitor)) {
1667 bool rescan = false;
1668
1669 /* Drain all events and verify that the event is valid.
1670 *
1671 * Note that libmount also monitors /run/mount mkdir if the
1672 * directory does not exist yet. The mkdir may generate event
1673 * which is irrelevant for us.
1674 *
1675 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1676 do {
1677 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1678 if (r == 0)
1679 rescan = true;
1680 else if (r < 0)
1681 return log_error_errno(r, "Failed to drain libmount events");
1682 } while (r == 0);
1683
1684 log_debug("libmount event [rescan: %s]", yes_no(rescan));
1685 if (!rescan)
1686 return 0;
1687 }
1688
1689 r = mount_load_proc_self_mountinfo(m, true);
1690 if (r < 0) {
1691 /* Reset flags, just in case, for later calls */
1692 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1693 Mount *mount = MOUNT(u);
1694
1695 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1696 }
1697
1698 return 0;
1699 }
1700
1701 manager_dispatch_load_queue(m);
1702
1703 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1704 Mount *mount = MOUNT(u);
1705
1706 if (!mount->is_mounted) {
1707
1708 /* A mount point is not around right now. It
1709 * might be gone, or might never have
1710 * existed. */
1711
1712 if (mount->from_proc_self_mountinfo &&
1713 mount->parameters_proc_self_mountinfo.what) {
1714
1715 /* Remember that this device might just have disappeared */
1716 if (set_ensure_allocated(&gone, &string_hash_ops) < 0 ||
1717 set_put(gone, mount->parameters_proc_self_mountinfo.what) < 0)
1718 log_oom(); /* we don't care too much about OOM here... */
1719 }
1720
1721 mount->from_proc_self_mountinfo = false;
1722
1723 switch (mount->state) {
1724
1725 case MOUNT_MOUNTED:
1726 /* This has just been unmounted by
1727 * somebody else, follow the state
1728 * change. */
1729 mount->result = MOUNT_SUCCESS; /* make sure we forget any earlier umount failures */
1730 mount_enter_dead(mount, MOUNT_SUCCESS);
1731 break;
1732
1733 default:
1734 break;
1735 }
1736
1737 } else if (mount->just_mounted || mount->just_changed) {
1738
1739 /* A mount point was added or changed */
1740
1741 switch (mount->state) {
1742
1743 case MOUNT_DEAD:
1744 case MOUNT_FAILED:
1745
1746 /* This has just been mounted by somebody else, follow the state change, but let's
1747 * generate a new invocation ID for this implicitly and automatically. */
1748 (void) unit_acquire_invocation_id(UNIT(mount));
1749 mount_enter_mounted(mount, MOUNT_SUCCESS);
1750 break;
1751
1752 case MOUNT_MOUNTING:
1753 mount_set_state(mount, MOUNT_MOUNTING_DONE);
1754 break;
1755
1756 default:
1757 /* Nothing really changed, but let's
1758 * issue an notification call
1759 * nonetheless, in case somebody is
1760 * waiting for this. (e.g. file system
1761 * ro/rw remounts.) */
1762 mount_set_state(mount, mount->state);
1763 break;
1764 }
1765 }
1766
1767 if (mount->is_mounted &&
1768 mount->from_proc_self_mountinfo &&
1769 mount->parameters_proc_self_mountinfo.what) {
1770
1771 if (set_ensure_allocated(&around, &string_hash_ops) < 0 ||
1772 set_put(around, mount->parameters_proc_self_mountinfo.what) < 0)
1773 log_oom();
1774 }
1775
1776 /* Reset the flags for later calls */
1777 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1778 }
1779
1780 SET_FOREACH(what, gone, i) {
1781 if (set_contains(around, what))
1782 continue;
1783
1784 /* Let the device units know that the device is no longer mounted */
1785 (void) device_found_node(m, what, false, DEVICE_FOUND_MOUNT, true);
1786 }
1787
1788 return 0;
1789 }
1790
1791 static void mount_reset_failed(Unit *u) {
1792 Mount *m = MOUNT(u);
1793
1794 assert(m);
1795
1796 if (m->state == MOUNT_FAILED)
1797 mount_set_state(m, MOUNT_DEAD);
1798
1799 m->result = MOUNT_SUCCESS;
1800 m->reload_result = MOUNT_SUCCESS;
1801 }
1802
1803 static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1804 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1805 }
1806
1807 static int mount_control_pid(Unit *u) {
1808 Mount *m = MOUNT(u);
1809
1810 assert(m);
1811
1812 return m->control_pid;
1813 }
1814
1815 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1816 [MOUNT_EXEC_MOUNT] = "ExecMount",
1817 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1818 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1819 };
1820
1821 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1822
1823 static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1824 [MOUNT_SUCCESS] = "success",
1825 [MOUNT_FAILURE_RESOURCES] = "resources",
1826 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1827 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1828 [MOUNT_FAILURE_SIGNAL] = "signal",
1829 [MOUNT_FAILURE_CORE_DUMP] = "core-dump",
1830 [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1831 };
1832
1833 DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1834
1835 const UnitVTable mount_vtable = {
1836 .object_size = sizeof(Mount),
1837 .exec_context_offset = offsetof(Mount, exec_context),
1838 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1839 .kill_context_offset = offsetof(Mount, kill_context),
1840 .exec_runtime_offset = offsetof(Mount, exec_runtime),
1841 .dynamic_creds_offset = offsetof(Mount, dynamic_creds),
1842
1843 .sections =
1844 "Unit\0"
1845 "Mount\0"
1846 "Install\0",
1847 .private_section = "Mount",
1848
1849 .init = mount_init,
1850 .load = mount_load,
1851 .done = mount_done,
1852
1853 .coldplug = mount_coldplug,
1854
1855 .dump = mount_dump,
1856
1857 .start = mount_start,
1858 .stop = mount_stop,
1859 .reload = mount_reload,
1860
1861 .kill = mount_kill,
1862
1863 .serialize = mount_serialize,
1864 .deserialize_item = mount_deserialize_item,
1865
1866 .active_state = mount_active_state,
1867 .sub_state_to_string = mount_sub_state_to_string,
1868
1869 .check_gc = mount_check_gc,
1870
1871 .sigchld_event = mount_sigchld_event,
1872
1873 .reset_failed = mount_reset_failed,
1874
1875 .control_pid = mount_control_pid,
1876
1877 .bus_vtable = bus_mount_vtable,
1878 .bus_set_property = bus_mount_set_property,
1879 .bus_commit_properties = bus_mount_commit_properties,
1880
1881 .get_timeout = mount_get_timeout,
1882
1883 .can_transient = true,
1884
1885 .enumerate = mount_enumerate,
1886 .shutdown = mount_shutdown,
1887
1888 .status_message_formats = {
1889 .starting_stopping = {
1890 [0] = "Mounting %s...",
1891 [1] = "Unmounting %s...",
1892 },
1893 .finished_start_job = {
1894 [JOB_DONE] = "Mounted %s.",
1895 [JOB_FAILED] = "Failed to mount %s.",
1896 [JOB_TIMEOUT] = "Timed out mounting %s.",
1897 },
1898 .finished_stop_job = {
1899 [JOB_DONE] = "Unmounted %s.",
1900 [JOB_FAILED] = "Failed unmounting %s.",
1901 [JOB_TIMEOUT] = "Timed out unmounting %s.",
1902 },
1903 },
1904 };