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