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