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