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