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