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