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