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