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