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