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