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