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