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