chiark / gitweb /
service: optionally, create INIT_PROCESS/DEAD_PROCESS entries for a service
[elogind.git] / src / mount.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #include "unit-name.h"
36 #include "dbus-mount.h"
37 #include "special.h"
38
39 static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
40         [MOUNT_DEAD] = UNIT_INACTIVE,
41         [MOUNT_MOUNTING] = UNIT_ACTIVATING,
42         [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
43         [MOUNT_MOUNTED] = UNIT_ACTIVE,
44         [MOUNT_REMOUNTING] = UNIT_RELOADING,
45         [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
46         [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
47         [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
48         [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
49         [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
50         [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
51         [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
52         [MOUNT_FAILED] = UNIT_FAILED
53 };
54
55 static void mount_init(Unit *u) {
56         Mount *m = MOUNT(u);
57
58         assert(u);
59         assert(u->meta.load_state == UNIT_STUB);
60
61         m->timeout_usec = DEFAULT_TIMEOUT_USEC;
62         m->directory_mode = 0755;
63
64         exec_context_init(&m->exec_context);
65
66         /* We need to make sure that /bin/mount is always called in
67          * the same process group as us, so that the autofs kernel
68          * side doesn't send us another mount request while we are
69          * already trying to comply its last one. */
70         m->exec_context.same_pgrp = true;
71
72         m->timer_watch.type = WATCH_INVALID;
73
74         m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
75 }
76
77 static void mount_unwatch_control_pid(Mount *m) {
78         assert(m);
79
80         if (m->control_pid <= 0)
81                 return;
82
83         unit_unwatch_pid(UNIT(m), m->control_pid);
84         m->control_pid = 0;
85 }
86
87 static void mount_parameters_done(MountParameters *p) {
88         assert(p);
89
90         free(p->what);
91         free(p->options);
92         free(p->fstype);
93
94         p->what = p->options = p->fstype = NULL;
95 }
96
97 static void mount_done(Unit *u) {
98         Mount *m = MOUNT(u);
99
100         assert(m);
101
102         free(m->where);
103         m->where = NULL;
104
105         mount_parameters_done(&m->parameters_etc_fstab);
106         mount_parameters_done(&m->parameters_proc_self_mountinfo);
107         mount_parameters_done(&m->parameters_fragment);
108
109         exec_context_done(&m->exec_context);
110         exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
111         m->control_command = NULL;
112
113         mount_unwatch_control_pid(m);
114
115         unit_unwatch_timer(u, &m->timer_watch);
116 }
117
118 static int mount_add_mount_links(Mount *m) {
119         Meta *other;
120         int r;
121         MountParameters *pm;
122
123         assert(m);
124
125         if (m->from_fragment)
126                 pm = &m->parameters_fragment;
127         else if (m->from_etc_fstab)
128                 pm = &m->parameters_etc_fstab;
129         else
130                 pm = NULL;
131
132         /* Adds in links to other mount points that might lie below or
133          * above us in the hierarchy */
134
135         LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_MOUNT]) {
136                 Mount *n = (Mount*) other;
137                 MountParameters *pn;
138
139                 if (n == m)
140                         continue;
141
142                 if (n->meta.load_state != UNIT_LOADED)
143                         continue;
144
145                 if (n->from_fragment)
146                         pn = &n->parameters_fragment;
147                 else if (n->from_etc_fstab)
148                         pn = &n->parameters_etc_fstab;
149                 else
150                         pn = NULL;
151
152                 if (path_startswith(m->where, n->where)) {
153
154                         if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
155                                 return r;
156
157                         if (n->from_etc_fstab || n->from_fragment)
158                                 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
159                                         return r;
160
161                 } else if (path_startswith(n->where, m->where)) {
162
163                         if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
164                                 return r;
165
166                         if (m->from_etc_fstab || m->from_fragment)
167                                 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
168                                         return r;
169
170                 } else if (pm && path_startswith(pm->what, n->where)) {
171
172                         if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(n), true)) < 0)
173                                 return r;
174
175                         if (m->from_etc_fstab || m->from_fragment)
176                                 if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(n), true)) < 0)
177                                         return r;
178
179                 } else if (pn && path_startswith(pn->what, m->where)) {
180
181                         if ((r = unit_add_dependency(UNIT(n), UNIT_AFTER, UNIT(m), true)) < 0)
182                                 return r;
183
184                         if (n->from_etc_fstab || n->from_fragment)
185                                 if ((r = unit_add_dependency(UNIT(n), UNIT_REQUIRES, UNIT(m), true)) < 0)
186                                         return r;
187                 }
188         }
189
190         return 0;
191 }
192
193 static int mount_add_swap_links(Mount *m) {
194         Meta *other;
195         int r;
196
197         assert(m);
198
199         LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SWAP])
200                 if ((r = swap_add_one_mount_link((Swap*) other, m)) < 0)
201                         return r;
202
203         return 0;
204 }
205
206 static int mount_add_path_links(Mount *m) {
207         Meta *other;
208         int r;
209
210         assert(m);
211
212         LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_PATH])
213                 if ((r = path_add_one_mount_link((Path*) other, m)) < 0)
214                         return r;
215
216         return 0;
217 }
218
219 static int mount_add_automount_links(Mount *m) {
220         Meta *other;
221         int r;
222
223         assert(m);
224
225         LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_AUTOMOUNT])
226                 if ((r = automount_add_one_mount_link((Automount*) other, m)) < 0)
227                         return r;
228
229         return 0;
230 }
231
232 static int mount_add_socket_links(Mount *m) {
233         Meta *other;
234         int r;
235
236         assert(m);
237
238         LIST_FOREACH(units_per_type, other, m->meta.manager->units_per_type[UNIT_SOCKET])
239                 if ((r = socket_add_one_mount_link((Socket*) other, m)) < 0)
240                         return r;
241
242         return 0;
243 }
244
245 static char* mount_test_option(const char *haystack, const char *needle) {
246         struct mntent me;
247
248         assert(needle);
249
250         /* Like glibc's hasmntopt(), but works on a string, not a
251          * struct mntent */
252
253         if (!haystack)
254                 return false;
255
256         zero(me);
257         me.mnt_opts = (char*) haystack;
258
259         return hasmntopt(&me, needle);
260 }
261
262 static int mount_add_target_links(Mount *m) {
263         const char *target, *after = NULL;
264         MountParameters *p;
265         Unit *tu;
266         int r;
267         bool noauto, handle, automount, user;
268
269         assert(m);
270
271         if (m->from_fragment)
272                 p = &m->parameters_fragment;
273         else if (m->from_etc_fstab)
274                 p = &m->parameters_etc_fstab;
275         else
276                 return 0;
277
278         noauto = !!mount_test_option(p->options, MNTOPT_NOAUTO);
279         user = mount_test_option(p->options, "user") || mount_test_option(p->options, "users");
280         handle = !!mount_test_option(p->options, "comment=systemd.mount") ||
281                 m->meta.manager->mount_auto;
282         automount = !!mount_test_option(p->options, "comment=systemd.automount");
283
284         if (mount_test_option(p->options, "_netdev") ||
285             (p->fstype && fstype_is_network(p->fstype))) {
286                 target = SPECIAL_REMOTE_FS_TARGET;
287
288                 if (m->meta.manager->running_as == MANAGER_SYSTEM)
289                         after = SPECIAL_NETWORK_TARGET;
290         } else
291                 target = SPECIAL_LOCAL_FS_TARGET;
292
293         if ((r = manager_load_unit(m->meta.manager, target, NULL, NULL, &tu)) < 0)
294                 return r;
295
296         if (after)
297                 if ((r = unit_add_dependency_by_name(tu, UNIT_AFTER, after, NULL, true)) < 0)
298                         return r;
299
300         if (automount && m->meta.manager->running_as == MANAGER_SYSTEM) {
301                 Unit *am;
302
303                 if ((r = unit_load_related_unit(UNIT(m), ".automount", &am)) < 0)
304                         return r;
305
306                 return unit_add_two_dependencies(tu, UNIT_AFTER, UNIT_WANTS, UNIT(am), true);
307         } else {
308
309                 /* Automatically add mount points that aren't natively
310                  * configured to local-fs.target */
311                 if (!noauto &&
312                     handle &&
313                     !m->from_fragment)
314                         if (user || m->meta.manager->running_as == MANAGER_SYSTEM)
315                                 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m), true)) < 0)
316                                         return r;
317
318                 return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu, true);
319         }
320 }
321
322 static bool mount_is_bind(MountParameters *p) {
323         assert(p);
324
325         if (p->fstype && streq(p->fstype, "bind"))
326                 return true;
327
328         if (mount_test_option(p->options, "bind"))
329                 return true;
330
331         return false;
332 }
333
334 static int mount_add_device_links(Mount *m) {
335         MountParameters *p;
336         bool nofail, noauto;
337
338         assert(m);
339
340         if (m->from_fragment)
341                 p = &m->parameters_fragment;
342         else if (m->from_etc_fstab)
343                 p = &m->parameters_etc_fstab;
344         else
345                 return 0;
346
347         if (!p->what || path_equal(m->where, "/"))
348                 return 0;
349
350         if (mount_is_bind(p))
351                 return 0;
352
353         noauto = !!mount_test_option(p->options, MNTOPT_NOAUTO);
354         nofail = !!mount_test_option(p->options, "nofail");
355
356         return unit_add_node_link(UNIT(m), p->what,
357                                   !noauto && nofail &&
358                                   UNIT(m)->meta.manager->running_as == MANAGER_SYSTEM);
359 }
360
361 static int mount_add_default_dependencies(Mount *m) {
362         int r;
363
364         assert(m);
365
366         if (m->meta.manager->running_as == MANAGER_SYSTEM &&
367             !path_equal(m->where, "/")) {
368
369                 if ((r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_FSCK_TARGET, NULL, true)) < 0)
370                         return r;
371
372                 if ((r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
373                         return r;
374         }
375
376         return 0;
377 }
378
379 static int mount_verify(Mount *m) {
380         bool b;
381         char *e;
382         assert(m);
383
384         if (m->meta.load_state != UNIT_LOADED)
385                 return 0;
386
387         if (!m->from_etc_fstab && !m->from_fragment && !m->from_proc_self_mountinfo)
388                 return -ENOENT;
389
390         if (!(e = unit_name_from_path(m->where, ".mount")))
391                 return -ENOMEM;
392
393         b = unit_has_name(UNIT(m), e);
394         free(e);
395
396         if (!b) {
397                 log_error("%s's Where setting doesn't match unit name. Refusing.", m->meta.id);
398                 return -EINVAL;
399         }
400
401         if (m->meta.fragment_path && !m->parameters_fragment.what) {
402                 log_error("%s's What setting is missing. Refusing.", m->meta.id);
403                 return -EBADMSG;
404         }
405
406         if (m->exec_context.pam_name && m->exec_context.kill_mode != KILL_CONTROL_GROUP) {
407                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", m->meta.id);
408                 return -EINVAL;
409         }
410
411         return 0;
412 }
413
414 static int mount_load(Unit *u) {
415         Mount *m = MOUNT(u);
416         int r;
417
418         assert(u);
419         assert(u->meta.load_state == UNIT_STUB);
420
421         if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
422                 return r;
423
424         /* This is a new unit? Then let's add in some extras */
425         if (u->meta.load_state == UNIT_LOADED) {
426                 const char *what = NULL;
427
428                 if (m->meta.fragment_path)
429                         m->from_fragment = true;
430
431                 if (!m->where)
432                         if (!(m->where = unit_name_to_path(u->meta.id)))
433                                 return -ENOMEM;
434
435                 path_kill_slashes(m->where);
436
437                 if (!m->meta.description)
438                         if ((r = unit_set_description(u, m->where)) < 0)
439                                 return r;
440
441                 if (m->from_fragment && m->parameters_fragment.what)
442                         what = m->parameters_fragment.what;
443                 else if (m->from_etc_fstab && m->parameters_etc_fstab.what)
444                         what = m->parameters_etc_fstab.what;
445                 else if (m->from_proc_self_mountinfo && m->parameters_proc_self_mountinfo.what)
446                         what = m->parameters_proc_self_mountinfo.what;
447
448                 if ((r = mount_add_device_links(m)) < 0)
449                         return r;
450
451                 if ((r = mount_add_mount_links(m)) < 0)
452                         return r;
453
454                 if ((r = mount_add_socket_links(m)) < 0)
455                         return r;
456
457                 if ((r = mount_add_swap_links(m)) < 0)
458                         return r;
459
460                 if ((r = mount_add_path_links(m)) < 0)
461                         return r;
462
463                 if ((r = mount_add_automount_links(m)) < 0)
464                         return r;
465
466                 if ((r = mount_add_target_links(m)) < 0)
467                         return r;
468
469                 if ((r = unit_add_default_cgroup(u)) < 0)
470                         return r;
471
472                 if (m->meta.default_dependencies)
473                         if ((r = mount_add_default_dependencies(m)) < 0)
474                                 return r;
475         }
476
477         return mount_verify(m);
478 }
479
480 static int mount_notify_automount(Mount *m, int status) {
481         Unit *p;
482         int r;
483
484         assert(m);
485
486         if ((r = unit_get_related_unit(UNIT(m), ".automount", &p)) < 0)
487                 return r == -ENOENT ? 0 : r;
488
489         return automount_send_ready(AUTOMOUNT(p), status);
490 }
491
492 static void mount_set_state(Mount *m, MountState state) {
493         MountState old_state;
494         assert(m);
495
496         old_state = m->state;
497         m->state = state;
498
499         if (state != MOUNT_MOUNTING &&
500             state != MOUNT_MOUNTING_DONE &&
501             state != MOUNT_REMOUNTING &&
502             state != MOUNT_UNMOUNTING &&
503             state != MOUNT_MOUNTING_SIGTERM &&
504             state != MOUNT_MOUNTING_SIGKILL &&
505             state != MOUNT_UNMOUNTING_SIGTERM &&
506             state != MOUNT_UNMOUNTING_SIGKILL &&
507             state != MOUNT_REMOUNTING_SIGTERM &&
508             state != MOUNT_REMOUNTING_SIGKILL) {
509                 unit_unwatch_timer(UNIT(m), &m->timer_watch);
510                 mount_unwatch_control_pid(m);
511                 m->control_command = NULL;
512                 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
513         }
514
515         if (state == MOUNT_MOUNTED ||
516             state == MOUNT_REMOUNTING)
517                 mount_notify_automount(m, 0);
518         else if (state == MOUNT_DEAD ||
519                  state == MOUNT_UNMOUNTING ||
520                  state == MOUNT_MOUNTING_SIGTERM ||
521                  state == MOUNT_MOUNTING_SIGKILL ||
522                  state == MOUNT_REMOUNTING_SIGTERM ||
523                  state == MOUNT_REMOUNTING_SIGKILL ||
524                  state == MOUNT_UNMOUNTING_SIGTERM ||
525                  state == MOUNT_UNMOUNTING_SIGKILL ||
526                  state == MOUNT_FAILED)
527                 mount_notify_automount(m, -ENODEV);
528
529         if (state != old_state)
530                 log_debug("%s changed %s -> %s",
531                           m->meta.id,
532                           mount_state_to_string(old_state),
533                           mount_state_to_string(state));
534
535         unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]);
536 }
537
538 static int mount_coldplug(Unit *u) {
539         Mount *m = MOUNT(u);
540         MountState new_state = MOUNT_DEAD;
541         int r;
542
543         assert(m);
544         assert(m->state == MOUNT_DEAD);
545
546         if (m->deserialized_state != m->state)
547                 new_state = m->deserialized_state;
548         else if (m->from_proc_self_mountinfo)
549                 new_state = MOUNT_MOUNTED;
550
551         if (new_state != m->state) {
552
553                 if (new_state == MOUNT_MOUNTING ||
554                     new_state == MOUNT_MOUNTING_DONE ||
555                     new_state == MOUNT_REMOUNTING ||
556                     new_state == MOUNT_UNMOUNTING ||
557                     new_state == MOUNT_MOUNTING_SIGTERM ||
558                     new_state == MOUNT_MOUNTING_SIGKILL ||
559                     new_state == MOUNT_UNMOUNTING_SIGTERM ||
560                     new_state == MOUNT_UNMOUNTING_SIGKILL ||
561                     new_state == MOUNT_REMOUNTING_SIGTERM ||
562                     new_state == MOUNT_REMOUNTING_SIGKILL) {
563
564                         if (m->control_pid <= 0)
565                                 return -EBADMSG;
566
567                         if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
568                                 return r;
569
570                         if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
571                                 return r;
572                 }
573
574                 mount_set_state(m, new_state);
575         }
576
577         return 0;
578 }
579
580 static void mount_dump(Unit *u, FILE *f, const char *prefix) {
581         Mount *m = MOUNT(u);
582         MountParameters *p;
583
584         assert(m);
585         assert(f);
586
587         if (m->from_proc_self_mountinfo)
588                 p = &m->parameters_proc_self_mountinfo;
589         else if (m->from_fragment)
590                 p = &m->parameters_fragment;
591         else
592                 p = &m->parameters_etc_fstab;
593
594         fprintf(f,
595                 "%sMount State: %s\n"
596                 "%sWhere: %s\n"
597                 "%sWhat: %s\n"
598                 "%sFile System Type: %s\n"
599                 "%sOptions: %s\n"
600                 "%sFrom /etc/fstab: %s\n"
601                 "%sFrom /proc/self/mountinfo: %s\n"
602                 "%sFrom fragment: %s\n"
603                 "%sDirectoryMode: %04o\n",
604                 prefix, mount_state_to_string(m->state),
605                 prefix, m->where,
606                 prefix, strna(p->what),
607                 prefix, strna(p->fstype),
608                 prefix, strna(p->options),
609                 prefix, yes_no(m->from_etc_fstab),
610                 prefix, yes_no(m->from_proc_self_mountinfo),
611                 prefix, yes_no(m->from_fragment),
612                 prefix, m->directory_mode);
613
614         if (m->control_pid > 0)
615                 fprintf(f,
616                         "%sControl PID: %lu\n",
617                         prefix, (unsigned long) m->control_pid);
618
619         exec_context_dump(&m->exec_context, f, prefix);
620 }
621
622 static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
623         pid_t pid;
624         int r;
625
626         assert(m);
627         assert(c);
628         assert(_pid);
629
630         if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
631                 goto fail;
632
633         if ((r = exec_spawn(c,
634                             NULL,
635                             &m->exec_context,
636                             NULL, 0,
637                             m->meta.manager->environment,
638                             true,
639                             true,
640                             true,
641                             m->meta.manager->confirm_spawn,
642                             m->meta.cgroup_bondings,
643                             &pid)) < 0)
644                 goto fail;
645
646         if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
647                 /* FIXME: we need to do something here */
648                 goto fail;
649
650         *_pid = pid;
651
652         return 0;
653
654 fail:
655         unit_unwatch_timer(UNIT(m), &m->timer_watch);
656
657         return r;
658 }
659
660 static void mount_enter_dead(Mount *m, bool success) {
661         assert(m);
662
663         if (!success)
664                 m->failure = true;
665
666         mount_set_state(m, m->failure ? MOUNT_FAILED : MOUNT_DEAD);
667 }
668
669 static void mount_enter_mounted(Mount *m, bool success) {
670         assert(m);
671
672         if (!success)
673                 m->failure = true;
674
675         mount_set_state(m, MOUNT_MOUNTED);
676 }
677
678 static void mount_enter_signal(Mount *m, MountState state, bool success) {
679         int r;
680         Set *pid_set = NULL;
681         bool wait_for_exit = false;
682
683         assert(m);
684
685         if (!success)
686                 m->failure = true;
687
688         if (m->exec_context.kill_mode != KILL_NONE) {
689                 int sig = (state == MOUNT_MOUNTING_SIGTERM ||
690                            state == MOUNT_UNMOUNTING_SIGTERM ||
691                            state == MOUNT_REMOUNTING_SIGTERM) ? m->exec_context.kill_signal : SIGKILL;
692
693                 if (m->control_pid > 0) {
694                         if (kill(m->exec_context.kill_mode == KILL_PROCESS_GROUP ?
695                                  -m->control_pid :
696                                  m->control_pid, sig) < 0 && errno != ESRCH)
697
698                                 log_warning("Failed to kill control process %li: %m", (long) m->control_pid);
699                         else
700                                 wait_for_exit = true;
701                 }
702
703                 if (m->exec_context.kill_mode == KILL_CONTROL_GROUP) {
704
705                         if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
706                                 r = -ENOMEM;
707                                 goto fail;
708                         }
709
710                         /* Exclude the control pid from being killed via the cgroup */
711                         if (m->control_pid > 0)
712                                 if ((r = set_put(pid_set, LONG_TO_PTR(m->control_pid))) < 0)
713                                         goto fail;
714
715                         if ((r = cgroup_bonding_kill_list(m->meta.cgroup_bondings, sig, pid_set)) < 0) {
716                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
717                                         log_warning("Failed to kill control group: %s", strerror(-r));
718                         } else if (r > 0)
719                                 wait_for_exit = true;
720
721                         set_free(pid_set);
722                 }
723         }
724
725         if (wait_for_exit) {
726                 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
727                         goto fail;
728
729                 mount_set_state(m, state);
730         } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
731                 mount_enter_mounted(m, true);
732         else
733                 mount_enter_dead(m, true);
734
735         return;
736
737 fail:
738         log_warning("%s failed to kill processes: %s", m->meta.id, strerror(-r));
739
740         if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
741                 mount_enter_mounted(m, false);
742         else
743                 mount_enter_dead(m, false);
744
745         if (pid_set)
746                 set_free(pid_set);
747 }
748
749 static void mount_enter_unmounting(Mount *m, bool success) {
750         int r;
751
752         assert(m);
753
754         if (!success)
755                 m->failure = true;
756
757         m->control_command_id = MOUNT_EXEC_UNMOUNT;
758         m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
759
760         if ((r = exec_command_set(
761                              m->control_command,
762                              "/bin/umount",
763                              m->where,
764                              NULL)) < 0)
765                 goto fail;
766
767         mount_unwatch_control_pid(m);
768
769         if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
770                 goto fail;
771
772         mount_set_state(m, MOUNT_UNMOUNTING);
773
774         return;
775
776 fail:
777         log_warning("%s failed to run 'umount' task: %s", m->meta.id, strerror(-r));
778         mount_enter_mounted(m, false);
779 }
780
781 static void mount_enter_mounting(Mount *m) {
782         int r;
783
784         assert(m);
785
786         m->control_command_id = MOUNT_EXEC_MOUNT;
787         m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
788
789         mkdir_p(m->where, m->directory_mode);
790
791         if (m->from_fragment)
792                 r = exec_command_set(
793                                 m->control_command,
794                                 "/bin/mount",
795                                 m->parameters_fragment.what,
796                                 m->where,
797                                 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
798                                 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
799                                 NULL);
800         else if (m->from_etc_fstab)
801                 r = exec_command_set(
802                                 m->control_command,
803                                 "/bin/mount",
804                                 m->where,
805                                 NULL);
806         else
807                 r = -ENOENT;
808
809         if (r < 0)
810                 goto fail;
811
812         mount_unwatch_control_pid(m);
813
814         if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
815                 goto fail;
816
817         mount_set_state(m, MOUNT_MOUNTING);
818
819         return;
820
821 fail:
822         log_warning("%s failed to run 'mount' task: %s", m->meta.id, strerror(-r));
823         mount_enter_dead(m, false);
824 }
825
826 static void mount_enter_mounting_done(Mount *m) {
827         assert(m);
828
829         mount_set_state(m, MOUNT_MOUNTING_DONE);
830 }
831
832 static void mount_enter_remounting(Mount *m, bool success) {
833         int r;
834
835         assert(m);
836
837         if (!success)
838                 m->failure = true;
839
840         m->control_command_id = MOUNT_EXEC_REMOUNT;
841         m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
842
843         if (m->from_fragment) {
844                 char *buf = NULL;
845                 const char *o;
846
847                 if (m->parameters_fragment.options) {
848                         if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
849                                 r = -ENOMEM;
850                                 goto fail;
851                         }
852
853                         o = buf;
854                 } else
855                         o = "remount";
856
857                 r = exec_command_set(
858                                 m->control_command,
859                                 "/bin/mount",
860                                 m->parameters_fragment.what,
861                                 m->where,
862                                 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
863                                 "-o", o,
864                                 NULL);
865
866                 free(buf);
867         } else if (m->from_etc_fstab)
868                 r = exec_command_set(
869                                 m->control_command,
870                                 "/bin/mount",
871                                 m->where,
872                                 "-o", "remount",
873                                 NULL);
874         else
875                 r = -ENOENT;
876
877         if (r < 0) {
878                 r = -ENOMEM;
879                 goto fail;
880         }
881
882         mount_unwatch_control_pid(m);
883
884         if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
885                 goto fail;
886
887         mount_set_state(m, MOUNT_REMOUNTING);
888
889         return;
890
891 fail:
892         log_warning("%s failed to run 'remount' task: %s", m->meta.id, strerror(-r));
893         mount_enter_mounted(m, false);
894 }
895
896 static int mount_start(Unit *u) {
897         Mount *m = MOUNT(u);
898
899         assert(m);
900
901         /* We cannot fulfill this request right now, try again later
902          * please! */
903         if (m->state == MOUNT_UNMOUNTING ||
904             m->state == MOUNT_UNMOUNTING_SIGTERM ||
905             m->state == MOUNT_UNMOUNTING_SIGKILL)
906                 return -EAGAIN;
907
908         /* Already on it! */
909         if (m->state == MOUNT_MOUNTING ||
910             m->state == MOUNT_MOUNTING_SIGTERM ||
911             m->state == MOUNT_MOUNTING_SIGKILL)
912                 return 0;
913
914         assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
915
916         m->failure = false;
917         mount_enter_mounting(m);
918         return 0;
919 }
920
921 static int mount_stop(Unit *u) {
922         Mount *m = MOUNT(u);
923
924         assert(m);
925
926         /* Already on it */
927         if (m->state == MOUNT_UNMOUNTING ||
928             m->state == MOUNT_UNMOUNTING_SIGKILL ||
929             m->state == MOUNT_UNMOUNTING_SIGTERM)
930                 return 0;
931
932         assert(m->state == MOUNT_MOUNTING ||
933                m->state == MOUNT_MOUNTING_DONE ||
934                m->state == MOUNT_MOUNTED ||
935                m->state == MOUNT_MOUNTING_SIGTERM ||
936                m->state == MOUNT_MOUNTING_SIGKILL ||
937                m->state == MOUNT_REMOUNTING ||
938                m->state == MOUNT_REMOUNTING_SIGTERM ||
939                m->state == MOUNT_REMOUNTING_SIGKILL);
940
941         mount_enter_unmounting(m, true);
942         return 0;
943 }
944
945 static int mount_reload(Unit *u) {
946         Mount *m = MOUNT(u);
947
948         assert(m);
949
950         if (m->state == MOUNT_MOUNTING_DONE)
951                 return -EAGAIN;
952
953         assert(m->state == MOUNT_MOUNTED);
954
955         mount_enter_remounting(m, true);
956         return 0;
957 }
958
959 static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
960         Mount *m = MOUNT(u);
961
962         assert(m);
963         assert(f);
964         assert(fds);
965
966         unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
967         unit_serialize_item(u, f, "failure", yes_no(m->failure));
968
969         if (m->control_pid > 0)
970                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
971
972         if (m->control_command_id >= 0)
973                 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
974
975         return 0;
976 }
977
978 static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
979         Mount *m = MOUNT(u);
980
981         assert(u);
982         assert(key);
983         assert(value);
984         assert(fds);
985
986         if (streq(key, "state")) {
987                 MountState state;
988
989                 if ((state = mount_state_from_string(value)) < 0)
990                         log_debug("Failed to parse state value %s", value);
991                 else
992                         m->deserialized_state = state;
993         } else if (streq(key, "failure")) {
994                 int b;
995
996                 if ((b = parse_boolean(value)) < 0)
997                         log_debug("Failed to parse failure value %s", value);
998                 else
999                         m->failure = b || m->failure;
1000
1001         } else if (streq(key, "control-pid")) {
1002                 pid_t pid;
1003
1004                 if (parse_pid(value, &pid) < 0)
1005                         log_debug("Failed to parse control-pid value %s", value);
1006                 else
1007                         m->control_pid = pid;
1008         } else if (streq(key, "control-command")) {
1009                 MountExecCommand id;
1010
1011                 if ((id = mount_exec_command_from_string(value)) < 0)
1012                         log_debug("Failed to parse exec-command value %s", value);
1013                 else {
1014                         m->control_command_id = id;
1015                         m->control_command = m->exec_command + id;
1016                 }
1017
1018         } else
1019                 log_debug("Unknown serialization key '%s'", key);
1020
1021         return 0;
1022 }
1023
1024 static UnitActiveState mount_active_state(Unit *u) {
1025         assert(u);
1026
1027         return state_translation_table[MOUNT(u)->state];
1028 }
1029
1030 static const char *mount_sub_state_to_string(Unit *u) {
1031         assert(u);
1032
1033         return mount_state_to_string(MOUNT(u)->state);
1034 }
1035
1036 static bool mount_check_gc(Unit *u) {
1037         Mount *m = MOUNT(u);
1038
1039         assert(m);
1040
1041         return m->from_etc_fstab || m->from_proc_self_mountinfo;
1042 }
1043
1044 static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1045         Mount *m = MOUNT(u);
1046         bool success;
1047
1048         assert(m);
1049         assert(pid >= 0);
1050
1051         if (pid != m->control_pid)
1052                 return;
1053
1054         m->control_pid = 0;
1055
1056         success = is_clean_exit(code, status);
1057         m->failure = m->failure || !success;
1058
1059         if (m->control_command) {
1060                 exec_status_exit(&m->control_command->exec_status, pid, code, status, m->exec_context.utmp_id);
1061                 m->control_command = NULL;
1062                 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1063         }
1064
1065         log_full(success ? LOG_DEBUG : LOG_NOTICE,
1066                  "%s mount process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1067
1068         /* Note that mount(8) returning and the kernel sending us a
1069          * mount table change event might happen out-of-order. If an
1070          * operation succeed we assume the kernel will follow soon too
1071          * and already change into the resulting state.  If it fails
1072          * we check if the kernel still knows about the mount. and
1073          * change state accordingly. */
1074
1075         switch (m->state) {
1076
1077         case MOUNT_MOUNTING:
1078         case MOUNT_MOUNTING_DONE:
1079         case MOUNT_MOUNTING_SIGKILL:
1080         case MOUNT_MOUNTING_SIGTERM:
1081         case MOUNT_REMOUNTING:
1082         case MOUNT_REMOUNTING_SIGKILL:
1083         case MOUNT_REMOUNTING_SIGTERM:
1084
1085                 if (success)
1086                         mount_enter_mounted(m, true);
1087                 else if (m->from_proc_self_mountinfo)
1088                         mount_enter_mounted(m, false);
1089                 else
1090                         mount_enter_dead(m, false);
1091                 break;
1092
1093         case MOUNT_UNMOUNTING:
1094         case MOUNT_UNMOUNTING_SIGKILL:
1095         case MOUNT_UNMOUNTING_SIGTERM:
1096
1097                 if (success)
1098                         mount_enter_dead(m, true);
1099                 else if (m->from_proc_self_mountinfo)
1100                         mount_enter_mounted(m, false);
1101                 else
1102                         mount_enter_dead(m, false);
1103                 break;
1104
1105         default:
1106                 assert_not_reached("Uh, control process died at wrong time.");
1107         }
1108
1109         /* Notify clients about changed exit status */
1110         unit_add_to_dbus_queue(u);
1111 }
1112
1113 static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1114         Mount *m = MOUNT(u);
1115
1116         assert(m);
1117         assert(elapsed == 1);
1118         assert(w == &m->timer_watch);
1119
1120         switch (m->state) {
1121
1122         case MOUNT_MOUNTING:
1123         case MOUNT_MOUNTING_DONE:
1124                 log_warning("%s mounting timed out. Stopping.", u->meta.id);
1125                 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
1126                 break;
1127
1128         case MOUNT_REMOUNTING:
1129                 log_warning("%s remounting timed out. Stopping.", u->meta.id);
1130                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
1131                 break;
1132
1133         case MOUNT_UNMOUNTING:
1134                 log_warning("%s unmounting timed out. Stopping.", u->meta.id);
1135                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
1136                 break;
1137
1138         case MOUNT_MOUNTING_SIGTERM:
1139                 log_warning("%s mounting timed out. Killing.", u->meta.id);
1140                 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
1141                 break;
1142
1143         case MOUNT_REMOUNTING_SIGTERM:
1144                 log_warning("%s remounting timed out. Killing.", u->meta.id);
1145                 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
1146                 break;
1147
1148         case MOUNT_UNMOUNTING_SIGTERM:
1149                 log_warning("%s unmounting timed out. Killing.", u->meta.id);
1150                 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
1151                 break;
1152
1153         case MOUNT_MOUNTING_SIGKILL:
1154         case MOUNT_REMOUNTING_SIGKILL:
1155         case MOUNT_UNMOUNTING_SIGKILL:
1156                 log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
1157
1158                 if (m->from_proc_self_mountinfo)
1159                         mount_enter_mounted(m, false);
1160                 else
1161                         mount_enter_dead(m, false);
1162                 break;
1163
1164         default:
1165                 assert_not_reached("Timeout at wrong time.");
1166         }
1167 }
1168
1169 static int mount_add_one(
1170                 Manager *m,
1171                 const char *what,
1172                 const char *where,
1173                 const char *options,
1174                 const char *fstype,
1175                 bool from_proc_self_mountinfo,
1176                 bool set_flags) {
1177         int r;
1178         Unit *u;
1179         bool delete;
1180         char *e, *w = NULL, *o = NULL, *f = NULL;
1181         MountParameters *p;
1182
1183         assert(m);
1184         assert(what);
1185         assert(where);
1186         assert(options);
1187         assert(fstype);
1188
1189         assert(!set_flags || from_proc_self_mountinfo);
1190
1191         /* Ignore API mount points. They should never be referenced in
1192          * dependencies ever. */
1193         if (mount_point_is_api(where))
1194                 return 0;
1195
1196         if (streq(fstype, "autofs"))
1197                 return 0;
1198
1199         /* probably some kind of swap, ignore */
1200         if (!is_path(where))
1201                 return 0;
1202
1203         if (!(e = unit_name_from_path(where, ".mount")))
1204                 return -ENOMEM;
1205
1206         if (!(u = manager_get_unit(m, e))) {
1207                 delete = true;
1208
1209                 if (!(u = unit_new(m))) {
1210                         free(e);
1211                         return -ENOMEM;
1212                 }
1213
1214                 r = unit_add_name(u, e);
1215                 free(e);
1216
1217                 if (r < 0)
1218                         goto fail;
1219
1220                 if (!(MOUNT(u)->where = strdup(where))) {
1221                         r = -ENOMEM;
1222                         goto fail;
1223                 }
1224
1225                 unit_add_to_load_queue(u);
1226         } else {
1227                 delete = false;
1228                 free(e);
1229         }
1230
1231         if (!(w = strdup(what)) ||
1232             !(o = strdup(options)) ||
1233             !(f = strdup(fstype))) {
1234                 r = -ENOMEM;
1235                 goto fail;
1236         }
1237
1238         if (from_proc_self_mountinfo) {
1239                 p = &MOUNT(u)->parameters_proc_self_mountinfo;
1240
1241                 if (set_flags) {
1242                         MOUNT(u)->is_mounted = true;
1243                         MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
1244                         MOUNT(u)->just_changed = !streq_ptr(p->options, o);
1245                 }
1246
1247                 MOUNT(u)->from_proc_self_mountinfo = true;
1248         } else {
1249                 p = &MOUNT(u)->parameters_etc_fstab;
1250                 MOUNT(u)->from_etc_fstab = true;
1251         }
1252
1253         free(p->what);
1254         p->what = w;
1255
1256         free(p->options);
1257         p->options = o;
1258
1259         free(p->fstype);
1260         p->fstype = f;
1261
1262         unit_add_to_dbus_queue(u);
1263
1264         return 0;
1265
1266 fail:
1267         free(w);
1268         free(o);
1269         free(f);
1270
1271         if (delete && u)
1272                 unit_free(u);
1273
1274         return r;
1275 }
1276
1277 static char *fstab_node_to_udev_node(char *p) {
1278         char *dn, *t, *u;
1279         int r;
1280
1281         /* FIXME: to follow udev's logic 100% we need to leave valid
1282          * UTF8 chars unescaped */
1283
1284         if (startswith(p, "LABEL=")) {
1285
1286                 if (!(u = unquote(p+6, "\"\'")))
1287                         return NULL;
1288
1289                 t = xescape(u, "/ ");
1290                 free(u);
1291
1292                 if (!t)
1293                         return NULL;
1294
1295                 r = asprintf(&dn, "/dev/disk/by-label/%s", t);
1296                 free(t);
1297
1298                 if (r < 0)
1299                         return NULL;
1300
1301                 return dn;
1302         }
1303
1304         if (startswith(p, "UUID=")) {
1305
1306                 if (!(u = unquote(p+5, "\"\'")))
1307                         return NULL;
1308
1309                 t = xescape(u, "/ ");
1310                 free(u);
1311
1312                 if (!t)
1313                         return NULL;
1314
1315                 r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t));
1316                 free(t);
1317
1318                 if (r < 0)
1319                         return NULL;
1320
1321                 return dn;
1322         }
1323
1324         return strdup(p);
1325 }
1326
1327 static int mount_find_pri(char *options) {
1328         char *end, *pri;
1329         unsigned long r;
1330
1331         if (!(pri = mount_test_option(options, "pri=")))
1332                 return 0;
1333
1334         pri += 4;
1335
1336         errno = 0;
1337         r = strtoul(pri, &end, 10);
1338
1339         if (errno != 0)
1340                 return -errno;
1341
1342         if (end == pri || (*end != ',' && *end != 0))
1343                 return -EINVAL;
1344
1345         return (int) r;
1346 }
1347
1348 static int mount_load_etc_fstab(Manager *m) {
1349         FILE *f;
1350         int r;
1351         struct mntent* me;
1352
1353         assert(m);
1354
1355         errno = 0;
1356         if (!(f = setmntent("/etc/fstab", "r")))
1357                 return -errno;
1358
1359         while ((me = getmntent(f))) {
1360                 char *where, *what;
1361
1362                 if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
1363                         r = -ENOMEM;
1364                         goto finish;
1365                 }
1366
1367                 if (!(where = strdup(me->mnt_dir))) {
1368                         free(what);
1369                         r = -ENOMEM;
1370                         goto finish;
1371                 }
1372
1373                 if (what[0] == '/')
1374                         path_kill_slashes(what);
1375
1376                 if (where[0] == '/')
1377                         path_kill_slashes(where);
1378
1379                 if (streq(me->mnt_type, "swap")) {
1380                         int pri;
1381
1382                         if ((pri = mount_find_pri(me->mnt_opts)) < 0)
1383                                 r = pri;
1384                         else
1385                                 r = swap_add_one(m,
1386                                                  what,
1387                                                  pri,
1388                                                  !!mount_test_option(me->mnt_opts, MNTOPT_NOAUTO),
1389                                                  !!mount_test_option(me->mnt_opts, "nofail"),
1390                                                  !!mount_test_option(me->mnt_opts, "comment=systemd.swapon"),
1391                                                  false);
1392                 } else
1393                         r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false);
1394
1395                 free(what);
1396                 free(where);
1397
1398                 if (r < 0)
1399                         goto finish;
1400         }
1401
1402         r = 0;
1403 finish:
1404
1405         endmntent(f);
1406         return r;
1407 }
1408
1409 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
1410         int r;
1411         unsigned i;
1412         char *device, *path, *options, *options2, *fstype, *d, *p, *o;
1413
1414         assert(m);
1415
1416         rewind(m->proc_self_mountinfo);
1417
1418         for (i = 1;; i++) {
1419                 int k;
1420
1421                 device = path = options = options2 = fstype = d = p = o = NULL;
1422
1423                 if ((k = fscanf(m->proc_self_mountinfo,
1424                                 "%*s "       /* (1) mount id */
1425                                 "%*s "       /* (2) parent id */
1426                                 "%*s "       /* (3) major:minor */
1427                                 "%*s "       /* (4) root */
1428                                 "%ms "       /* (5) mount point */
1429                                 "%ms"        /* (6) mount options */
1430                                 "%*[^-]"     /* (7) optional fields */
1431                                 "- "         /* (8) separator */
1432                                 "%ms "       /* (9) file system type */
1433                                 "%ms"        /* (10) mount source */
1434                                 "%ms"        /* (11) mount options 2 */
1435                                 "%*[^\n]",   /* some rubbish at the end */
1436                                 &path,
1437                                 &options,
1438                                 &fstype,
1439                                 &device,
1440                                 &options2)) != 5) {
1441
1442                         if (k == EOF)
1443                                 break;
1444
1445                         log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1446                         goto clean_up;
1447                 }
1448
1449                 if (asprintf(&o, "%s,%s", options, options2) < 0) {
1450                         r = -ENOMEM;
1451                         goto finish;
1452                 }
1453
1454                 if (!(d = cunescape(device)) ||
1455                     !(p = cunescape(path))) {
1456                         r = -ENOMEM;
1457                         goto finish;
1458                 }
1459
1460                 if ((r = mount_add_one(m, d, p, o, fstype, true, set_flags)) < 0)
1461                         goto finish;
1462
1463 clean_up:
1464                 free(device);
1465                 free(path);
1466                 free(options);
1467                 free(options2);
1468                 free(fstype);
1469                 free(d);
1470                 free(p);
1471                 free(o);
1472         }
1473
1474         r = 0;
1475
1476 finish:
1477         free(device);
1478         free(path);
1479         free(options);
1480         free(options2);
1481         free(fstype);
1482         free(d);
1483         free(p);
1484         free(o);
1485
1486         return r;
1487 }
1488
1489 static void mount_shutdown(Manager *m) {
1490         assert(m);
1491
1492         if (m->proc_self_mountinfo) {
1493                 fclose(m->proc_self_mountinfo);
1494                 m->proc_self_mountinfo = NULL;
1495         }
1496 }
1497
1498 static int mount_enumerate(Manager *m) {
1499         int r;
1500         struct epoll_event ev;
1501         assert(m);
1502
1503         if (!m->proc_self_mountinfo) {
1504                 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
1505                         return -errno;
1506
1507                 m->mount_watch.type = WATCH_MOUNT;
1508                 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
1509
1510                 zero(ev);
1511                 ev.events = EPOLLERR;
1512                 ev.data.ptr = &m->mount_watch;
1513
1514                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1515                         return -errno;
1516         }
1517
1518         if ((r = mount_load_etc_fstab(m)) < 0)
1519                 goto fail;
1520
1521         if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
1522                 goto fail;
1523
1524         return 0;
1525
1526 fail:
1527         mount_shutdown(m);
1528         return r;
1529 }
1530
1531 void mount_fd_event(Manager *m, int events) {
1532         Meta *meta;
1533         int r;
1534
1535         assert(m);
1536         assert(events == EPOLLERR);
1537
1538         /* The manager calls this for every fd event happening on the
1539          * /proc/self/mountinfo file, which informs us about mounting
1540          * table changes */
1541
1542         if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) {
1543                 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
1544
1545                 /* Reset flags, just in case, for later calls */
1546                 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1547                         Mount *mount = (Mount*) meta;
1548
1549                         mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1550                 }
1551
1552                 return;
1553         }
1554
1555         manager_dispatch_load_queue(m);
1556
1557         LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1558                 Mount *mount = (Mount*) meta;
1559
1560                 if (!mount->is_mounted) {
1561                         /* This has just been unmounted. */
1562
1563                         mount->from_proc_self_mountinfo = false;
1564
1565                         switch (mount->state) {
1566
1567                         case MOUNT_MOUNTED:
1568                                 mount_enter_dead(mount, true);
1569                                 break;
1570
1571                         default:
1572                                 mount_set_state(mount, mount->state);
1573                                 break;
1574
1575                         }
1576
1577                 } else if (mount->just_mounted || mount->just_changed) {
1578
1579                         /* New or changed entrymount */
1580
1581                         switch (mount->state) {
1582
1583                         case MOUNT_DEAD:
1584                         case MOUNT_FAILED:
1585                                 mount_enter_mounted(mount, true);
1586                                 break;
1587
1588                         case MOUNT_MOUNTING:
1589                                 mount_enter_mounting_done(mount);
1590                                 break;
1591
1592                         default:
1593                                 /* Nothing really changed, but let's
1594                                  * issue an notification call
1595                                  * nonetheless, in case somebody is
1596                                  * waiting for this. (e.g. file system
1597                                  * ro/rw remounts.) */
1598                                 mount_set_state(mount, mount->state);
1599                                 break;
1600                         }
1601                 }
1602
1603                 /* Reset the flags for later calls */
1604                 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1605         }
1606 }
1607
1608 static void mount_reset_failed(Unit *u) {
1609         Mount *m = MOUNT(u);
1610
1611         assert(m);
1612
1613         if (m->state == MOUNT_FAILED)
1614                 mount_set_state(m, MOUNT_DEAD);
1615
1616         m->failure = false;
1617 }
1618
1619 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1620         [MOUNT_DEAD] = "dead",
1621         [MOUNT_MOUNTING] = "mounting",
1622         [MOUNT_MOUNTING_DONE] = "mounting-done",
1623         [MOUNT_MOUNTED] = "mounted",
1624         [MOUNT_REMOUNTING] = "remounting",
1625         [MOUNT_UNMOUNTING] = "unmounting",
1626         [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1627         [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1628         [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1629         [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1630         [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1631         [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
1632         [MOUNT_FAILED] = "failed"
1633 };
1634
1635 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1636
1637 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1638         [MOUNT_EXEC_MOUNT] = "ExecMount",
1639         [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1640         [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1641 };
1642
1643 DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1644
1645 const UnitVTable mount_vtable = {
1646         .suffix = ".mount",
1647
1648         .no_alias = true,
1649         .no_instances = true,
1650         .no_isolate = true,
1651         .show_status = true,
1652
1653         .init = mount_init,
1654         .load = mount_load,
1655         .done = mount_done,
1656
1657         .coldplug = mount_coldplug,
1658
1659         .dump = mount_dump,
1660
1661         .start = mount_start,
1662         .stop = mount_stop,
1663         .reload = mount_reload,
1664
1665         .serialize = mount_serialize,
1666         .deserialize_item = mount_deserialize_item,
1667
1668         .active_state = mount_active_state,
1669         .sub_state_to_string = mount_sub_state_to_string,
1670
1671         .check_gc = mount_check_gc,
1672
1673         .sigchld_event = mount_sigchld_event,
1674         .timer_event = mount_timer_event,
1675
1676         .reset_failed = mount_reset_failed,
1677
1678         .bus_interface = "org.freedesktop.systemd1.Mount",
1679         .bus_message_handler = bus_mount_message_handler,
1680         .bus_invalidating_properties =  bus_mount_invalidating_properties,
1681
1682         .enumerate = mount_enumerate,
1683         .shutdown = mount_shutdown
1684 };