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