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