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