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