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