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