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