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