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