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