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