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