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