]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
5cb5a6ff | 3 | #include <errno.h> |
4f5dd394 | 4 | #include <signal.h> |
b08d03ff | 5 | #include <stdio.h> |
ef734fd6 | 6 | #include <sys/epoll.h> |
5cb5a6ff | 7 | |
20ad4cfd | 8 | #include "sd-messages.h" |
4f5dd394 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
4139c1b2 | 11 | #include "dbus-mount.h" |
6fcbec6f | 12 | #include "dbus-unit.h" |
57b7a260 | 13 | #include "device.h" |
9a57c629 | 14 | #include "exit-status.h" |
f97b34a6 | 15 | #include "format-util.h" |
4f5dd394 | 16 | #include "fstab-util.h" |
fb36b133 | 17 | #include "libmount-util.h" |
4f5dd394 LP |
18 | #include "log.h" |
19 | #include "manager.h" | |
35cd0ba5 | 20 | #include "mkdir-label.h" |
4f5dd394 LP |
21 | #include "mount-setup.h" |
22 | #include "mount.h" | |
049af8ad | 23 | #include "mountpoint-util.h" |
6bedfcbb | 24 | #include "parse-util.h" |
4f5dd394 | 25 | #include "path-util.h" |
7b3e062c | 26 | #include "process-util.h" |
d68c645b | 27 | #include "serialize.h" |
4f5dd394 | 28 | #include "special.h" |
8b43440b | 29 | #include "string-table.h" |
b5efdb8a | 30 | #include "string-util.h" |
4f5dd394 LP |
31 | #include "strv.h" |
32 | #include "unit-name.h" | |
33 | #include "unit.h" | |
5cb5a6ff | 34 | |
7d54a03a LP |
35 | #define RETRY_UMOUNT_MAX 32 |
36 | ||
f50e0a01 LP |
37 | static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = { |
38 | [MOUNT_DEAD] = UNIT_INACTIVE, | |
39 | [MOUNT_MOUNTING] = UNIT_ACTIVATING, | |
b6ba0c16 | 40 | [MOUNT_MOUNTING_DONE] = UNIT_ACTIVATING, |
f50e0a01 | 41 | [MOUNT_MOUNTED] = UNIT_ACTIVE, |
032ff4af | 42 | [MOUNT_REMOUNTING] = UNIT_RELOADING, |
f50e0a01 | 43 | [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING, |
032ff4af LP |
44 | [MOUNT_REMOUNTING_SIGTERM] = UNIT_RELOADING, |
45 | [MOUNT_REMOUNTING_SIGKILL] = UNIT_RELOADING, | |
e537352b LP |
46 | [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING, |
47 | [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING, | |
17e9d53d YW |
48 | [MOUNT_FAILED] = UNIT_FAILED, |
49 | [MOUNT_CLEANING] = UNIT_MAINTENANCE, | |
f50e0a01 | 50 | }; |
5cb5a6ff | 51 | |
718db961 LP |
52 | static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
53 | static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
35080486 | 54 | static int mount_process_proc_self_mountinfo(Manager *m); |
718db961 | 55 | |
c634f3d2 LP |
56 | static bool MOUNT_STATE_WITH_PROCESS(MountState state) { |
57 | return IN_SET(state, | |
58 | MOUNT_MOUNTING, | |
59 | MOUNT_MOUNTING_DONE, | |
60 | MOUNT_REMOUNTING, | |
61 | MOUNT_REMOUNTING_SIGTERM, | |
62 | MOUNT_REMOUNTING_SIGKILL, | |
63 | MOUNT_UNMOUNTING, | |
64 | MOUNT_UNMOUNTING_SIGTERM, | |
17e9d53d YW |
65 | MOUNT_UNMOUNTING_SIGKILL, |
66 | MOUNT_CLEANING); | |
c634f3d2 LP |
67 | } |
68 | ||
47cd17ea LP |
69 | static MountParameters* get_mount_parameters_fragment(Mount *m) { |
70 | assert(m); | |
71 | ||
72 | if (m->from_fragment) | |
73 | return &m->parameters_fragment; | |
74 | ||
75 | return NULL; | |
76 | } | |
77 | ||
78 | static MountParameters* get_mount_parameters(Mount *m) { | |
79 | assert(m); | |
80 | ||
81 | if (m->from_proc_self_mountinfo) | |
82 | return &m->parameters_proc_self_mountinfo; | |
83 | ||
84 | return get_mount_parameters_fragment(m); | |
85 | } | |
86 | ||
7121cbcf LP |
87 | static bool mount_is_network(const MountParameters *p) { |
88 | assert(p); | |
89 | ||
90 | if (fstab_test_option(p->options, "_netdev\0")) | |
fc676b00 ZJS |
91 | return true; |
92 | ||
7121cbcf | 93 | if (p->fstype && fstype_is_network(p->fstype)) |
fc676b00 ZJS |
94 | return true; |
95 | ||
96 | return false; | |
97 | } | |
98 | ||
5a7c4f4f FB |
99 | static bool mount_is_nofail(const Mount *m) { |
100 | assert(m); | |
101 | ||
102 | if (!m->from_fragment) | |
103 | return false; | |
104 | ||
105 | return fstab_test_yes_no_option(m->parameters_fragment.options, "nofail\0" "fail\0"); | |
106 | } | |
107 | ||
d3bd0986 MK |
108 | static bool mount_is_loop(const MountParameters *p) { |
109 | assert(p); | |
110 | ||
111 | if (fstab_test_option(p->options, "loop\0")) | |
112 | return true; | |
113 | ||
114 | return false; | |
115 | } | |
116 | ||
e6a7b9f4 | 117 | static bool mount_is_bind(const MountParameters *p) { |
fc676b00 ZJS |
118 | assert(p); |
119 | ||
d15d0333 | 120 | if (fstab_test_option(p->options, "bind\0" "rbind\0")) |
fc676b00 ZJS |
121 | return true; |
122 | ||
d15d0333 | 123 | if (p->fstype && STR_IN_SET(p->fstype, "bind", "rbind")) |
fc676b00 ZJS |
124 | return true; |
125 | ||
126 | return false; | |
127 | } | |
128 | ||
47cd17ea | 129 | static bool mount_is_bound_to_device(Mount *m) { |
ebc8968b FB |
130 | const MountParameters *p; |
131 | ||
47cd17ea LP |
132 | assert(m); |
133 | ||
134 | /* Determines whether to place a Requires= or BindsTo= dependency on the backing device unit. We do | |
135 | * this by checking for the x-systemd.device-bound mount option. Iff it is set we use BindsTo=, | |
136 | * otherwise Requires=. But note that we might combine the latter with StopPropagatedFrom=, see | |
137 | * below. */ | |
138 | ||
139 | p = get_mount_parameters(m); | |
140 | if (!p) | |
141 | return false; | |
ebc8968b | 142 | |
ebc8968b FB |
143 | return fstab_test_option(p->options, "x-systemd.device-bound\0"); |
144 | } | |
145 | ||
47cd17ea LP |
146 | static bool mount_propagate_stop(Mount *m) { |
147 | assert(m); | |
148 | ||
149 | if (mount_is_bound_to_device(m)) /* If we are using BindsTo= the stop propagation is implicit, no need to bother */ | |
150 | return false; | |
151 | ||
152 | return m->from_fragment; /* let's propagate stop whenever this is an explicitly configured unit, | |
153 | * otherwise let's not bother. */ | |
154 | } | |
155 | ||
06e457b1 | 156 | static bool mount_needs_quota(const MountParameters *p) { |
fc676b00 ZJS |
157 | assert(p); |
158 | ||
06e457b1 LP |
159 | /* Quotas are not enabled on network filesystems, but we want them, for example, on storage connected via |
160 | * iscsi. We hence don't use mount_is_network() here, as that would also return true for _netdev devices. */ | |
11041c84 | 161 | if (p->fstype && fstype_is_network(p->fstype)) |
fc676b00 ZJS |
162 | return false; |
163 | ||
164 | if (mount_is_bind(p)) | |
165 | return false; | |
166 | ||
d15d0333 ZJS |
167 | return fstab_test_option(p->options, |
168 | "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0"); | |
fc676b00 ZJS |
169 | } |
170 | ||
a16e1123 LP |
171 | static void mount_init(Unit *u) { |
172 | Mount *m = MOUNT(u); | |
5cb5a6ff | 173 | |
c73f413d | 174 | assert(m); |
a16e1123 | 175 | assert(u); |
ac155bb8 | 176 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 177 | |
1f19a534 | 178 | m->timeout_usec = u->manager->default_timeout_start_usec; |
5804e1b6 AJ |
179 | |
180 | m->exec_context.std_output = u->manager->default_std_output; | |
181 | m->exec_context.std_error = u->manager->default_std_error; | |
182 | ||
3e5235b0 LP |
183 | m->directory_mode = 0755; |
184 | ||
f00929ad DJL |
185 | /* We need to make sure that /usr/bin/mount is always called |
186 | * in the same process group as us, so that the autofs kernel | |
a16e1123 LP |
187 | * side doesn't send us another mount request while we are |
188 | * already trying to comply its last one. */ | |
74922904 | 189 | m->exec_context.same_pgrp = true; |
8d567588 | 190 | |
a16e1123 | 191 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; |
c8f4d764 | 192 | |
5bcb0f2b | 193 | u->ignore_on_isolate = true; |
8d567588 LP |
194 | } |
195 | ||
36c16a7c | 196 | static int mount_arm_timer(Mount *m, usec_t usec) { |
718db961 LP |
197 | int r; |
198 | ||
199 | assert(m); | |
200 | ||
718db961 | 201 | if (m->timer_event_source) { |
36c16a7c | 202 | r = sd_event_source_set_time(m->timer_event_source, usec); |
718db961 LP |
203 | if (r < 0) |
204 | return r; | |
205 | ||
206 | return sd_event_source_set_enabled(m->timer_event_source, SD_EVENT_ONESHOT); | |
207 | } | |
208 | ||
36c16a7c LP |
209 | if (usec == USEC_INFINITY) |
210 | return 0; | |
211 | ||
7dfbe2e3 | 212 | r = sd_event_add_time( |
6a0f1f6d LP |
213 | UNIT(m)->manager->event, |
214 | &m->timer_event_source, | |
215 | CLOCK_MONOTONIC, | |
36c16a7c | 216 | usec, 0, |
6a0f1f6d | 217 | mount_dispatch_timer, m); |
7dfbe2e3 TG |
218 | if (r < 0) |
219 | return r; | |
220 | ||
221 | (void) sd_event_source_set_description(m->timer_event_source, "mount-timer"); | |
222 | ||
223 | return 0; | |
718db961 LP |
224 | } |
225 | ||
a16e1123 | 226 | static void mount_unwatch_control_pid(Mount *m) { |
5e94833f LP |
227 | assert(m); |
228 | ||
229 | if (m->control_pid <= 0) | |
230 | return; | |
231 | ||
8f03de53 | 232 | unit_unwatch_pid(UNIT(m), TAKE_PID(m->control_pid)); |
5e94833f LP |
233 | } |
234 | ||
e537352b LP |
235 | static void mount_parameters_done(MountParameters *p) { |
236 | assert(p); | |
237 | ||
a26592cf LP |
238 | p->what = mfree(p->what); |
239 | p->options = mfree(p->options); | |
240 | p->fstype = mfree(p->fstype); | |
e537352b LP |
241 | } |
242 | ||
87f0e418 | 243 | static void mount_done(Unit *u) { |
ef734fd6 | 244 | Mount *m = MOUNT(u); |
034c6ed7 | 245 | |
ef734fd6 | 246 | assert(m); |
034c6ed7 | 247 | |
a1e58e8e | 248 | m->where = mfree(m->where); |
f50e0a01 | 249 | |
e537352b LP |
250 | mount_parameters_done(&m->parameters_proc_self_mountinfo); |
251 | mount_parameters_done(&m->parameters_fragment); | |
ef734fd6 | 252 | |
e8a565cb | 253 | m->exec_runtime = exec_runtime_unref(m->exec_runtime, false); |
e537352b LP |
254 | exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); |
255 | m->control_command = NULL; | |
f50e0a01 | 256 | |
29206d46 LP |
257 | dynamic_creds_unref(&m->dynamic_creds); |
258 | ||
a16e1123 | 259 | mount_unwatch_control_pid(m); |
f50e0a01 | 260 | |
5dcadb4c | 261 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
f50e0a01 LP |
262 | } |
263 | ||
9ddaa3e4 | 264 | static int update_parameters_proc_self_mountinfo( |
b7bbf890 ZJS |
265 | Mount *m, |
266 | const char *what, | |
267 | const char *options, | |
268 | const char *fstype) { | |
269 | ||
270 | MountParameters *p; | |
271 | int r, q, w; | |
272 | ||
273 | p = &m->parameters_proc_self_mountinfo; | |
274 | ||
275 | r = free_and_strdup(&p->what, what); | |
276 | if (r < 0) | |
277 | return r; | |
278 | ||
279 | q = free_and_strdup(&p->options, options); | |
280 | if (q < 0) | |
281 | return q; | |
282 | ||
283 | w = free_and_strdup(&p->fstype, fstype); | |
284 | if (w < 0) | |
285 | return w; | |
286 | ||
287 | return r > 0 || q > 0 || w > 0; | |
288 | } | |
289 | ||
eef85c4a | 290 | static int mount_add_mount_dependencies(Mount *m) { |
5c78d8e2 | 291 | MountParameters *pm; |
ac155bb8 | 292 | Unit *other; |
a57f7e2c | 293 | Set *s; |
b08d03ff LP |
294 | int r; |
295 | ||
6e2ef85b | 296 | assert(m); |
b08d03ff | 297 | |
a57f7e2c | 298 | if (!path_equal(m->where, "/")) { |
eef85c4a LP |
299 | _cleanup_free_ char *parent = NULL; |
300 | ||
301 | /* Adds in links to other mount points that might lie further up in the hierarchy */ | |
5f311f8c LP |
302 | |
303 | parent = dirname_malloc(m->where); | |
304 | if (!parent) | |
305 | return -ENOMEM; | |
01f78473 | 306 | |
eef85c4a | 307 | r = unit_require_mounts_for(UNIT(m), parent, UNIT_DEPENDENCY_IMPLICIT); |
4f0eedb7 | 308 | if (r < 0) |
01f78473 | 309 | return r; |
4f0eedb7 | 310 | } |
01f78473 | 311 | |
eef85c4a LP |
312 | /* Adds in dependencies to other mount points that might be needed for the source path (if this is a bind mount |
313 | * or a loop mount) to be available. */ | |
a57f7e2c | 314 | pm = get_mount_parameters_fragment(m); |
fc676b00 ZJS |
315 | if (pm && pm->what && |
316 | path_is_absolute(pm->what) && | |
d3bd0986 | 317 | (mount_is_bind(pm) || mount_is_loop(pm) || !mount_is_network(pm))) { |
fc676b00 | 318 | |
eef85c4a | 319 | r = unit_require_mounts_for(UNIT(m), pm->what, UNIT_DEPENDENCY_FILE); |
4f0eedb7 | 320 | if (r < 0) |
6e2ef85b | 321 | return r; |
4f0eedb7 | 322 | } |
b08d03ff | 323 | |
eef85c4a | 324 | /* Adds in dependencies to other units that use this path or paths further down in the hierarchy */ |
a57f7e2c | 325 | s = manager_get_units_requiring_mounts_for(UNIT(m)->manager, m->where); |
90e74a66 | 326 | SET_FOREACH(other, s) { |
b08d03ff | 327 | |
a57f7e2c LP |
328 | if (other->load_state != UNIT_LOADED) |
329 | continue; | |
b08d03ff | 330 | |
a57f7e2c LP |
331 | if (other == UNIT(m)) |
332 | continue; | |
b08d03ff | 333 | |
eef85c4a | 334 | r = unit_add_dependency(other, UNIT_AFTER, UNIT(m), true, UNIT_DEPENDENCY_PATH); |
4f0eedb7 | 335 | if (r < 0) |
6e2ef85b | 336 | return r; |
b08d03ff | 337 | |
a57f7e2c LP |
338 | if (UNIT(m)->fragment_path) { |
339 | /* If we have fragment configuration, then make this dependency required */ | |
eef85c4a | 340 | r = unit_add_dependency(other, UNIT_REQUIRES, UNIT(m), true, UNIT_DEPENDENCY_PATH); |
a57f7e2c LP |
341 | if (r < 0) |
342 | return r; | |
343 | } | |
7c8fa05c LP |
344 | } |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
eef85c4a | 349 | static int mount_add_device_dependencies(Mount *m) { |
eef85c4a LP |
350 | UnitDependencyMask mask; |
351 | MountParameters *p; | |
ebc8968b | 352 | UnitDependency dep; |
9fff8a1f | 353 | int r; |
173a8d04 LP |
354 | |
355 | assert(m); | |
356 | ||
06e97888 | 357 | p = get_mount_parameters(m); |
6b1dc2bd | 358 | if (!p) |
173a8d04 LP |
359 | return 0; |
360 | ||
9fff8a1f | 361 | if (!p->what) |
173a8d04 | 362 | return 0; |
5c78d8e2 | 363 | |
dd144c63 LP |
364 | if (mount_is_bind(p)) |
365 | return 0; | |
366 | ||
367 | if (!is_device_path(p->what)) | |
368 | return 0; | |
369 | ||
5de0acf4 LP |
370 | /* /dev/root is a really weird thing, it's not a real device, but just a path the kernel exports for |
371 | * the root file system specified on the kernel command line. Ignore it here. */ | |
372 | if (PATH_IN_SET(p->what, "/dev/root", "/dev/nfs")) | |
7ba2711d LP |
373 | return 0; |
374 | ||
dd144c63 LP |
375 | if (path_equal(m->where, "/")) |
376 | return 0; | |
377 | ||
44b0d1fd | 378 | /* Mount units from /proc/self/mountinfo are not bound to devices by default since they're subject to |
47cd17ea LP |
379 | * races when mounts are established by other tools with different backing devices than what we |
380 | * maintain. The user can still force this to be a BindsTo= dependency with an appropriate option (or | |
381 | * udev property) so the mount units are automatically stopped when the device disappears | |
44b0d1fd | 382 | * suddenly. */ |
ebc8968b FB |
383 | dep = mount_is_bound_to_device(m) ? UNIT_BINDS_TO : UNIT_REQUIRES; |
384 | ||
c52c2dc6 ZJS |
385 | /* We always use 'what' from /proc/self/mountinfo if mounted */ |
386 | mask = m->from_proc_self_mountinfo ? UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT : UNIT_DEPENDENCY_FILE; | |
eef85c4a | 387 | |
d336ba9f | 388 | r = unit_add_node_dependency(UNIT(m), p->what, dep, mask); |
dd144c63 LP |
389 | if (r < 0) |
390 | return r; | |
47cd17ea LP |
391 | if (mount_propagate_stop(m)) { |
392 | r = unit_add_node_dependency(UNIT(m), p->what, UNIT_STOP_PROPAGATED_FROM, mask); | |
393 | if (r < 0) | |
394 | return r; | |
395 | } | |
9fff8a1f | 396 | |
44b0d1fd | 397 | return unit_add_blockdev_dependency(UNIT(m), p->what, mask); |
173a8d04 LP |
398 | } |
399 | ||
eef85c4a LP |
400 | static int mount_add_quota_dependencies(Mount *m) { |
401 | UnitDependencyMask mask; | |
6b1dc2bd | 402 | MountParameters *p; |
eef85c4a | 403 | int r; |
6b1dc2bd LP |
404 | |
405 | assert(m); | |
406 | ||
463d0d15 | 407 | if (!MANAGER_IS_SYSTEM(UNIT(m)->manager)) |
6b1dc2bd LP |
408 | return 0; |
409 | ||
410 | p = get_mount_parameters_fragment(m); | |
411 | if (!p) | |
412 | return 0; | |
413 | ||
06e457b1 | 414 | if (!mount_needs_quota(p)) |
6b1dc2bd LP |
415 | return 0; |
416 | ||
eef85c4a LP |
417 | mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT; |
418 | ||
5a724170 | 419 | r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTACHECK_SERVICE, true, mask); |
6b1dc2bd LP |
420 | if (r < 0) |
421 | return r; | |
422 | ||
5a724170 | 423 | r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_WANTS, SPECIAL_QUOTAON_SERVICE, true, mask); |
6b1dc2bd LP |
424 | if (r < 0) |
425 | return r; | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
39c79477 | 430 | static bool mount_is_extrinsic(Unit *u) { |
88ac30a1 | 431 | MountParameters *p; |
39c79477 | 432 | Mount *m = MOUNT(u); |
ad2706db | 433 | assert(m); |
88ac30a1 | 434 | |
bc9e5a4c FB |
435 | /* Returns true for all units that are "magic" and should be excluded from the usual |
436 | * start-up and shutdown dependencies. We call them "extrinsic" here, as they are generally | |
437 | * mounted outside of the systemd dependency logic. We shouldn't attempt to manage them | |
438 | * ourselves but it's fine if the user operates on them with us. */ | |
ad2706db | 439 | |
bc9e5a4c | 440 | /* We only automatically manage mounts if we are in system mode */ |
39c79477 | 441 | if (MANAGER_IS_USER(u->manager)) |
b8e5776d LP |
442 | return true; |
443 | ||
88ac30a1 | 444 | p = get_mount_parameters(m); |
bc9e5a4c | 445 | if (p && fstab_is_extrinsic(m->where, p->options)) |
ad2706db | 446 | return true; |
88ac30a1 | 447 | |
ad2706db | 448 | return false; |
88ac30a1 TG |
449 | } |
450 | ||
61154cf9 FB |
451 | static int mount_add_default_ordering_dependencies( |
452 | Mount *m, | |
453 | MountParameters *p, | |
454 | UnitDependencyMask mask) { | |
455 | ||
83cdc870 | 456 | const char *after, *before, *e; |
d54bab90 | 457 | int r; |
2edd4434 LP |
458 | |
459 | assert(m); | |
460 | ||
83cdc870 FB |
461 | e = path_startswith(m->where, "/sysroot"); |
462 | if (e && in_initrd()) { | |
463 | /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW, | |
464 | * it's not technically part of the basic initrd filesystem itself, and so | |
465 | * shouldn't inherit the default Before=local-fs.target dependency. */ | |
466 | ||
467 | after = NULL; | |
468 | before = isempty(e) ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_INITRD_FS_TARGET; | |
469 | ||
470 | } else if (mount_is_network(p)) { | |
ea0ec5ce | 471 | after = SPECIAL_REMOTE_FS_PRE_TARGET; |
d54bab90 | 472 | before = SPECIAL_REMOTE_FS_TARGET; |
2ec15c4f | 473 | |
d54bab90 | 474 | } else { |
ea0ec5ce | 475 | after = SPECIAL_LOCAL_FS_PRE_TARGET; |
d54bab90 LP |
476 | before = SPECIAL_LOCAL_FS_TARGET; |
477 | } | |
478 | ||
195baa1b | 479 | if (!mount_is_nofail(m)) { |
8c8203db YW |
480 | r = unit_add_dependency_by_name(UNIT(m), UNIT_BEFORE, before, true, mask); |
481 | if (r < 0) | |
482 | return r; | |
483 | } | |
ea0ec5ce | 484 | |
83cdc870 FB |
485 | if (after) { |
486 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, after, true, mask); | |
487 | if (r < 0) | |
488 | return r; | |
489 | } | |
e8d2f6cd | 490 | |
61154cf9 FB |
491 | return unit_add_two_dependencies_by_name(UNIT(m), UNIT_BEFORE, UNIT_CONFLICTS, |
492 | SPECIAL_UMOUNT_TARGET, true, mask); | |
493 | } | |
494 | ||
2edd4434 | 495 | static int mount_add_default_dependencies(Mount *m) { |
eef85c4a | 496 | UnitDependencyMask mask; |
9ddc4a26 | 497 | MountParameters *p; |
d54bab90 | 498 | int r; |
2edd4434 LP |
499 | |
500 | assert(m); | |
501 | ||
4c9ea260 LP |
502 | if (!UNIT(m)->default_dependencies) |
503 | return 0; | |
504 | ||
61154cf9 FB |
505 | /* We do not add any default dependencies to /, /usr or /run/initramfs/, since they are |
506 | * guaranteed to stay mounted the whole time, since our system is on it. Also, don't | |
507 | * bother with anything mounted below virtual file systems, it's also going to be virtual, | |
508 | * and hence not worth the effort. */ | |
39c79477 | 509 | if (mount_is_extrinsic(UNIT(m))) |
6b1dc2bd | 510 | return 0; |
2edd4434 | 511 | |
874d3404 LP |
512 | p = get_mount_parameters(m); |
513 | if (!p) | |
6b1dc2bd LP |
514 | return 0; |
515 | ||
eef85c4a | 516 | mask = m->from_fragment ? UNIT_DEPENDENCY_FILE : UNIT_DEPENDENCY_MOUNTINFO_DEFAULT; |
61154cf9 FB |
517 | |
518 | r = mount_add_default_ordering_dependencies(m, p, mask); | |
ad2706db LP |
519 | if (r < 0) |
520 | return r; | |
eef85c4a | 521 | |
e8d2f6cd | 522 | if (mount_is_network(p)) { |
2ec15c4f FB |
523 | /* We order ourselves after network.target. This is primarily useful at shutdown: |
524 | * services that take down the network should order themselves before | |
525 | * network.target, so that they are shut down only after this mount unit is | |
526 | * stopped. */ | |
6b1dc2bd | 527 | |
35d8c19a | 528 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_NETWORK_TARGET, true, mask); |
a63a5c46 LP |
529 | if (r < 0) |
530 | return r; | |
a63a5c46 | 531 | |
2ec15c4f FB |
532 | /* We pull in network-online.target, and order ourselves after it. This is useful |
533 | * at start-up to actively pull in tools that want to be started before we start | |
534 | * mounting network file systems, and whose purpose it is to delay this until the | |
535 | * network is "up". */ | |
ea0ec5ce | 536 | |
5a724170 | 537 | r = unit_add_two_dependencies_by_name(UNIT(m), UNIT_WANTS, UNIT_AFTER, SPECIAL_NETWORK_ONLINE_TARGET, true, mask); |
e8d2f6cd LP |
538 | if (r < 0) |
539 | return r; | |
d54bab90 LP |
540 | } |
541 | ||
3e3852b3 | 542 | /* If this is a tmpfs mount then we have to unmount it before we try to deactivate swaps */ |
74b1731c | 543 | if (streq_ptr(p->fstype, "tmpfs")) { |
35d8c19a | 544 | r = unit_add_dependency_by_name(UNIT(m), UNIT_AFTER, SPECIAL_SWAP_TARGET, true, mask); |
3e3852b3 LP |
545 | if (r < 0) |
546 | return r; | |
547 | } | |
548 | ||
2edd4434 LP |
549 | return 0; |
550 | } | |
551 | ||
8d567588 | 552 | static int mount_verify(Mount *m) { |
a57f7e2c | 553 | _cleanup_free_ char *e = NULL; |
b294b79f | 554 | MountParameters *p; |
7410616c | 555 | int r; |
a57f7e2c | 556 | |
8d567588 | 557 | assert(m); |
75193d41 | 558 | assert(UNIT(m)->load_state == UNIT_LOADED); |
8d567588 | 559 | |
1df96fcb | 560 | if (!m->from_fragment && !m->from_proc_self_mountinfo && !UNIT(m)->perpetual) |
8cbef760 LP |
561 | return -ENOENT; |
562 | ||
7410616c LP |
563 | r = unit_name_from_path(m->where, ".mount", &e); |
564 | if (r < 0) | |
f2341e0a | 565 | return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m"); |
8d567588 | 566 | |
d85ff944 YW |
567 | if (!unit_has_name(UNIT(m), e)) |
568 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing."); | |
8d567588 | 569 | |
d85ff944 YW |
570 | if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) |
571 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Cannot create mount unit for API file system %s. Refusing.", m->where); | |
33ff02c9 | 572 | |
b294b79f | 573 | p = get_mount_parameters_fragment(m); |
0879fbd6 LP |
574 | if (p && !p->what && !UNIT(m)->perpetual) |
575 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), | |
576 | "What= setting is missing. Refusing."); | |
4e85aff4 | 577 | |
d85ff944 YW |
578 | if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) |
579 | return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing."); | |
4d0e5dbd | 580 | |
8d567588 LP |
581 | return 0; |
582 | } | |
583 | ||
bf7eedbf LP |
584 | static int mount_add_non_exec_dependencies(Mount *m) { |
585 | int r; | |
586 | assert(m); | |
587 | ||
588 | /* Adds in all dependencies directly responsible for ordering the mount, as opposed to dependencies | |
589 | * resulting from the ExecContext and such. */ | |
590 | ||
591 | r = mount_add_device_dependencies(m); | |
592 | if (r < 0) | |
593 | return r; | |
594 | ||
595 | r = mount_add_mount_dependencies(m); | |
596 | if (r < 0) | |
597 | return r; | |
598 | ||
599 | r = mount_add_quota_dependencies(m); | |
600 | if (r < 0) | |
601 | return r; | |
602 | ||
603 | r = mount_add_default_dependencies(m); | |
604 | if (r < 0) | |
605 | return r; | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
1a4ac875 MS |
610 | static int mount_add_extras(Mount *m) { |
611 | Unit *u = UNIT(m); | |
e537352b LP |
612 | int r; |
613 | ||
e821075a LP |
614 | assert(m); |
615 | ||
1f736476 LP |
616 | /* Note: this call might be called after we already have been loaded once (and even when it has already been |
617 | * activated), in case data from /proc/self/mountinfo has changed. This means all code here needs to be ready | |
618 | * to run with an already set up unit. */ | |
619 | ||
e821075a | 620 | if (u->fragment_path) |
1a4ac875 | 621 | m->from_fragment = true; |
e537352b | 622 | |
1a4ac875 | 623 | if (!m->where) { |
7410616c | 624 | r = unit_name_to_path(u->id, &m->where); |
1d0727e7 MS |
625 | if (r == -ENAMETOOLONG) |
626 | log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. " | |
627 | "Set \"Where=\" in the unit file explicitly."); | |
7410616c LP |
628 | if (r < 0) |
629 | return r; | |
1a4ac875 | 630 | } |
a16e1123 | 631 | |
4ff361cc | 632 | path_simplify(m->where); |
e537352b | 633 | |
e821075a | 634 | if (!u->description) { |
1a4ac875 MS |
635 | r = unit_set_description(u, m->where); |
636 | if (r < 0) | |
173a8d04 | 637 | return r; |
1a4ac875 | 638 | } |
6e2ef85b | 639 | |
598459ce LP |
640 | r = unit_patch_contexts(u); |
641 | if (r < 0) | |
642 | return r; | |
4e67ddd6 | 643 | |
598459ce | 644 | r = unit_add_exec_dependencies(u, &m->exec_context); |
a016b922 LP |
645 | if (r < 0) |
646 | return r; | |
647 | ||
d79200e2 | 648 | r = unit_set_default_slice(u); |
1a4ac875 MS |
649 | if (r < 0) |
650 | return r; | |
651 | ||
bf7eedbf | 652 | r = mount_add_non_exec_dependencies(m); |
4c9ea260 LP |
653 | if (r < 0) |
654 | return r; | |
598459ce | 655 | |
1a4ac875 MS |
656 | return 0; |
657 | } | |
658 | ||
75193d41 | 659 | static void mount_load_root_mount(Unit *u) { |
11222d0f LP |
660 | assert(u); |
661 | ||
662 | if (!unit_has_name(u, SPECIAL_ROOT_MOUNT)) | |
75193d41 | 663 | return; |
11222d0f LP |
664 | |
665 | u->perpetual = true; | |
666 | u->default_dependencies = false; | |
667 | ||
668 | /* The stdio/kmsg bridge socket is on /, in order to avoid a dep loop, don't use kmsg logging for -.mount */ | |
669 | MOUNT(u)->exec_context.std_output = EXEC_OUTPUT_NULL; | |
670 | MOUNT(u)->exec_context.std_input = EXEC_INPUT_NULL; | |
671 | ||
672 | if (!u->description) | |
673 | u->description = strdup("Root Mount"); | |
11222d0f LP |
674 | } |
675 | ||
1a4ac875 MS |
676 | static int mount_load(Unit *u) { |
677 | Mount *m = MOUNT(u); | |
75193d41 | 678 | int r, q = 0; |
1a4ac875 | 679 | |
c73f413d | 680 | assert(m); |
1a4ac875 MS |
681 | assert(u); |
682 | assert(u->load_state == UNIT_STUB); | |
683 | ||
75193d41 | 684 | mount_load_root_mount(u); |
11222d0f | 685 | |
c3620770 | 686 | bool fragment_optional = m->from_proc_self_mountinfo || u->perpetual; |
75193d41 | 687 | r = unit_load_fragment_and_dropin(u, !fragment_optional); |
780ae022 LP |
688 | |
689 | /* Add in some extras. Note we do this in all cases (even if we failed to load the unit) when announced by the | |
690 | * kernel, because we need some things to be set up no matter what when the kernel establishes a mount and thus | |
691 | * we need to update the state in our unit to track it. After all, consider that we don't allow changing the | |
692 | * 'slice' field for a unit once it is active. */ | |
693 | if (u->load_state == UNIT_LOADED || m->from_proc_self_mountinfo || u->perpetual) | |
75193d41 | 694 | q = mount_add_extras(m); |
780ae022 | 695 | |
1a4ac875 MS |
696 | if (r < 0) |
697 | return r; | |
780ae022 LP |
698 | if (q < 0) |
699 | return q; | |
75193d41 ZJS |
700 | if (u->load_state != UNIT_LOADED) |
701 | return 0; | |
e537352b | 702 | |
8d567588 | 703 | return mount_verify(m); |
e537352b LP |
704 | } |
705 | ||
706 | static void mount_set_state(Mount *m, MountState state) { | |
707 | MountState old_state; | |
708 | assert(m); | |
709 | ||
6fcbec6f LP |
710 | if (m->state != state) |
711 | bus_unit_send_pending_change_signal(UNIT(m), false); | |
712 | ||
e537352b LP |
713 | old_state = m->state; |
714 | m->state = state; | |
715 | ||
c634f3d2 | 716 | if (!MOUNT_STATE_WITH_PROCESS(state)) { |
5dcadb4c | 717 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
a16e1123 | 718 | mount_unwatch_control_pid(m); |
e537352b | 719 | m->control_command = NULL; |
a16e1123 | 720 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; |
e537352b LP |
721 | } |
722 | ||
723 | if (state != old_state) | |
f2341e0a | 724 | log_unit_debug(UNIT(m), "Changed %s -> %s", mount_state_to_string(old_state), mount_state_to_string(state)); |
e537352b | 725 | |
2ad2e41a LP |
726 | unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state], |
727 | m->reload_result == MOUNT_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE); | |
e537352b LP |
728 | } |
729 | ||
be847e82 | 730 | static int mount_coldplug(Unit *u) { |
e537352b | 731 | Mount *m = MOUNT(u); |
a16e1123 LP |
732 | MountState new_state = MOUNT_DEAD; |
733 | int r; | |
e537352b LP |
734 | |
735 | assert(m); | |
736 | assert(m->state == MOUNT_DEAD); | |
737 | ||
a16e1123 LP |
738 | if (m->deserialized_state != m->state) |
739 | new_state = m->deserialized_state; | |
740 | else if (m->from_proc_self_mountinfo) | |
741 | new_state = MOUNT_MOUNTED; | |
e537352b | 742 | |
5bcb0f2b LP |
743 | if (new_state == m->state) |
744 | return 0; | |
e537352b | 745 | |
c386f588 LP |
746 | if (m->control_pid > 0 && |
747 | pid_is_unwaited(m->control_pid) && | |
c634f3d2 | 748 | MOUNT_STATE_WITH_PROCESS(new_state)) { |
5bcb0f2b | 749 | |
f75f613d | 750 | r = unit_watch_pid(UNIT(m), m->control_pid, false); |
5bcb0f2b LP |
751 | if (r < 0) |
752 | return r; | |
e537352b | 753 | |
36c16a7c | 754 | r = mount_arm_timer(m, usec_add(u->state_change_timestamp.monotonic, m->timeout_usec)); |
5bcb0f2b LP |
755 | if (r < 0) |
756 | return r; | |
a16e1123 | 757 | } |
e537352b | 758 | |
e8a565cb | 759 | if (!IN_SET(new_state, MOUNT_DEAD, MOUNT_FAILED)) { |
29206d46 | 760 | (void) unit_setup_dynamic_creds(u); |
e8a565cb YW |
761 | (void) unit_setup_exec_runtime(u); |
762 | } | |
29206d46 | 763 | |
5bcb0f2b | 764 | mount_set_state(m, new_state); |
e537352b | 765 | return 0; |
e537352b LP |
766 | } |
767 | ||
768 | static void mount_dump(Unit *u, FILE *f, const char *prefix) { | |
769 | Mount *m = MOUNT(u); | |
770 | MountParameters *p; | |
771 | ||
772 | assert(m); | |
773 | assert(f); | |
774 | ||
cb39ed3f | 775 | p = get_mount_parameters(m); |
e537352b LP |
776 | |
777 | fprintf(f, | |
778 | "%sMount State: %s\n" | |
81a5c6d0 | 779 | "%sResult: %s\n" |
17e9d53d | 780 | "%sClean Result: %s\n" |
e537352b LP |
781 | "%sWhere: %s\n" |
782 | "%sWhat: %s\n" | |
783 | "%sFile System Type: %s\n" | |
784 | "%sOptions: %s\n" | |
e537352b LP |
785 | "%sFrom /proc/self/mountinfo: %s\n" |
786 | "%sFrom fragment: %s\n" | |
ad2706db | 787 | "%sExtrinsic: %s\n" |
e520950a | 788 | "%sDirectoryMode: %04o\n" |
49915de2 | 789 | "%sSloppyOptions: %s\n" |
4f8d40a9 | 790 | "%sLazyUnmount: %s\n" |
91899792 | 791 | "%sForceUnmount: %s\n" |
c600357b | 792 | "%sReadWriteOnly: %s\n" |
ac8956ef | 793 | "%sTimeoutSec: %s\n", |
a16e1123 | 794 | prefix, mount_state_to_string(m->state), |
81a5c6d0 | 795 | prefix, mount_result_to_string(m->result), |
17e9d53d | 796 | prefix, mount_result_to_string(m->clean_result), |
e537352b | 797 | prefix, m->where, |
1e4fc9b1 HH |
798 | prefix, p ? strna(p->what) : "n/a", |
799 | prefix, p ? strna(p->fstype) : "n/a", | |
800 | prefix, p ? strna(p->options) : "n/a", | |
e537352b LP |
801 | prefix, yes_no(m->from_proc_self_mountinfo), |
802 | prefix, yes_no(m->from_fragment), | |
39c79477 | 803 | prefix, yes_no(mount_is_extrinsic(u)), |
e520950a | 804 | prefix, m->directory_mode, |
49915de2 | 805 | prefix, yes_no(m->sloppy_options), |
4f8d40a9 | 806 | prefix, yes_no(m->lazy_unmount), |
91899792 | 807 | prefix, yes_no(m->force_unmount), |
c600357b | 808 | prefix, yes_no(m->read_write_only), |
5291f26d | 809 | prefix, FORMAT_TIMESPAN(m->timeout_usec, USEC_PER_SEC)); |
e537352b LP |
810 | |
811 | if (m->control_pid > 0) | |
812 | fprintf(f, | |
ccd06097 ZJS |
813 | "%sControl PID: "PID_FMT"\n", |
814 | prefix, m->control_pid); | |
e537352b LP |
815 | |
816 | exec_context_dump(&m->exec_context, f, prefix); | |
4819ff03 | 817 | kill_context_dump(&m->kill_context, f, prefix); |
bc0623df | 818 | cgroup_context_dump(UNIT(m), f, prefix); |
e537352b LP |
819 | } |
820 | ||
a16e1123 | 821 | static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) { |
3c7416b6 | 822 | |
b9c04eaf | 823 | _cleanup_(exec_params_clear) ExecParameters exec_params = { |
5686391b LP |
824 | .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN, |
825 | .stdin_fd = -1, | |
826 | .stdout_fd = -1, | |
827 | .stderr_fd = -1, | |
828 | .exec_fd = -1, | |
9fa95f85 | 829 | }; |
3c7416b6 LP |
830 | pid_t pid; |
831 | int r; | |
a16e1123 LP |
832 | |
833 | assert(m); | |
834 | assert(c); | |
835 | assert(_pid); | |
836 | ||
3c7416b6 | 837 | r = unit_prepare_exec(UNIT(m)); |
29206d46 LP |
838 | if (r < 0) |
839 | return r; | |
840 | ||
36c16a7c | 841 | r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec)); |
36697dc0 | 842 | if (r < 0) |
36c16a7c | 843 | return r; |
a16e1123 | 844 | |
1ad6e8b3 LP |
845 | r = unit_set_exec_params(UNIT(m), &exec_params); |
846 | if (r < 0) | |
847 | return r; | |
9fa95f85 | 848 | |
f2341e0a LP |
849 | r = exec_spawn(UNIT(m), |
850 | c, | |
4ad49000 | 851 | &m->exec_context, |
9fa95f85 | 852 | &exec_params, |
613b411c | 853 | m->exec_runtime, |
29206d46 | 854 | &m->dynamic_creds, |
4ad49000 LP |
855 | &pid); |
856 | if (r < 0) | |
36c16a7c | 857 | return r; |
a16e1123 | 858 | |
f75f613d | 859 | r = unit_watch_pid(UNIT(m), pid, true); |
4ad49000 | 860 | if (r < 0) |
36c16a7c | 861 | return r; |
a16e1123 LP |
862 | |
863 | *_pid = pid; | |
864 | ||
865 | return 0; | |
a16e1123 LP |
866 | } |
867 | ||
9d2f5178 | 868 | static void mount_enter_dead(Mount *m, MountResult f) { |
e537352b LP |
869 | assert(m); |
870 | ||
a0fef983 | 871 | if (m->result == MOUNT_SUCCESS) |
9d2f5178 | 872 | m->result = f; |
e537352b | 873 | |
aac99f30 | 874 | unit_log_result(UNIT(m), m->result == MOUNT_SUCCESS, mount_result_to_string(m->result)); |
4c425434 LP |
875 | unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_stop); |
876 | ||
29206d46 LP |
877 | mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD); |
878 | ||
e8a565cb | 879 | m->exec_runtime = exec_runtime_unref(m->exec_runtime, true); |
613b411c | 880 | |
bb0c0d6f | 881 | unit_destroy_runtime_data(UNIT(m), &m->exec_context); |
e66cf1a3 | 882 | |
00d9ef85 LP |
883 | unit_unref_uid_gid(UNIT(m), true); |
884 | ||
29206d46 | 885 | dynamic_creds_destroy(&m->dynamic_creds); |
c52c2dc6 ZJS |
886 | |
887 | /* Any dependencies based on /proc/self/mountinfo are now stale */ | |
888 | unit_remove_dependencies(UNIT(m), UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT); | |
e537352b LP |
889 | } |
890 | ||
9d2f5178 | 891 | static void mount_enter_mounted(Mount *m, MountResult f) { |
80876c20 LP |
892 | assert(m); |
893 | ||
a0fef983 | 894 | if (m->result == MOUNT_SUCCESS) |
9d2f5178 | 895 | m->result = f; |
80876c20 LP |
896 | |
897 | mount_set_state(m, MOUNT_MOUNTED); | |
898 | } | |
899 | ||
22af0e58 LP |
900 | static void mount_enter_dead_or_mounted(Mount *m, MountResult f) { |
901 | assert(m); | |
902 | ||
903 | /* Enter DEAD or MOUNTED state, depending on what the kernel currently says about the mount point. We use this | |
904 | * whenever we executed an operation, so that our internal state reflects what the kernel says again, after all | |
905 | * ultimately we just mirror the kernel's internal state on this. */ | |
906 | ||
907 | if (m->from_proc_self_mountinfo) | |
908 | mount_enter_mounted(m, f); | |
909 | else | |
910 | mount_enter_dead(m, f); | |
911 | } | |
912 | ||
913 | static int state_to_kill_operation(MountState state) { | |
914 | switch (state) { | |
915 | ||
916 | case MOUNT_REMOUNTING_SIGTERM: | |
a232ebcc ZJS |
917 | return KILL_RESTART; |
918 | ||
22af0e58 LP |
919 | case MOUNT_UNMOUNTING_SIGTERM: |
920 | return KILL_TERMINATE; | |
921 | ||
922 | case MOUNT_REMOUNTING_SIGKILL: | |
923 | case MOUNT_UNMOUNTING_SIGKILL: | |
924 | return KILL_KILL; | |
925 | ||
926 | default: | |
927 | return _KILL_OPERATION_INVALID; | |
928 | } | |
929 | } | |
930 | ||
9d2f5178 | 931 | static void mount_enter_signal(Mount *m, MountState state, MountResult f) { |
e537352b LP |
932 | int r; |
933 | ||
934 | assert(m); | |
935 | ||
a0fef983 | 936 | if (m->result == MOUNT_SUCCESS) |
9d2f5178 | 937 | m->result = f; |
e537352b | 938 | |
cd2086fe LP |
939 | r = unit_kill_context( |
940 | UNIT(m), | |
941 | &m->kill_context, | |
22af0e58 | 942 | state_to_kill_operation(state), |
cd2086fe LP |
943 | -1, |
944 | m->control_pid, | |
945 | false); | |
946 | if (r < 0) | |
947 | goto fail; | |
e537352b | 948 | |
cd2086fe | 949 | if (r > 0) { |
36c16a7c | 950 | r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->timeout_usec)); |
36697dc0 | 951 | if (r < 0) |
80876c20 | 952 | goto fail; |
e537352b | 953 | |
80876c20 | 954 | mount_set_state(m, state); |
22af0e58 | 955 | } else if (state == MOUNT_REMOUNTING_SIGTERM && m->kill_context.send_sigkill) |
ac84d1fb | 956 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS); |
22af0e58 | 957 | else if (IN_SET(state, MOUNT_REMOUNTING_SIGTERM, MOUNT_REMOUNTING_SIGKILL)) |
9d2f5178 | 958 | mount_enter_mounted(m, MOUNT_SUCCESS); |
22af0e58 | 959 | else if (state == MOUNT_UNMOUNTING_SIGTERM && m->kill_context.send_sigkill) |
ac84d1fb | 960 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS); |
80876c20 | 961 | else |
22af0e58 | 962 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS); |
e537352b LP |
963 | |
964 | return; | |
965 | ||
966 | fail: | |
f2341e0a | 967 | log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m"); |
22af0e58 | 968 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES); |
5261ba90 TT |
969 | } |
970 | ||
9d2f5178 | 971 | static void mount_enter_unmounting(Mount *m) { |
e537352b LP |
972 | int r; |
973 | ||
974 | assert(m); | |
975 | ||
7d54a03a LP |
976 | /* Start counting our attempts */ |
977 | if (!IN_SET(m->state, | |
978 | MOUNT_UNMOUNTING, | |
979 | MOUNT_UNMOUNTING_SIGTERM, | |
980 | MOUNT_UNMOUNTING_SIGKILL)) | |
981 | m->n_retry_umount = 0; | |
982 | ||
a16e1123 LP |
983 | m->control_command_id = MOUNT_EXEC_UNMOUNT; |
984 | m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT; | |
e537352b | 985 | |
83897d54 | 986 | r = exec_command_set(m->control_command, UMOUNT_PATH, m->where, "-c", NULL); |
e520950a BR |
987 | if (r >= 0 && m->lazy_unmount) |
988 | r = exec_command_append(m->control_command, "-l", NULL); | |
4f8d40a9 BR |
989 | if (r >= 0 && m->force_unmount) |
990 | r = exec_command_append(m->control_command, "-f", NULL); | |
7d54a03a | 991 | if (r < 0) |
e537352b LP |
992 | goto fail; |
993 | ||
a16e1123 | 994 | mount_unwatch_control_pid(m); |
5e94833f | 995 | |
7d54a03a LP |
996 | r = mount_spawn(m, m->control_command, &m->control_pid); |
997 | if (r < 0) | |
e537352b LP |
998 | goto fail; |
999 | ||
1000 | mount_set_state(m, MOUNT_UNMOUNTING); | |
1001 | ||
1002 | return; | |
1003 | ||
1004 | fail: | |
f2341e0a | 1005 | log_unit_warning_errno(UNIT(m), r, "Failed to run 'umount' task: %m"); |
22af0e58 | 1006 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES); |
e537352b LP |
1007 | } |
1008 | ||
8d567588 | 1009 | static void mount_enter_mounting(Mount *m) { |
e537352b | 1010 | int r; |
cb39ed3f | 1011 | MountParameters *p; |
e537352b LP |
1012 | |
1013 | assert(m); | |
1014 | ||
25cd4964 | 1015 | r = unit_fail_if_noncanonical(UNIT(m), m->where); |
f2341e0a LP |
1016 | if (r < 0) |
1017 | goto fail; | |
1018 | ||
1019 | (void) mkdir_p_label(m->where, m->directory_mode); | |
3e5235b0 | 1020 | |
f2341e0a | 1021 | unit_warn_if_dir_nonempty(UNIT(m), m->where); |
4c425434 | 1022 | unit_warn_leftover_processes(UNIT(m), unit_log_leftover_process_start); |
a4634b21 LP |
1023 | |
1024 | m->control_command_id = MOUNT_EXEC_MOUNT; | |
1025 | m->control_command = m->exec_command + MOUNT_EXEC_MOUNT; | |
1026 | ||
cb39ed3f | 1027 | /* Create the source directory for bind-mounts if needed */ |
6b1dc2bd | 1028 | p = get_mount_parameters_fragment(m); |
5dc60faa AP |
1029 | if (p && mount_is_bind(p)) { |
1030 | r = mkdir_p_label(p->what, m->directory_mode); | |
237ecfee SA |
1031 | /* mkdir_p_label() can return -EEXIST if the target path exists and is not a directory - which is |
1032 | * totally OK, in case the user wants us to overmount a non-directory inode. */ | |
e4de58c8 | 1033 | if (r < 0 && r != -EEXIST) { |
5dc60faa | 1034 | log_unit_error_errno(UNIT(m), r, "Failed to make bind mount source '%s': %m", p->what); |
e4de58c8 YW |
1035 | goto fail; |
1036 | } | |
5dc60faa | 1037 | } |
5261ba90 | 1038 | |
b294b79f | 1039 | if (p) { |
17a1c597 ZJS |
1040 | _cleanup_free_ char *opts = NULL; |
1041 | ||
ff0c31bc | 1042 | r = fstab_filter_options(p->options, "nofail\0" "noauto\0" "auto\0", NULL, NULL, NULL, &opts); |
17a1c597 ZJS |
1043 | if (r < 0) |
1044 | goto fail; | |
1045 | ||
b294b79f | 1046 | r = exec_command_set(m->control_command, MOUNT_PATH, p->what, m->where, NULL); |
e86b3761 ZJS |
1047 | if (r >= 0 && m->sloppy_options) |
1048 | r = exec_command_append(m->control_command, "-s", NULL); | |
c600357b MH |
1049 | if (r >= 0 && m->read_write_only) |
1050 | r = exec_command_append(m->control_command, "-w", NULL); | |
b294b79f MO |
1051 | if (r >= 0 && p->fstype) |
1052 | r = exec_command_append(m->control_command, "-t", p->fstype, NULL); | |
0c47569a | 1053 | if (r >= 0 && !isempty(opts)) |
17a1c597 | 1054 | r = exec_command_append(m->control_command, "-o", opts, NULL); |
e86b3761 | 1055 | } else |
e537352b | 1056 | r = -ENOENT; |
e537352b LP |
1057 | if (r < 0) |
1058 | goto fail; | |
1059 | ||
a16e1123 | 1060 | mount_unwatch_control_pid(m); |
5e94833f | 1061 | |
257f1d8e LP |
1062 | r = mount_spawn(m, m->control_command, &m->control_pid); |
1063 | if (r < 0) | |
e537352b LP |
1064 | goto fail; |
1065 | ||
1066 | mount_set_state(m, MOUNT_MOUNTING); | |
1067 | ||
1068 | return; | |
1069 | ||
1070 | fail: | |
f2341e0a | 1071 | log_unit_warning_errno(UNIT(m), r, "Failed to run 'mount' task: %m"); |
22af0e58 | 1072 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_RESOURCES); |
e537352b LP |
1073 | } |
1074 | ||
850b7410 LP |
1075 | static void mount_set_reload_result(Mount *m, MountResult result) { |
1076 | assert(m); | |
1077 | ||
1078 | /* Only store the first error we encounter */ | |
1079 | if (m->reload_result != MOUNT_SUCCESS) | |
1080 | return; | |
1081 | ||
1082 | m->reload_result = result; | |
1083 | } | |
1084 | ||
9d2f5178 | 1085 | static void mount_enter_remounting(Mount *m) { |
e537352b | 1086 | int r; |
b294b79f | 1087 | MountParameters *p; |
e537352b LP |
1088 | |
1089 | assert(m); | |
1090 | ||
850b7410 LP |
1091 | /* Reset reload result when we are about to start a new remount operation */ |
1092 | m->reload_result = MOUNT_SUCCESS; | |
1093 | ||
a16e1123 LP |
1094 | m->control_command_id = MOUNT_EXEC_REMOUNT; |
1095 | m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT; | |
e537352b | 1096 | |
b294b79f MO |
1097 | p = get_mount_parameters_fragment(m); |
1098 | if (p) { | |
e537352b LP |
1099 | const char *o; |
1100 | ||
b294b79f MO |
1101 | if (p->options) |
1102 | o = strjoina("remount,", p->options); | |
718db961 | 1103 | else |
e537352b LP |
1104 | o = "remount"; |
1105 | ||
f00929ad | 1106 | r = exec_command_set(m->control_command, MOUNT_PATH, |
b294b79f | 1107 | p->what, m->where, |
e86b3761 | 1108 | "-o", o, NULL); |
e86b3761 ZJS |
1109 | if (r >= 0 && m->sloppy_options) |
1110 | r = exec_command_append(m->control_command, "-s", NULL); | |
c600357b MH |
1111 | if (r >= 0 && m->read_write_only) |
1112 | r = exec_command_append(m->control_command, "-w", NULL); | |
b294b79f MO |
1113 | if (r >= 0 && p->fstype) |
1114 | r = exec_command_append(m->control_command, "-t", p->fstype, NULL); | |
6b1dc2bd | 1115 | } else |
e537352b | 1116 | r = -ENOENT; |
60b912f6 | 1117 | if (r < 0) |
e537352b | 1118 | goto fail; |
e537352b | 1119 | |
a16e1123 | 1120 | mount_unwatch_control_pid(m); |
5e94833f | 1121 | |
718db961 LP |
1122 | r = mount_spawn(m, m->control_command, &m->control_pid); |
1123 | if (r < 0) | |
e537352b LP |
1124 | goto fail; |
1125 | ||
1126 | mount_set_state(m, MOUNT_REMOUNTING); | |
1127 | ||
1128 | return; | |
1129 | ||
1130 | fail: | |
f2341e0a | 1131 | log_unit_warning_errno(UNIT(m), r, "Failed to run 'remount' task: %m"); |
850b7410 | 1132 | mount_set_reload_result(m, MOUNT_FAILURE_RESOURCES); |
22af0e58 | 1133 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS); |
e537352b LP |
1134 | } |
1135 | ||
7eba1463 LP |
1136 | static void mount_cycle_clear(Mount *m) { |
1137 | assert(m); | |
1138 | ||
1139 | /* Clear all state we shall forget for this new cycle */ | |
1140 | ||
1141 | m->result = MOUNT_SUCCESS; | |
1142 | m->reload_result = MOUNT_SUCCESS; | |
1143 | exec_command_reset_status_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); | |
1144 | UNIT(m)->reset_accounting = true; | |
1145 | } | |
1146 | ||
e537352b LP |
1147 | static int mount_start(Unit *u) { |
1148 | Mount *m = MOUNT(u); | |
07299350 | 1149 | int r; |
e537352b LP |
1150 | |
1151 | assert(m); | |
1152 | ||
1153 | /* We cannot fulfill this request right now, try again later | |
1154 | * please! */ | |
f2aed307 LP |
1155 | if (IN_SET(m->state, |
1156 | MOUNT_UNMOUNTING, | |
1157 | MOUNT_UNMOUNTING_SIGTERM, | |
17e9d53d YW |
1158 | MOUNT_UNMOUNTING_SIGKILL, |
1159 | MOUNT_CLEANING)) | |
e537352b LP |
1160 | return -EAGAIN; |
1161 | ||
1162 | /* Already on it! */ | |
db39a627 | 1163 | if (IN_SET(m->state, MOUNT_MOUNTING, MOUNT_MOUNTING_DONE)) |
e537352b LP |
1164 | return 0; |
1165 | ||
f2aed307 | 1166 | assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED)); |
e537352b | 1167 | |
4b58153d LP |
1168 | r = unit_acquire_invocation_id(u); |
1169 | if (r < 0) | |
1170 | return r; | |
1171 | ||
7eba1463 | 1172 | mount_cycle_clear(m); |
8d567588 | 1173 | mount_enter_mounting(m); |
7eba1463 | 1174 | |
82a2b6bb | 1175 | return 1; |
e537352b LP |
1176 | } |
1177 | ||
1178 | static int mount_stop(Unit *u) { | |
1179 | Mount *m = MOUNT(u); | |
1180 | ||
1181 | assert(m); | |
1182 | ||
22af0e58 LP |
1183 | switch (m->state) { |
1184 | ||
1185 | case MOUNT_UNMOUNTING: | |
1186 | case MOUNT_UNMOUNTING_SIGKILL: | |
1187 | case MOUNT_UNMOUNTING_SIGTERM: | |
1188 | /* Already on it */ | |
e537352b LP |
1189 | return 0; |
1190 | ||
22af0e58 LP |
1191 | case MOUNT_MOUNTING: |
1192 | case MOUNT_MOUNTING_DONE: | |
1193 | case MOUNT_REMOUNTING: | |
1194 | /* If we are still waiting for /bin/mount, we go directly into kill mode. */ | |
1195 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_SUCCESS); | |
1196 | return 0; | |
e537352b | 1197 | |
22af0e58 LP |
1198 | case MOUNT_REMOUNTING_SIGTERM: |
1199 | /* If we are already waiting for a hung remount, convert this to the matching unmounting state */ | |
1200 | mount_set_state(m, MOUNT_UNMOUNTING_SIGTERM); | |
1201 | return 0; | |
1202 | ||
1203 | case MOUNT_REMOUNTING_SIGKILL: | |
1204 | /* as above */ | |
1205 | mount_set_state(m, MOUNT_UNMOUNTING_SIGKILL); | |
1206 | return 0; | |
1207 | ||
1208 | case MOUNT_MOUNTED: | |
1209 | mount_enter_unmounting(m); | |
1210 | return 1; | |
1211 | ||
17e9d53d YW |
1212 | case MOUNT_CLEANING: |
1213 | /* If we are currently cleaning, then abort it, brutally. */ | |
1214 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_SUCCESS); | |
1215 | return 0; | |
1216 | ||
22af0e58 | 1217 | default: |
04499a70 | 1218 | assert_not_reached(); |
22af0e58 | 1219 | } |
e537352b LP |
1220 | } |
1221 | ||
1222 | static int mount_reload(Unit *u) { | |
1223 | Mount *m = MOUNT(u); | |
1224 | ||
1225 | assert(m); | |
e537352b LP |
1226 | assert(m->state == MOUNT_MOUNTED); |
1227 | ||
9d2f5178 | 1228 | mount_enter_remounting(m); |
22af0e58 | 1229 | |
2d018ae2 | 1230 | return 1; |
e537352b LP |
1231 | } |
1232 | ||
a16e1123 LP |
1233 | static int mount_serialize(Unit *u, FILE *f, FDSet *fds) { |
1234 | Mount *m = MOUNT(u); | |
1235 | ||
1236 | assert(m); | |
1237 | assert(f); | |
1238 | assert(fds); | |
1239 | ||
d68c645b LP |
1240 | (void) serialize_item(f, "state", mount_state_to_string(m->state)); |
1241 | (void) serialize_item(f, "result", mount_result_to_string(m->result)); | |
1242 | (void) serialize_item(f, "reload-result", mount_result_to_string(m->reload_result)); | |
2c09fb81 | 1243 | (void) serialize_item_format(f, "n-retry-umount", "%u", m->n_retry_umount); |
a16e1123 LP |
1244 | |
1245 | if (m->control_pid > 0) | |
d68c645b | 1246 | (void) serialize_item_format(f, "control-pid", PID_FMT, m->control_pid); |
a16e1123 LP |
1247 | |
1248 | if (m->control_command_id >= 0) | |
d68c645b | 1249 | (void) serialize_item(f, "control-command", mount_exec_command_to_string(m->control_command_id)); |
a16e1123 LP |
1250 | |
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1255 | Mount *m = MOUNT(u); | |
2c09fb81 | 1256 | int r; |
a16e1123 | 1257 | |
c73f413d | 1258 | assert(m); |
a16e1123 LP |
1259 | assert(u); |
1260 | assert(key); | |
1261 | assert(value); | |
1262 | assert(fds); | |
1263 | ||
1264 | if (streq(key, "state")) { | |
1265 | MountState state; | |
1266 | ||
7fb1d980 YW |
1267 | state = mount_state_from_string(value); |
1268 | if (state < 0) | |
1269 | log_unit_debug_errno(u, state, "Failed to parse state value: %s", value); | |
a16e1123 LP |
1270 | else |
1271 | m->deserialized_state = state; | |
2c09fb81 | 1272 | |
9d2f5178 LP |
1273 | } else if (streq(key, "result")) { |
1274 | MountResult f; | |
a16e1123 | 1275 | |
9d2f5178 LP |
1276 | f = mount_result_from_string(value); |
1277 | if (f < 0) | |
7fb1d980 | 1278 | log_unit_debug_errno(u, f, "Failed to parse result value: %s", value); |
9d2f5178 LP |
1279 | else if (f != MOUNT_SUCCESS) |
1280 | m->result = f; | |
1281 | ||
1282 | } else if (streq(key, "reload-result")) { | |
1283 | MountResult f; | |
1284 | ||
1285 | f = mount_result_from_string(value); | |
1286 | if (f < 0) | |
7fb1d980 | 1287 | log_unit_debug_errno(u, f, "Failed to parse reload result value: %s", value); |
9d2f5178 LP |
1288 | else if (f != MOUNT_SUCCESS) |
1289 | m->reload_result = f; | |
a16e1123 | 1290 | |
2c09fb81 LP |
1291 | } else if (streq(key, "n-retry-umount")) { |
1292 | ||
1293 | r = safe_atou(value, &m->n_retry_umount); | |
1294 | if (r < 0) | |
7fb1d980 | 1295 | log_unit_debug_errno(u, r, "Failed to parse n-retry-umount value: %s", value); |
2c09fb81 | 1296 | |
a16e1123 | 1297 | } else if (streq(key, "control-pid")) { |
a16e1123 | 1298 | |
7fb1d980 YW |
1299 | r = parse_pid(value, &m->control_pid); |
1300 | if (r < 0) | |
1301 | log_unit_debug_errno(u, r, "Failed to parse control-pid value: %s", value); | |
3f459cd9 | 1302 | |
a16e1123 LP |
1303 | } else if (streq(key, "control-command")) { |
1304 | MountExecCommand id; | |
1305 | ||
f2341e0a LP |
1306 | id = mount_exec_command_from_string(value); |
1307 | if (id < 0) | |
7fb1d980 | 1308 | log_unit_debug_errno(u, id, "Failed to parse exec-command value: %s", value); |
a16e1123 LP |
1309 | else { |
1310 | m->control_command_id = id; | |
1311 | m->control_command = m->exec_command + id; | |
1312 | } | |
a16e1123 | 1313 | } else |
f2341e0a | 1314 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
1315 | |
1316 | return 0; | |
1317 | } | |
1318 | ||
44a6b1b6 | 1319 | _pure_ static UnitActiveState mount_active_state(Unit *u) { |
e537352b LP |
1320 | assert(u); |
1321 | ||
1322 | return state_translation_table[MOUNT(u)->state]; | |
1323 | } | |
1324 | ||
44a6b1b6 | 1325 | _pure_ static const char *mount_sub_state_to_string(Unit *u) { |
10a94420 LP |
1326 | assert(u); |
1327 | ||
a16e1123 | 1328 | return mount_state_to_string(MOUNT(u)->state); |
10a94420 LP |
1329 | } |
1330 | ||
f2f725e5 | 1331 | _pure_ static bool mount_may_gc(Unit *u) { |
701cc384 LP |
1332 | Mount *m = MOUNT(u); |
1333 | ||
1334 | assert(m); | |
1335 | ||
f2f725e5 ZJS |
1336 | if (m->from_proc_self_mountinfo) |
1337 | return false; | |
1338 | ||
1339 | return true; | |
701cc384 LP |
1340 | } |
1341 | ||
e537352b LP |
1342 | static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1343 | Mount *m = MOUNT(u); | |
9d2f5178 | 1344 | MountResult f; |
e537352b LP |
1345 | |
1346 | assert(m); | |
1347 | assert(pid >= 0); | |
1348 | ||
8c47c732 LP |
1349 | if (pid != m->control_pid) |
1350 | return; | |
e537352b | 1351 | |
35080486 LP |
1352 | /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether |
1353 | * they established/remove a mount. This is important when mounting, but even more so when unmounting | |
1354 | * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat | |
1355 | * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from | |
1356 | * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we | |
1357 | * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we | |
1358 | * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got | |
1359 | * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just | |
1360 | * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might | |
1361 | * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for | |
1362 | * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that | |
1363 | * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount | |
1364 | * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see | |
1365 | * /proc/self/mountinfo changes before our mount/umount exits. */ | |
1366 | (void) mount_process_proc_self_mountinfo(u->manager); | |
1367 | ||
e537352b LP |
1368 | m->control_pid = 0; |
1369 | ||
1f0958f6 | 1370 | if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) |
9d2f5178 LP |
1371 | f = MOUNT_SUCCESS; |
1372 | else if (code == CLD_EXITED) | |
1373 | f = MOUNT_FAILURE_EXIT_CODE; | |
1374 | else if (code == CLD_KILLED) | |
1375 | f = MOUNT_FAILURE_SIGNAL; | |
1376 | else if (code == CLD_DUMPED) | |
1377 | f = MOUNT_FAILURE_CORE_DUMP; | |
1378 | else | |
04499a70 | 1379 | assert_not_reached(); |
9d2f5178 | 1380 | |
850b7410 LP |
1381 | if (IN_SET(m->state, MOUNT_REMOUNTING, MOUNT_REMOUNTING_SIGKILL, MOUNT_REMOUNTING_SIGTERM)) |
1382 | mount_set_reload_result(m, f); | |
1383 | else if (m->result == MOUNT_SUCCESS) | |
9d2f5178 | 1384 | m->result = f; |
8c47c732 | 1385 | |
a16e1123 | 1386 | if (m->control_command) { |
6ea832a2 | 1387 | exec_status_exit(&m->control_command->exec_status, &m->exec_context, pid, code, status); |
9d2f5178 | 1388 | |
a16e1123 LP |
1389 | m->control_command = NULL; |
1390 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
1391 | } | |
1392 | ||
91bbd9b7 | 1393 | unit_log_process_exit( |
5cc2cd1c | 1394 | u, |
91bbd9b7 LP |
1395 | "Mount process", |
1396 | mount_exec_command_to_string(m->control_command_id), | |
5cc2cd1c | 1397 | f == MOUNT_SUCCESS, |
91bbd9b7 | 1398 | code, status); |
e537352b | 1399 | |
006aabbd AJ |
1400 | /* Note that due to the io event priority logic, we can be sure the new mountinfo is loaded |
1401 | * before we process the SIGCHLD for the mount command. */ | |
e537352b LP |
1402 | |
1403 | switch (m->state) { | |
1404 | ||
1405 | case MOUNT_MOUNTING: | |
006aabbd | 1406 | /* Our mount point has not appeared in mountinfo. Something went wrong. */ |
e537352b | 1407 | |
006aabbd AJ |
1408 | if (f == MOUNT_SUCCESS) { |
1409 | /* Either /bin/mount has an unexpected definition of success, | |
1410 | * or someone raced us and we lost. */ | |
1411 | log_unit_warning(UNIT(m), "Mount process finished, but there is no mount."); | |
1412 | f = MOUNT_FAILURE_PROTOCOL; | |
1413 | } | |
1414 | mount_enter_dead(m, f); | |
1415 | break; | |
1416 | ||
1417 | case MOUNT_MOUNTING_DONE: | |
1418 | mount_enter_mounted(m, f); | |
e537352b LP |
1419 | break; |
1420 | ||
e2f3b44c | 1421 | case MOUNT_REMOUNTING: |
e2f3b44c | 1422 | case MOUNT_REMOUNTING_SIGTERM: |
22af0e58 LP |
1423 | case MOUNT_REMOUNTING_SIGKILL: |
1424 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS); | |
e2f3b44c LP |
1425 | break; |
1426 | ||
e537352b | 1427 | case MOUNT_UNMOUNTING: |
e537352b | 1428 | |
3cc96856 | 1429 | if (f == MOUNT_SUCCESS && m->from_proc_self_mountinfo) { |
22af0e58 LP |
1430 | |
1431 | /* Still a mount point? If so, let's try again. Most likely there were multiple mount points | |
57018361 AJ |
1432 | * stacked on top of each other. We might exceed the timeout specified by the user overall, |
1433 | * but we will stop as soon as any one umount times out. */ | |
22af0e58 LP |
1434 | |
1435 | if (m->n_retry_umount < RETRY_UMOUNT_MAX) { | |
1436 | log_unit_debug(u, "Mount still present, trying again."); | |
1437 | m->n_retry_umount++; | |
1438 | mount_enter_unmounting(m); | |
1439 | } else { | |
006aabbd | 1440 | log_unit_warning(u, "Mount still present after %u attempts to unmount, giving up.", m->n_retry_umount); |
22af0e58 LP |
1441 | mount_enter_mounted(m, f); |
1442 | } | |
1443 | } else | |
3cc96856 | 1444 | mount_enter_dead_or_mounted(m, f); |
22af0e58 | 1445 | |
e537352b | 1446 | break; |
57018361 AJ |
1447 | |
1448 | case MOUNT_UNMOUNTING_SIGKILL: | |
1449 | case MOUNT_UNMOUNTING_SIGTERM: | |
1450 | mount_enter_dead_or_mounted(m, f); | |
1451 | break; | |
e537352b | 1452 | |
17e9d53d YW |
1453 | case MOUNT_CLEANING: |
1454 | if (m->clean_result == MOUNT_SUCCESS) | |
1455 | m->clean_result = f; | |
1456 | ||
1457 | mount_enter_dead(m, MOUNT_SUCCESS); | |
1458 | break; | |
1459 | ||
e537352b | 1460 | default: |
04499a70 | 1461 | assert_not_reached(); |
e537352b | 1462 | } |
c4e2ceae LP |
1463 | |
1464 | /* Notify clients about changed exit status */ | |
1465 | unit_add_to_dbus_queue(u); | |
e537352b LP |
1466 | } |
1467 | ||
718db961 LP |
1468 | static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
1469 | Mount *m = MOUNT(userdata); | |
e537352b LP |
1470 | |
1471 | assert(m); | |
718db961 | 1472 | assert(m->timer_event_source == source); |
e537352b LP |
1473 | |
1474 | switch (m->state) { | |
1475 | ||
1476 | case MOUNT_MOUNTING: | |
1477 | case MOUNT_MOUNTING_DONE: | |
22af0e58 LP |
1478 | log_unit_warning(UNIT(m), "Mounting timed out. Terminating."); |
1479 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT); | |
e537352b LP |
1480 | break; |
1481 | ||
1482 | case MOUNT_REMOUNTING: | |
79aafbd1 | 1483 | log_unit_warning(UNIT(m), "Remounting timed out. Terminating remount process."); |
22af0e58 LP |
1484 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); |
1485 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, MOUNT_SUCCESS); | |
e537352b LP |
1486 | break; |
1487 | ||
22af0e58 LP |
1488 | case MOUNT_REMOUNTING_SIGTERM: |
1489 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); | |
e537352b | 1490 | |
4819ff03 | 1491 | if (m->kill_context.send_sigkill) { |
22af0e58 LP |
1492 | log_unit_warning(UNIT(m), "Remounting timed out. Killing."); |
1493 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, MOUNT_SUCCESS); | |
ba035df2 | 1494 | } else { |
22af0e58 LP |
1495 | log_unit_warning(UNIT(m), "Remounting timed out. Skipping SIGKILL. Ignoring."); |
1496 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS); | |
ba035df2 | 1497 | } |
e537352b LP |
1498 | break; |
1499 | ||
22af0e58 LP |
1500 | case MOUNT_REMOUNTING_SIGKILL: |
1501 | mount_set_reload_result(m, MOUNT_FAILURE_TIMEOUT); | |
ba035df2 | 1502 | |
22af0e58 LP |
1503 | log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring."); |
1504 | mount_enter_dead_or_mounted(m, MOUNT_SUCCESS); | |
1505 | break; | |
1506 | ||
1507 | case MOUNT_UNMOUNTING: | |
79aafbd1 | 1508 | log_unit_warning(UNIT(m), "Unmounting timed out. Terminating."); |
22af0e58 | 1509 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, MOUNT_FAILURE_TIMEOUT); |
e537352b LP |
1510 | break; |
1511 | ||
1512 | case MOUNT_UNMOUNTING_SIGTERM: | |
4819ff03 | 1513 | if (m->kill_context.send_sigkill) { |
22af0e58 | 1514 | log_unit_warning(UNIT(m), "Mount process timed out. Killing."); |
9d2f5178 | 1515 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, MOUNT_FAILURE_TIMEOUT); |
ba035df2 | 1516 | } else { |
22af0e58 LP |
1517 | log_unit_warning(UNIT(m), "Mount process timed out. Skipping SIGKILL. Ignoring."); |
1518 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT); | |
ba035df2 | 1519 | } |
e537352b LP |
1520 | break; |
1521 | ||
e537352b | 1522 | case MOUNT_UNMOUNTING_SIGKILL: |
22af0e58 LP |
1523 | log_unit_warning(UNIT(m), "Mount process still around after SIGKILL. Ignoring."); |
1524 | mount_enter_dead_or_mounted(m, MOUNT_FAILURE_TIMEOUT); | |
e537352b LP |
1525 | break; |
1526 | ||
17e9d53d YW |
1527 | case MOUNT_CLEANING: |
1528 | log_unit_warning(UNIT(m), "Cleaning timed out. killing."); | |
1529 | ||
1530 | if (m->clean_result == MOUNT_SUCCESS) | |
1531 | m->clean_result = MOUNT_FAILURE_TIMEOUT; | |
1532 | ||
1533 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, 0); | |
1534 | break; | |
1535 | ||
e537352b | 1536 | default: |
04499a70 | 1537 | assert_not_reached(); |
e537352b | 1538 | } |
718db961 LP |
1539 | |
1540 | return 0; | |
e537352b LP |
1541 | } |
1542 | ||
03b8cfed | 1543 | static int mount_setup_new_unit( |
839ee058 LP |
1544 | Manager *m, |
1545 | const char *name, | |
03b8cfed FB |
1546 | const char *what, |
1547 | const char *where, | |
1548 | const char *options, | |
1549 | const char *fstype, | |
ec88d1ea | 1550 | MountProcFlags *ret_flags, |
839ee058 | 1551 | Unit **ret) { |
03b8cfed | 1552 | |
839ee058 | 1553 | _cleanup_(unit_freep) Unit *u = NULL; |
bbee24bc | 1554 | int r; |
03b8cfed | 1555 | |
839ee058 LP |
1556 | assert(m); |
1557 | assert(name); | |
ec88d1ea | 1558 | assert(ret_flags); |
839ee058 LP |
1559 | assert(ret); |
1560 | ||
1561 | r = unit_new_for_name(m, sizeof(Mount), name, &u); | |
1562 | if (r < 0) | |
1563 | return r; | |
03b8cfed | 1564 | |
e10fe042 LP |
1565 | r = free_and_strdup(&u->source_path, "/proc/self/mountinfo"); |
1566 | if (r < 0) | |
1567 | return r; | |
03b8cfed | 1568 | |
e10fe042 LP |
1569 | r = free_and_strdup(&MOUNT(u)->where, where); |
1570 | if (r < 0) | |
1571 | return r; | |
03b8cfed | 1572 | |
9ddaa3e4 | 1573 | r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype); |
bbee24bc LP |
1574 | if (r < 0) |
1575 | return r; | |
03b8cfed | 1576 | |
aebff2e7 YW |
1577 | r = mount_add_non_exec_dependencies(MOUNT(u)); |
1578 | if (r < 0) | |
1579 | return r; | |
1580 | ||
6d7e89b0 LP |
1581 | /* This unit was generated because /proc/self/mountinfo reported it. Remember this, so that by the time we load |
1582 | * the unit file for it (and thus add in extra deps right after) we know what source to attributes the deps | |
7802194a | 1583 | * to. */ |
6d7e89b0 LP |
1584 | MOUNT(u)->from_proc_self_mountinfo = true; |
1585 | ||
1586 | /* We have only allocated the stub now, let's enqueue this unit for loading now, so that everything else is | |
1587 | * loaded in now. */ | |
cfcd4318 | 1588 | unit_add_to_load_queue(u); |
26e35b16 | 1589 | |
ec88d1ea | 1590 | *ret_flags = MOUNT_PROC_IS_MOUNTED | MOUNT_PROC_JUST_MOUNTED | MOUNT_PROC_JUST_CHANGED; |
839ee058 | 1591 | *ret = TAKE_PTR(u); |
03b8cfed FB |
1592 | return 0; |
1593 | } | |
1594 | ||
1595 | static int mount_setup_existing_unit( | |
1596 | Unit *u, | |
1597 | const char *what, | |
1598 | const char *where, | |
1599 | const char *options, | |
1600 | const char *fstype, | |
ec88d1ea | 1601 | MountProcFlags *ret_flags) { |
03b8cfed | 1602 | |
bbee24bc | 1603 | int r; |
03b8cfed FB |
1604 | |
1605 | assert(u); | |
4bb68f2f | 1606 | assert(ret_flags); |
03b8cfed FB |
1607 | |
1608 | if (!MOUNT(u)->where) { | |
1609 | MOUNT(u)->where = strdup(where); | |
1610 | if (!MOUNT(u)->where) | |
1611 | return -ENOMEM; | |
1612 | } | |
1613 | ||
4bb68f2f LP |
1614 | /* In case we have multiple mounts established on the same mount point, let's merge flags set already |
1615 | * for the current unit. Note that the flags field is reset on each iteration of reading | |
1616 | * /proc/self/mountinfo, hence we know for sure anything already set here is from the current | |
1617 | * iteration and thus worthy of taking into account. */ | |
1618 | MountProcFlags flags = | |
1619 | MOUNT(u)->proc_flags | MOUNT_PROC_IS_MOUNTED; | |
1620 | ||
9ddaa3e4 | 1621 | r = update_parameters_proc_self_mountinfo(MOUNT(u), what, options, fstype); |
bbee24bc LP |
1622 | if (r < 0) |
1623 | return r; | |
ec88d1ea LP |
1624 | if (r > 0) |
1625 | flags |= MOUNT_PROC_JUST_CHANGED; | |
03b8cfed | 1626 | |
4bb68f2f LP |
1627 | /* There are two conditions when we consider a mount point just mounted: when we haven't seen it in |
1628 | * /proc/self/mountinfo before or when MOUNT_MOUNTING is our current state. Why bother with the | |
1629 | * latter? Shouldn't that be covered by the former? No, during reload it is not because we might then | |
1630 | * encounter a new /proc/self/mountinfo in combination with an old mount unit state (since it stems | |
1631 | * from the serialized state), and need to catch up. Since we know that the MOUNT_MOUNTING state is | |
1632 | * reached when we wait for the mount to appear we hence can assume that if we are in it, we are | |
1633 | * actually seeing it established for the first time. */ | |
1634 | if (!MOUNT(u)->from_proc_self_mountinfo || MOUNT(u)->state == MOUNT_MOUNTING) | |
ec88d1ea | 1635 | flags |= MOUNT_PROC_JUST_MOUNTED; |
d253a45e YW |
1636 | |
1637 | MOUNT(u)->from_proc_self_mountinfo = true; | |
03b8cfed | 1638 | |
f8064c4f LP |
1639 | if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) { |
1640 | /* The unit was previously not found or otherwise not loaded. Now that the unit shows up in | |
1641 | * /proc/self/mountinfo we should reconsider it this, hence set it to UNIT_LOADED. */ | |
03b8cfed FB |
1642 | u->load_state = UNIT_LOADED; |
1643 | u->load_error = 0; | |
1644 | ||
ec88d1ea | 1645 | flags |= MOUNT_PROC_JUST_CHANGED; |
03b8cfed FB |
1646 | } |
1647 | ||
ec88d1ea | 1648 | if (FLAGS_SET(flags, MOUNT_PROC_JUST_CHANGED)) { |
a3742204 LP |
1649 | /* If things changed, then make sure that all deps are regenerated. Let's |
1650 | * first remove all automatic deps, and then add in the new ones. */ | |
1651 | ||
1652 | unit_remove_dependencies(u, UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT); | |
1653 | ||
bf7eedbf | 1654 | r = mount_add_non_exec_dependencies(MOUNT(u)); |
a3742204 LP |
1655 | if (r < 0) |
1656 | return r; | |
1657 | } | |
03b8cfed | 1658 | |
ec88d1ea | 1659 | *ret_flags = flags; |
03b8cfed FB |
1660 | return 0; |
1661 | } | |
1662 | ||
628c89cc | 1663 | static int mount_setup_unit( |
e537352b LP |
1664 | Manager *m, |
1665 | const char *what, | |
1666 | const char *where, | |
1667 | const char *options, | |
1668 | const char *fstype, | |
e537352b | 1669 | bool set_flags) { |
057d9ab8 | 1670 | |
03b8cfed | 1671 | _cleanup_free_ char *e = NULL; |
ec88d1ea | 1672 | MountProcFlags flags; |
057d9ab8 LP |
1673 | Unit *u; |
1674 | int r; | |
b08d03ff | 1675 | |
f50e0a01 | 1676 | assert(m); |
b08d03ff LP |
1677 | assert(what); |
1678 | assert(where); | |
e537352b LP |
1679 | assert(options); |
1680 | assert(fstype); | |
1681 | ||
e537352b LP |
1682 | /* Ignore API mount points. They should never be referenced in |
1683 | * dependencies ever. */ | |
33ff02c9 | 1684 | if (mount_point_is_api(where) || mount_point_ignore(where)) |
57f2a956 | 1685 | return 0; |
b08d03ff | 1686 | |
8d567588 LP |
1687 | if (streq(fstype, "autofs")) |
1688 | return 0; | |
1689 | ||
4e85aff4 LP |
1690 | /* probably some kind of swap, ignore */ |
1691 | if (!is_path(where)) | |
b08d03ff LP |
1692 | return 0; |
1693 | ||
2c905207 LP |
1694 | /* Mount unit names have to be (like all other unit names) short enough to fit into file names. This |
1695 | * means there's a good chance that overly long mount point paths after mangling them to look like a | |
1696 | * unit name would result in unit names we don't actually consider valid. This should be OK however | |
1697 | * as such long mount point paths should not happen on regular systems — and if they appear | |
1698 | * nonetheless they are generally synthesized by software, and thus managed by that other | |
1699 | * software. Having such long names just means you cannot use systemd to manage those specific mount | |
1700 | * points, which should be an OK restriction to make. After all we don't have to be able to manage | |
1701 | * all mount points in the world — as long as we don't choke on them when we encounter them. */ | |
7410616c | 1702 | r = unit_name_from_path(where, ".mount", &e); |
2c905207 LP |
1703 | if (r < 0) { |
1704 | static RateLimit rate_limit = { /* Let's log about this at warning level at most once every | |
1705 | * 5s. Given that we generate this whenever we read the file | |
1706 | * otherwise we probably shouldn't flood the logs with | |
1707 | * this */ | |
1708 | .interval = 5 * USEC_PER_SEC, | |
1709 | .burst = 1, | |
1710 | }; | |
1711 | ||
3ebc9b9b LP |
1712 | if (r == -ENAMETOOLONG) |
1713 | return log_struct_errno( | |
1714 | ratelimit_below(&rate_limit) ? LOG_WARNING : LOG_DEBUG, r, | |
1715 | "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR, | |
1716 | "MOUNT_POINT=%s", where, | |
1717 | LOG_MESSAGE("Mount point path '%s' too long to fit into unit name, ignoring mount point.", where)); | |
1718 | ||
2c905207 LP |
1719 | return log_struct_errno( |
1720 | ratelimit_below(&rate_limit) ? LOG_WARNING : LOG_DEBUG, r, | |
1721 | "MESSAGE_ID=" SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR, | |
1722 | "MOUNT_POINT=%s", where, | |
3ebc9b9b | 1723 | LOG_MESSAGE("Failed to generate valid unit name from mount point path '%s', ignoring mount point: %m", where)); |
2c905207 | 1724 | } |
b08d03ff | 1725 | |
7d17cfbc | 1726 | u = manager_get_unit(m, e); |
839ee058 | 1727 | if (u) |
03b8cfed | 1728 | r = mount_setup_existing_unit(u, what, where, options, fstype, &flags); |
839ee058 LP |
1729 | else |
1730 | /* First time we see this mount point meaning that it's not been initiated by a mount unit but rather | |
1731 | * by the sysadmin having called mount(8) directly. */ | |
1732 | r = mount_setup_new_unit(m, e, what, where, options, fstype, &flags, &u); | |
03b8cfed | 1733 | if (r < 0) |
2c905207 | 1734 | return log_warning_errno(r, "Failed to set up mount unit for '%s': %m", where); |
ff5f34d0 | 1735 | |
ec88d1ea LP |
1736 | /* If the mount changed properties or state, let's notify our clients */ |
1737 | if (flags & (MOUNT_PROC_JUST_CHANGED|MOUNT_PROC_JUST_MOUNTED)) | |
ff5f34d0 | 1738 | unit_add_to_dbus_queue(u); |
c1e1601e | 1739 | |
ec88d1ea LP |
1740 | if (set_flags) |
1741 | MOUNT(u)->proc_flags = flags; | |
1742 | ||
b08d03ff | 1743 | return 0; |
b08d03ff LP |
1744 | } |
1745 | ||
ef734fd6 | 1746 | static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) { |
13dcfe46 ZJS |
1747 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
1748 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
ba0d56f5 | 1749 | int r; |
b08d03ff LP |
1750 | |
1751 | assert(m); | |
1752 | ||
e2857b3d | 1753 | r = libmount_parse(NULL, NULL, &table, &iter); |
5cca8def | 1754 | if (r < 0) |
628c89cc | 1755 | return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m"); |
e537352b | 1756 | |
5cca8def | 1757 | for (;;) { |
95b862b0 | 1758 | struct libmnt_fs *fs; |
8d3ae2bd | 1759 | const char *device, *path, *options, *fstype; |
b08d03ff | 1760 | |
13dcfe46 ZJS |
1761 | r = mnt_table_next_fs(table, iter, &fs); |
1762 | if (r == 1) | |
5cca8def | 1763 | break; |
13dcfe46 ZJS |
1764 | if (r < 0) |
1765 | return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
5cca8def | 1766 | |
8d3ae2bd CL |
1767 | device = mnt_fs_get_source(fs); |
1768 | path = mnt_fs_get_target(fs); | |
1769 | options = mnt_fs_get_options(fs); | |
1770 | fstype = mnt_fs_get_fstype(fs); | |
a2e0f3d3 | 1771 | |
c0a7f8d3 DM |
1772 | if (!device || !path) |
1773 | continue; | |
1774 | ||
9d1b2b22 | 1775 | device_found_node(m, device, DEVICE_FOUND_MOUNT, DEVICE_FOUND_MOUNT); |
628c89cc | 1776 | |
9d1b2b22 | 1777 | (void) mount_setup_unit(m, device, path, options, fstype, set_flags); |
b08d03ff LP |
1778 | } |
1779 | ||
ba0d56f5 | 1780 | return 0; |
e537352b LP |
1781 | } |
1782 | ||
1783 | static void mount_shutdown(Manager *m) { | |
1784 | assert(m); | |
1785 | ||
5dcadb4c | 1786 | m->mount_event_source = sd_event_source_disable_unref(m->mount_event_source); |
718db961 | 1787 | |
d379d442 KZ |
1788 | mnt_unref_monitor(m->mount_monitor); |
1789 | m->mount_monitor = NULL; | |
b08d03ff LP |
1790 | } |
1791 | ||
7a7821c8 | 1792 | static int mount_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 1793 | Mount *m = MOUNT(u); |
7a7821c8 | 1794 | usec_t t; |
68db7a3b ZJS |
1795 | int r; |
1796 | ||
c73f413d FS |
1797 | assert(m); |
1798 | assert(u); | |
1799 | ||
68db7a3b ZJS |
1800 | if (!m->timer_event_source) |
1801 | return 0; | |
1802 | ||
7a7821c8 | 1803 | r = sd_event_source_get_time(m->timer_event_source, &t); |
68db7a3b ZJS |
1804 | if (r < 0) |
1805 | return r; | |
7a7821c8 LP |
1806 | if (t == USEC_INFINITY) |
1807 | return 0; | |
68db7a3b | 1808 | |
7a7821c8 | 1809 | *timeout = t; |
68db7a3b ZJS |
1810 | return 1; |
1811 | } | |
1812 | ||
04eb582a | 1813 | static void mount_enumerate_perpetual(Manager *m) { |
11222d0f LP |
1814 | Unit *u; |
1815 | int r; | |
1816 | ||
1817 | assert(m); | |
1818 | ||
1819 | /* Whatever happens, we know for sure that the root directory is around, and cannot go away. Let's | |
1820 | * unconditionally synthesize it here and mark it as perpetual. */ | |
1821 | ||
1822 | u = manager_get_unit(m, SPECIAL_ROOT_MOUNT); | |
1823 | if (!u) { | |
a581e45a | 1824 | r = unit_new_for_name(m, sizeof(Mount), SPECIAL_ROOT_MOUNT, &u); |
04eb582a LP |
1825 | if (r < 0) { |
1826 | log_error_errno(r, "Failed to allocate the special " SPECIAL_ROOT_MOUNT " unit: %m"); | |
1827 | return; | |
1828 | } | |
11222d0f LP |
1829 | } |
1830 | ||
1831 | u->perpetual = true; | |
1832 | MOUNT(u)->deserialized_state = MOUNT_MOUNTED; | |
1833 | ||
1834 | unit_add_to_load_queue(u); | |
1835 | unit_add_to_dbus_queue(u); | |
1836 | } | |
1837 | ||
1838 | static bool mount_is_mounted(Mount *m) { | |
1839 | assert(m); | |
1840 | ||
ec88d1ea | 1841 | return UNIT(m)->perpetual || FLAGS_SET(m->proc_flags, MOUNT_PROC_IS_MOUNTED); |
11222d0f LP |
1842 | } |
1843 | ||
edc027b4 MS |
1844 | static int mount_on_ratelimit_expire(sd_event_source *s, void *userdata) { |
1845 | Manager *m = userdata; | |
c29e6a95 | 1846 | Job *j; |
edc027b4 MS |
1847 | |
1848 | assert(m); | |
1849 | ||
c29e6a95 MS |
1850 | /* Let's enqueue all start jobs that were previously skipped because of active ratelimit. */ |
1851 | HASHMAP_FOREACH(j, m->jobs) { | |
1852 | if (j->unit->type != UNIT_MOUNT) | |
1853 | continue; | |
1854 | ||
1855 | job_add_to_run_queue(j); | |
1856 | } | |
1857 | ||
b0c4b282 LP |
1858 | /* By entering ratelimited state we made all mount start jobs not runnable, now rate limit is over so |
1859 | * let's make sure we dispatch them in the next iteration. */ | |
1860 | manager_trigger_run_queue(m); | |
edc027b4 MS |
1861 | |
1862 | return 0; | |
1863 | } | |
1864 | ||
ba64af90 | 1865 | static void mount_enumerate(Manager *m) { |
b08d03ff | 1866 | int r; |
d379d442 | 1867 | |
b08d03ff LP |
1868 | assert(m); |
1869 | ||
8d3ae2bd CL |
1870 | mnt_init_debug(0); |
1871 | ||
d379d442 KZ |
1872 | if (!m->mount_monitor) { |
1873 | int fd; | |
ef734fd6 | 1874 | |
d379d442 KZ |
1875 | m->mount_monitor = mnt_new_monitor(); |
1876 | if (!m->mount_monitor) { | |
ba64af90 | 1877 | log_oom(); |
718db961 | 1878 | goto fail; |
d379d442 | 1879 | } |
29083707 | 1880 | |
d379d442 | 1881 | r = mnt_monitor_enable_kernel(m->mount_monitor, 1); |
ba64af90 LP |
1882 | if (r < 0) { |
1883 | log_error_errno(r, "Failed to enable watching of kernel mount events: %m"); | |
29083707 | 1884 | goto fail; |
ba64af90 LP |
1885 | } |
1886 | ||
d379d442 | 1887 | r = mnt_monitor_enable_userspace(m->mount_monitor, 1, NULL); |
ba64af90 LP |
1888 | if (r < 0) { |
1889 | log_error_errno(r, "Failed to enable watching of userspace mount events: %m"); | |
f7c1ad4f | 1890 | goto fail; |
ba64af90 | 1891 | } |
90598531 | 1892 | |
d379d442 KZ |
1893 | /* mnt_unref_monitor() will close the fd */ |
1894 | fd = r = mnt_monitor_get_fd(m->mount_monitor); | |
ba64af90 LP |
1895 | if (r < 0) { |
1896 | log_error_errno(r, "Failed to acquire watch file descriptor: %m"); | |
f7c1ad4f | 1897 | goto fail; |
ba64af90 | 1898 | } |
befb6d54 | 1899 | |
d379d442 | 1900 | r = sd_event_add_io(m->event, &m->mount_event_source, fd, EPOLLIN, mount_dispatch_io, m); |
ba64af90 LP |
1901 | if (r < 0) { |
1902 | log_error_errno(r, "Failed to watch mount file descriptor: %m"); | |
befb6d54 | 1903 | goto fail; |
ba64af90 | 1904 | } |
befb6d54 | 1905 | |
83231637 | 1906 | r = sd_event_source_set_priority(m->mount_event_source, SD_EVENT_PRIORITY_NORMAL-10); |
ba64af90 LP |
1907 | if (r < 0) { |
1908 | log_error_errno(r, "Failed to adjust mount watch priority: %m"); | |
befb6d54 | 1909 | goto fail; |
ba64af90 | 1910 | } |
7dfbe2e3 | 1911 | |
d586f642 MS |
1912 | r = sd_event_source_set_ratelimit(m->mount_event_source, 1 * USEC_PER_SEC, 5); |
1913 | if (r < 0) { | |
1914 | log_error_errno(r, "Failed to enable rate limit for mount events: %m"); | |
1915 | goto fail; | |
1916 | } | |
1917 | ||
edc027b4 MS |
1918 | r = sd_event_source_set_ratelimit_expire_callback(m->mount_event_source, mount_on_ratelimit_expire); |
1919 | if (r < 0) { | |
1920 | log_error_errno(r, "Failed to enable rate limit for mount events: %m"); | |
1921 | goto fail; | |
1922 | } | |
1923 | ||
d379d442 | 1924 | (void) sd_event_source_set_description(m->mount_event_source, "mount-monitor-dispatch"); |
befb6d54 CL |
1925 | } |
1926 | ||
e62d8c39 ZJS |
1927 | r = mount_load_proc_self_mountinfo(m, false); |
1928 | if (r < 0) | |
b08d03ff LP |
1929 | goto fail; |
1930 | ||
ba64af90 | 1931 | return; |
b08d03ff LP |
1932 | |
1933 | fail: | |
1934 | mount_shutdown(m); | |
5cb5a6ff LP |
1935 | } |
1936 | ||
fcd8e119 LP |
1937 | static int drain_libmount(Manager *m) { |
1938 | bool rescan = false; | |
1939 | int r; | |
1940 | ||
1941 | assert(m); | |
1942 | ||
1943 | /* Drain all events and verify that the event is valid. | |
1944 | * | |
1945 | * Note that libmount also monitors /run/mount mkdir if the directory does not exist yet. The mkdir | |
1946 | * may generate event which is irrelevant for us. | |
1947 | * | |
1948 | * error: r < 0; valid: r == 0, false positive: r == 1 */ | |
1949 | do { | |
1950 | r = mnt_monitor_next_change(m->mount_monitor, NULL, NULL); | |
1951 | if (r < 0) | |
1952 | return log_error_errno(r, "Failed to drain libmount events: %m"); | |
1953 | if (r == 0) | |
1954 | rescan = true; | |
1955 | } while (r == 0); | |
1956 | ||
1957 | return rescan; | |
1958 | } | |
1959 | ||
35080486 | 1960 | static int mount_process_proc_self_mountinfo(Manager *m) { |
ec8126d7 | 1961 | _cleanup_set_free_free_ Set *around = NULL, *gone = NULL; |
ec8126d7 | 1962 | const char *what; |
ef734fd6 LP |
1963 | int r; |
1964 | ||
1965 | assert(m); | |
ef734fd6 | 1966 | |
fcd8e119 LP |
1967 | r = drain_libmount(m); |
1968 | if (r <= 0) | |
1969 | return r; | |
ef734fd6 | 1970 | |
4f0eedb7 ZJS |
1971 | r = mount_load_proc_self_mountinfo(m, true); |
1972 | if (r < 0) { | |
e537352b | 1973 | /* Reset flags, just in case, for later calls */ |
ec88d1ea LP |
1974 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) |
1975 | MOUNT(u)->proc_flags = 0; | |
e537352b | 1976 | |
ec8126d7 | 1977 | return 0; |
ef734fd6 LP |
1978 | } |
1979 | ||
1980 | manager_dispatch_load_queue(m); | |
1981 | ||
595ed347 MS |
1982 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) { |
1983 | Mount *mount = MOUNT(u); | |
ef734fd6 | 1984 | |
11222d0f | 1985 | if (!mount_is_mounted(mount)) { |
e537352b | 1986 | |
394763f6 LP |
1987 | /* A mount point is not around right now. It |
1988 | * might be gone, or might never have | |
1989 | * existed. */ | |
1990 | ||
1991 | if (mount->from_proc_self_mountinfo && | |
1992 | mount->parameters_proc_self_mountinfo.what) { | |
1993 | ||
1994 | /* Remember that this device might just have disappeared */ | |
548f6937 | 1995 | if (set_ensure_allocated(&gone, &path_hash_ops) < 0 || |
be327321 | 1996 | set_put_strdup(&gone, mount->parameters_proc_self_mountinfo.what) < 0) |
394763f6 LP |
1997 | log_oom(); /* we don't care too much about OOM here... */ |
1998 | } | |
fcd8b266 | 1999 | |
ef734fd6 | 2000 | mount->from_proc_self_mountinfo = false; |
9ddaa3e4 | 2001 | assert_se(update_parameters_proc_self_mountinfo(mount, NULL, NULL, NULL) >= 0); |
e537352b LP |
2002 | |
2003 | switch (mount->state) { | |
2004 | ||
2005 | case MOUNT_MOUNTED: | |
7eba1463 | 2006 | /* This has just been unmounted by somebody else, follow the state change. */ |
9d2f5178 | 2007 | mount_enter_dead(mount, MOUNT_SUCCESS); |
e537352b LP |
2008 | break; |
2009 | ||
2fa0bd7d YW |
2010 | case MOUNT_MOUNTING_DONE: |
2011 | /* The mount command may add the corresponding proc mountinfo entry and | |
2012 | * then remove it because of an internal error. E.g., fuse.sshfs seems | |
2013 | * to do that when the connection fails. See #17617. To handle such the | |
2014 | * case, let's once set the state back to mounting. Then, the unit can | |
2015 | * correctly enter the failed state later in mount_sigchld(). */ | |
2016 | mount_set_state(mount, MOUNT_MOUNTING); | |
2017 | break; | |
2018 | ||
e537352b | 2019 | default: |
e537352b | 2020 | break; |
e537352b LP |
2021 | } |
2022 | ||
ec88d1ea | 2023 | } else if (mount->proc_flags & (MOUNT_PROC_JUST_MOUNTED|MOUNT_PROC_JUST_CHANGED)) { |
e537352b | 2024 | |
fcd8b266 | 2025 | /* A mount point was added or changed */ |
e537352b LP |
2026 | |
2027 | switch (mount->state) { | |
2028 | ||
2029 | case MOUNT_DEAD: | |
fdf20a31 | 2030 | case MOUNT_FAILED: |
4b58153d LP |
2031 | |
2032 | /* This has just been mounted by somebody else, follow the state change, but let's | |
2033 | * generate a new invocation ID for this implicitly and automatically. */ | |
7eba1463 LP |
2034 | (void) unit_acquire_invocation_id(u); |
2035 | mount_cycle_clear(mount); | |
9d2f5178 | 2036 | mount_enter_mounted(mount, MOUNT_SUCCESS); |
e537352b LP |
2037 | break; |
2038 | ||
2039 | case MOUNT_MOUNTING: | |
5bcb0f2b | 2040 | mount_set_state(mount, MOUNT_MOUNTING_DONE); |
e537352b LP |
2041 | break; |
2042 | ||
2043 | default: | |
2044 | /* Nothing really changed, but let's | |
2045 | * issue an notification call | |
2046 | * nonetheless, in case somebody is | |
2047 | * waiting for this. (e.g. file system | |
2048 | * ro/rw remounts.) */ | |
2049 | mount_set_state(mount, mount->state); | |
2050 | break; | |
2051 | } | |
394763f6 | 2052 | } |
fcd8b266 | 2053 | |
11222d0f | 2054 | if (mount_is_mounted(mount) && |
394763f6 LP |
2055 | mount->from_proc_self_mountinfo && |
2056 | mount->parameters_proc_self_mountinfo.what) { | |
b6418dc9 | 2057 | /* Track devices currently used */ |
fcd8b266 | 2058 | |
548f6937 | 2059 | if (set_ensure_allocated(&around, &path_hash_ops) < 0 || |
be327321 | 2060 | set_put_strdup(&around, mount->parameters_proc_self_mountinfo.what) < 0) |
394763f6 | 2061 | log_oom(); |
e537352b LP |
2062 | } |
2063 | ||
2064 | /* Reset the flags for later calls */ | |
ec88d1ea | 2065 | mount->proc_flags = 0; |
e537352b | 2066 | } |
718db961 | 2067 | |
90e74a66 | 2068 | SET_FOREACH(what, gone) { |
fcd8b266 LP |
2069 | if (set_contains(around, what)) |
2070 | continue; | |
2071 | ||
2072 | /* Let the device units know that the device is no longer mounted */ | |
485ae697 | 2073 | device_found_node(m, what, 0, DEVICE_FOUND_MOUNT); |
fcd8b266 | 2074 | } |
ec8126d7 ZJS |
2075 | |
2076 | return 0; | |
e537352b LP |
2077 | } |
2078 | ||
35080486 LP |
2079 | static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
2080 | Manager *m = userdata; | |
2081 | ||
2082 | assert(m); | |
2083 | assert(revents & EPOLLIN); | |
2084 | ||
2085 | return mount_process_proc_self_mountinfo(m); | |
2086 | } | |
2087 | ||
fdf20a31 | 2088 | static void mount_reset_failed(Unit *u) { |
5632e374 LP |
2089 | Mount *m = MOUNT(u); |
2090 | ||
2091 | assert(m); | |
2092 | ||
fdf20a31 | 2093 | if (m->state == MOUNT_FAILED) |
5632e374 LP |
2094 | mount_set_state(m, MOUNT_DEAD); |
2095 | ||
9d2f5178 LP |
2096 | m->result = MOUNT_SUCCESS; |
2097 | m->reload_result = MOUNT_SUCCESS; | |
17e9d53d | 2098 | m->clean_result = MOUNT_SUCCESS; |
5632e374 LP |
2099 | } |
2100 | ||
718db961 | 2101 | static int mount_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
22af0e58 LP |
2102 | Mount *m = MOUNT(u); |
2103 | ||
2104 | assert(m); | |
2105 | ||
ee36fed4 | 2106 | return unit_kill_common(u, who, signo, -1, m->control_pid, error); |
8a0867d6 LP |
2107 | } |
2108 | ||
291d565a LP |
2109 | static int mount_control_pid(Unit *u) { |
2110 | Mount *m = MOUNT(u); | |
2111 | ||
2112 | assert(m); | |
2113 | ||
2114 | return m->control_pid; | |
2115 | } | |
2116 | ||
17e9d53d YW |
2117 | static int mount_clean(Unit *u, ExecCleanMask mask) { |
2118 | _cleanup_strv_free_ char **l = NULL; | |
2119 | Mount *m = MOUNT(u); | |
2120 | int r; | |
2121 | ||
2122 | assert(m); | |
2123 | assert(mask != 0); | |
2124 | ||
2125 | if (m->state != MOUNT_DEAD) | |
2126 | return -EBUSY; | |
2127 | ||
2128 | r = exec_context_get_clean_directories(&m->exec_context, u->manager->prefix, mask, &l); | |
2129 | if (r < 0) | |
2130 | return r; | |
2131 | ||
2132 | if (strv_isempty(l)) | |
2133 | return -EUNATCH; | |
2134 | ||
2135 | mount_unwatch_control_pid(m); | |
2136 | m->clean_result = MOUNT_SUCCESS; | |
2137 | m->control_command = NULL; | |
2138 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
2139 | ||
2140 | r = mount_arm_timer(m, usec_add(now(CLOCK_MONOTONIC), m->exec_context.timeout_clean_usec)); | |
2141 | if (r < 0) | |
2142 | goto fail; | |
2143 | ||
2144 | r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid); | |
2145 | if (r < 0) | |
2146 | goto fail; | |
2147 | ||
2148 | mount_set_state(m, MOUNT_CLEANING); | |
2149 | ||
2150 | return 0; | |
2151 | ||
2152 | fail: | |
2153 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); | |
2154 | m->clean_result = MOUNT_FAILURE_RESOURCES; | |
5dcadb4c | 2155 | m->timer_event_source = sd_event_source_disable_unref(m->timer_event_source); |
17e9d53d YW |
2156 | return r; |
2157 | } | |
2158 | ||
2159 | static int mount_can_clean(Unit *u, ExecCleanMask *ret) { | |
2160 | Mount *m = MOUNT(u); | |
2161 | ||
2162 | assert(m); | |
2163 | ||
2164 | return exec_context_get_clean_mask(&m->exec_context, ret); | |
2165 | } | |
2166 | ||
705578c3 | 2167 | static int mount_can_start(Unit *u) { |
9727f242 DDM |
2168 | Mount *m = MOUNT(u); |
2169 | int r; | |
2170 | ||
2171 | assert(m); | |
2172 | ||
a7c93dfe MS |
2173 | if (sd_event_source_is_ratelimited(u->manager->mount_event_source)) |
2174 | return -EAGAIN; | |
2175 | ||
9727f242 DDM |
2176 | r = unit_test_start_limit(u); |
2177 | if (r < 0) { | |
2178 | mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT); | |
2179 | return r; | |
2180 | } | |
2181 | ||
705578c3 | 2182 | return 1; |
9727f242 DDM |
2183 | } |
2184 | ||
a16e1123 | 2185 | static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = { |
48d83e33 | 2186 | [MOUNT_EXEC_MOUNT] = "ExecMount", |
a16e1123 LP |
2187 | [MOUNT_EXEC_UNMOUNT] = "ExecUnmount", |
2188 | [MOUNT_EXEC_REMOUNT] = "ExecRemount", | |
2189 | }; | |
2190 | ||
2191 | DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand); | |
2192 | ||
9d2f5178 | 2193 | static const char* const mount_result_table[_MOUNT_RESULT_MAX] = { |
48d83e33 ZJS |
2194 | [MOUNT_SUCCESS] = "success", |
2195 | [MOUNT_FAILURE_RESOURCES] = "resources", | |
2196 | [MOUNT_FAILURE_TIMEOUT] = "timeout", | |
2197 | [MOUNT_FAILURE_EXIT_CODE] = "exit-code", | |
2198 | [MOUNT_FAILURE_SIGNAL] = "signal", | |
2199 | [MOUNT_FAILURE_CORE_DUMP] = "core-dump", | |
07299350 | 2200 | [MOUNT_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
48d83e33 | 2201 | [MOUNT_FAILURE_PROTOCOL] = "protocol", |
9d2f5178 LP |
2202 | }; |
2203 | ||
2204 | DEFINE_STRING_TABLE_LOOKUP(mount_result, MountResult); | |
2205 | ||
87f0e418 | 2206 | const UnitVTable mount_vtable = { |
7d17cfbc | 2207 | .object_size = sizeof(Mount), |
718db961 LP |
2208 | .exec_context_offset = offsetof(Mount, exec_context), |
2209 | .cgroup_context_offset = offsetof(Mount, cgroup_context), | |
2210 | .kill_context_offset = offsetof(Mount, kill_context), | |
613b411c | 2211 | .exec_runtime_offset = offsetof(Mount, exec_runtime), |
29206d46 | 2212 | .dynamic_creds_offset = offsetof(Mount, dynamic_creds), |
3ef63c31 | 2213 | |
f975e971 LP |
2214 | .sections = |
2215 | "Unit\0" | |
2216 | "Mount\0" | |
2217 | "Install\0", | |
4ad49000 | 2218 | .private_section = "Mount", |
71645aca | 2219 | |
c80a9a33 LP |
2220 | .can_transient = true, |
2221 | .can_fail = true, | |
755021d4 | 2222 | .exclude_from_switch_root_serialization = true, |
c80a9a33 | 2223 | |
e537352b LP |
2224 | .init = mount_init, |
2225 | .load = mount_load, | |
034c6ed7 | 2226 | .done = mount_done, |
e537352b | 2227 | |
f50e0a01 LP |
2228 | .coldplug = mount_coldplug, |
2229 | ||
034c6ed7 | 2230 | .dump = mount_dump, |
5cb5a6ff | 2231 | |
e537352b LP |
2232 | .start = mount_start, |
2233 | .stop = mount_stop, | |
2234 | .reload = mount_reload, | |
2235 | ||
8a0867d6 | 2236 | .kill = mount_kill, |
17e9d53d YW |
2237 | .clean = mount_clean, |
2238 | .can_clean = mount_can_clean, | |
8a0867d6 | 2239 | |
a16e1123 LP |
2240 | .serialize = mount_serialize, |
2241 | .deserialize_item = mount_deserialize_item, | |
2242 | ||
f50e0a01 | 2243 | .active_state = mount_active_state, |
10a94420 | 2244 | .sub_state_to_string = mount_sub_state_to_string, |
b08d03ff | 2245 | |
52a12341 YW |
2246 | .will_restart = unit_will_restart_default, |
2247 | ||
f2f725e5 | 2248 | .may_gc = mount_may_gc, |
39c79477 | 2249 | .is_extrinsic = mount_is_extrinsic, |
701cc384 | 2250 | |
e537352b | 2251 | .sigchld_event = mount_sigchld_event, |
e537352b | 2252 | |
fdf20a31 | 2253 | .reset_failed = mount_reset_failed, |
291d565a LP |
2254 | |
2255 | .control_pid = mount_control_pid, | |
5632e374 | 2256 | |
74c964d3 LP |
2257 | .bus_set_property = bus_mount_set_property, |
2258 | .bus_commit_properties = bus_mount_commit_properties, | |
4139c1b2 | 2259 | |
68db7a3b ZJS |
2260 | .get_timeout = mount_get_timeout, |
2261 | ||
04eb582a | 2262 | .enumerate_perpetual = mount_enumerate_perpetual, |
f50e0a01 | 2263 | .enumerate = mount_enumerate, |
c6918296 MS |
2264 | .shutdown = mount_shutdown, |
2265 | ||
2266 | .status_message_formats = { | |
2267 | .starting_stopping = { | |
2268 | [0] = "Mounting %s...", | |
2269 | [1] = "Unmounting %s...", | |
2270 | }, | |
2271 | .finished_start_job = { | |
2272 | [JOB_DONE] = "Mounted %s.", | |
2273 | [JOB_FAILED] = "Failed to mount %s.", | |
c6918296 MS |
2274 | [JOB_TIMEOUT] = "Timed out mounting %s.", |
2275 | }, | |
2276 | .finished_stop_job = { | |
2277 | [JOB_DONE] = "Unmounted %s.", | |
2278 | [JOB_FAILED] = "Failed unmounting %s.", | |
2279 | [JOB_TIMEOUT] = "Timed out unmounting %s.", | |
2280 | }, | |
2281 | }, | |
9727f242 | 2282 | |
705578c3 | 2283 | .can_start = mount_can_start, |
5cb5a6ff | 2284 | }; |