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