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