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