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