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