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