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