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