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