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