]> git.proxmox.com Git - systemd.git/blame - src/core/mount.c
Merge tag 'upstream/229'
[systemd.git] / src / core / mount.c
CommitLineData
663996b3
MS
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
db2df898 21#include <signal.h>
663996b3 22#include <stdio.h>
663996b3 23#include <sys/epoll.h>
663996b3 24
663996b3 25#include "sd-messages.h"
db2df898
MP
26
27#include "alloc-util.h"
663996b3 28#include "dbus-mount.h"
db2df898 29#include "escape.h"
663996b3 30#include "exit-status.h"
e3bff60a 31#include "formats-util.h"
db2df898
MP
32#include "fstab-util.h"
33#include "log.h"
34#include "manager.h"
35#include "mkdir.h"
36#include "mount-setup.h"
37#include "mount-util.h"
38#include "mount.h"
39#include "parse-util.h"
40#include "path-util.h"
41#include "process-util.h"
42#include "special.h"
43#include "string-table.h"
44#include "string-util.h"
45#include "strv.h"
46#include "unit-name.h"
47#include "unit.h"
e735f4d4
MP
48
49#define RETRY_UMOUNT_MAX 32
663996b3 50
f47781d8
MP
51DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
52DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
53
663996b3
MS
54static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
55 [MOUNT_DEAD] = UNIT_INACTIVE,
56 [MOUNT_MOUNTING] = UNIT_ACTIVATING,
57 [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE,
58 [MOUNT_MOUNTED] = UNIT_ACTIVE,
59 [MOUNT_REMOUNTING] = UNIT_RELOADING,
60 [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING,
61 [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING,
62 [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING,
63 [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING,
64 [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING,
65 [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING,
66 [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING,
67 [MOUNT_FAILED] = UNIT_FAILED
68};
69
60f067b4
JS
70static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
71static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
72
f47781d8 73static bool mount_needs_network(const char *options, const char *fstype) {
e735f4d4 74 if (fstab_test_option(options, "_netdev\0"))
60f067b4
JS
75 return true;
76
f47781d8 77 if (fstype && fstype_is_network(fstype))
60f067b4
JS
78 return true;
79
80 return false;
81}
82
f47781d8
MP
83static bool mount_is_network(const MountParameters *p) {
84 assert(p);
85
86 return mount_needs_network(p->options, p->fstype);
87}
88
89static bool mount_is_bind(const MountParameters *p) {
60f067b4
JS
90 assert(p);
91
e735f4d4 92 if (fstab_test_option(p->options, "bind\0" "rbind\0"))
60f067b4
JS
93 return true;
94
e735f4d4 95 if (p->fstype && STR_IN_SET(p->fstype, "bind", "rbind"))
60f067b4
JS
96 return true;
97
98 return false;
99}
100
f47781d8 101static bool mount_is_auto(const MountParameters *p) {
60f067b4
JS
102 assert(p);
103
e735f4d4 104 return !fstab_test_option(p->options, "noauto\0");
60f067b4
JS
105}
106
f47781d8 107static bool needs_quota(const MountParameters *p) {
60f067b4
JS
108 assert(p);
109
e3bff60a
MP
110 /* Quotas are not enabled on network filesystems,
111 * but we want them, for example, on storage connected via iscsi */
112 if (p->fstype && fstype_is_network(p->fstype))
60f067b4
JS
113 return false;
114
115 if (mount_is_bind(p))
116 return false;
117
e735f4d4
MP
118 return fstab_test_option(p->options,
119 "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0");
60f067b4
JS
120}
121
663996b3
MS
122static void mount_init(Unit *u) {
123 Mount *m = MOUNT(u);
124
125 assert(u);
126 assert(u->load_state == UNIT_STUB);
127
60f067b4 128 m->timeout_usec = u->manager->default_timeout_start_usec;
663996b3
MS
129 m->directory_mode = 0755;
130
663996b3
MS
131 if (unit_has_name(u, "-.mount")) {
132 /* Don't allow start/stop for root directory */
60f067b4
JS
133 u->refuse_manual_start = true;
134 u->refuse_manual_stop = true;
663996b3
MS
135 } else {
136 /* The stdio/kmsg bridge socket is on /, in order to avoid a
137 * dep loop, don't use kmsg logging for -.mount */
138 m->exec_context.std_output = u->manager->default_std_output;
139 m->exec_context.std_error = u->manager->default_std_error;
140 }
141
e3bff60a
MP
142 /* We need to make sure that /usr/bin/mount is always called
143 * in the same process group as us, so that the autofs kernel
663996b3
MS
144 * side doesn't send us another mount request while we are
145 * already trying to comply its last one. */
146 m->exec_context.same_pgrp = true;
147
663996b3
MS
148 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
149
60f067b4
JS
150 u->ignore_on_isolate = true;
151}
152
4c89c718 153static int mount_arm_timer(Mount *m, usec_t usec) {
60f067b4
JS
154 int r;
155
156 assert(m);
157
60f067b4 158 if (m->timer_event_source) {
4c89c718 159 r = sd_event_source_set_time(m->timer_event_source, usec);
60f067b4
JS
160 if (r < 0)
161 return r;
162
163 return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT);
164 }
165
4c89c718
MP
166 if (usec == USEC_INFINITY)
167 return 0;
168
e3bff60a 169 r = sd_event_add_time(
60f067b4
JS
170 UNIT(m)->manager->event,
171 &m->timer_event_source,
172 CLOCK_MONOTONIC,
4c89c718 173 usec, 0,
60f067b4 174 mount_dispatch_timer, m);
e3bff60a
MP
175 if (r < 0)
176 return r;
177
178 (void) sd_event_source_set_description(m->timer_event_source, "mount-timer");
179
180 return 0;
663996b3
MS
181}
182
183static void mount_unwatch_control_pid(Mount *m) {
184 assert(m);
185
186 if (m->control_pid <= 0)
187 return;
188
189 unit_unwatch_pid(UNIT(m), m->control_pid);
190 m->control_pid = 0;
191}
192
193static void mount_parameters_done(MountParameters *p) {
194 assert(p);
195
196 free(p->what);
197 free(p->options);
198 free(p->fstype);
199
200 p->what = p->options = p->fstype = NULL;
201}
202
203static void mount_done(Unit *u) {
204 Mount *m = MOUNT(u);
205
206 assert(m);
207
6300502b 208 m->where = mfree(m->where);
663996b3
MS
209
210 mount_parameters_done(&m->parameters_proc_self_mountinfo);
211 mount_parameters_done(&m->parameters_fragment);
212
60f067b4 213 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
663996b3
MS
214 exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
215 m->control_command = NULL;
216
217 mount_unwatch_control_pid(m);
218
60f067b4 219 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
663996b3
MS
220}
221
222_pure_ static MountParameters* get_mount_parameters_fragment(Mount *m) {
223 assert(m);
224
225 if (m->from_fragment)
226 return &m->parameters_fragment;
227
228 return NULL;
229}
230
231_pure_ static MountParameters* get_mount_parameters(Mount *m) {
232 assert(m);
233
234 if (m->from_proc_self_mountinfo)
235 return &m->parameters_proc_self_mountinfo;
236
237 return get_mount_parameters_fragment(m);
238}
239
240static int mount_add_mount_links(Mount *m) {
14228c0d 241 _cleanup_free_ char *parent = NULL;
663996b3 242 MountParameters *pm;
663996b3 243 Unit *other;
14228c0d
MB
244 Iterator i;
245 Set *s;
663996b3
MS
246 int r;
247
248 assert(m);
249
14228c0d
MB
250 if (!path_equal(m->where, "/")) {
251 /* Adds in links to other mount points that might lie further
252 * up in the hierarchy */
db2df898
MP
253
254 parent = dirname_malloc(m->where);
255 if (!parent)
256 return -ENOMEM;
663996b3 257
14228c0d 258 r = unit_require_mounts_for(UNIT(m), parent);
663996b3
MS
259 if (r < 0)
260 return r;
261 }
262
14228c0d
MB
263 /* Adds in links to other mount points that might be needed
264 * for the source path (if this is a bind mount) to be
265 * available. */
266 pm = get_mount_parameters_fragment(m);
60f067b4
JS
267 if (pm && pm->what &&
268 path_is_absolute(pm->what) &&
269 !mount_is_network(pm)) {
270
14228c0d 271 r = unit_require_mounts_for(UNIT(m), pm->what);
663996b3
MS
272 if (r < 0)
273 return r;
274 }
275
14228c0d
MB
276 /* Adds in links to other units that use this path or paths
277 * further down in the hierarchy */
278 s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where);
279 SET_FOREACH(other, s, i) {
663996b3 280
14228c0d
MB
281 if (other->load_state != UNIT_LOADED)
282 continue;
663996b3 283
14228c0d
MB
284 if (other == UNIT(m))
285 continue;
663996b3 286
14228c0d 287 r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true);
663996b3
MS
288 if (r < 0)
289 return r;
663996b3 290
14228c0d
MB
291 if (UNIT(m)->fragment_path) {
292 /* If we have fragment configuration, then make this dependency required */
293 r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true);
294 if (r < 0)
295 return r;
296 }
663996b3
MS
297 }
298
299 return 0;
300}
301
663996b3
MS
302static int mount_add_device_links(Mount *m) {
303 MountParameters *p;
14228c0d 304 bool device_wants_mount = false;
663996b3
MS
305 int r;
306
307 assert(m);
308
e735f4d4 309 p = get_mount_parameters(m);
663996b3
MS
310 if (!p)
311 return 0;
312
313 if (!p->what)
314 return 0;
315
316 if (mount_is_bind(p))
317 return 0;
318
319 if (!is_device_path(p->what))
320 return 0;
321
e3bff60a
MP
322 /* /dev/root is a really weird thing, it's not a real device,
323 * but just a path the kernel exports for the root file system
324 * specified on the kernel command line. Ignore it here. */
325 if (path_equal(p->what, "/dev/root"))
326 return 0;
327
663996b3
MS
328 if (path_equal(m->where, "/"))
329 return 0;
330
e3bff60a 331 if (mount_is_auto(p) && UNIT(m)->manager->running_as == MANAGER_SYSTEM)
14228c0d
MB
332 device_wants_mount = true;
333
4c89c718 334 r = unit_add_node_link(UNIT(m), p->what, device_wants_mount, m->from_fragment ? UNIT_BINDS_TO : UNIT_REQUIRES);
663996b3
MS
335 if (r < 0)
336 return r;
337
663996b3
MS
338 return 0;
339}
340
341static int mount_add_quota_links(Mount *m) {
342 int r;
343 MountParameters *p;
344
345 assert(m);
346
e3bff60a 347 if (UNIT(m)->manager->running_as != MANAGER_SYSTEM)
663996b3
MS
348 return 0;
349
350 p = get_mount_parameters_fragment(m);
351 if (!p)
352 return 0;
353
354 if (!needs_quota(p))
355 return 0;
356
357 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, NULL, true);
358 if (r < 0)
359 return r;
360
361 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, NULL, true);
362 if (r < 0)
363 return r;
364
365 return 0;
366}
367
14228c0d
MB
368static bool should_umount(Mount *m) {
369 MountParameters *p;
370
371 if (path_equal(m->where, "/") ||
372 path_equal(m->where, "/usr"))
373 return false;
374
375 p = get_mount_parameters(m);
e735f4d4 376 if (p && fstab_test_option(p->options, "x-initrd.mount\0") &&
14228c0d
MB
377 !in_initrd())
378 return false;
379
380 return true;
381}
382
663996b3 383static int mount_add_default_dependencies(Mount *m) {
663996b3 384 MountParameters *p;
db2df898 385 const char *after;
663996b3
MS
386 int r;
387
388 assert(m);
389
db2df898
MP
390 if (!UNIT(m)->default_dependencies)
391 return 0;
392
e3bff60a 393 if (UNIT(m)->manager->running_as != MANAGER_SYSTEM)
663996b3
MS
394 return 0;
395
e735f4d4
MP
396 /* We do not add any default dependencies to / and /usr, since
397 * they are guaranteed to stay mounted the whole time, since
398 * our system is on it. Also, don't bother with anything
399 * mounted below virtual file systems, it's also going to be
400 * virtual, and hence not worth the effort. */
401 if (path_equal(m->where, "/") ||
402 path_equal(m->where, "/usr") ||
403 path_startswith(m->where, "/proc") ||
404 path_startswith(m->where, "/sys") ||
405 path_startswith(m->where, "/dev"))
663996b3
MS
406 return 0;
407
e735f4d4
MP
408 p = get_mount_parameters(m);
409 if (!p)
663996b3
MS
410 return 0;
411
412 if (mount_is_network(p)) {
db2df898
MP
413 /* We order ourselves after network.target. This is
414 * primarily useful at shutdown: services that take
415 * down the network should order themselves before
416 * network.target, so that they are shut down only
417 * after this mount unit is stopped. */
663996b3 418
db2df898 419 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, NULL, true);
663996b3
MS
420 if (r < 0)
421 return r;
663996b3 422
db2df898
MP
423 /* We pull in network-online.target, and order
424 * ourselves after it. This is useful at start-up to
425 * actively pull in tools that want to be started
426 * before we start mounting network file systems, and
427 * whose purpose it is to delay this until the network
428 * is "up". */
429
430 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, NULL, true);
663996b3
MS
431 if (r < 0)
432 return r;
db2df898
MP
433
434 after = SPECIAL_REMOTE_FS_PRE_TARGET;
435 } else
436 after = SPECIAL_LOCAL_FS_PRE_TARGET;
437
438 r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, NULL, true);
439 if (r < 0)
440 return r;
663996b3 441
14228c0d
MB
442 if (should_umount(m)) {
443 r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
444 if (r < 0)
445 return r;
446 }
663996b3
MS
447
448 return 0;
449}
450
663996b3 451static int mount_verify(Mount *m) {
14228c0d 452 _cleanup_free_ char *e = NULL;
e3bff60a 453 int r;
14228c0d 454
663996b3
MS
455 assert(m);
456
457 if (UNIT(m)->load_state != UNIT_LOADED)
458 return 0;
459
460 if (!m->from_fragment && !m->from_proc_self_mountinfo)
461 return -ENOENT;
462
e3bff60a
MP
463 r = unit_name_from_path(m->where, ".mount", &e);
464 if (r < 0)
465 return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m");
663996b3 466
e3bff60a
MP
467 if (!unit_has_name(UNIT(m), e)) {
468 log_unit_error(UNIT(m), "Where= setting doesn't match unit name. Refusing.");
663996b3
MS
469 return -EINVAL;
470 }
471
472 if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) {
e3bff60a 473 log_unit_error(UNIT(m), "Cannot create mount unit for API file system %s. Refusing.", m->where);
663996b3
MS
474 return -EINVAL;
475 }
476
477 if (UNIT(m)->fragment_path && !m->parameters_fragment.what) {
e3bff60a 478 log_unit_error(UNIT(m), "What= setting is missing. Refusing.");
663996b3
MS
479 return -EBADMSG;
480 }
481
482 if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) {
e3bff60a 483 log_unit_error(UNIT(m), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing.");
663996b3
MS
484 return -EINVAL;
485 }
486
487 return 0;
488}
489
490static int mount_add_extras(Mount *m) {
491 Unit *u = UNIT(m);
492 int r;
493
60f067b4
JS
494 assert(m);
495
496 if (u->fragment_path)
663996b3
MS
497 m->from_fragment = true;
498
499 if (!m->where) {
e3bff60a
MP
500 r = unit_name_to_path(u->id, &m->where);
501 if (r < 0)
502 return r;
663996b3
MS
503 }
504
505 path_kill_slashes(m->where);
506
60f067b4 507 if (!u->description) {
663996b3
MS
508 r = unit_set_description(u, m->where);
509 if (r < 0)
510 return r;
511 }
512
513 r = mount_add_device_links(m);
514 if (r < 0)
515 return r;
516
517 r = mount_add_mount_links(m);
518 if (r < 0)
519 return r;
520
663996b3
MS
521 r = mount_add_quota_links(m);
522 if (r < 0)
523 return r;
524
60f067b4
JS
525 r = unit_patch_contexts(u);
526 if (r < 0)
527 return r;
528
529 r = unit_add_exec_dependencies(u, &m->exec_context);
530 if (r < 0)
531 return r;
663996b3 532
d9dfd233 533 r = unit_set_default_slice(u);
663996b3
MS
534 if (r < 0)
535 return r;
536
db2df898
MP
537 r = mount_add_default_dependencies(m);
538 if (r < 0)
539 return r;
60f067b4 540
663996b3
MS
541 return 0;
542}
543
544static int mount_load(Unit *u) {
545 Mount *m = MOUNT(u);
546 int r;
547
548 assert(u);
549 assert(u->load_state == UNIT_STUB);
550
551 if (m->from_proc_self_mountinfo)
552 r = unit_load_fragment_and_dropin_optional(u);
553 else
554 r = unit_load_fragment_and_dropin(u);
555
556 if (r < 0)
557 return r;
558
559 /* This is a new unit? Then let's add in some extras */
560 if (u->load_state == UNIT_LOADED) {
561 r = mount_add_extras(m);
562 if (r < 0)
563 return r;
663996b3
MS
564 }
565
566 return mount_verify(m);
567}
568
e3bff60a 569static int mount_notify_automount(Mount *m, MountState old_state, MountState state) {
663996b3
MS
570 Unit *p;
571 int r;
572 Iterator i;
573
574 assert(m);
575
576 SET_FOREACH(p, UNIT(m)->dependencies[UNIT_TRIGGERED_BY], i)
577 if (p->type == UNIT_AUTOMOUNT) {
e3bff60a 578 r = automount_update_mount(AUTOMOUNT(p), old_state, state);
663996b3
MS
579 if (r < 0)
580 return r;
581 }
582
583 return 0;
584}
585
586static void mount_set_state(Mount *m, MountState state) {
587 MountState old_state;
588 assert(m);
589
590 old_state = m->state;
591 m->state = state;
592
593 if (state != MOUNT_MOUNTING &&
594 state != MOUNT_MOUNTING_DONE &&
595 state != MOUNT_REMOUNTING &&
596 state != MOUNT_UNMOUNTING &&
597 state != MOUNT_MOUNTING_SIGTERM &&
598 state != MOUNT_MOUNTING_SIGKILL &&
599 state != MOUNT_UNMOUNTING_SIGTERM &&
600 state != MOUNT_UNMOUNTING_SIGKILL &&
601 state != MOUNT_REMOUNTING_SIGTERM &&
602 state != MOUNT_REMOUNTING_SIGKILL) {
60f067b4 603 m->timer_event_source = sd_event_source_unref(m->timer_event_source);
663996b3
MS
604 mount_unwatch_control_pid(m);
605 m->control_command = NULL;
606 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
607 }
608
e3bff60a 609 mount_notify_automount(m, old_state, state);
663996b3
MS
610
611 if (state != old_state)
e3bff60a 612 log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state));
663996b3
MS
613
614 unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], m->reload_result == MOUNT_SUCCESS);
615 m->reload_result = MOUNT_SUCCESS;
616}
617
618static int mount_coldplug(Unit *u) {
619 Mount *m = MOUNT(u);
620 MountState new_state = MOUNT_DEAD;
621 int r;
622
623 assert(m);
624 assert(m->state == MOUNT_DEAD);
625
626 if (m->deserialized_state != m->state)
627 new_state = m->deserialized_state;
628 else if (m->from_proc_self_mountinfo)
629 new_state = MOUNT_MOUNTED;
630
60f067b4
JS
631 if (new_state == m->state)
632 return 0;
663996b3 633
db2df898
MP
634 if (m->control_pid > 0 &&
635 pid_is_unwaited(m->control_pid) &&
636 IN_SET(new_state,
637 MOUNT_MOUNTING,
638 MOUNT_MOUNTING_DONE,
639 MOUNT_REMOUNTING,
640 MOUNT_UNMOUNTING,
641 MOUNT_MOUNTING_SIGTERM,
642 MOUNT_MOUNTING_SIGKILL,
643 MOUNT_UNMOUNTING_SIGTERM,
644 MOUNT_UNMOUNTING_SIGKILL,
645 MOUNT_REMOUNTING_SIGTERM,
646 MOUNT_REMOUNTING_SIGKILL)) {
60f067b4
JS
647
648 r = unit_watch_pid(UNIT(m), m->control_pid);
649 if (r < 0)
650 return r;
663996b3 651
4c89c718 652 r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec));
60f067b4
JS
653 if (r < 0)
654 return r;
663996b3
MS
655 }
656
60f067b4 657 mount_set_state(m, new_state);
663996b3
MS
658 return 0;
659}
660
661static void mount_dump(Unit *u, FILE *f, const char *prefix) {
662 Mount *m = MOUNT(u);
663 MountParameters *p;
664
665 assert(m);
666 assert(f);
667
668 p = get_mount_parameters(m);
669
670 fprintf(f,
671 "%sMount State: %s\n"
672 "%sResult: %s\n"
673 "%sWhere: %s\n"
674 "%sWhat: %s\n"
675 "%sFile System Type: %s\n"
676 "%sOptions: %s\n"
677 "%sFrom /proc/self/mountinfo: %s\n"
678 "%sFrom fragment: %s\n"
679 "%sDirectoryMode: %04o\n",
680 prefix, mount_state_to_string(m->state),
681 prefix, mount_result_to_string(m->result),
682 prefix, m->where,
14228c0d
MB
683 prefix, p ? strna(p->what) : "n/a",
684 prefix, p ? strna(p->fstype) : "n/a",
685 prefix, p ? strna(p->options) : "n/a",
663996b3
MS
686 prefix, yes_no(m->from_proc_self_mountinfo),
687 prefix, yes_no(m->from_fragment),
688 prefix, m->directory_mode);
689
690 if (m->control_pid > 0)
691 fprintf(f,
60f067b4
JS
692 "%sControl PID: "PID_FMT"\n",
693 prefix, m->control_pid);
663996b3
MS
694
695 exec_context_dump(&m->exec_context, f, prefix);
696 kill_context_dump(&m->kill_context, f, prefix);
697}
698
699static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
700 pid_t pid;
701 int r;
5eef597e
MP
702 ExecParameters exec_params = {
703 .apply_permissions = true,
704 .apply_chroot = true,
705 .apply_tty_stdin = true,
e3bff60a 706 .bus_endpoint_fd = -1,
db2df898
MP
707 .stdin_fd = -1,
708 .stdout_fd = -1,
709 .stderr_fd = -1,
5eef597e 710 };
663996b3
MS
711
712 assert(m);
713 assert(c);
714 assert(_pid);
715
e3bff60a
MP
716 (void) unit_realize_cgroup(UNIT(m));
717 if (m->reset_cpu_usage) {
718 (void) unit_reset_cpu_usage(UNIT(m));
719 m->reset_cpu_usage = false;
720 }
14228c0d 721
60f067b4
JS
722 r = unit_setup_exec_runtime(UNIT(m));
723 if (r < 0)
4c89c718 724 return r;
60f067b4 725
4c89c718 726 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
663996b3 727 if (r < 0)
4c89c718 728 return r;
663996b3 729
5eef597e
MP
730 exec_params.environment = UNIT(m)->manager->environment;
731 exec_params.confirm_spawn = UNIT(m)->manager->confirm_spawn;
732 exec_params.cgroup_supported = UNIT(m)->manager->cgroup_supported;
733 exec_params.cgroup_path = UNIT(m)->cgroup_path;
f47781d8 734 exec_params.cgroup_delegate = m->cgroup_context.delegate;
5eef597e 735 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(m)->manager);
5eef597e 736
e3bff60a
MP
737 r = exec_spawn(UNIT(m),
738 c,
14228c0d 739 &m->exec_context,
5eef597e 740 &exec_params,
60f067b4 741 m->exec_runtime,
14228c0d
MB
742 &pid);
743 if (r < 0)
4c89c718 744 return r;
663996b3 745
14228c0d
MB
746 r = unit_watch_pid(UNIT(m), pid);
747 if (r < 0)
663996b3 748 /* FIXME: we need to do something here */
4c89c718 749 return r;
663996b3
MS
750
751 *_pid = pid;
752
753 return 0;
663996b3
MS
754}
755
756static void mount_enter_dead(Mount *m, MountResult f) {
757 assert(m);
758
759 if (f != MOUNT_SUCCESS)
760 m->result = f;
761
60f067b4
JS
762 exec_runtime_destroy(m->exec_runtime);
763 m->exec_runtime = exec_runtime_unref(m->exec_runtime);
764
765 exec_context_destroy_runtime_directory(&m->exec_context, manager_get_runtime_prefix(UNIT(m)->manager));
766
663996b3
MS
767 mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
768}
769
770static void mount_enter_mounted(Mount *m, MountResult f) {
771 assert(m);
772
773 if (f != MOUNT_SUCCESS)
774 m->result = f;
775
776 mount_set_state(m, MOUNT_MOUNTED);
777}
778
779static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
780 int r;
781
782 assert(m);
783
784 if (f != MOUNT_SUCCESS)
785 m->result = f;
786
787 r = unit_kill_context(
788 UNIT(m),
789 &m->kill_context,
5eef597e
MP
790 (state != MOUNT_MOUNTING_SIGTERM && state != MOUNT_UNMOUNTING_SIGTERM && state != MOUNT_REMOUNTING_SIGTERM) ?
791 KILL_KILL : KILL_TERMINATE,
663996b3
MS
792 -1,
793 m->control_pid,
794 false);
795 if (r < 0)
796 goto fail;
797
798 if (r > 0) {
4c89c718 799 r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec));
663996b3
MS
800 if (r < 0)
801 goto fail;
802
803 mount_set_state(m, state);
60f067b4
JS
804 } else if (state == MOUNT_REMOUNTING_SIGTERM)
805 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS);
806 else if (state == MOUNT_REMOUNTING_SIGKILL)
663996b3 807 mount_enter_mounted(m, MOUNT_SUCCESS);
60f067b4
JS
808 else if (state == MOUNT_MOUNTING_SIGTERM)
809 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_SUCCESS);
810 else if (state == MOUNT_UNMOUNTING_SIGTERM)
811 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS);
663996b3
MS
812 else
813 mount_enter_dead(m, MOUNT_SUCCESS);
814
815 return;
816
817fail:
e3bff60a 818 log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
663996b3
MS
819
820 if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL)
821 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
822 else
823 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
824}
825
663996b3
MS
826static void mount_enter_unmounting(Mount *m) {
827 int r;
828
829 assert(m);
830
e735f4d4
MP
831 /* Start counting our attempts */
832 if (!IN_SET(m->state,
833 MOUNT_UNMOUNTING,
834 MOUNT_UNMOUNTING_SIGTERM,
835 MOUNT_UNMOUNTING_SIGKILL))
836 m->n_retry_umount = 0;
837
663996b3
MS
838 m->control_command_id = MOUNT_EXEC_UNMOUNT;
839 m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT;
840
e3bff60a 841 r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, NULL);
e735f4d4 842 if (r < 0)
663996b3
MS
843 goto fail;
844
845 mount_unwatch_control_pid(m);
846
e735f4d4
MP
847 r = mount_spawn(m, m->control_command, &m->control_pid);
848 if (r < 0)
663996b3
MS
849 goto fail;
850
851 mount_set_state(m, MOUNT_UNMOUNTING);
852
853 return;
854
855fail:
e3bff60a 856 log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m");
663996b3
MS
857 mount_enter_mounted(m, MOUNT_FAILURE_RESOURCES);
858}
859
db2df898
MP
860static int mount_get_opts(Mount *m, char **ret) {
861 return fstab_filter_options(m->parameters_fragment.options,
862 "nofail\0" "noauto\0" "auto\0", NULL, NULL, ret);
863}
864
663996b3
MS
865static void mount_enter_mounting(Mount *m) {
866 int r;
867 MountParameters *p;
868
869 assert(m);
870
871 m->control_command_id = MOUNT_EXEC_MOUNT;
872 m->control_command = m->exec_command + MOUNT_EXEC_MOUNT;
873
e3bff60a
MP
874 r = unit_fail_if_symlink(UNIT(m), m->where);
875 if (r < 0)
876 goto fail;
663996b3 877
e3bff60a
MP
878 (void) mkdir_p_label(m->where, m->directory_mode);
879
880 unit_warn_if_dir_nonempty(UNIT(m), m->where);
663996b3
MS
881
882 /* Create the source directory for bind-mounts if needed */
883 p = get_mount_parameters_fragment(m);
884 if (p && mount_is_bind(p))
e3bff60a 885 (void) mkdir_p_label(p->what, m->directory_mode);
5eef597e 886
e735f4d4
MP
887 if (m->from_fragment) {
888 _cleanup_free_ char *opts = NULL;
889
db2df898 890 r = mount_get_opts(m, &opts);
e735f4d4
MP
891 if (r < 0)
892 goto fail;
893
e3bff60a 894 r = exec_command_set(m->control_command, MOUNT_PATH,
e735f4d4 895 m->parameters_fragment.what, m->where, NULL);
e735f4d4
MP
896 if (r >= 0 && m->sloppy_options)
897 r = exec_command_append(m->control_command, "-s", NULL);
898 if (r >= 0 && m->parameters_fragment.fstype)
899 r = exec_command_append(m->control_command, "-t", m->parameters_fragment.fstype, NULL);
900 if (r >= 0 && !isempty(opts))
901 r = exec_command_append(m->control_command, "-o", opts, NULL);
902 } else
663996b3
MS
903 r = -ENOENT;
904
905 if (r < 0)
906 goto fail;
907
908 mount_unwatch_control_pid(m);
909
910 r = mount_spawn(m, m->control_command, &m->control_pid);
911 if (r < 0)
912 goto fail;
913
914 mount_set_state(m, MOUNT_MOUNTING);
915
916 return;
917
918fail:
e3bff60a 919 log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m");
663996b3
MS
920 mount_enter_dead(m, MOUNT_FAILURE_RESOURCES);
921}
922
663996b3
MS
923static void mount_enter_remounting(Mount *m) {
924 int r;
925
926 assert(m);
927
928 m->control_command_id = MOUNT_EXEC_REMOUNT;
929 m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT;
930
931 if (m->from_fragment) {
663996b3
MS
932 const char *o;
933
60f067b4 934 if (m->parameters_fragment.options)
e735f4d4 935 o = strjoina("remount,", m->parameters_fragment.options);
60f067b4 936 else
663996b3
MS
937 o = "remount";
938
e3bff60a 939 r = exec_command_set(m->control_command, MOUNT_PATH,
e735f4d4
MP
940 m->parameters_fragment.what, m->where,
941 "-o", o, NULL);
e735f4d4
MP
942 if (r >= 0 && m->sloppy_options)
943 r = exec_command_append(m->control_command, "-s", NULL);
944 if (r >= 0 && m->parameters_fragment.fstype)
945 r = exec_command_append(m->control_command, "-t", m->parameters_fragment.fstype, NULL);
663996b3
MS
946 } else
947 r = -ENOENT;
948
949 if (r < 0)
950 goto fail;
951
952 mount_unwatch_control_pid(m);
953
60f067b4
JS
954 r = mount_spawn(m, m->control_command, &m->control_pid);
955 if (r < 0)
663996b3
MS
956 goto fail;
957
958 mount_set_state(m, MOUNT_REMOUNTING);
959
960 return;
961
962fail:
e3bff60a 963 log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m");
663996b3
MS
964 m->reload_result = MOUNT_FAILURE_RESOURCES;
965 mount_enter_mounted(m, MOUNT_SUCCESS);
966}
967
968static int mount_start(Unit *u) {
969 Mount *m = MOUNT(u);
970
971 assert(m);
972
973 /* We cannot fulfill this request right now, try again later
974 * please! */
975 if (m->state == MOUNT_UNMOUNTING ||
976 m->state == MOUNT_UNMOUNTING_SIGTERM ||
977 m->state == MOUNT_UNMOUNTING_SIGKILL ||
978 m->state == MOUNT_MOUNTING_SIGTERM ||
979 m->state == MOUNT_MOUNTING_SIGKILL)
980 return -EAGAIN;
981
982 /* Already on it! */
983 if (m->state == MOUNT_MOUNTING)
984 return 0;
985
986 assert(m->state == MOUNT_DEAD || m->state == MOUNT_FAILED);
987
988 m->result = MOUNT_SUCCESS;
989 m->reload_result = MOUNT_SUCCESS;
e3bff60a 990 m->reset_cpu_usage = true;
663996b3
MS
991
992 mount_enter_mounting(m);
e735f4d4 993 return 1;
663996b3
MS
994}
995
996static int mount_stop(Unit *u) {
997 Mount *m = MOUNT(u);
998
999 assert(m);
1000
1001 /* Already on it */
1002 if (m->state == MOUNT_UNMOUNTING ||
1003 m->state == MOUNT_UNMOUNTING_SIGKILL ||
1004 m->state == MOUNT_UNMOUNTING_SIGTERM ||
1005 m->state == MOUNT_MOUNTING_SIGTERM ||
1006 m->state == MOUNT_MOUNTING_SIGKILL)
1007 return 0;
1008
1009 assert(m->state == MOUNT_MOUNTING ||
1010 m->state == MOUNT_MOUNTING_DONE ||
1011 m->state == MOUNT_MOUNTED ||
1012 m->state == MOUNT_REMOUNTING ||
1013 m->state == MOUNT_REMOUNTING_SIGTERM ||
1014 m->state == MOUNT_REMOUNTING_SIGKILL);
1015
1016 mount_enter_unmounting(m);
e735f4d4 1017 return 1;
663996b3
MS
1018}
1019
1020static int mount_reload(Unit *u) {
1021 Mount *m = MOUNT(u);
1022
1023 assert(m);
1024
1025 if (m->state == MOUNT_MOUNTING_DONE)
1026 return -EAGAIN;
1027
1028 assert(m->state == MOUNT_MOUNTED);
1029
1030 mount_enter_remounting(m);
7035cd9e 1031 return 1;
663996b3
MS
1032}
1033
1034static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
1035 Mount *m = MOUNT(u);
1036
1037 assert(m);
1038 assert(f);
1039 assert(fds);
1040
1041 unit_serialize_item(u, f, "state", mount_state_to_string(m->state));
1042 unit_serialize_item(u, f, "result", mount_result_to_string(m->result));
1043 unit_serialize_item(u, f, "reload-result", mount_result_to_string(m->reload_result));
1044
1045 if (m->control_pid > 0)
60f067b4 1046 unit_serialize_item_format(u, f, "control-pid", PID_FMT, m->control_pid);
663996b3
MS
1047
1048 if (m->control_command_id >= 0)
1049 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
1050
663996b3
MS
1051 return 0;
1052}
1053
1054static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1055 Mount *m = MOUNT(u);
1056
1057 assert(u);
1058 assert(key);
1059 assert(value);
1060 assert(fds);
1061
1062 if (streq(key, "state")) {
1063 MountState state;
1064
1065 if ((state = mount_state_from_string(value)) < 0)
e3bff60a 1066 log_unit_debug(u, "Failed to parse state value: %s", value);
663996b3
MS
1067 else
1068 m->deserialized_state = state;
1069 } else if (streq(key, "result")) {
1070 MountResult f;
1071
1072 f = mount_result_from_string(value);
1073 if (f < 0)
e3bff60a 1074 log_unit_debug(u, "Failed to parse result value: %s", value);
663996b3
MS
1075 else if (f != MOUNT_SUCCESS)
1076 m->result = f;
1077
1078 } else if (streq(key, "reload-result")) {
1079 MountResult f;
1080
1081 f = mount_result_from_string(value);
1082 if (f < 0)
e3bff60a 1083 log_unit_debug(u, "Failed to parse reload result value: %s", value);
663996b3
MS
1084 else if (f != MOUNT_SUCCESS)
1085 m->reload_result = f;
1086
1087 } else if (streq(key, "control-pid")) {
1088 pid_t pid;
1089
1090 if (parse_pid(value, &pid) < 0)
e3bff60a 1091 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
663996b3
MS
1092 else
1093 m->control_pid = pid;
1094 } else if (streq(key, "control-command")) {
1095 MountExecCommand id;
1096
e3bff60a
MP
1097 id = mount_exec_command_from_string(value);
1098 if (id < 0)
1099 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
663996b3
MS
1100 else {
1101 m->control_command_id = id;
1102 m->control_command = m->exec_command + id;
1103 }
663996b3 1104 } else
e3bff60a 1105 log_unit_debug(u, "Unknown serialization key: %s", key);
663996b3
MS
1106
1107 return 0;
1108}
1109
1110_pure_ static UnitActiveState mount_active_state(Unit *u) {
1111 assert(u);
1112
1113 return state_translation_table[MOUNT(u)->state];
1114}
1115
1116_pure_ static const char *mount_sub_state_to_string(Unit *u) {
1117 assert(u);
1118
1119 return mount_state_to_string(MOUNT(u)->state);
1120}
1121
1122_pure_ static bool mount_check_gc(Unit *u) {
1123 Mount *m = MOUNT(u);
1124
1125 assert(m);
1126
1127 return m->from_proc_self_mountinfo;
1128}
1129
1130static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1131 Mount *m = MOUNT(u);
1132 MountResult f;
1133
1134 assert(m);
1135 assert(pid >= 0);
1136
1137 if (pid != m->control_pid)
1138 return;
1139
1140 m->control_pid = 0;
1141
1142 if (is_clean_exit(code, status, NULL))
1143 f = MOUNT_SUCCESS;
1144 else if (code == CLD_EXITED)
1145 f = MOUNT_FAILURE_EXIT_CODE;
1146 else if (code == CLD_KILLED)
1147 f = MOUNT_FAILURE_SIGNAL;
1148 else if (code == CLD_DUMPED)
1149 f = MOUNT_FAILURE_CORE_DUMP;
1150 else
1151 assert_not_reached("Unknown code");
1152
1153 if (f != MOUNT_SUCCESS)
1154 m->result = f;
1155
1156 if (m->control_command) {
1157 exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status);
1158
1159 m->control_command = NULL;
1160 m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
1161 }
1162
e3bff60a
MP
1163 log_unit_full(u, f == MOUNT_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
1164 "Mount process exited, code=%s status=%i", sigchld_code_to_string(code), status);
663996b3
MS
1165
1166 /* Note that mount(8) returning and the kernel sending us a
1167 * mount table change event might happen out-of-order. If an
1168 * operation succeed we assume the kernel will follow soon too
1169 * and already change into the resulting state. If it fails
1170 * we check if the kernel still knows about the mount. and
1171 * change state accordingly. */
1172
1173 switch (m->state) {
1174
1175 case MOUNT_MOUNTING:
1176 case MOUNT_MOUNTING_DONE:
1177 case MOUNT_MOUNTING_SIGKILL:
1178 case MOUNT_MOUNTING_SIGTERM:
1179
1180 if (f == MOUNT_SUCCESS)
1181 mount_enter_mounted(m, f);
1182 else if (m->from_proc_self_mountinfo)
1183 mount_enter_mounted(m, f);
1184 else
1185 mount_enter_dead(m, f);
1186 break;
1187
1188 case MOUNT_REMOUNTING:
1189 case MOUNT_REMOUNTING_SIGKILL:
1190 case MOUNT_REMOUNTING_SIGTERM:
1191
1192 m->reload_result = f;
1193 if (m->from_proc_self_mountinfo)
1194 mount_enter_mounted(m, MOUNT_SUCCESS);
1195 else
1196 mount_enter_dead(m, MOUNT_SUCCESS);
1197
1198 break;
1199
1200 case MOUNT_UNMOUNTING:
1201 case MOUNT_UNMOUNTING_SIGKILL:
1202 case MOUNT_UNMOUNTING_SIGTERM:
1203
e735f4d4
MP
1204 if (f == MOUNT_SUCCESS) {
1205
1206 if (m->from_proc_self_mountinfo) {
1207
1208 /* Still a mount point? If so, let's
1209 * try again. Most likely there were
1210 * multiple mount points stacked on
1211 * top of each other. Note that due to
1212 * the io event priority logic we can
1213 * be sure the new mountinfo is loaded
1214 * before we process the SIGCHLD for
1215 * the mount command. */
1216
1217 if (m->n_retry_umount < RETRY_UMOUNT_MAX) {
e3bff60a 1218 log_unit_debug(u, "Mount still present, trying again.");
e735f4d4
MP
1219 m->n_retry_umount++;
1220 mount_enter_unmounting(m);
1221 } else {
e3bff60a 1222 log_unit_debug(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount);
e735f4d4
MP
1223 mount_enter_mounted(m, f);
1224 }
1225 } else
1226 mount_enter_dead(m, f);
1227
1228 } else if (m->from_proc_self_mountinfo)
663996b3
MS
1229 mount_enter_mounted(m, f);
1230 else
1231 mount_enter_dead(m, f);
1232 break;
1233
1234 default:
1235 assert_not_reached("Uh, control process died at wrong time.");
1236 }
1237
1238 /* Notify clients about changed exit status */
1239 unit_add_to_dbus_queue(u);
1240}
1241
60f067b4
JS
1242static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1243 Mount *m = MOUNT(userdata);
663996b3
MS
1244
1245 assert(m);
60f067b4 1246 assert(m->timer_event_source == source);
663996b3
MS
1247
1248 switch (m->state) {
1249
1250 case MOUNT_MOUNTING:
1251 case MOUNT_MOUNTING_DONE:
e3bff60a 1252 log_unit_warning(UNIT(m), "Mounting timed out. Stopping.");
663996b3
MS
1253 mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1254 break;
1255
1256 case MOUNT_REMOUNTING:
e3bff60a 1257 log_unit_warning(UNIT(m), "Remounting timed out. Stopping.");
663996b3
MS
1258 m->reload_result = MOUNT_FAILURE_TIMEOUT;
1259 mount_enter_mounted(m, MOUNT_SUCCESS);
1260 break;
1261
1262 case MOUNT_UNMOUNTING:
e3bff60a 1263 log_unit_warning(UNIT(m), "Unmounting timed out. Stopping.");
663996b3
MS
1264 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT);
1265 break;
1266
1267 case MOUNT_MOUNTING_SIGTERM:
1268 if (m->kill_context.send_sigkill) {
e3bff60a 1269 log_unit_warning(UNIT(m), "Mounting timed out. Killing.");
663996b3
MS
1270 mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1271 } else {
e3bff60a 1272 log_unit_warning(UNIT(m), "Mounting timed out. Skipping SIGKILL. Ignoring.");
663996b3
MS
1273
1274 if (m->from_proc_self_mountinfo)
1275 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1276 else
1277 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1278 }
1279 break;
1280
1281 case MOUNT_REMOUNTING_SIGTERM:
1282 if (m->kill_context.send_sigkill) {
e3bff60a 1283 log_unit_warning(UNIT(m), "Remounting timed out. Killing.");
663996b3
MS
1284 mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1285 } else {
e3bff60a 1286 log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring.");
663996b3
MS
1287
1288 if (m->from_proc_self_mountinfo)
1289 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1290 else
1291 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1292 }
1293 break;
1294
1295 case MOUNT_UNMOUNTING_SIGTERM:
1296 if (m->kill_context.send_sigkill) {
e3bff60a 1297 log_unit_warning(UNIT(m), "Unmounting timed out. Killing.");
663996b3
MS
1298 mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT);
1299 } else {
e3bff60a 1300 log_unit_warning(UNIT(m), "Unmounting timed out. Skipping SIGKILL. Ignoring.");
663996b3
MS
1301
1302 if (m->from_proc_self_mountinfo)
1303 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1304 else
1305 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1306 }
1307 break;
1308
1309 case MOUNT_MOUNTING_SIGKILL:
1310 case MOUNT_REMOUNTING_SIGKILL:
1311 case MOUNT_UNMOUNTING_SIGKILL:
e3bff60a 1312 log_unit_warning(UNIT(m),"Mount process still around after SIGKILL. Ignoring.");
663996b3
MS
1313
1314 if (m->from_proc_self_mountinfo)
1315 mount_enter_mounted(m, MOUNT_FAILURE_TIMEOUT);
1316 else
1317 mount_enter_dead(m, MOUNT_FAILURE_TIMEOUT);
1318 break;
1319
1320 default:
1321 assert_not_reached("Timeout at wrong time.");
1322 }
60f067b4
JS
1323
1324 return 0;
663996b3
MS
1325}
1326
e3bff60a 1327static int mount_setup_unit(
663996b3
MS
1328 Manager *m,
1329 const char *what,
1330 const char *where,
1331 const char *options,
1332 const char *fstype,
663996b3 1333 bool set_flags) {
60f067b4
JS
1334
1335 _cleanup_free_ char *e = NULL, *w = NULL, *o = NULL, *f = NULL;
663996b3 1336 bool load_extras = false;
60f067b4
JS
1337 MountParameters *p;
1338 bool delete, changed = false;
1339 Unit *u;
1340 int r;
663996b3
MS
1341
1342 assert(m);
1343 assert(what);
1344 assert(where);
1345 assert(options);
1346 assert(fstype);
1347
1348 /* Ignore API mount points. They should never be referenced in
1349 * dependencies ever. */
1350 if (mount_point_is_api(where) || mount_point_ignore(where))
1351 return 0;
1352
1353 if (streq(fstype, "autofs"))
1354 return 0;
1355
1356 /* probably some kind of swap, ignore */
1357 if (!is_path(where))
1358 return 0;
1359
e3bff60a
MP
1360 r = unit_name_from_path(where, ".mount", &e);
1361 if (r < 0)
1362 return r;
663996b3
MS
1363
1364 u = manager_get_unit(m, e);
1365 if (!u) {
1366 delete = true;
1367
1368 u = unit_new(m, sizeof(Mount));
60f067b4 1369 if (!u)
e3bff60a 1370 return log_oom();
663996b3
MS
1371
1372 r = unit_add_name(u, e);
663996b3
MS
1373 if (r < 0)
1374 goto fail;
1375
1376 MOUNT(u)->where = strdup(where);
1377 if (!MOUNT(u)->where) {
1378 r = -ENOMEM;
1379 goto fail;
1380 }
1381
1382 u->source_path = strdup("/proc/self/mountinfo");
1383 if (!u->source_path) {
1384 r = -ENOMEM;
1385 goto fail;
1386 }
1387
e3bff60a 1388 if (m->running_as == MANAGER_SYSTEM) {
60f067b4
JS
1389 const char* target;
1390
f47781d8 1391 target = mount_needs_network(options, fstype) ? SPECIAL_REMOTE_FS_TARGET : SPECIAL_LOCAL_FS_TARGET;
60f067b4 1392 r = unit_add_dependency_by_name(u, UNIT_BEFORE, target, NULL, true);
14228c0d
MB
1393 if (r < 0)
1394 goto fail;
60f067b4
JS
1395
1396 if (should_umount(MOUNT(u))) {
1397 r = unit_add_dependency_by_name(u, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
1398 if (r < 0)
1399 goto fail;
1400 }
14228c0d 1401 }
663996b3
MS
1402
1403 unit_add_to_load_queue(u);
60f067b4 1404 changed = true;
663996b3
MS
1405 } else {
1406 delete = false;
663996b3
MS
1407
1408 if (!MOUNT(u)->where) {
1409 MOUNT(u)->where = strdup(where);
1410 if (!MOUNT(u)->where) {
1411 r = -ENOMEM;
1412 goto fail;
1413 }
1414 }
1415
e3bff60a 1416 if (m->running_as == MANAGER_SYSTEM &&
f47781d8
MP
1417 mount_needs_network(options, fstype)) {
1418 /* _netdev option may have shown up late, or on a
1419 * remount. Add remote-fs dependencies, even though
1420 * local-fs ones may already be there. */
1421 unit_add_dependency_by_name(u, UNIT_BEFORE, SPECIAL_REMOTE_FS_TARGET, NULL, true);
1422 load_extras = true;
1423 }
1424
14228c0d 1425 if (u->load_state == UNIT_NOT_FOUND) {
663996b3
MS
1426 u->load_state = UNIT_LOADED;
1427 u->load_error = 0;
1428
1429 /* Load in the extras later on, after we
1430 * finished initialization of the unit */
1431 load_extras = true;
60f067b4 1432 changed = true;
663996b3
MS
1433 }
1434 }
1435
60f067b4
JS
1436 w = strdup(what);
1437 o = strdup(options);
1438 f = strdup(fstype);
1439 if (!w || !o || !f) {
663996b3
MS
1440 r = -ENOMEM;
1441 goto fail;
1442 }
1443
1444 p = &MOUNT(u)->parameters_proc_self_mountinfo;
60f067b4
JS
1445
1446 changed = changed ||
1447 !streq_ptr(p->options, options) ||
1448 !streq_ptr(p->what, what) ||
1449 !streq_ptr(p->fstype, fstype);
1450
663996b3
MS
1451 if (set_flags) {
1452 MOUNT(u)->is_mounted = true;
1453 MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo;
60f067b4 1454 MOUNT(u)->just_changed = changed;
663996b3
MS
1455 }
1456
1457 MOUNT(u)->from_proc_self_mountinfo = true;
1458
1459 free(p->what);
1460 p->what = w;
60f067b4 1461 w = NULL;
663996b3
MS
1462
1463 free(p->options);
1464 p->options = o;
60f067b4 1465 o = NULL;
663996b3
MS
1466
1467 free(p->fstype);
1468 p->fstype = f;
60f067b4 1469 f = NULL;
663996b3
MS
1470
1471 if (load_extras) {
1472 r = mount_add_extras(MOUNT(u));
1473 if (r < 0)
1474 goto fail;
1475 }
1476
60f067b4
JS
1477 if (changed)
1478 unit_add_to_dbus_queue(u);
663996b3
MS
1479
1480 return 0;
1481
1482fail:
e3bff60a
MP
1483 log_warning_errno(r, "Failed to set up mount unit: %m");
1484
663996b3
MS
1485 if (delete && u)
1486 unit_free(u);
1487
1488 return r;
1489}
1490
1491static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
e3bff60a
MP
1492 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
1493 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
663996b3 1494 int r = 0;
663996b3
MS
1495
1496 assert(m);
1497
e3bff60a
MP
1498 t = mnt_new_table();
1499 if (!t)
f47781d8
MP
1500 return log_oom();
1501
e3bff60a
MP
1502 i = mnt_new_iter(MNT_ITER_FORWARD);
1503 if (!i)
1504 return log_oom();
1505
1506 r = mnt_table_parse_mtab(t, NULL);
f47781d8 1507 if (r < 0)
e3bff60a 1508 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
663996b3 1509
f47781d8
MP
1510 r = 0;
1511 for (;;) {
1512 const char *device, *path, *options, *fstype;
e3bff60a
MP
1513 _cleanup_free_ char *d = NULL, *p = NULL;
1514 struct libmnt_fs *fs;
663996b3
MS
1515 int k;
1516
e3bff60a 1517 k = mnt_table_next_fs(t, i, &fs);
f47781d8 1518 if (k == 1)
14228c0d 1519 break;
e3bff60a
MP
1520 if (k < 0)
1521 return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
14228c0d 1522
f47781d8
MP
1523 device = mnt_fs_get_source(fs);
1524 path = mnt_fs_get_target(fs);
1525 options = mnt_fs_get_options(fs);
1526 fstype = mnt_fs_get_fstype(fs);
663996b3 1527
86f210e9
MP
1528 if (!device || !path)
1529 continue;
1530
e3bff60a 1531 if (cunescape(device, UNESCAPE_RELAX, &d) < 0)
14228c0d 1532 return log_oom();
663996b3 1533
e3bff60a
MP
1534 if (cunescape(path, UNESCAPE_RELAX, &p) < 0)
1535 return log_oom();
1536
1537 (void) device_found_node(m, d, true, DEVICE_FOUND_MOUNT, set_flags);
1538
1539 k = mount_setup_unit(m, d, p, options, fstype, set_flags);
f47781d8 1540 if (r == 0 && k < 0)
663996b3 1541 r = k;
663996b3
MS
1542 }
1543
663996b3
MS
1544 return r;
1545}
1546
1547static void mount_shutdown(Manager *m) {
6300502b 1548
663996b3
MS
1549 assert(m);
1550
60f067b4
JS
1551 m->mount_event_source = sd_event_source_unref(m->mount_event_source);
1552
6300502b
MP
1553 mnt_unref_monitor(m->mount_monitor);
1554 m->mount_monitor = NULL;
663996b3
MS
1555}
1556
4c89c718 1557static int mount_get_timeout(Unit *u, usec_t *timeout) {
60f067b4 1558 Mount *m = MOUNT(u);
4c89c718 1559 usec_t t;
60f067b4
JS
1560 int r;
1561
1562 if (!m->timer_event_source)
1563 return 0;
1564
4c89c718 1565 r = sd_event_source_get_time(m->timer_event_source, &t);
60f067b4
JS
1566 if (r < 0)
1567 return r;
4c89c718
MP
1568 if (t == USEC_INFINITY)
1569 return 0;
60f067b4 1570
4c89c718 1571 *timeout = t;
60f067b4
JS
1572 return 1;
1573}
1574
db2df898 1575static void mount_enumerate(Manager *m) {
663996b3 1576 int r;
6300502b 1577
663996b3
MS
1578 assert(m);
1579
f47781d8
MP
1580 mnt_init_debug(0);
1581
6300502b
MP
1582 if (!m->mount_monitor) {
1583 int fd;
663996b3 1584
6300502b
MP
1585 m->mount_monitor = mnt_new_monitor();
1586 if (!m->mount_monitor) {
db2df898 1587 log_oom();
60f067b4 1588 goto fail;
6300502b 1589 }
663996b3 1590
6300502b 1591 r = mnt_monitor_enable_kernel(m->mount_monitor, 1);
db2df898
MP
1592 if (r < 0) {
1593 log_error_errno(r, "Failed to enable watching of kernel mount events: %m");
60f067b4 1594 goto fail;
db2df898
MP
1595 }
1596
6300502b 1597 r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL);
db2df898
MP
1598 if (r < 0) {
1599 log_error_errno(r, "Failed to enable watching of userspace mount events: %m");
f47781d8 1600 goto fail;
db2df898 1601 }
f47781d8 1602
6300502b
MP
1603 /* mnt_unref_monitor() will close the fd */
1604 fd = r = mnt_monitor_get_fd(m->mount_monitor);
db2df898
MP
1605 if (r < 0) {
1606 log_error_errno(r, "Failed to acquire watch file descriptor: %m");
f47781d8 1607 goto fail;
db2df898 1608 }
f47781d8 1609
6300502b 1610 r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m);
db2df898
MP
1611 if (r < 0) {
1612 log_error_errno(r, "Failed to watch mount file descriptor: %m");
f47781d8 1613 goto fail;
db2df898 1614 }
f47781d8 1615
6300502b 1616 r = sd_event_source_set_priority(m->mount_event_source, -10);
db2df898
MP
1617 if (r < 0) {
1618 log_error_errno(r, "Failed to adjust mount watch priority: %m");
f47781d8 1619 goto fail;
db2df898 1620 }
e3bff60a 1621
6300502b 1622 (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch");
f47781d8
MP
1623 }
1624
663996b3
MS
1625 r = mount_load_proc_self_mountinfo(m, false);
1626 if (r < 0)
1627 goto fail;
1628
db2df898 1629 return;
663996b3
MS
1630
1631fail:
1632 mount_shutdown(m);
663996b3
MS
1633}
1634
60f067b4 1635static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
e3bff60a 1636 _cleanup_set_free_ Set *around = NULL, *gone = NULL;
60f067b4 1637 Manager *m = userdata;
e3bff60a
MP
1638 const char *what;
1639 Iterator i;
663996b3
MS
1640 Unit *u;
1641 int r;
1642
1643 assert(m);
6300502b 1644 assert(revents & EPOLLIN);
f47781d8 1645
6300502b 1646 if (fd == mnt_monitor_get_fd(m->mount_monitor)) {
f47781d8
MP
1647 bool rescan = false;
1648
6300502b
MP
1649 /* Drain all events and verify that the event is valid.
1650 *
1651 * Note that libmount also monitors /run/mount mkdir if the
1652 * directory does not exist yet. The mkdir may generate event
1653 * which is irrelevant for us.
1654 *
1655 * error: r < 0; valid: r == 0, false positive: rc == 1 */
1656 do {
1657 r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL);
1658 if (r == 0)
1659 rescan = true;
1660 else if (r < 0)
1661 return log_error_errno(r, "Failed to drain libmount events");
1662 } while (r == 0);
1663
1664 log_debug("libmount event [rescan: %s]", yes_no(rescan));
f47781d8
MP
1665 if (!rescan)
1666 return 0;
1667 }
663996b3
MS
1668
1669 r = mount_load_proc_self_mountinfo(m, true);
1670 if (r < 0) {
663996b3
MS
1671 /* Reset flags, just in case, for later calls */
1672 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1673 Mount *mount = MOUNT(u);
1674
1675 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1676 }
1677
60f067b4 1678 return 0;
663996b3
MS
1679 }
1680
1681 manager_dispatch_load_queue(m);
1682
1683 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {
1684 Mount *mount = MOUNT(u);
1685
1686 if (!mount->is_mounted) {
663996b3 1687
e3bff60a
MP
1688 /* A mount point is not around right now. It
1689 * might be gone, or might never have
1690 * existed. */
1691
1692 if (mount->from_proc_self_mountinfo &&
1693 mount->parameters_proc_self_mountinfo.what) {
1694
1695 /* Remember that this device might just have disappeared */
1696 if (set_ensure_allocated(&gone, &string_hash_ops) < 0 ||
1697 set_put(gone, mount->parameters_proc_self_mountinfo.what) < 0)
1698 log_oom(); /* we don't care too much about OOM here... */
1699 }
1700
663996b3
MS
1701 mount->from_proc_self_mountinfo = false;
1702
1703 switch (mount->state) {
1704
1705 case MOUNT_MOUNTED:
60f067b4
JS
1706 /* This has just been unmounted by
1707 * somebody else, follow the state
1708 * change. */
663996b3
MS
1709 mount_enter_dead(mount, MOUNT_SUCCESS);
1710 break;
1711
1712 default:
663996b3 1713 break;
663996b3
MS
1714 }
1715
1716 } else if (mount->just_mounted || mount->just_changed) {
1717
e3bff60a 1718 /* A mount point was added or changed */
663996b3
MS
1719
1720 switch (mount->state) {
1721
1722 case MOUNT_DEAD:
1723 case MOUNT_FAILED:
60f067b4
JS
1724 /* This has just been mounted by
1725 * somebody else, follow the state
1726 * change. */
663996b3
MS
1727 mount_enter_mounted(mount, MOUNT_SUCCESS);
1728 break;
1729
1730 case MOUNT_MOUNTING:
60f067b4 1731 mount_set_state(mount, MOUNT_MOUNTING_DONE);
663996b3
MS
1732 break;
1733
1734 default:
1735 /* Nothing really changed, but let's
1736 * issue an notification call
1737 * nonetheless, in case somebody is
1738 * waiting for this. (e.g. file system
1739 * ro/rw remounts.) */
1740 mount_set_state(mount, mount->state);
1741 break;
1742 }
1743 }
1744
e3bff60a
MP
1745 if (mount->is_mounted &&
1746 mount->from_proc_self_mountinfo &&
1747 mount->parameters_proc_self_mountinfo.what) {
1748
1749 if (set_ensure_allocated(&around, &string_hash_ops) < 0 ||
1750 set_put(around, mount->parameters_proc_self_mountinfo.what) < 0)
1751 log_oom();
1752 }
1753
663996b3
MS
1754 /* Reset the flags for later calls */
1755 mount->is_mounted = mount->just_mounted = mount->just_changed = false;
1756 }
60f067b4 1757
e3bff60a
MP
1758 SET_FOREACH(what, gone, i) {
1759 if (set_contains(around, what))
1760 continue;
1761
1762 /* Let the device units know that the device is no longer mounted */
1763 (void) device_found_node(m, what, false, DEVICE_FOUND_MOUNT, true);
1764 }
1765
60f067b4 1766 return 0;
663996b3
MS
1767}
1768
1769static void mount_reset_failed(Unit *u) {
1770 Mount *m = MOUNT(u);
1771
1772 assert(m);
1773
1774 if (m->state == MOUNT_FAILED)
1775 mount_set_state(m, MOUNT_DEAD);
1776
1777 m->result = MOUNT_SUCCESS;
1778 m->reload_result = MOUNT_SUCCESS;
1779}
1780
60f067b4 1781static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
663996b3
MS
1782 return unit_kill_common(u, who, signo, -1, MOUNT(u)->control_pid, error);
1783}
1784
663996b3
MS
1785static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
1786 [MOUNT_EXEC_MOUNT] = "ExecMount",
1787 [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
1788 [MOUNT_EXEC_REMOUNT] = "ExecRemount",
1789};
1790
1791DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand);
1792
1793static const char* const mount_result_table[_MOUNT_RESULT_MAX] = {
1794 [MOUNT_SUCCESS] = "success",
1795 [MOUNT_FAILURE_RESOURCES] = "resources",
1796 [MOUNT_FAILURE_TIMEOUT] = "timeout",
1797 [MOUNT_FAILURE_EXIT_CODE] = "exit-code",
1798 [MOUNT_FAILURE_SIGNAL] = "signal",
1799 [MOUNT_FAILURE_CORE_DUMP] = "core-dump"
1800};
1801
1802DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult);
1803
1804const UnitVTable mount_vtable = {
1805 .object_size = sizeof(Mount),
60f067b4
JS
1806 .exec_context_offset = offsetof(Mount, exec_context),
1807 .cgroup_context_offset = offsetof(Mount, cgroup_context),
1808 .kill_context_offset = offsetof(Mount, kill_context),
1809 .exec_runtime_offset = offsetof(Mount, exec_runtime),
663996b3
MS
1810
1811 .sections =
1812 "Unit\0"
1813 "Mount\0"
1814 "Install\0",
14228c0d 1815 .private_section = "Mount",
663996b3
MS
1816
1817 .no_alias = true,
1818 .no_instances = true,
1819
1820 .init = mount_init,
1821 .load = mount_load,
1822 .done = mount_done,
1823
1824 .coldplug = mount_coldplug,
1825
1826 .dump = mount_dump,
1827
1828 .start = mount_start,
1829 .stop = mount_stop,
1830 .reload = mount_reload,
1831
1832 .kill = mount_kill,
1833
1834 .serialize = mount_serialize,
1835 .deserialize_item = mount_deserialize_item,
1836
1837 .active_state = mount_active_state,
1838 .sub_state_to_string = mount_sub_state_to_string,
1839
1840 .check_gc = mount_check_gc,
1841
1842 .sigchld_event = mount_sigchld_event,
663996b3
MS
1843
1844 .reset_failed = mount_reset_failed,
1845
60f067b4 1846 .bus_vtable = bus_mount_vtable,
14228c0d
MB
1847 .bus_set_property = bus_mount_set_property,
1848 .bus_commit_properties = bus_mount_commit_properties,
663996b3 1849
60f067b4
JS
1850 .get_timeout = mount_get_timeout,
1851
1852 .can_transient = true,
1853
663996b3
MS
1854 .enumerate = mount_enumerate,
1855 .shutdown = mount_shutdown,
1856
1857 .status_message_formats = {
1858 .starting_stopping = {
1859 [0] = "Mounting %s...",
1860 [1] = "Unmounting %s...",
1861 },
1862 .finished_start_job = {
1863 [JOB_DONE] = "Mounted %s.",
1864 [JOB_FAILED] = "Failed to mount %s.",
663996b3
MS
1865 [JOB_TIMEOUT] = "Timed out mounting %s.",
1866 },
1867 .finished_stop_job = {
1868 [JOB_DONE] = "Unmounted %s.",
1869 [JOB_FAILED] = "Failed unmounting %s.",
1870 [JOB_TIMEOUT] = "Timed out unmounting %s.",
1871 },
1872 },
1873};