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