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