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