]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/mount.c
cgroup: by default, duplicate service cgroup in the cpu hierarchy
[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
LP
556
557 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]);
558}
559
560static int mount_coldplug(Unit *u) {
561 Mount *m = MOUNT(u);
a16e1123
LP
562 MountState new_state = MOUNT_DEAD;
563 int r;
e537352b
LP
564
565 assert(m);
566 assert(m->state == MOUNT_DEAD);
567
a16e1123
LP
568 if (m->deserialized_state != m->state)
569 new_state = m->deserialized_state;
570 else if (m->from_proc_self_mountinfo)
571 new_state = MOUNT_MOUNTED;
e537352b 572
a16e1123 573 if (new_state != m->state) {
e537352b 574
a16e1123
LP
575 if (new_state == MOUNT_MOUNTING ||
576 new_state == MOUNT_MOUNTING_DONE ||
577 new_state == MOUNT_REMOUNTING ||
578 new_state == MOUNT_UNMOUNTING ||
579 new_state == MOUNT_MOUNTING_SIGTERM ||
580 new_state == MOUNT_MOUNTING_SIGKILL ||
581 new_state == MOUNT_UNMOUNTING_SIGTERM ||
582 new_state == MOUNT_UNMOUNTING_SIGKILL ||
583 new_state == MOUNT_REMOUNTING_SIGTERM ||
584 new_state == MOUNT_REMOUNTING_SIGKILL) {
e537352b 585
a16e1123
LP
586 if (m->control_pid <= 0)
587 return -EBADMSG;
e537352b 588
a16e1123
LP
589 if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0)
590 return r;
e537352b 591
a16e1123
LP
592 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
593 return r;
594 }
e537352b 595
a16e1123
LP
596 mount_set_state(m, new_state);
597 }
e537352b
LP
598
599 return 0;
e537352b
LP
600}
601
602static void mount_dump(Unit *u, FILE *f, const char *prefix) {
603 Mount *m = MOUNT(u);
604 MountParameters *p;
605
606 assert(m);
607 assert(f);
608
609 if (m->from_proc_self_mountinfo)
610 p = &m->parameters_proc_self_mountinfo;
611 else if (m->from_fragment)
612 p = &m->parameters_fragment;
613 else
614 p = &m->parameters_etc_fstab;
615
616 fprintf(f,
617 "%sMount State: %s\n"
618 "%sWhere: %s\n"
619 "%sWhat: %s\n"
620 "%sFile System Type: %s\n"
621 "%sOptions: %s\n"
622 "%sFrom /etc/fstab: %s\n"
623 "%sFrom /proc/self/mountinfo: %s\n"
624 "%sFrom fragment: %s\n"
3e5235b0 625 "%sDirectoryMode: %04o\n",
a16e1123 626 prefix, mount_state_to_string(m->state),
e537352b
LP
627 prefix, m->where,
628 prefix, strna(p->what),
629 prefix, strna(p->fstype),
630 prefix, strna(p->options),
631 prefix, yes_no(m->from_etc_fstab),
632 prefix, yes_no(m->from_proc_self_mountinfo),
633 prefix, yes_no(m->from_fragment),
3e5235b0 634 prefix, m->directory_mode);
e537352b
LP
635
636 if (m->control_pid > 0)
637 fprintf(f,
bb00e604
LP
638 "%sControl PID: %lu\n",
639 prefix, (unsigned long) m->control_pid);
e537352b
LP
640
641 exec_context_dump(&m->exec_context, f, prefix);
642}
643
a16e1123
LP
644static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
645 pid_t pid;
646 int r;
647
648 assert(m);
649 assert(c);
650 assert(_pid);
651
652 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
653 goto fail;
654
655 if ((r = exec_spawn(c,
656 NULL,
657 &m->exec_context,
658 NULL, 0,
1137a57c 659 m->meta.manager->environment,
a16e1123
LP
660 true,
661 true,
1e3ad081 662 true,
c3686083 663 m->meta.manager->confirm_spawn,
4cd1fbcc 664 m->meta.cgroup_bondings,
a16e1123
LP
665 &pid)) < 0)
666 goto fail;
667
668 if ((r = unit_watch_pid(UNIT(m), pid)) < 0)
669 /* FIXME: we need to do something here */
670 goto fail;
671
672 *_pid = pid;
673
674 return 0;
675
676fail:
677 unit_unwatch_timer(UNIT(m), &m->timer_watch);
678
679 return r;
680}
681
e537352b
LP
682static void mount_enter_dead(Mount *m, bool success) {
683 assert(m);
684
685 if (!success)
686 m->failure = true;
687
fdf20a31 688 mount_set_state(m, m->failure ? MOUNT_FAILED : MOUNT_DEAD);
e537352b
LP
689}
690
80876c20
LP
691static void mount_enter_mounted(Mount *m, bool success) {
692 assert(m);
693
694 if (!success)
695 m->failure = true;
696
697 mount_set_state(m, MOUNT_MOUNTED);
698}
699
e537352b
LP
700static void mount_enter_signal(Mount *m, MountState state, bool success) {
701 int r;
ca949c9d
LP
702 Set *pid_set = NULL;
703 bool wait_for_exit = false;
e537352b
LP
704
705 assert(m);
706
707 if (!success)
708 m->failure = true;
709
2e22afe9 710 if (m->exec_context.kill_mode != KILL_NONE) {
80876c20
LP
711 int sig = (state == MOUNT_MOUNTING_SIGTERM ||
712 state == MOUNT_UNMOUNTING_SIGTERM ||
2e22afe9 713 state == MOUNT_REMOUNTING_SIGTERM) ? m->exec_context.kill_signal : SIGKILL;
e537352b 714
ca949c9d
LP
715 if (m->control_pid > 0) {
716 if (kill(m->exec_context.kill_mode == KILL_PROCESS_GROUP ?
717 -m->control_pid :
718 m->control_pid, sig) < 0 && errno != ESRCH)
e537352b 719
ca949c9d
LP
720 log_warning("Failed to kill control process %li: %m", (long) m->control_pid);
721 else
722 wait_for_exit = true;
e537352b
LP
723 }
724
ca949c9d 725 if (m->exec_context.kill_mode == KILL_CONTROL_GROUP) {
2e22afe9 726
ca949c9d
LP
727 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
728 r = -ENOMEM;
e537352b
LP
729 goto fail;
730 }
ca949c9d
LP
731
732 /* Exclude the control pid from being killed via the cgroup */
733 if (m->control_pid > 0)
734 if ((r = set_put(pid_set, LONG_TO_PTR(m->control_pid))) < 0)
735 goto fail;
736
737 if ((r = cgroup_bonding_kill_list(m->meta.cgroup_bondings, sig, pid_set)) < 0) {
738 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
739 log_warning("Failed to kill control group: %s", strerror(-r));
740 } else if (r > 0)
741 wait_for_exit = true;
742
743 set_free(pid_set);
744 }
e537352b
LP
745 }
746
ca949c9d 747 if (wait_for_exit) {
80876c20
LP
748 if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0)
749 goto fail;
e537352b 750
80876c20
LP
751 mount_set_state(m, state);
752 } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
753 mount_enter_mounted(m, true);
754 else
e537352b
LP
755 mount_enter_dead(m, true);
756
757 return;
758
759fail:
4cd1fbcc 760 log_warning("%s failed to kill processes: %s", m->meta.id, strerror(-r));
e537352b 761
80876c20
LP
762 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
763 mount_enter_mounted(m, false);
764 else
765 mount_enter_dead(m, false);
ca949c9d
LP
766
767 if (pid_set)
768 set_free(pid_set);
e537352b
LP
769}
770
771static void mount_enter_unmounting(Mount *m, bool success) {
e537352b
LP
772 int r;
773
774 assert(m);
775
776 if (!success)
777 m->failure = true;
778
a16e1123
LP
779 m->control_command_id = MOUNT_EXEC_UNMOUNT;
780 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
e537352b
LP
781
782 if ((r = exec_command_set(
a16e1123 783 m->control_command,
e537352b
LP
784 "/bin/umount",
785 m->where,
786 NULL)) < 0)
787 goto fail;
788
a16e1123 789 mount_unwatch_control_pid(m);
5e94833f 790
a16e1123 791 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
e537352b
LP
792 goto fail;
793
794 mount_set_state(m, MOUNT_UNMOUNTING);
795
796 return;
797
798fail:
4cd1fbcc 799 log_warning("%s failed to run 'umount' task: %s", m->meta.id, strerror(-r));
e537352b
LP
800 mount_enter_mounted(m, false);
801}
802
8d567588 803static void mount_enter_mounting(Mount *m) {
e537352b
LP
804 int r;
805
806 assert(m);
807
a16e1123
LP
808 m->control_command_id = MOUNT_EXEC_MOUNT;
809 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
e537352b 810
3e5235b0
LP
811 mkdir_p(m->where, m->directory_mode);
812
e537352b
LP
813 if (m->from_fragment)
814 r = exec_command_set(
a16e1123 815 m->control_command,
e537352b
LP
816 "/bin/mount",
817 m->parameters_fragment.what,
818 m->where,
40b8a332 819 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
8d567588 820 m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options,
e537352b
LP
821 NULL);
822 else if (m->from_etc_fstab)
823 r = exec_command_set(
a16e1123 824 m->control_command,
e537352b
LP
825 "/bin/mount",
826 m->where,
827 NULL);
828 else
829 r = -ENOENT;
830
831 if (r < 0)
832 goto fail;
833
a16e1123 834 mount_unwatch_control_pid(m);
5e94833f 835
a16e1123 836 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
e537352b
LP
837 goto fail;
838
839 mount_set_state(m, MOUNT_MOUNTING);
840
841 return;
842
843fail:
4cd1fbcc 844 log_warning("%s failed to run 'mount' task: %s", m->meta.id, strerror(-r));
e537352b
LP
845 mount_enter_dead(m, false);
846}
847
8d567588 848static void mount_enter_mounting_done(Mount *m) {
e537352b
LP
849 assert(m);
850
e537352b
LP
851 mount_set_state(m, MOUNT_MOUNTING_DONE);
852}
853
854static void mount_enter_remounting(Mount *m, bool success) {
e537352b
LP
855 int r;
856
857 assert(m);
858
859 if (!success)
860 m->failure = true;
861
a16e1123
LP
862 m->control_command_id = MOUNT_EXEC_REMOUNT;
863 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
e537352b
LP
864
865 if (m->from_fragment) {
866 char *buf = NULL;
867 const char *o;
868
869 if (m->parameters_fragment.options) {
870 if (!(buf = strappend("remount,", m->parameters_fragment.options))) {
871 r = -ENOMEM;
872 goto fail;
873 }
874
875 o = buf;
876 } else
877 o = "remount";
878
879 r = exec_command_set(
a16e1123 880 m->control_command,
e537352b
LP
881 "/bin/mount",
882 m->parameters_fragment.what,
883 m->where,
40b8a332 884 "-t", m->parameters_fragment.fstype ? m->parameters_fragment.fstype : "auto",
e537352b
LP
885 "-o", o,
886 NULL);
887
888 free(buf);
889 } else if (m->from_etc_fstab)
890 r = exec_command_set(
a16e1123 891 m->control_command,
e537352b
LP
892 "/bin/mount",
893 m->where,
894 "-o", "remount",
895 NULL);
896 else
897 r = -ENOENT;
898
60b912f6 899 if (r < 0)
e537352b 900 goto fail;
e537352b 901
a16e1123 902 mount_unwatch_control_pid(m);
5e94833f 903
a16e1123 904 if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0)
e537352b
LP
905 goto fail;
906
907 mount_set_state(m, MOUNT_REMOUNTING);
908
909 return;
910
911fail:
e364ad06 912 log_warning("%s failed to run 'remount' task: %s", m->meta.id, strerror(-r));
e537352b
LP
913 mount_enter_mounted(m, false);
914}
915
916static int mount_start(Unit *u) {
917 Mount *m = MOUNT(u);
918
919 assert(m);
920
921 /* We cannot fulfill this request right now, try again later
922 * please! */
923 if (m->state == MOUNT_UNMOUNTING ||
924 m->state == MOUNT_UNMOUNTING_SIGTERM ||
60b912f6
LP
925 m->state == MOUNT_UNMOUNTING_SIGKILL ||
926 m->state == MOUNT_MOUNTING_SIGTERM ||
927 m->state == MOUNT_MOUNTING_SIGKILL)
e537352b
LP
928 return -EAGAIN;
929
930 /* Already on it! */
60b912f6 931 if (m->state == MOUNT_MOUNTING)
e537352b
LP
932 return 0;
933
fdf20a31 934 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
e537352b
LP
935
936 m->failure = false;
8d567588 937 mount_enter_mounting(m);
e537352b
LP
938 return 0;
939}
940
941static int mount_stop(Unit *u) {
942 Mount *m = MOUNT(u);
943
944 assert(m);
945
e537352b
LP
946 /* Already on it */
947 if (m->state == MOUNT_UNMOUNTING ||
948 m->state == MOUNT_UNMOUNTING_SIGKILL ||
60b912f6
LP
949 m->state == MOUNT_UNMOUNTING_SIGTERM ||
950 m->state == MOUNT_MOUNTING_SIGTERM ||
951 m->state == MOUNT_MOUNTING_SIGKILL)
e537352b
LP
952 return 0;
953
3f6c78dc
LP
954 assert(m->state == MOUNT_MOUNTING ||
955 m->state == MOUNT_MOUNTING_DONE ||
956 m->state == MOUNT_MOUNTED ||
3f6c78dc
LP
957 m->state == MOUNT_REMOUNTING ||
958 m->state == MOUNT_REMOUNTING_SIGTERM ||
959 m->state == MOUNT_REMOUNTING_SIGKILL);
e537352b
LP
960
961 mount_enter_unmounting(m, true);
962 return 0;
963}
964
965static int mount_reload(Unit *u) {
966 Mount *m = MOUNT(u);
967
968 assert(m);
969
970 if (m->state == MOUNT_MOUNTING_DONE)
971 return -EAGAIN;
972
973 assert(m->state == MOUNT_MOUNTED);
974
975 mount_enter_remounting(m, true);
976 return 0;
977}
978
a16e1123
LP
979static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
980 Mount *m = MOUNT(u);
981
982 assert(m);
983 assert(f);
984 assert(fds);
985
986 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
987 unit_serialize_item(u, f, "failure", yes_no(m->failure));
988
989 if (m->control_pid > 0)
5925dd3c 990 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) m->control_pid);
a16e1123
LP
991
992 if (m->control_command_id >= 0)
993 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
994
995 return 0;
996}
997
998static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
999 Mount *m = MOUNT(u);
a16e1123
LP
1000
1001 assert(u);
1002 assert(key);
1003 assert(value);
1004 assert(fds);
1005
1006 if (streq(key, "state")) {
1007 MountState state;
1008
1009 if ((state = mount_state_from_string(value)) < 0)
1010 log_debug("Failed to parse state value %s", value);
1011 else
1012 m->deserialized_state = state;
1013 } else if (streq(key, "failure")) {
1014 int b;
1015
1016 if ((b = parse_boolean(value)) < 0)
1017 log_debug("Failed to parse failure value %s", value);
1018 else
1019 m->failure = b || m->failure;
1020
1021 } else if (streq(key, "control-pid")) {
5925dd3c 1022 pid_t pid;
a16e1123 1023
e364ad06 1024 if (parse_pid(value, &pid) < 0)
a16e1123
LP
1025 log_debug("Failed to parse control-pid value %s", value);
1026 else
5925dd3c 1027 m->control_pid = pid;
a16e1123
LP
1028 } else if (streq(key, "control-command")) {
1029 MountExecCommand id;
1030
1031 if ((id = mount_exec_command_from_string(value)) < 0)
1032 log_debug("Failed to parse exec-command value %s", value);
1033 else {
1034 m->control_command_id = id;
1035 m->control_command = m->exec_command + id;
1036 }
1037
1038 } else
1039 log_debug("Unknown serialization key '%s'", key);
1040
1041 return 0;
1042}
1043
e537352b
LP
1044static UnitActiveState mount_active_state(Unit *u) {
1045 assert(u);
1046
1047 return state_translation_table[MOUNT(u)->state];
1048}
1049
10a94420
LP
1050static const char *mount_sub_state_to_string(Unit *u) {
1051 assert(u);
1052
a16e1123 1053 return mount_state_to_string(MOUNT(u)->state);
10a94420
LP
1054}
1055
701cc384
LP
1056static bool mount_check_gc(Unit *u) {
1057 Mount *m = MOUNT(u);
1058
1059 assert(m);
1060
1061 return m->from_etc_fstab || m->from_proc_self_mountinfo;
1062}
1063
e537352b
LP
1064static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1065 Mount *m = MOUNT(u);
1066 bool success;
1067
1068 assert(m);
1069 assert(pid >= 0);
1070
8c47c732
LP
1071 if (pid != m->control_pid)
1072 return;
e537352b 1073
e537352b
LP
1074 m->control_pid = 0;
1075
8c47c732
LP
1076 success = is_clean_exit(code, status);
1077 m->failure = m->failure || !success;
1078
a16e1123 1079 if (m->control_command) {
169c1bda 1080 exec_status_exit(&m->control_command->exec_status, pid, code, status, m->exec_context.utmp_id);
a16e1123
LP
1081 m->control_command = NULL;
1082 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1083 }
1084
92abbefb
LP
1085 log_full(success ? LOG_DEBUG : LOG_NOTICE,
1086 "%s mount process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
e537352b
LP
1087
1088 /* Note that mount(8) returning and the kernel sending us a
1089 * mount table change event might happen out-of-order. If an
1090 * operation succeed we assume the kernel will follow soon too
1091 * and already change into the resulting state. If it fails
1092 * we check if the kernel still knows about the mount. and
1093 * change state accordingly. */
1094
1095 switch (m->state) {
1096
1097 case MOUNT_MOUNTING:
1098 case MOUNT_MOUNTING_DONE:
1099 case MOUNT_MOUNTING_SIGKILL:
1100 case MOUNT_MOUNTING_SIGTERM:
1101 case MOUNT_REMOUNTING:
1102 case MOUNT_REMOUNTING_SIGKILL:
1103 case MOUNT_REMOUNTING_SIGTERM:
1104
19b160fa 1105 if (success)
e537352b
LP
1106 mount_enter_mounted(m, true);
1107 else if (m->from_proc_self_mountinfo)
1108 mount_enter_mounted(m, false);
1109 else
1110 mount_enter_dead(m, false);
1111 break;
1112
1113 case MOUNT_UNMOUNTING:
1114 case MOUNT_UNMOUNTING_SIGKILL:
1115 case MOUNT_UNMOUNTING_SIGTERM:
1116
1117 if (success)
1118 mount_enter_dead(m, true);
1119 else if (m->from_proc_self_mountinfo)
1120 mount_enter_mounted(m, false);
1121 else
1122 mount_enter_dead(m, false);
1123 break;
1124
1125 default:
1126 assert_not_reached("Uh, control process died at wrong time.");
1127 }
c4e2ceae
LP
1128
1129 /* Notify clients about changed exit status */
1130 unit_add_to_dbus_queue(u);
e537352b
LP
1131}
1132
1133static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
1134 Mount *m = MOUNT(u);
1135
1136 assert(m);
1137 assert(elapsed == 1);
1138 assert(w == &m->timer_watch);
1139
1140 switch (m->state) {
1141
1142 case MOUNT_MOUNTING:
1143 case MOUNT_MOUNTING_DONE:
9e2f7c11 1144 log_warning("%s mounting timed out. Stopping.", u->meta.id);
e537352b
LP
1145 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false);
1146 break;
1147
1148 case MOUNT_REMOUNTING:
9e2f7c11 1149 log_warning("%s remounting timed out. Stopping.", u->meta.id);
e537352b
LP
1150 mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false);
1151 break;
1152
1153 case MOUNT_UNMOUNTING:
9e2f7c11 1154 log_warning("%s unmounting timed out. Stopping.", u->meta.id);
e537352b
LP
1155 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false);
1156 break;
1157
1158 case MOUNT_MOUNTING_SIGTERM:
9e2f7c11 1159 log_warning("%s mounting timed out. Killing.", u->meta.id);
e537352b
LP
1160 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false);
1161 break;
1162
1163 case MOUNT_REMOUNTING_SIGTERM:
9e2f7c11 1164 log_warning("%s remounting timed out. Killing.", u->meta.id);
e537352b
LP
1165 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false);
1166 break;
1167
1168 case MOUNT_UNMOUNTING_SIGTERM:
9e2f7c11 1169 log_warning("%s unmounting timed out. Killing.", u->meta.id);
e537352b
LP
1170 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false);
1171 break;
1172
1173 case MOUNT_MOUNTING_SIGKILL:
1174 case MOUNT_REMOUNTING_SIGKILL:
1175 case MOUNT_UNMOUNTING_SIGKILL:
9e2f7c11 1176 log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id);
e537352b
LP
1177
1178 if (m->from_proc_self_mountinfo)
1179 mount_enter_mounted(m, false);
1180 else
1181 mount_enter_dead(m, false);
1182 break;
1183
1184 default:
1185 assert_not_reached("Timeout at wrong time.");
1186 }
1187}
1188
1189static int mount_add_one(
1190 Manager *m,
1191 const char *what,
1192 const char *where,
1193 const char *options,
1194 const char *fstype,
9fff8a1f 1195 int passno,
e537352b
LP
1196 bool from_proc_self_mountinfo,
1197 bool set_flags) {
b08d03ff
LP
1198 int r;
1199 Unit *u;
1200 bool delete;
e537352b 1201 char *e, *w = NULL, *o = NULL, *f = NULL;
4e85aff4 1202 MountParameters *p;
b08d03ff 1203
f50e0a01 1204 assert(m);
b08d03ff
LP
1205 assert(what);
1206 assert(where);
e537352b
LP
1207 assert(options);
1208 assert(fstype);
1209
1210 assert(!set_flags || from_proc_self_mountinfo);
1211
1212 /* Ignore API mount points. They should never be referenced in
1213 * dependencies ever. */
1214 if (mount_point_is_api(where))
1215 return 0;
57f2a956
KS
1216 if (mount_point_ignore(where))
1217 return 0;
b08d03ff 1218
8d567588
LP
1219 if (streq(fstype, "autofs"))
1220 return 0;
1221
4e85aff4
LP
1222 /* probably some kind of swap, ignore */
1223 if (!is_path(where))
b08d03ff
LP
1224 return 0;
1225
a16e1123 1226 if (!(e = unit_name_from_path(where, ".mount")))
b08d03ff
LP
1227 return -ENOMEM;
1228
1229 if (!(u = manager_get_unit(m, e))) {
1230 delete = true;
1231
1232 if (!(u = unit_new(m))) {
1233 free(e);
1234 return -ENOMEM;
1235 }
1236
1237 r = unit_add_name(u, e);
1238 free(e);
1239
1240 if (r < 0)
1241 goto fail;
1242
e537352b 1243 if (!(MOUNT(u)->where = strdup(where))) {
07b0b134
ML
1244 r = -ENOMEM;
1245 goto fail;
1246 }
f50e0a01 1247
f94ea366 1248 unit_add_to_load_queue(u);
b08d03ff
LP
1249 } else {
1250 delete = false;
1251 free(e);
1252 }
1253
e537352b
LP
1254 if (!(w = strdup(what)) ||
1255 !(o = strdup(options)) ||
1256 !(f = strdup(fstype))) {
1257 r = -ENOMEM;
1258 goto fail;
1259 }
1260
1261 if (from_proc_self_mountinfo) {
4e85aff4 1262 p = &MOUNT(u)->parameters_proc_self_mountinfo;
e537352b
LP
1263
1264 if (set_flags) {
1265 MOUNT(u)->is_mounted = true;
1266 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
4e85aff4 1267 MOUNT(u)->just_changed = !streq_ptr(p->options, o);
e537352b 1268 }
ef734fd6 1269
f50e0a01 1270 MOUNT(u)->from_proc_self_mountinfo = true;
ef734fd6 1271 } else {
4e85aff4 1272 p = &MOUNT(u)->parameters_etc_fstab;
f50e0a01 1273 MOUNT(u)->from_etc_fstab = true;
ef734fd6 1274 }
f50e0a01 1275
4e85aff4
LP
1276 free(p->what);
1277 p->what = w;
b08d03ff 1278
4e85aff4
LP
1279 free(p->options);
1280 p->options = o;
e537352b 1281
4e85aff4
LP
1282 free(p->fstype);
1283 p->fstype = f;
b08d03ff 1284
9fff8a1f
LP
1285 p->passno = passno;
1286
c1e1601e
LP
1287 unit_add_to_dbus_queue(u);
1288
b08d03ff
LP
1289 return 0;
1290
1291fail:
e537352b
LP
1292 free(w);
1293 free(o);
1294 free(f);
1295
b08d03ff
LP
1296 if (delete && u)
1297 unit_free(u);
1298
4e85aff4 1299 return r;
b08d03ff
LP
1300}
1301
07b0b134
ML
1302static int mount_find_pri(char *options) {
1303 char *end, *pri;
1304 unsigned long r;
1305
1306 if (!(pri = mount_test_option(options, "pri=")))
1307 return 0;
1308
1309 pri += 4;
1310
1311 errno = 0;
1312 r = strtoul(pri, &end, 10);
1313
1314 if (errno != 0)
1315 return -errno;
1316
1317 if (end == pri || (*end != ',' && *end != 0))
1318 return -EINVAL;
1319
1320 return (int) r;
1321}
1322
e537352b 1323static int mount_load_etc_fstab(Manager *m) {
b08d03ff 1324 FILE *f;
60b912f6 1325 int r = 0;
b08d03ff
LP
1326 struct mntent* me;
1327
1328 assert(m);
1329
1330 errno = 0;
1331 if (!(f = setmntent("/etc/fstab", "r")))
1332 return -errno;
1333
1334 while ((me = getmntent(f))) {
1335 char *where, *what;
60b912f6 1336 int k;
b08d03ff
LP
1337
1338 if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
1339 r = -ENOMEM;
1340 goto finish;
1341 }
1342
1343 if (!(where = strdup(me->mnt_dir))) {
1344 free(what);
1345 r = -ENOMEM;
1346 goto finish;
1347 }
1348
1349 if (what[0] == '/')
1350 path_kill_slashes(what);
1351
1352 if (where[0] == '/')
1353 path_kill_slashes(where);
1354
07b0b134
ML
1355 if (streq(me->mnt_type, "swap")) {
1356 int pri;
1357
1358 if ((pri = mount_find_pri(me->mnt_opts)) < 0)
60b912f6 1359 k = pri;
07b0b134 1360 else
60b912f6 1361 k = swap_add_one(m,
07b0b134 1362 what,
60b912f6 1363 NULL,
07b0b134 1364 pri,
4e85aff4 1365 !!mount_test_option(me->mnt_opts, MNTOPT_NOAUTO),
173a8d04 1366 !!mount_test_option(me->mnt_opts, "nofail"),
4e85aff4 1367 !!mount_test_option(me->mnt_opts, "comment=systemd.swapon"),
07b0b134
ML
1368 false);
1369 } else
9fff8a1f 1370 k = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, me->mnt_passno, false, false);
07b0b134 1371
b08d03ff
LP
1372 free(what);
1373 free(where);
1374
1375 if (r < 0)
60b912f6 1376 r = k;
b08d03ff
LP
1377 }
1378
b08d03ff
LP
1379finish:
1380
1381 endmntent(f);
1382 return r;
1383}
1384
ef734fd6 1385static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
60b912f6 1386 int r = 0;
1ddff895 1387 unsigned i;
a2e0f3d3 1388 char *device, *path, *options, *options2, *fstype, *d, *p, *o;
b08d03ff
LP
1389
1390 assert(m);
1391
ef734fd6 1392 rewind(m->proc_self_mountinfo);
b08d03ff 1393
1ddff895 1394 for (i = 1;; i++) {
b08d03ff 1395 int k;
e537352b 1396
a2e0f3d3 1397 device = path = options = options2 = fstype = d = p = o = NULL;
b08d03ff 1398
ef734fd6 1399 if ((k = fscanf(m->proc_self_mountinfo,
b08d03ff
LP
1400 "%*s " /* (1) mount id */
1401 "%*s " /* (2) parent id */
1402 "%*s " /* (3) major:minor */
1403 "%*s " /* (4) root */
1404 "%ms " /* (5) mount point */
e537352b 1405 "%ms" /* (6) mount options */
b08d03ff 1406 "%*[^-]" /* (7) optional fields */
c899f8c6 1407 "- " /* (8) separator */
e537352b 1408 "%ms " /* (9) file system type */
ef734fd6 1409 "%ms" /* (10) mount source */
a2e0f3d3 1410 "%ms" /* (11) mount options 2 */
ef734fd6 1411 "%*[^\n]", /* some rubbish at the end */
b08d03ff 1412 &path,
e537352b
LP
1413 &options,
1414 &fstype,
a2e0f3d3
LP
1415 &device,
1416 &options2)) != 5) {
b08d03ff 1417
ef734fd6
LP
1418 if (k == EOF)
1419 break;
b08d03ff 1420
1ddff895
FF
1421 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
1422 goto clean_up;
b08d03ff
LP
1423 }
1424
a2e0f3d3
LP
1425 if (asprintf(&o, "%s,%s", options, options2) < 0) {
1426 r = -ENOMEM;
1427 goto finish;
1428 }
1429
e537352b
LP
1430 if (!(d = cunescape(device)) ||
1431 !(p = cunescape(path))) {
1432 r = -ENOMEM;
1433 goto finish;
b08d03ff 1434 }
b08d03ff 1435
9fff8a1f 1436 if ((k = mount_add_one(m, d, p, o, fstype, 0, true, set_flags)) < 0)
60b912f6 1437 r = k;
b08d03ff 1438
1ddff895 1439clean_up:
e537352b
LP
1440 free(device);
1441 free(path);
1442 free(options);
a2e0f3d3 1443 free(options2);
e537352b 1444 free(fstype);
b08d03ff
LP
1445 free(d);
1446 free(p);
a2e0f3d3 1447 free(o);
b08d03ff
LP
1448 }
1449
e537352b
LP
1450finish:
1451 free(device);
1452 free(path);
1453 free(options);
a2e0f3d3 1454 free(options2);
e537352b
LP
1455 free(fstype);
1456 free(d);
1457 free(p);
a2e0f3d3 1458 free(o);
e537352b
LP
1459
1460 return r;
1461}
1462
1463static void mount_shutdown(Manager *m) {
1464 assert(m);
1465
a16e1123 1466 if (m->proc_self_mountinfo) {
e537352b 1467 fclose(m->proc_self_mountinfo);
a16e1123
LP
1468 m->proc_self_mountinfo = NULL;
1469 }
b08d03ff
LP
1470}
1471
1472static int mount_enumerate(Manager *m) {
1473 int r;
ef734fd6 1474 struct epoll_event ev;
b08d03ff
LP
1475 assert(m);
1476
a16e1123
LP
1477 if (!m->proc_self_mountinfo) {
1478 if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
1479 return -errno;
ef734fd6 1480
a16e1123
LP
1481 m->mount_watch.type = WATCH_MOUNT;
1482 m->mount_watch.fd = fileno(m->proc_self_mountinfo);
ef734fd6 1483
a16e1123 1484 zero(ev);
4e434314 1485 ev.events = EPOLLPRI;
a16e1123 1486 ev.data.ptr = &m->mount_watch;
ef734fd6 1487
a16e1123
LP
1488 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
1489 return -errno;
1490 }
ef734fd6 1491
e537352b 1492 if ((r = mount_load_etc_fstab(m)) < 0)
b08d03ff
LP
1493 goto fail;
1494
ef734fd6 1495 if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
b08d03ff
LP
1496 goto fail;
1497
1498 return 0;
1499
1500fail:
1501 mount_shutdown(m);
1502 return r;
5cb5a6ff
LP
1503}
1504
ef734fd6
LP
1505void mount_fd_event(Manager *m, int events) {
1506 Meta *meta;
1507 int r;
1508
1509 assert(m);
4e434314 1510 assert(events & EPOLLPRI);
ef734fd6
LP
1511
1512 /* The manager calls this for every fd event happening on the
1513 * /proc/self/mountinfo file, which informs us about mounting
1514 * table changes */
1515
1516 if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) {
e364ad06 1517 log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
e537352b
LP
1518
1519 /* Reset flags, just in case, for later calls */
1520 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1521 Mount *mount = (Mount*) meta;
1522
1523 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1524 }
1525
ef734fd6
LP
1526 return;
1527 }
1528
1529 manager_dispatch_load_queue(m);
1530
1531 LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
1532 Mount *mount = (Mount*) meta;
1533
e537352b
LP
1534 if (!mount->is_mounted) {
1535 /* This has just been unmounted. */
1536
ef734fd6 1537 mount->from_proc_self_mountinfo = false;
e537352b
LP
1538
1539 switch (mount->state) {
1540
1541 case MOUNT_MOUNTED:
1542 mount_enter_dead(mount, true);
1543 break;
1544
1545 default:
1546 mount_set_state(mount, mount->state);
1547 break;
1548
1549 }
1550
1551 } else if (mount->just_mounted || mount->just_changed) {
1552
60b912f6 1553 /* New or changed mount entry */
e537352b
LP
1554
1555 switch (mount->state) {
1556
1557 case MOUNT_DEAD:
fdf20a31 1558 case MOUNT_FAILED:
e537352b
LP
1559 mount_enter_mounted(mount, true);
1560 break;
1561
1562 case MOUNT_MOUNTING:
8d567588 1563 mount_enter_mounting_done(mount);
e537352b
LP
1564 break;
1565
1566 default:
1567 /* Nothing really changed, but let's
1568 * issue an notification call
1569 * nonetheless, in case somebody is
1570 * waiting for this. (e.g. file system
1571 * ro/rw remounts.) */
1572 mount_set_state(mount, mount->state);
1573 break;
1574 }
1575 }
1576
1577 /* Reset the flags for later calls */
1578 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1579 }
1580}
1581
fdf20a31 1582static void mount_reset_failed(Unit *u) {
5632e374
LP
1583 Mount *m = MOUNT(u);
1584
1585 assert(m);
1586
fdf20a31 1587 if (m->state == MOUNT_FAILED)
5632e374
LP
1588 mount_set_state(m, MOUNT_DEAD);
1589
1590 m->failure = false;
1591}
1592
8a0867d6
LP
1593static int mount_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1594 Mount *m = MOUNT(u);
1595 int r = 0;
1596 Set *pid_set = NULL;
1597
1598 assert(m);
1599
1600 if (who == KILL_MAIN) {
1601 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Mount units have no main processes");
1602 return -EINVAL;
1603 }
1604
1605 if (m->control_pid <= 0 && who == KILL_CONTROL) {
1606 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1607 return -ENOENT;
1608 }
1609
1610 if (m->control_pid > 0)
1611 if (kill(mode == KILL_PROCESS_GROUP ? -m->control_pid : m->control_pid, signo) < 0)
1612 r = -errno;
1613
1614 if (mode == KILL_CONTROL_GROUP) {
1615 int q;
1616
1617 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1618 return -ENOMEM;
1619
1620 /* Exclude the control pid from being killed via the cgroup */
1621 if (m->control_pid > 0)
1622 if ((q = set_put(pid_set, LONG_TO_PTR(m->control_pid))) < 0) {
1623 r = q;
1624 goto finish;
1625 }
1626
1627 if ((q = cgroup_bonding_kill_list(m->meta.cgroup_bondings, signo, pid_set)) < 0)
1628 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1629 r = q;
1630 }
1631
1632finish:
1633 if (pid_set)
1634 set_free(pid_set);
1635
1636 return r;
1637}
1638
a16e1123
LP
1639static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
1640 [MOUNT_DEAD] = "dead",
1641 [MOUNT_MOUNTING] = "mounting",
1642 [MOUNT_MOUNTING_DONE] = "mounting-done",
1643 [MOUNT_MOUNTED] = "mounted",
1644 [MOUNT_REMOUNTING] = "remounting",
1645 [MOUNT_UNMOUNTING] = "unmounting",
1646 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
1647 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
1648 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
1649 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
1650 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
1651 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
fdf20a31 1652 [MOUNT_FAILED] = "failed"
a16e1123
LP
1653};
1654
1655DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
1656
1657static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1658 [MOUNT_EXEC_MOUNT] = "ExecMount",
1659 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1660 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1661};
1662
1663DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1664
87f0e418 1665const UnitVTable mount_vtable = {
5cb5a6ff
LP
1666 .suffix = ".mount",
1667
e537352b 1668 .no_alias = true,
9e2f7c11 1669 .no_instances = true,
c497c7a9 1670 .no_isolate = true,
9e58ff9c 1671 .show_status = true,
e537352b
LP
1672
1673 .init = mount_init,
1674 .load = mount_load,
034c6ed7 1675 .done = mount_done,
e537352b 1676
f50e0a01
LP
1677 .coldplug = mount_coldplug,
1678
034c6ed7 1679 .dump = mount_dump,
5cb5a6ff 1680
e537352b
LP
1681 .start = mount_start,
1682 .stop = mount_stop,
1683 .reload = mount_reload,
1684
8a0867d6
LP
1685 .kill = mount_kill,
1686
a16e1123
LP
1687 .serialize = mount_serialize,
1688 .deserialize_item = mount_deserialize_item,
1689
f50e0a01 1690 .active_state = mount_active_state,
10a94420 1691 .sub_state_to_string = mount_sub_state_to_string,
b08d03ff 1692
701cc384
LP
1693 .check_gc = mount_check_gc,
1694
e537352b
LP
1695 .sigchld_event = mount_sigchld_event,
1696 .timer_event = mount_timer_event,
1697
fdf20a31 1698 .reset_failed = mount_reset_failed,
5632e374 1699
c4e2ceae 1700 .bus_interface = "org.freedesktop.systemd1.Mount",
4139c1b2 1701 .bus_message_handler = bus_mount_message_handler,
c4e2ceae 1702 .bus_invalidating_properties = bus_mount_invalidating_properties,
4139c1b2 1703
f50e0a01
LP
1704 .enumerate = mount_enumerate,
1705 .shutdown = mount_shutdown
5cb5a6ff 1706};