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