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