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