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