]> git.ipfire.org Git - people/ms/systemd.git/blob - mount.c
mount-setup: automatically mount binfmt_misc
[people/ms/systemd.git] / mount.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU 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 <mntent.h>
25 #include <sys/epoll.h>
26 #include <signal.h>
27
28 #include "unit.h"
29 #include "mount.h"
30 #include "load-fragment.h"
31 #include "load-dropin.h"
32 #include "log.h"
33 #include "strv.h"
34 #include "mount-setup.h"
35 #include "unit-name.h"
36
37 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
38 [MOUNT_DEAD] = UNIT_INACTIVE,
39 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
40 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
41 [MOUNT_MOUNTED] = UNIT_ACTIVE,
42 [MOUNT_REMOUNTING] = UNIT_ACTIVE_RELOADING,
43 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
44 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
45 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
46 [MOUNT_REMOUNTING_SIGTERM] = UNIT_ACTIVE_RELOADING,
47 [MOUNT_REMOUNTING_SIGKILL] = UNIT_ACTIVE_RELOADING,
48 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
49 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
50 [MOUNT_MAINTAINANCE] = UNIT_INACTIVE,
51 };
52
53 static const char* const state_string_table[_MOUNT_STATE_MAX] = {
54 [MOUNT_DEAD] = "dead",
55 [MOUNT_MOUNTING] = "mounting",
56 [MOUNT_MOUNTING_DONE] = "mounting-done",
57 [MOUNT_MOUNTED] = "mounted",
58 [MOUNT_REMOUNTING] = "remounting",
59 [MOUNT_UNMOUNTING] = "unmounting",
60 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
61 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
62 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
63 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
64 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
65 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
66 [MOUNT_MAINTAINANCE] = "maintainance"
67 };
68
69 static char *mount_name_from_where(const char *where) {
70 assert(where);
71
72 if (streq(where, "/"))
73 return strdup("-.mount");
74
75 return unit_name_build_escape(where+1, NULL, ".mount");
76 }
77
78 static void service_unwatch_control_pid(Mount *m) {
79 assert(m);
80
81 if (m->control_pid <= 0)
82 return;
83
84 unit_unwatch_pid(UNIT(m), m->control_pid);
85 m->control_pid = 0;
86 }
87
88 static void mount_parameters_done(MountParameters *p) {
89 assert(p);
90
91 free(p->what);
92 free(p->options);
93 free(p->fstype);
94
95 p->what = p->options = p->fstype = NULL;
96 }
97
98 static void mount_done(Unit *u) {
99 Mount *m = MOUNT(u);
100
101 assert(m);
102
103 free(m->where);
104 m->where = NULL;
105
106 mount_parameters_done(&m->parameters_etc_fstab);
107 mount_parameters_done(&m->parameters_proc_self_mountinfo);
108 mount_parameters_done(&m->parameters_fragment);
109
110 exec_context_done(&m->exec_context);
111 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
112 m->control_command = NULL;
113
114 service_unwatch_control_pid(m);
115
116 unit_unwatch_timer(u, &m->timer_watch);
117 }
118
119 static void mount_init(Unit *u) {
120 Mount *m = MOUNT(u);
121
122 assert(u);
123 assert(u->meta.load_state == UNIT_STUB);
124
125 m->timeout_usec = DEFAULT_TIMEOUT_USEC;
126 exec_context_init(&m->exec_context);
127
128 /* We need to make sure that /bin/mount is always called in
129 * the same process group as us, so that the autofs kernel
130 * side doesn't send us another mount request while we are
131 * already trying to comply its last one. */
132 m->exec_context.no_setsid = true;
133
134 m->timer_watch.type = WATCH_INVALID;
135 }
136
137 static int mount_notify_automount(Mount *m, int status) {
138 Unit *p;
139 char *k;
140
141 assert(m);
142
143 if (!(k = unit_name_change_suffix(UNIT(m)->meta.id, ".automount")))
144 return -ENOMEM;
145
146 p = manager_get_unit(UNIT(m)->meta.manager, k);
147 free(k);
148
149 if (!p)
150 return 0;
151
152 return automount_send_ready(AUTOMOUNT(p), status);
153 }
154
155 static int mount_add_node_links(Mount *m) {
156 Unit *device;
157 char *e;
158 int r;
159 const char *what;
160
161 assert(m);
162
163 /* Adds in links to the device that this node is based on */
164
165 if (m->parameters_fragment.what)
166 what = m->parameters_fragment.what;
167 else if (m->parameters_etc_fstab.what)
168 what = m->parameters_etc_fstab.what;
169 else
170 /* We observe kernel mounts only while they are live,
171 * hence don't create any links for them */
172 return 0;
173
174 if (!path_startswith(what, "/dev/"))
175 return 0;
176
177 if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
178 return -ENOMEM;
179
180 r = manager_load_unit(UNIT(m)->meta.manager, e, NULL, &device);
181 free(e);
182
183 if (r < 0)
184 return r;
185
186 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, device)) < 0)
187 return r;
188
189 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, device)) < 0)
190 return r;
191
192 if (UNIT(m)->meta.manager->running_as == MANAGER_INIT ||
193 UNIT(m)->meta.manager->running_as == MANAGER_SYSTEM)
194 if ((r = unit_add_dependency(device, UNIT_WANTS, UNIT(m))) < 0)
195 return r;
196
197 return 0;
198 }
199
200 static int mount_add_path_links(Mount *m) {
201 Meta *other;
202 int r;
203
204 assert(m);
205
206 /* Adds in link to other mount points, that might lie below or
207 * above us in the hierarchy */
208
209 LIST_FOREACH(units_per_type, other, UNIT(m)->meta.manager->units_per_type[UNIT_MOUNT]) {
210 Mount *n;
211
212 n = (Mount*) other;
213
214 if (n == m)
215 continue;
216
217 if (m->meta.load_state != UNIT_LOADED)
218 continue;
219
220 if (path_startswith(m->where, n->where)) {
221
222 if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(other))) < 0)
223 return r;
224
225 if (n->from_etc_fstab || n->from_fragment)
226 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(other))) < 0)
227 return r;
228
229 } else if (path_startswith(n->where, m->where)) {
230
231 if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(other))) < 0)
232 return r;
233
234 if (m->from_etc_fstab || m->from_fragment)
235 if ((r = unit_add_dependency(UNIT(other), UNIT_REQUIRES, UNIT(m))) < 0)
236 return r;
237 }
238 }
239
240 return 0;
241 }
242
243 static bool mount_test_option(const char *haystack, const char *needle) {
244 struct mntent me;
245
246 assert(needle);
247
248 /* Like glibc's hasmntopt(), but works on a string, not a
249 * struct mntent */
250
251 if (!haystack)
252 return false;
253
254 zero(me);
255 me.mnt_opts = (char*) haystack;
256
257 return !!hasmntopt(&me, needle);
258 }
259
260 static int mount_add_target_links(Mount *m) {
261 const char *target;
262 MountParameters *p;
263 Unit *u;
264 int r;
265 bool noauto;
266 bool handle;
267
268 assert(m);
269
270 if (m->from_fragment)
271 p = &m->parameters_fragment;
272 else if (m->from_etc_fstab)
273 p = &m->parameters_etc_fstab;
274 else
275 return 0;
276
277 noauto = mount_test_option(p->options, MNTOPT_NOAUTO);
278 handle = mount_test_option(p->options, "comment=systemd.mount");
279
280 if (noauto && !handle)
281 return 0;
282
283 if (mount_test_option(p->options, "_netdev") ||
284 fstype_is_network(p->fstype))
285 target = SPECIAL_REMOTE_FS_TARGET;
286 else
287 target = SPECIAL_LOCAL_FS_TARGET;
288
289 if ((r = manager_load_unit(UNIT(m)->meta.manager, target, NULL, &u)) < 0)
290 return r;
291
292 if (handle)
293 if ((r = unit_add_dependency(u, UNIT_WANTS, UNIT(m))) < 0)
294 return r;
295
296 return unit_add_dependency(UNIT(m), UNIT_BEFORE, u);
297 }
298
299 static int mount_verify(Mount *m) {
300 bool b;
301 char *e;
302 assert(m);
303
304 if (UNIT(m)->meta.load_state != UNIT_LOADED)
305 return 0;
306
307 if (!m->where) {
308 log_error("%s lacks Where setting. Refusing.", UNIT(m)->meta.id);
309 return -EINVAL;
310 }
311
312 path_kill_slashes(m->where);
313
314 if (!(e = mount_name_from_where(m->where)))
315 return -ENOMEM;
316
317 b = unit_has_name(UNIT(m), e);
318 free(e);
319
320 if (!b) {
321 log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->meta.id);
322 return -EINVAL;
323 }
324
325 return 0;
326 }
327
328 static int mount_load(Unit *u) {
329 Mount *m = MOUNT(u);
330 int r;
331
332 assert(u);
333 assert(u->meta.load_state == UNIT_STUB);
334
335 if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
336 return r;
337
338 /* This is a new unit? Then let's add in some extras */
339 if (u->meta.load_state == UNIT_LOADED) {
340
341 /* Minor validity checking */
342 if ((m->parameters_fragment.options || m->parameters_fragment.fstype) && !m->parameters_fragment.what)
343 return -EBADMSG;
344
345 if (m->parameters_fragment.what)
346 m->from_fragment = true;
347
348 if ((r = mount_add_node_links(MOUNT(u))) < 0)
349 return r;
350
351 if ((r = mount_add_path_links(MOUNT(u))) < 0)
352 return r;
353
354 if ((r = mount_add_target_links(MOUNT(u))) < 0)
355 return r;
356
357 if ((r = unit_add_default_cgroup(u)) < 0)
358 return r;
359 }
360
361 return mount_verify(m);
362 }
363
364 static void mount_set_state(Mount *m, MountState state) {
365 MountState old_state;
366 assert(m);
367
368 old_state = m->state;
369 m->state = state;
370
371 if (state != MOUNT_MOUNTING &&
372 state != MOUNT_MOUNTING_DONE &&
373 state != MOUNT_REMOUNTING &&
374 state != MOUNT_UNMOUNTING &&
375 state != MOUNT_MOUNTING_SIGTERM &&
376 state != MOUNT_MOUNTING_SIGKILL &&
377 state != MOUNT_UNMOUNTING_SIGTERM &&
378 state != MOUNT_UNMOUNTING_SIGKILL &&
379 state != MOUNT_REMOUNTING_SIGTERM &&
380 state != MOUNT_REMOUNTING_SIGKILL) {
381 unit_unwatch_timer(UNIT(m), &m->timer_watch);
382 service_unwatch_control_pid(m);
383 m->control_command = NULL;
384 }
385
386 if (state == MOUNT_MOUNTED ||
387 state == MOUNT_REMOUNTING)
388 mount_notify_automount(m, 0);
389 else if (state == MOUNT_DEAD ||
390 state == MOUNT_UNMOUNTING ||
391 state == MOUNT_MOUNTING_SIGTERM ||
392 state == MOUNT_MOUNTING_SIGKILL ||
393 state == MOUNT_REMOUNTING_SIGTERM ||
394 state == MOUNT_REMOUNTING_SIGKILL ||
395 state == MOUNT_UNMOUNTING_SIGTERM ||
396 state == MOUNT_UNMOUNTING_SIGKILL ||
397 state == MOUNT_MAINTAINANCE)
398 mount_notify_automount(m, -ENODEV);
399
400 if (state != old_state)
401 log_debug("%s changed %s → %s", UNIT(m)->meta.id, state_string_table[old_state], state_string_table[state]);
402
403 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]);
404 }
405
406 static int mount_coldplug(Unit *u) {
407 Mount *m = MOUNT(u);
408
409 assert(m);
410 assert(m->state == MOUNT_DEAD);
411
412 if (m->from_proc_self_mountinfo)
413 mount_set_state(m, MOUNT_MOUNTED);
414
415 return 0;
416 }
417
418 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
419 pid_t pid;
420 int r;
421
422 assert(m);
423 assert(c);
424 assert(_pid);
425
426 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
427 goto fail;
428
429 if ((r = exec_spawn(c,
430 NULL,
431 &m->exec_context,
432 NULL, 0,
433 true,
434 true,
435 UNIT(m)->meta.manager->confirm_spawn,
436 UNIT(m)->meta.cgroup_bondings,
437 &pid)) < 0)
438 goto fail;
439
440 if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
441 /* FIXME: we need to do something here */
442 goto fail;
443
444 *_pid = pid;
445
446 return 0;
447
448 fail:
449 unit_unwatch_timer(UNIT(m), &m->timer_watch);
450
451 return r;
452 }
453
454 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
455 Mount *m = MOUNT(u);
456 MountParameters *p;
457
458 assert(m);
459 assert(f);
460
461 if (m->from_proc_self_mountinfo)
462 p = &m->parameters_proc_self_mountinfo;
463 else if (m->from_fragment)
464 p = &m->parameters_fragment;
465 else
466 p = &m->parameters_etc_fstab;
467
468 fprintf(f,
469 "%sMount State: %s\n"
470 "%sWhere: %s\n"
471 "%sWhat: %s\n"
472 "%sFile System Type: %s\n"
473 "%sOptions: %s\n"
474 "%sFrom /etc/fstab: %s\n"
475 "%sFrom /proc/self/mountinfo: %s\n"
476 "%sFrom fragment: %s\n"
477 "%sKillMode: %s\n",
478 prefix, state_string_table[m->state],
479 prefix, m->where,
480 prefix, strna(p->what),
481 prefix, strna(p->fstype),
482 prefix, strna(p->options),
483 prefix, yes_no(m->from_etc_fstab),
484 prefix, yes_no(m->from_proc_self_mountinfo),
485 prefix, yes_no(m->from_fragment),
486 prefix, kill_mode_to_string(m->kill_mode));
487
488 if (m->control_pid > 0)
489 fprintf(f,
490 "%sControl PID: %llu\n",
491 prefix, (unsigned long long) m->control_pid);
492
493 exec_context_dump(&m->exec_context, f, prefix);
494 }
495
496 static void mount_enter_dead(Mount *m, bool success) {
497 assert(m);
498
499 if (!success)
500 m->failure = true;
501
502 mount_set_state(m, m->failure ? MOUNT_MAINTAINANCE : MOUNT_DEAD);
503 }
504
505 static void mount_enter_mounted(Mount *m, bool success) {
506 assert(m);
507
508 if (!success)
509 m->failure = true;
510
511 mount_set_state(m, MOUNT_MOUNTED);
512 }
513
514 static void mount_enter_signal(Mount *m, MountState state, bool success) {
515 int r;
516 bool sent = false;
517
518 assert(m);
519
520 if (!success)
521 m->failure = true;
522
523 if (m->kill_mode != KILL_NONE) {
524 int sig = (state == MOUNT_MOUNTING_SIGTERM ||
525 state == MOUNT_UNMOUNTING_SIGTERM ||
526 state == MOUNT_REMOUNTING_SIGTERM) ? SIGTERM : SIGKILL;
527
528 if (m->kill_mode == KILL_CONTROL_GROUP) {
529
530 if ((r = cgroup_bonding_kill_list(UNIT(m)->meta.cgroup_bondings, sig)) < 0) {
531 if (r != -EAGAIN && r != -ESRCH)
532 goto fail;
533 } else
534 sent = true;
535 }
536
537 if (!sent && m->control_pid > 0)
538 if (kill(m->kill_mode == KILL_PROCESS ? m->control_pid : -m->control_pid, sig) < 0 && errno != ESRCH) {
539 r = -errno;
540 goto fail;
541 }
542 }
543
544 if (sent) {
545 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
546 goto fail;
547
548 mount_set_state(m, state);
549 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
550 mount_enter_mounted(m, true);
551 else
552 mount_enter_dead(m, true);
553
554 return;
555
556 fail:
557 log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r));
558
559 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
560 mount_enter_mounted(m, false);
561 else
562 mount_enter_dead(m, false);
563 }
564
565 static void mount_enter_unmounting(Mount *m, bool success) {
566 ExecCommand *c;
567 int r;
568
569 assert(m);
570
571 if (!success)
572 m->failure = true;
573
574 m->control_command = c = m->exec_command + MOUNT_EXEC_UNMOUNT;
575
576 if ((r = exec_command_set(
577 c,
578 "/bin/umount",
579 m->where,
580 NULL)) < 0)
581 goto fail;
582
583 service_unwatch_control_pid(m);
584
585 if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
586 goto fail;
587
588 mount_set_state(m, MOUNT_UNMOUNTING);
589
590 return;
591
592 fail:
593 log_warning("%s failed to run umount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
594 mount_enter_mounted(m, false);
595 }
596
597 static void mount_enter_mounting(Mount *m) {
598 ExecCommand *c;
599 int r;
600
601 assert(m);
602
603 m->control_command = c = m->exec_command + MOUNT_EXEC_MOUNT;
604
605 if (m->from_fragment)
606 r = exec_command_set(
607 c,
608 "/bin/mount",
609 m->parameters_fragment.what,
610 m->where,
611 "-t", m->parameters_fragment.fstype,
612 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
613 NULL);
614 else if (m->from_etc_fstab)
615 r = exec_command_set(
616 c,
617 "/bin/mount",
618 m->where,
619 NULL);
620 else
621 r = -ENOENT;
622
623 if (r < 0)
624 goto fail;
625
626 service_unwatch_control_pid(m);
627
628 if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
629 goto fail;
630
631 mount_set_state(m, MOUNT_MOUNTING);
632
633 return;
634
635 fail:
636 log_warning("%s failed to run mount exectuable: %s", UNIT(m)->meta.id, strerror(-r));
637 mount_enter_dead(m, false);
638 }
639
640 static void mount_enter_mounting_done(Mount *m) {
641 assert(m);
642
643 mount_set_state(m, MOUNT_MOUNTING_DONE);
644 }
645
646 static void mount_enter_remounting(Mount *m, bool success) {
647 ExecCommand *c;
648 int r;
649
650 assert(m);
651
652 if (!success)
653 m->failure = true;
654
655 m->control_command = c = m->exec_command + MOUNT_EXEC_REMOUNT;
656
657 if (m->from_fragment) {
658 char *buf = NULL;
659 const char *o;
660
661 if (m->parameters_fragment.options) {
662 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
663 r = -ENOMEM;
664 goto fail;
665 }
666
667 o = buf;
668 } else
669 o = "remount";
670
671 r = exec_command_set(
672 c,
673 "/bin/mount",
674 m->parameters_fragment.what,
675 m->where,
676 "-t", m->parameters_fragment.fstype,
677 "-o", o,
678 NULL);
679
680 free(buf);
681 } else if (m->from_etc_fstab)
682 r = exec_command_set(
683 c,
684 "/bin/mount",
685 m->where,
686 "-o", "remount",
687 NULL);
688 else
689 r = -ENOENT;
690
691 if (r < 0) {
692 r = -ENOMEM;
693 goto fail;
694 }
695
696 service_unwatch_control_pid(m);
697
698 if ((r = mount_spawn(m, c, &m->control_pid)) < 0)
699 goto fail;
700
701 mount_set_state(m, MOUNT_REMOUNTING);
702
703 return;
704
705 fail:
706 mount_enter_mounted(m, false);
707 }
708
709 static int mount_start(Unit *u) {
710 Mount *m = MOUNT(u);
711
712 assert(m);
713
714 /* We cannot fulfill this request right now, try again later
715 * please! */
716 if (m->state == MOUNT_UNMOUNTING ||
717 m->state == MOUNT_UNMOUNTING_SIGTERM ||
718 m->state == MOUNT_UNMOUNTING_SIGKILL)
719 return -EAGAIN;
720
721 /* Already on it! */
722 if (m->state == MOUNT_MOUNTING ||
723 m->state == MOUNT_MOUNTING_SIGTERM ||
724 m->state == MOUNT_MOUNTING_SIGKILL)
725 return 0;
726
727 assert(m->state == MOUNT_DEAD || m->state == MOUNT_MAINTAINANCE);
728
729 m->failure = false;
730
731 mount_enter_mounting(m);
732 return 0;
733 }
734
735 static int mount_stop(Unit *u) {
736 Mount *m = MOUNT(u);
737
738 assert(m);
739
740 /* Cann't do this right now. */
741 if (m->state == MOUNT_MOUNTING ||
742 m->state == MOUNT_MOUNTING_DONE ||
743 m->state == MOUNT_MOUNTING_SIGTERM ||
744 m->state == MOUNT_MOUNTING_SIGKILL ||
745 m->state == MOUNT_REMOUNTING ||
746 m->state == MOUNT_REMOUNTING_SIGTERM ||
747 m->state == MOUNT_REMOUNTING_SIGKILL)
748 return -EAGAIN;
749
750 /* Already on it */
751 if (m->state == MOUNT_UNMOUNTING ||
752 m->state == MOUNT_UNMOUNTING_SIGKILL ||
753 m->state == MOUNT_UNMOUNTING_SIGTERM)
754 return 0;
755
756 assert(m->state == MOUNT_MOUNTED);
757
758 mount_enter_unmounting(m, true);
759 return 0;
760 }
761
762 static int mount_reload(Unit *u) {
763 Mount *m = MOUNT(u);
764
765 assert(m);
766
767 if (m->state == MOUNT_MOUNTING_DONE)
768 return -EAGAIN;
769
770 assert(m->state == MOUNT_MOUNTED);
771
772 mount_enter_remounting(m, true);
773 return 0;
774 }
775
776 static UnitActiveState mount_active_state(Unit *u) {
777 assert(u);
778
779 return state_translation_table[MOUNT(u)->state];
780 }
781
782 static const char *mount_sub_state_to_string(Unit *u) {
783 assert(u);
784
785 return state_string_table[MOUNT(u)->state];
786 }
787
788 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
789 Mount *m = MOUNT(u);
790 bool success;
791
792 assert(m);
793 assert(pid >= 0);
794
795 success = code == CLD_EXITED && status == 0;
796 m->failure = m->failure || !success;
797
798 assert(m->control_pid == pid);
799 assert(m->control_command);
800
801 exec_status_fill(&m->control_command->exec_status, pid, code, status);
802 m->control_pid = 0;
803
804 log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
805
806 /* Note that mount(8) returning and the kernel sending us a
807 * mount table change event might happen out-of-order. If an
808 * operation succeed we assume the kernel will follow soon too
809 * and already change into the resulting state. If it fails
810 * we check if the kernel still knows about the mount. and
811 * change state accordingly. */
812
813 switch (m->state) {
814
815 case MOUNT_MOUNTING:
816 case MOUNT_MOUNTING_DONE:
817 case MOUNT_MOUNTING_SIGKILL:
818 case MOUNT_MOUNTING_SIGTERM:
819 case MOUNT_REMOUNTING:
820 case MOUNT_REMOUNTING_SIGKILL:
821 case MOUNT_REMOUNTING_SIGTERM:
822
823 if (success && m->from_proc_self_mountinfo)
824 mount_enter_mounted(m, true);
825 else if (m->from_proc_self_mountinfo)
826 mount_enter_mounted(m, false);
827 else
828 mount_enter_dead(m, false);
829 break;
830
831 case MOUNT_UNMOUNTING:
832 case MOUNT_UNMOUNTING_SIGKILL:
833 case MOUNT_UNMOUNTING_SIGTERM:
834
835 if (success)
836 mount_enter_dead(m, true);
837 else if (m->from_proc_self_mountinfo)
838 mount_enter_mounted(m, false);
839 else
840 mount_enter_dead(m, false);
841 break;
842
843 default:
844 assert_not_reached("Uh, control process died at wrong time.");
845 }
846 }
847
848 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
849 Mount *m = MOUNT(u);
850
851 assert(m);
852 assert(elapsed == 1);
853 assert(w == &m->timer_watch);
854
855 switch (m->state) {
856
857 case MOUNT_MOUNTING:
858 case MOUNT_MOUNTING_DONE:
859 log_warning("%s mounting timed out. Stopping.", u->meta.id);
860 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
861 break;
862
863 case MOUNT_REMOUNTING:
864 log_warning("%s remounting timed out. Stopping.", u->meta.id);
865 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
866 break;
867
868 case MOUNT_UNMOUNTING:
869 log_warning("%s unmounting timed out. Stopping.", u->meta.id);
870 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
871 break;
872
873 case MOUNT_MOUNTING_SIGTERM:
874 log_warning("%s mounting timed out. Killing.", u->meta.id);
875 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
876 break;
877
878 case MOUNT_REMOUNTING_SIGTERM:
879 log_warning("%s remounting timed out. Killing.", u->meta.id);
880 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
881 break;
882
883 case MOUNT_UNMOUNTING_SIGTERM:
884 log_warning("%s unmounting timed out. Killing.", u->meta.id);
885 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
886 break;
887
888 case MOUNT_MOUNTING_SIGKILL:
889 case MOUNT_REMOUNTING_SIGKILL:
890 case MOUNT_UNMOUNTING_SIGKILL:
891 log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
892
893 if (m->from_proc_self_mountinfo)
894 mount_enter_mounted(m, false);
895 else
896 mount_enter_dead(m, false);
897 break;
898
899 default:
900 assert_not_reached("Timeout at wrong time.");
901 }
902 }
903
904 static int mount_add_one(
905 Manager *m,
906 const char *what,
907 const char *where,
908 const char *options,
909 const char *fstype,
910 bool from_proc_self_mountinfo,
911 bool set_flags) {
912 int r;
913 Unit *u;
914 bool delete;
915 char *e, *w = NULL, *o = NULL, *f = NULL;
916 MountParameters *mp;
917
918 assert(m);
919 assert(what);
920 assert(where);
921 assert(options);
922 assert(fstype);
923
924 assert(!set_flags || from_proc_self_mountinfo);
925
926 /* Ignore API mount points. They should never be referenced in
927 * dependencies ever. */
928 if (mount_point_is_api(where))
929 return 0;
930
931 if (streq(fstype, "autofs"))
932 return 0;
933
934 /* probably some kind of swap, which we don't cover for now */
935 if (where[0] != '/')
936 return 0;
937
938 e = mount_name_from_where(where);
939
940 if (!e)
941 return -ENOMEM;
942
943 if (!(u = manager_get_unit(m, e))) {
944 delete = true;
945
946 if (!(u = unit_new(m))) {
947 free(e);
948 return -ENOMEM;
949 }
950
951 r = unit_add_name(u, e);
952 free(e);
953
954 if (r < 0)
955 goto fail;
956
957 if (!(MOUNT(u)->where = strdup(where))) {
958 r = -ENOMEM;
959 goto fail;
960 }
961
962 if ((r = unit_set_description(u, where)) < 0)
963 goto fail;
964
965 unit_add_to_load_queue(u);
966 } else {
967 delete = false;
968 free(e);
969 }
970
971 if (!(w = strdup(what)) ||
972 !(o = strdup(options)) ||
973 !(f = strdup(fstype))) {
974 r = -ENOMEM;
975 goto fail;
976 }
977
978 if (from_proc_self_mountinfo) {
979 mp = &MOUNT(u)->parameters_proc_self_mountinfo;
980
981 if (set_flags) {
982 MOUNT(u)->is_mounted = true;
983 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
984 MOUNT(u)->just_changed = !streq_ptr(MOUNT(u)->parameters_proc_self_mountinfo.options, o);
985 }
986
987 MOUNT(u)->from_proc_self_mountinfo = true;
988
989 } else {
990 mp = &MOUNT(u)->parameters_etc_fstab;
991
992 MOUNT(u)->from_etc_fstab = true;
993 }
994
995 free(mp->what);
996 mp->what = w;
997
998 free(mp->options);
999 mp->options = o;
1000
1001 free(mp->fstype);
1002 mp->fstype = f;
1003
1004 unit_add_to_dbus_queue(u);
1005
1006 return 0;
1007
1008 fail:
1009 free(w);
1010 free(o);
1011 free(f);
1012
1013 if (delete && u)
1014 unit_free(u);
1015
1016 return 0;
1017 }
1018
1019 static char *fstab_node_to_udev_node(char *p) {
1020 char *dn, *t;
1021 int r;
1022
1023 /* FIXME: to follow udev's logic 100% we need to leave valid
1024 * UTF8 chars unescaped */
1025
1026 if (startswith(p, "LABEL=")) {
1027
1028 if (!(t = xescape(p+6, "/ ")))
1029 return NULL;
1030
1031 r = asprintf(&dn, "/dev/disk/by-label/%s", t);
1032 free(t);
1033
1034 if (r < 0)
1035 return NULL;
1036
1037 return dn;
1038 }
1039
1040 if (startswith(p, "UUID=")) {
1041
1042 if (!(t = xescape(p+5, "/ ")))
1043 return NULL;
1044
1045 r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t));
1046 free(t);
1047
1048 if (r < 0)
1049 return NULL;
1050
1051 return dn;
1052 }
1053
1054 return strdup(p);
1055 }
1056
1057 static int mount_load_etc_fstab(Manager *m) {
1058 FILE *f;
1059 int r;
1060 struct mntent* me;
1061
1062 assert(m);
1063
1064 errno = 0;
1065 if (!(f = setmntent("/etc/fstab", "r")))
1066 return -errno;
1067
1068 while ((me = getmntent(f))) {
1069 char *where, *what;
1070
1071 if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
1072 r = -ENOMEM;
1073 goto finish;
1074 }
1075
1076 if (!(where = strdup(me->mnt_dir))) {
1077 free(what);
1078 r = -ENOMEM;
1079 goto finish;
1080 }
1081
1082 if (what[0] == '/')
1083 path_kill_slashes(what);
1084
1085 if (where[0] == '/')
1086 path_kill_slashes(where);
1087
1088 r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false);
1089 free(what);
1090 free(where);
1091
1092 if (r < 0)
1093 goto finish;
1094 }
1095
1096 r = 0;
1097 finish:
1098
1099 endmntent(f);
1100 return r;
1101 }
1102
1103 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1104 int r;
1105 char *device, *path, *options, *fstype, *d, *p;
1106
1107 assert(m);
1108
1109 rewind(m->proc_self_mountinfo);
1110
1111 for (;;) {
1112 int k;
1113
1114 device = path = options = fstype = d = p = NULL;
1115
1116 if ((k = fscanf(m->proc_self_mountinfo,
1117 "%*s " /* (1) mount id */
1118 "%*s " /* (2) parent id */
1119 "%*s " /* (3) major:minor */
1120 "%*s " /* (4) root */
1121 "%ms " /* (5) mount point */
1122 "%ms" /* (6) mount options */
1123 "%*[^-]" /* (7) optional fields */
1124 "- " /* (8) seperator */
1125 "%ms " /* (9) file system type */
1126 "%ms" /* (10) mount source */
1127 "%*[^\n]", /* some rubbish at the end */
1128 &path,
1129 &options,
1130 &fstype,
1131 &device)) != 4) {
1132
1133 if (k == EOF)
1134 break;
1135
1136 r = -EBADMSG;
1137 goto finish;
1138 }
1139
1140 if (!(d = cunescape(device)) ||
1141 !(p = cunescape(path))) {
1142 r = -ENOMEM;
1143 goto finish;
1144 }
1145
1146 if ((r = mount_add_one(m, d, p, options, fstype, true, set_flags)) < 0)
1147 goto finish;
1148
1149 free(device);
1150 free(path);
1151 free(options);
1152 free(fstype);
1153 free(d);
1154 free(p);
1155 }
1156
1157 r = 0;
1158
1159 finish:
1160 free(device);
1161 free(path);
1162 free(options);
1163 free(fstype);
1164 free(d);
1165 free(p);
1166
1167 return r;
1168 }
1169
1170 static void mount_shutdown(Manager *m) {
1171 assert(m);
1172
1173 if (m->proc_self_mountinfo)
1174 fclose(m->proc_self_mountinfo);
1175 }
1176
1177 static int mount_enumerate(Manager *m) {
1178 int r;
1179 struct epoll_event ev;
1180 assert(m);
1181
1182 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "r")))
1183 return -errno;
1184
1185 m->mount_watch.type = WATCH_MOUNT;
1186 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1187
1188 zero(ev);
1189 ev.events = EPOLLERR;
1190 ev.data.ptr = &m->mount_watch;
1191
1192 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1193 return -errno;
1194
1195 if ((r = mount_load_etc_fstab(m)) < 0)
1196 goto fail;
1197
1198 if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
1199 goto fail;
1200
1201 return 0;
1202
1203 fail:
1204 mount_shutdown(m);
1205 return r;
1206 }
1207
1208 void mount_fd_event(Manager *m, int events) {
1209 Meta *meta;
1210 int r;
1211
1212 assert(m);
1213 assert(events == EPOLLERR);
1214
1215 /* The manager calls this for every fd event happening on the
1216 * /proc/self/mountinfo file, which informs us about mounting
1217 * table changes */
1218
1219 if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) {
1220 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno));
1221
1222 /* Reset flags, just in case, for later calls */
1223 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1224 Mount *mount = (Mount*) meta;
1225
1226 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1227 }
1228
1229 return;
1230 }
1231
1232 manager_dispatch_load_queue(m);
1233
1234 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1235 Mount *mount = (Mount*) meta;
1236
1237 if (!mount->is_mounted) {
1238 /* This has just been unmounted. */
1239
1240 mount->from_proc_self_mountinfo = false;
1241
1242 switch (mount->state) {
1243
1244 case MOUNT_MOUNTED:
1245 mount_enter_dead(mount, true);
1246 break;
1247
1248 default:
1249 mount_set_state(mount, mount->state);
1250 break;
1251
1252 }
1253
1254 } else if (mount->just_mounted || mount->just_changed) {
1255
1256 /* New or changed entrymount */
1257
1258 switch (mount->state) {
1259
1260 case MOUNT_DEAD:
1261 case MOUNT_MAINTAINANCE:
1262 mount_enter_mounted(mount, true);
1263 break;
1264
1265 case MOUNT_MOUNTING:
1266 mount_enter_mounting_done(mount);
1267 break;
1268
1269 default:
1270 /* Nothing really changed, but let's
1271 * issue an notification call
1272 * nonetheless, in case somebody is
1273 * waiting for this. (e.g. file system
1274 * ro/rw remounts.) */
1275 mount_set_state(mount, mount->state);
1276 break;
1277 }
1278 }
1279
1280 /* Reset the flags for later calls */
1281 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1282 }
1283 }
1284
1285 int mount_path_is_mounted(Manager *m, const char* path) {
1286 char *t;
1287 int r;
1288
1289 assert(m);
1290 assert(path);
1291
1292 if (path[0] != '/')
1293 return 1;
1294
1295 if (!(t = strdup(path)))
1296 return -ENOMEM;
1297
1298 path_kill_slashes(t);
1299
1300 for (;;) {
1301 char *e, *slash;
1302 Unit *u;
1303
1304 if (!(e = mount_name_from_where(t))) {
1305 r = -ENOMEM;
1306 goto finish;
1307 }
1308
1309 u = manager_get_unit(m, e);
1310 free(e);
1311
1312 if (u &&
1313 (MOUNT(u)->from_etc_fstab || MOUNT(u)->from_fragment) &&
1314 MOUNT(u)->state != MOUNT_MOUNTED) {
1315 r = 0;
1316 goto finish;
1317 }
1318
1319 assert_se(slash = strrchr(t, '/'));
1320
1321 if (slash == t) {
1322 r = 1;
1323 goto finish;
1324 }
1325
1326 *slash = 0;
1327 }
1328
1329 r = 1;
1330
1331 finish:
1332 free(t);
1333 return r;
1334 }
1335
1336 const UnitVTable mount_vtable = {
1337 .suffix = ".mount",
1338
1339 .no_alias = true,
1340 .no_instances = true,
1341
1342 .init = mount_init,
1343 .load = mount_load,
1344 .done = mount_done,
1345
1346 .coldplug = mount_coldplug,
1347
1348 .dump = mount_dump,
1349
1350 .start = mount_start,
1351 .stop = mount_stop,
1352 .reload = mount_reload,
1353
1354 .active_state = mount_active_state,
1355 .sub_state_to_string = mount_sub_state_to_string,
1356
1357 .sigchld_event = mount_sigchld_event,
1358 .timer_event = mount_timer_event,
1359
1360 .enumerate = mount_enumerate,
1361 .shutdown = mount_shutdown
1362 };