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