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