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