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