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