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