]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
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 General Public License as published by | |
10 | the Free Software Foundation; either version 2 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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <errno.h> |
b08d03ff LP |
23 | #include <stdio.h> |
24 | #include <mntent.h> | |
ef734fd6 | 25 | #include <sys/epoll.h> |
e537352b | 26 | #include <signal.h> |
5cb5a6ff | 27 | |
87f0e418 | 28 | #include "unit.h" |
5cb5a6ff LP |
29 | #include "mount.h" |
30 | #include "load-fragment.h" | |
5cb5a6ff | 31 | #include "load-dropin.h" |
b08d03ff | 32 | #include "log.h" |
e537352b LP |
33 | #include "strv.h" |
34 | #include "mount-setup.h" | |
9e2f7c11 | 35 | #include "unit-name.h" |
4139c1b2 LP |
36 | #include "mount.h" |
37 | #include "dbus-mount.h" | |
5cb5a6ff | 38 | |
f50e0a01 LP |
39 | static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = { |
40 | [MOUNT_DEAD] = UNIT_INACTIVE, | |
41 | [MOUNT_MOUNTING] = UNIT_ACTIVATING, | |
e537352b | 42 | [MOUNT_MOUNTING_DONE] = UNIT_ACTIVE, |
f50e0a01 | 43 | [MOUNT_MOUNTED] = UNIT_ACTIVE, |
e537352b | 44 | [MOUNT_REMOUNTING] = UNIT_ACTIVE_RELOADING, |
f50e0a01 | 45 | [MOUNT_UNMOUNTING] = UNIT_DEACTIVATING, |
e537352b LP |
46 | [MOUNT_MOUNTING_SIGTERM] = UNIT_DEACTIVATING, |
47 | [MOUNT_MOUNTING_SIGKILL] = UNIT_DEACTIVATING, | |
48 | [MOUNT_REMOUNTING_SIGTERM] = UNIT_ACTIVE_RELOADING, | |
49 | [MOUNT_REMOUNTING_SIGKILL] = UNIT_ACTIVE_RELOADING, | |
50 | [MOUNT_UNMOUNTING_SIGTERM] = UNIT_DEACTIVATING, | |
51 | [MOUNT_UNMOUNTING_SIGKILL] = UNIT_DEACTIVATING, | |
f50e0a01 LP |
52 | [MOUNT_MAINTAINANCE] = UNIT_INACTIVE, |
53 | }; | |
5cb5a6ff | 54 | |
a16e1123 LP |
55 | static void mount_init(Unit *u) { |
56 | Mount *m = MOUNT(u); | |
5cb5a6ff | 57 | |
a16e1123 LP |
58 | assert(u); |
59 | assert(u->meta.load_state == UNIT_STUB); | |
60 | ||
61 | m->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
62 | exec_context_init(&m->exec_context); | |
8d567588 | 63 | |
a16e1123 LP |
64 | /* We need to make sure that /bin/mount is always called in |
65 | * the same process group as us, so that the autofs kernel | |
66 | * side doesn't send us another mount request while we are | |
67 | * already trying to comply its last one. */ | |
68 | m->exec_context.no_setsid = true; | |
8d567588 | 69 | |
a16e1123 LP |
70 | m->timer_watch.type = WATCH_INVALID; |
71 | ||
72 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
8d567588 LP |
73 | } |
74 | ||
a16e1123 | 75 | static void mount_unwatch_control_pid(Mount *m) { |
5e94833f LP |
76 | assert(m); |
77 | ||
78 | if (m->control_pid <= 0) | |
79 | return; | |
80 | ||
81 | unit_unwatch_pid(UNIT(m), m->control_pid); | |
82 | m->control_pid = 0; | |
83 | } | |
84 | ||
e537352b LP |
85 | static void mount_parameters_done(MountParameters *p) { |
86 | assert(p); | |
87 | ||
88 | free(p->what); | |
89 | free(p->options); | |
90 | free(p->fstype); | |
91 | ||
92 | p->what = p->options = p->fstype = NULL; | |
93 | } | |
94 | ||
87f0e418 | 95 | static void mount_done(Unit *u) { |
ef734fd6 | 96 | Mount *m = MOUNT(u); |
034c6ed7 | 97 | |
ef734fd6 | 98 | assert(m); |
034c6ed7 | 99 | |
e537352b LP |
100 | free(m->where); |
101 | m->where = NULL; | |
f50e0a01 | 102 | |
e537352b LP |
103 | mount_parameters_done(&m->parameters_etc_fstab); |
104 | mount_parameters_done(&m->parameters_proc_self_mountinfo); | |
105 | mount_parameters_done(&m->parameters_fragment); | |
ef734fd6 | 106 | |
e537352b LP |
107 | exec_context_done(&m->exec_context); |
108 | exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX); | |
109 | m->control_command = NULL; | |
f50e0a01 | 110 | |
a16e1123 | 111 | mount_unwatch_control_pid(m); |
f50e0a01 | 112 | |
e537352b | 113 | unit_unwatch_timer(u, &m->timer_watch); |
f50e0a01 LP |
114 | } |
115 | ||
07b0b134 | 116 | int mount_add_node_links(Unit *u, const char *what) { |
b08d03ff LP |
117 | Unit *device; |
118 | char *e; | |
119 | int r; | |
120 | ||
07b0b134 | 121 | assert(u); |
b08d03ff | 122 | |
07b0b134 | 123 | if (!what) |
e537352b LP |
124 | /* We observe kernel mounts only while they are live, |
125 | * hence don't create any links for them */ | |
126 | return 0; | |
127 | ||
07b0b134 ML |
128 | /* Adds in links to the device that this node is based on */ |
129 | ||
e537352b | 130 | if (!path_startswith(what, "/dev/")) |
b08d03ff LP |
131 | return 0; |
132 | ||
9e2f7c11 | 133 | if (!(e = unit_name_build_escape(what+1, NULL, ".device"))) |
b08d03ff LP |
134 | return -ENOMEM; |
135 | ||
07b0b134 | 136 | r = manager_load_unit(u->meta.manager, e, NULL, &device); |
b08d03ff LP |
137 | free(e); |
138 | ||
139 | if (r < 0) | |
140 | return r; | |
141 | ||
07b0b134 | 142 | if ((r = unit_add_dependency(u, UNIT_AFTER, device, true)) < 0) |
b08d03ff LP |
143 | return r; |
144 | ||
07b0b134 | 145 | if ((r = unit_add_dependency(u, UNIT_REQUIRES, device, true)) < 0) |
b08d03ff LP |
146 | return r; |
147 | ||
07b0b134 ML |
148 | if (u->meta.manager->running_as == MANAGER_INIT || |
149 | u->meta.manager->running_as == MANAGER_SYSTEM) | |
150 | if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0) | |
e537352b | 151 | return r; |
b08d03ff LP |
152 | |
153 | return 0; | |
154 | } | |
155 | ||
07b0b134 | 156 | int mount_add_path_links(Unit *u, const char *where, bool requires) { |
ef734fd6 | 157 | Meta *other; |
b08d03ff LP |
158 | int r; |
159 | ||
07b0b134 | 160 | assert(u); |
e537352b | 161 | |
b08d03ff LP |
162 | /* Adds in link to other mount points, that might lie below or |
163 | * above us in the hierarchy */ | |
164 | ||
07b0b134 | 165 | LIST_FOREACH(units_per_type, other, u->meta.manager->units_per_type[UNIT_MOUNT]) { |
b08d03ff LP |
166 | Mount *n; |
167 | ||
ef734fd6 | 168 | n = (Mount*) other; |
b08d03ff | 169 | |
07b0b134 | 170 | if (UNIT(n) == u) |
79ef004b | 171 | continue; |
b08d03ff | 172 | |
07b0b134 | 173 | if (u->meta.load_state != UNIT_LOADED) |
e537352b LP |
174 | continue; |
175 | ||
07b0b134 | 176 | if (path_startswith(where, n->where)) { |
b08d03ff | 177 | |
07b0b134 | 178 | if ((r = unit_add_dependency(u, UNIT_AFTER, UNIT(other), true)) < 0) |
b08d03ff LP |
179 | return r; |
180 | ||
07b0b134 ML |
181 | if (requires) |
182 | if ((r = unit_add_dependency(u, UNIT_REQUIRES, UNIT(other), true)) < 0) | |
e537352b | 183 | return r; |
b08d03ff | 184 | |
07b0b134 | 185 | } else if (path_startswith(n->where, where)) { |
b08d03ff | 186 | |
07b0b134 | 187 | if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(other), true)) < 0) |
b08d03ff LP |
188 | return r; |
189 | ||
07b0b134 ML |
190 | if (requires) |
191 | if ((r = unit_add_dependency(UNIT(other), UNIT_REQUIRES, u, true)) < 0) | |
e537352b | 192 | return r; |
b08d03ff LP |
193 | } |
194 | } | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
07b0b134 | 199 | static char* mount_test_option(const char *haystack, const char *needle) { |
e537352b LP |
200 | struct mntent me; |
201 | ||
202 | assert(needle); | |
203 | ||
204 | /* Like glibc's hasmntopt(), but works on a string, not a | |
205 | * struct mntent */ | |
206 | ||
207 | if (!haystack) | |
208 | return false; | |
209 | ||
210 | zero(me); | |
211 | me.mnt_opts = (char*) haystack; | |
212 | ||
07b0b134 | 213 | return hasmntopt(&me, needle); |
e537352b LP |
214 | } |
215 | ||
216 | static int mount_add_target_links(Mount *m) { | |
217 | const char *target; | |
218 | MountParameters *p; | |
a16e1123 | 219 | Unit *tu; |
e537352b | 220 | int r; |
a16e1123 | 221 | bool noauto, handle, automount; |
e537352b LP |
222 | |
223 | assert(m); | |
224 | ||
225 | if (m->from_fragment) | |
226 | p = &m->parameters_fragment; | |
227 | else if (m->from_etc_fstab) | |
228 | p = &m->parameters_etc_fstab; | |
229 | else | |
230 | return 0; | |
231 | ||
07b0b134 ML |
232 | noauto = !!mount_test_option(p->options, MNTOPT_NOAUTO); |
233 | handle = !!mount_test_option(p->options, "comment=systemd.mount"); | |
234 | automount = !!mount_test_option(p->options, "comment=systemd.automount"); | |
e537352b | 235 | |
a16e1123 | 236 | if (noauto && !handle && !automount) |
e537352b LP |
237 | return 0; |
238 | ||
239 | if (mount_test_option(p->options, "_netdev") || | |
240 | fstype_is_network(p->fstype)) | |
241 | target = SPECIAL_REMOTE_FS_TARGET; | |
242 | else | |
243 | target = SPECIAL_LOCAL_FS_TARGET; | |
244 | ||
a16e1123 | 245 | if ((r = manager_load_unit(UNIT(m)->meta.manager, target, NULL, &tu)) < 0) |
e537352b LP |
246 | return r; |
247 | ||
a16e1123 LP |
248 | if (automount) { |
249 | Unit *am; | |
250 | ||
251 | if ((r = unit_load_related_unit(UNIT(m), ".automount", &am)) < 0) | |
9fcc065a | 252 | return r; |
e537352b | 253 | |
701cc384 | 254 | if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(am), true)) < 0) |
a16e1123 LP |
255 | return r; |
256 | ||
701cc384 | 257 | return unit_add_dependency(UNIT(am), UNIT_BEFORE, tu, true); |
a16e1123 LP |
258 | |
259 | } else { | |
260 | ||
261 | if (handle) | |
701cc384 | 262 | if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m), true)) < 0) |
a16e1123 LP |
263 | return r; |
264 | ||
701cc384 | 265 | return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu, true); |
a16e1123 | 266 | } |
e537352b LP |
267 | } |
268 | ||
8d567588 LP |
269 | static int mount_verify(Mount *m) { |
270 | bool b; | |
271 | char *e; | |
272 | assert(m); | |
273 | ||
274 | if (UNIT(m)->meta.load_state != UNIT_LOADED) | |
275 | return 0; | |
276 | ||
a16e1123 | 277 | if (!(e = unit_name_from_path(m->where, ".mount"))) |
8d567588 LP |
278 | return -ENOMEM; |
279 | ||
280 | b = unit_has_name(UNIT(m), e); | |
281 | free(e); | |
282 | ||
283 | if (!b) { | |
284 | log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(m)->meta.id); | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
e537352b LP |
291 | static int mount_load(Unit *u) { |
292 | Mount *m = MOUNT(u); | |
293 | int r; | |
294 | ||
295 | assert(u); | |
296 | assert(u->meta.load_state == UNIT_STUB); | |
297 | ||
298 | if ((r = unit_load_fragment_and_dropin_optional(u)) < 0) | |
299 | return r; | |
300 | ||
301 | /* This is a new unit? Then let's add in some extras */ | |
302 | if (u->meta.load_state == UNIT_LOADED) { | |
07b0b134 ML |
303 | const char *what = m->parameters_fragment.what; |
304 | if (!what) | |
305 | what = m->parameters_etc_fstab.what; | |
e537352b | 306 | |
a16e1123 LP |
307 | if (!m->where) |
308 | if (!(m->where = unit_name_to_path(u->meta.id))) | |
309 | return -ENOMEM; | |
310 | ||
311 | path_kill_slashes(m->where); | |
312 | ||
e537352b LP |
313 | /* Minor validity checking */ |
314 | if ((m->parameters_fragment.options || m->parameters_fragment.fstype) && !m->parameters_fragment.what) | |
315 | return -EBADMSG; | |
316 | ||
317 | if (m->parameters_fragment.what) | |
318 | m->from_fragment = true; | |
319 | ||
07b0b134 | 320 | if ((r = mount_add_node_links(u, what)) < 0) |
e537352b LP |
321 | return r; |
322 | ||
07b0b134 | 323 | if ((r = mount_add_path_links(u, m->where, m->from_etc_fstab || m->from_fragment)) < 0) |
e537352b LP |
324 | return r; |
325 | ||
326 | if ((r = mount_add_target_links(MOUNT(u))) < 0) | |
327 | return r; | |
328 | ||
329 | if ((r = unit_add_default_cgroup(u)) < 0) | |
330 | return r; | |
331 | } | |
332 | ||
8d567588 | 333 | return mount_verify(m); |
e537352b LP |
334 | } |
335 | ||
a16e1123 LP |
336 | static int mount_notify_automount(Mount *m, int status) { |
337 | Unit *p; | |
338 | int r; | |
339 | ||
340 | assert(m); | |
341 | ||
342 | if ((r = unit_get_related_unit(UNIT(m), ".automount", &p)) < 0) | |
343 | return r == -ENOENT ? 0 : r; | |
344 | ||
345 | return automount_send_ready(AUTOMOUNT(p), status); | |
346 | } | |
347 | ||
e537352b LP |
348 | static void mount_set_state(Mount *m, MountState state) { |
349 | MountState old_state; | |
350 | assert(m); | |
351 | ||
352 | old_state = m->state; | |
353 | m->state = state; | |
354 | ||
355 | if (state != MOUNT_MOUNTING && | |
356 | state != MOUNT_MOUNTING_DONE && | |
357 | state != MOUNT_REMOUNTING && | |
358 | state != MOUNT_UNMOUNTING && | |
359 | state != MOUNT_MOUNTING_SIGTERM && | |
360 | state != MOUNT_MOUNTING_SIGKILL && | |
361 | state != MOUNT_UNMOUNTING_SIGTERM && | |
362 | state != MOUNT_UNMOUNTING_SIGKILL && | |
363 | state != MOUNT_REMOUNTING_SIGTERM && | |
364 | state != MOUNT_REMOUNTING_SIGKILL) { | |
365 | unit_unwatch_timer(UNIT(m), &m->timer_watch); | |
a16e1123 | 366 | mount_unwatch_control_pid(m); |
e537352b | 367 | m->control_command = NULL; |
a16e1123 | 368 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; |
e537352b LP |
369 | } |
370 | ||
8d567588 LP |
371 | if (state == MOUNT_MOUNTED || |
372 | state == MOUNT_REMOUNTING) | |
373 | mount_notify_automount(m, 0); | |
374 | else if (state == MOUNT_DEAD || | |
375 | state == MOUNT_UNMOUNTING || | |
376 | state == MOUNT_MOUNTING_SIGTERM || | |
377 | state == MOUNT_MOUNTING_SIGKILL || | |
378 | state == MOUNT_REMOUNTING_SIGTERM || | |
379 | state == MOUNT_REMOUNTING_SIGKILL || | |
380 | state == MOUNT_UNMOUNTING_SIGTERM || | |
381 | state == MOUNT_UNMOUNTING_SIGKILL || | |
382 | state == MOUNT_MAINTAINANCE) | |
383 | mount_notify_automount(m, -ENODEV); | |
384 | ||
e537352b | 385 | if (state != old_state) |
40d50879 | 386 | log_debug("%s changed %s -> %s", |
a16e1123 LP |
387 | UNIT(m)->meta.id, |
388 | mount_state_to_string(old_state), | |
389 | mount_state_to_string(state)); | |
e537352b LP |
390 | |
391 | unit_notify(UNIT(m), state_translation_table[old_state], state_translation_table[state]); | |
392 | } | |
393 | ||
394 | static int mount_coldplug(Unit *u) { | |
395 | Mount *m = MOUNT(u); | |
a16e1123 LP |
396 | MountState new_state = MOUNT_DEAD; |
397 | int r; | |
e537352b LP |
398 | |
399 | assert(m); | |
400 | assert(m->state == MOUNT_DEAD); | |
401 | ||
a16e1123 LP |
402 | if (m->deserialized_state != m->state) |
403 | new_state = m->deserialized_state; | |
404 | else if (m->from_proc_self_mountinfo) | |
405 | new_state = MOUNT_MOUNTED; | |
e537352b | 406 | |
a16e1123 | 407 | if (new_state != m->state) { |
e537352b | 408 | |
a16e1123 LP |
409 | if (new_state == MOUNT_MOUNTING || |
410 | new_state == MOUNT_MOUNTING_DONE || | |
411 | new_state == MOUNT_REMOUNTING || | |
412 | new_state == MOUNT_UNMOUNTING || | |
413 | new_state == MOUNT_MOUNTING_SIGTERM || | |
414 | new_state == MOUNT_MOUNTING_SIGKILL || | |
415 | new_state == MOUNT_UNMOUNTING_SIGTERM || | |
416 | new_state == MOUNT_UNMOUNTING_SIGKILL || | |
417 | new_state == MOUNT_REMOUNTING_SIGTERM || | |
418 | new_state == MOUNT_REMOUNTING_SIGKILL) { | |
e537352b | 419 | |
a16e1123 LP |
420 | if (m->control_pid <= 0) |
421 | return -EBADMSG; | |
e537352b | 422 | |
a16e1123 LP |
423 | if ((r = unit_watch_pid(UNIT(m), m->control_pid)) < 0) |
424 | return r; | |
e537352b | 425 | |
a16e1123 LP |
426 | if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0) |
427 | return r; | |
428 | } | |
e537352b | 429 | |
a16e1123 LP |
430 | mount_set_state(m, new_state); |
431 | } | |
e537352b LP |
432 | |
433 | return 0; | |
e537352b LP |
434 | } |
435 | ||
436 | static void mount_dump(Unit *u, FILE *f, const char *prefix) { | |
437 | Mount *m = MOUNT(u); | |
438 | MountParameters *p; | |
439 | ||
440 | assert(m); | |
441 | assert(f); | |
442 | ||
443 | if (m->from_proc_self_mountinfo) | |
444 | p = &m->parameters_proc_self_mountinfo; | |
445 | else if (m->from_fragment) | |
446 | p = &m->parameters_fragment; | |
447 | else | |
448 | p = &m->parameters_etc_fstab; | |
449 | ||
450 | fprintf(f, | |
451 | "%sMount State: %s\n" | |
452 | "%sWhere: %s\n" | |
453 | "%sWhat: %s\n" | |
454 | "%sFile System Type: %s\n" | |
455 | "%sOptions: %s\n" | |
456 | "%sFrom /etc/fstab: %s\n" | |
457 | "%sFrom /proc/self/mountinfo: %s\n" | |
458 | "%sFrom fragment: %s\n" | |
459 | "%sKillMode: %s\n", | |
a16e1123 | 460 | prefix, mount_state_to_string(m->state), |
e537352b LP |
461 | prefix, m->where, |
462 | prefix, strna(p->what), | |
463 | prefix, strna(p->fstype), | |
464 | prefix, strna(p->options), | |
465 | prefix, yes_no(m->from_etc_fstab), | |
466 | prefix, yes_no(m->from_proc_self_mountinfo), | |
467 | prefix, yes_no(m->from_fragment), | |
468 | prefix, kill_mode_to_string(m->kill_mode)); | |
469 | ||
470 | if (m->control_pid > 0) | |
471 | fprintf(f, | |
472 | "%sControl PID: %llu\n", | |
473 | prefix, (unsigned long long) m->control_pid); | |
474 | ||
475 | exec_context_dump(&m->exec_context, f, prefix); | |
476 | } | |
477 | ||
a16e1123 LP |
478 | static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) { |
479 | pid_t pid; | |
480 | int r; | |
481 | ||
482 | assert(m); | |
483 | assert(c); | |
484 | assert(_pid); | |
485 | ||
486 | if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0) | |
487 | goto fail; | |
488 | ||
489 | if ((r = exec_spawn(c, | |
490 | NULL, | |
491 | &m->exec_context, | |
492 | NULL, 0, | |
1137a57c | 493 | m->meta.manager->environment, |
a16e1123 LP |
494 | true, |
495 | true, | |
496 | UNIT(m)->meta.manager->confirm_spawn, | |
497 | UNIT(m)->meta.cgroup_bondings, | |
498 | &pid)) < 0) | |
499 | goto fail; | |
500 | ||
501 | if ((r = unit_watch_pid(UNIT(m), pid)) < 0) | |
502 | /* FIXME: we need to do something here */ | |
503 | goto fail; | |
504 | ||
505 | *_pid = pid; | |
506 | ||
507 | return 0; | |
508 | ||
509 | fail: | |
510 | unit_unwatch_timer(UNIT(m), &m->timer_watch); | |
511 | ||
512 | return r; | |
513 | } | |
514 | ||
e537352b LP |
515 | static void mount_enter_dead(Mount *m, bool success) { |
516 | assert(m); | |
517 | ||
518 | if (!success) | |
519 | m->failure = true; | |
520 | ||
521 | mount_set_state(m, m->failure ? MOUNT_MAINTAINANCE : MOUNT_DEAD); | |
522 | } | |
523 | ||
80876c20 LP |
524 | static void mount_enter_mounted(Mount *m, bool success) { |
525 | assert(m); | |
526 | ||
527 | if (!success) | |
528 | m->failure = true; | |
529 | ||
530 | mount_set_state(m, MOUNT_MOUNTED); | |
531 | } | |
532 | ||
e537352b LP |
533 | static void mount_enter_signal(Mount *m, MountState state, bool success) { |
534 | int r; | |
80876c20 | 535 | bool sent = false; |
e537352b LP |
536 | |
537 | assert(m); | |
538 | ||
539 | if (!success) | |
540 | m->failure = true; | |
541 | ||
80876c20 LP |
542 | if (m->kill_mode != KILL_NONE) { |
543 | int sig = (state == MOUNT_MOUNTING_SIGTERM || | |
544 | state == MOUNT_UNMOUNTING_SIGTERM || | |
545 | state == MOUNT_REMOUNTING_SIGTERM) ? SIGTERM : SIGKILL; | |
e537352b LP |
546 | |
547 | if (m->kill_mode == KILL_CONTROL_GROUP) { | |
548 | ||
549 | if ((r = cgroup_bonding_kill_list(UNIT(m)->meta.cgroup_bondings, sig)) < 0) { | |
550 | if (r != -EAGAIN && r != -ESRCH) | |
551 | goto fail; | |
552 | } else | |
553 | sent = true; | |
554 | } | |
555 | ||
80876c20 | 556 | if (!sent && m->control_pid > 0) |
e537352b LP |
557 | if (kill(m->kill_mode == KILL_PROCESS ? m->control_pid : -m->control_pid, sig) < 0 && errno != ESRCH) { |
558 | r = -errno; | |
559 | goto fail; | |
560 | } | |
561 | } | |
562 | ||
80876c20 LP |
563 | if (sent) { |
564 | if ((r = unit_watch_timer(UNIT(m), m->timeout_usec, &m->timer_watch)) < 0) | |
565 | goto fail; | |
e537352b | 566 | |
80876c20 LP |
567 | mount_set_state(m, state); |
568 | } else if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL) | |
569 | mount_enter_mounted(m, true); | |
570 | else | |
e537352b LP |
571 | mount_enter_dead(m, true); |
572 | ||
573 | return; | |
574 | ||
575 | fail: | |
9e2f7c11 | 576 | log_warning("%s failed to kill processes: %s", UNIT(m)->meta.id, strerror(-r)); |
e537352b | 577 | |
80876c20 LP |
578 | if (state == MOUNT_REMOUNTING_SIGTERM || state == MOUNT_REMOUNTING_SIGKILL) |
579 | mount_enter_mounted(m, false); | |
580 | else | |
581 | mount_enter_dead(m, false); | |
e537352b LP |
582 | } |
583 | ||
584 | static void mount_enter_unmounting(Mount *m, bool success) { | |
e537352b LP |
585 | int r; |
586 | ||
587 | assert(m); | |
588 | ||
589 | if (!success) | |
590 | m->failure = true; | |
591 | ||
a16e1123 LP |
592 | m->control_command_id = MOUNT_EXEC_UNMOUNT; |
593 | m->control_command = m->exec_command + MOUNT_EXEC_UNMOUNT; | |
e537352b LP |
594 | |
595 | if ((r = exec_command_set( | |
a16e1123 | 596 | m->control_command, |
e537352b LP |
597 | "/bin/umount", |
598 | m->where, | |
599 | NULL)) < 0) | |
600 | goto fail; | |
601 | ||
a16e1123 | 602 | mount_unwatch_control_pid(m); |
5e94833f | 603 | |
a16e1123 | 604 | if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0) |
e537352b LP |
605 | goto fail; |
606 | ||
607 | mount_set_state(m, MOUNT_UNMOUNTING); | |
608 | ||
609 | return; | |
610 | ||
611 | fail: | |
9e2f7c11 | 612 | log_warning("%s failed to run umount exectuable: %s", UNIT(m)->meta.id, strerror(-r)); |
e537352b LP |
613 | mount_enter_mounted(m, false); |
614 | } | |
615 | ||
8d567588 | 616 | static void mount_enter_mounting(Mount *m) { |
e537352b LP |
617 | int r; |
618 | ||
619 | assert(m); | |
620 | ||
a16e1123 LP |
621 | m->control_command_id = MOUNT_EXEC_MOUNT; |
622 | m->control_command = m->exec_command + MOUNT_EXEC_MOUNT; | |
e537352b LP |
623 | |
624 | if (m->from_fragment) | |
625 | r = exec_command_set( | |
a16e1123 | 626 | m->control_command, |
e537352b LP |
627 | "/bin/mount", |
628 | m->parameters_fragment.what, | |
629 | m->where, | |
630 | "-t", m->parameters_fragment.fstype, | |
8d567588 | 631 | m->parameters_fragment.options ? "-o" : NULL, m->parameters_fragment.options, |
e537352b LP |
632 | NULL); |
633 | else if (m->from_etc_fstab) | |
634 | r = exec_command_set( | |
a16e1123 | 635 | m->control_command, |
e537352b LP |
636 | "/bin/mount", |
637 | m->where, | |
638 | NULL); | |
639 | else | |
640 | r = -ENOENT; | |
641 | ||
642 | if (r < 0) | |
643 | goto fail; | |
644 | ||
a16e1123 | 645 | mount_unwatch_control_pid(m); |
5e94833f | 646 | |
a16e1123 | 647 | if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0) |
e537352b LP |
648 | goto fail; |
649 | ||
650 | mount_set_state(m, MOUNT_MOUNTING); | |
651 | ||
652 | return; | |
653 | ||
654 | fail: | |
9e2f7c11 | 655 | log_warning("%s failed to run mount exectuable: %s", UNIT(m)->meta.id, strerror(-r)); |
e537352b LP |
656 | mount_enter_dead(m, false); |
657 | } | |
658 | ||
8d567588 | 659 | static void mount_enter_mounting_done(Mount *m) { |
e537352b LP |
660 | assert(m); |
661 | ||
e537352b LP |
662 | mount_set_state(m, MOUNT_MOUNTING_DONE); |
663 | } | |
664 | ||
665 | static void mount_enter_remounting(Mount *m, bool success) { | |
e537352b LP |
666 | int r; |
667 | ||
668 | assert(m); | |
669 | ||
670 | if (!success) | |
671 | m->failure = true; | |
672 | ||
a16e1123 LP |
673 | m->control_command_id = MOUNT_EXEC_REMOUNT; |
674 | m->control_command = m->exec_command + MOUNT_EXEC_REMOUNT; | |
e537352b LP |
675 | |
676 | if (m->from_fragment) { | |
677 | char *buf = NULL; | |
678 | const char *o; | |
679 | ||
680 | if (m->parameters_fragment.options) { | |
681 | if (!(buf = strappend("remount,", m->parameters_fragment.options))) { | |
682 | r = -ENOMEM; | |
683 | goto fail; | |
684 | } | |
685 | ||
686 | o = buf; | |
687 | } else | |
688 | o = "remount"; | |
689 | ||
690 | r = exec_command_set( | |
a16e1123 | 691 | m->control_command, |
e537352b LP |
692 | "/bin/mount", |
693 | m->parameters_fragment.what, | |
694 | m->where, | |
695 | "-t", m->parameters_fragment.fstype, | |
696 | "-o", o, | |
697 | NULL); | |
698 | ||
699 | free(buf); | |
700 | } else if (m->from_etc_fstab) | |
701 | r = exec_command_set( | |
a16e1123 | 702 | m->control_command, |
e537352b LP |
703 | "/bin/mount", |
704 | m->where, | |
705 | "-o", "remount", | |
706 | NULL); | |
707 | else | |
708 | r = -ENOENT; | |
709 | ||
710 | if (r < 0) { | |
711 | r = -ENOMEM; | |
712 | goto fail; | |
713 | } | |
714 | ||
a16e1123 | 715 | mount_unwatch_control_pid(m); |
5e94833f | 716 | |
a16e1123 | 717 | if ((r = mount_spawn(m, m->control_command, &m->control_pid)) < 0) |
e537352b LP |
718 | goto fail; |
719 | ||
720 | mount_set_state(m, MOUNT_REMOUNTING); | |
721 | ||
722 | return; | |
723 | ||
724 | fail: | |
725 | mount_enter_mounted(m, false); | |
726 | } | |
727 | ||
728 | static int mount_start(Unit *u) { | |
729 | Mount *m = MOUNT(u); | |
730 | ||
731 | assert(m); | |
732 | ||
733 | /* We cannot fulfill this request right now, try again later | |
734 | * please! */ | |
735 | if (m->state == MOUNT_UNMOUNTING || | |
736 | m->state == MOUNT_UNMOUNTING_SIGTERM || | |
737 | m->state == MOUNT_UNMOUNTING_SIGKILL) | |
738 | return -EAGAIN; | |
739 | ||
740 | /* Already on it! */ | |
741 | if (m->state == MOUNT_MOUNTING || | |
742 | m->state == MOUNT_MOUNTING_SIGTERM || | |
743 | m->state == MOUNT_MOUNTING_SIGKILL) | |
744 | return 0; | |
745 | ||
746 | assert(m->state == MOUNT_DEAD || m->state == MOUNT_MAINTAINANCE); | |
747 | ||
748 | m->failure = false; | |
8d567588 | 749 | mount_enter_mounting(m); |
e537352b LP |
750 | return 0; |
751 | } | |
752 | ||
753 | static int mount_stop(Unit *u) { | |
754 | Mount *m = MOUNT(u); | |
755 | ||
756 | assert(m); | |
757 | ||
758 | /* Cann't do this right now. */ | |
759 | if (m->state == MOUNT_MOUNTING || | |
760 | m->state == MOUNT_MOUNTING_DONE || | |
761 | m->state == MOUNT_MOUNTING_SIGTERM || | |
762 | m->state == MOUNT_MOUNTING_SIGKILL || | |
763 | m->state == MOUNT_REMOUNTING || | |
764 | m->state == MOUNT_REMOUNTING_SIGTERM || | |
765 | m->state == MOUNT_REMOUNTING_SIGKILL) | |
766 | return -EAGAIN; | |
767 | ||
768 | /* Already on it */ | |
769 | if (m->state == MOUNT_UNMOUNTING || | |
770 | m->state == MOUNT_UNMOUNTING_SIGKILL || | |
771 | m->state == MOUNT_UNMOUNTING_SIGTERM) | |
772 | return 0; | |
773 | ||
774 | assert(m->state == MOUNT_MOUNTED); | |
775 | ||
776 | mount_enter_unmounting(m, true); | |
777 | return 0; | |
778 | } | |
779 | ||
780 | static int mount_reload(Unit *u) { | |
781 | Mount *m = MOUNT(u); | |
782 | ||
783 | assert(m); | |
784 | ||
785 | if (m->state == MOUNT_MOUNTING_DONE) | |
786 | return -EAGAIN; | |
787 | ||
788 | assert(m->state == MOUNT_MOUNTED); | |
789 | ||
790 | mount_enter_remounting(m, true); | |
791 | return 0; | |
792 | } | |
793 | ||
a16e1123 LP |
794 | static int mount_serialize(Unit *u, FILE *f, FDSet *fds) { |
795 | Mount *m = MOUNT(u); | |
796 | ||
797 | assert(m); | |
798 | assert(f); | |
799 | assert(fds); | |
800 | ||
801 | unit_serialize_item(u, f, "state", mount_state_to_string(m->state)); | |
802 | unit_serialize_item(u, f, "failure", yes_no(m->failure)); | |
803 | ||
804 | if (m->control_pid > 0) | |
805 | unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) m->control_pid); | |
806 | ||
807 | if (m->control_command_id >= 0) | |
808 | unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id)); | |
809 | ||
810 | return 0; | |
811 | } | |
812 | ||
813 | static int mount_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
814 | Mount *m = MOUNT(u); | |
815 | int r; | |
816 | ||
817 | assert(u); | |
818 | assert(key); | |
819 | assert(value); | |
820 | assert(fds); | |
821 | ||
822 | if (streq(key, "state")) { | |
823 | MountState state; | |
824 | ||
825 | if ((state = mount_state_from_string(value)) < 0) | |
826 | log_debug("Failed to parse state value %s", value); | |
827 | else | |
828 | m->deserialized_state = state; | |
829 | } else if (streq(key, "failure")) { | |
830 | int b; | |
831 | ||
832 | if ((b = parse_boolean(value)) < 0) | |
833 | log_debug("Failed to parse failure value %s", value); | |
834 | else | |
835 | m->failure = b || m->failure; | |
836 | ||
837 | } else if (streq(key, "control-pid")) { | |
838 | unsigned pid; | |
839 | ||
840 | if ((r = safe_atou(value, &pid)) < 0 || pid <= 0) | |
841 | log_debug("Failed to parse control-pid value %s", value); | |
842 | else | |
843 | m->control_pid = (pid_t) pid; | |
844 | } else if (streq(key, "control-command")) { | |
845 | MountExecCommand id; | |
846 | ||
847 | if ((id = mount_exec_command_from_string(value)) < 0) | |
848 | log_debug("Failed to parse exec-command value %s", value); | |
849 | else { | |
850 | m->control_command_id = id; | |
851 | m->control_command = m->exec_command + id; | |
852 | } | |
853 | ||
854 | } else | |
855 | log_debug("Unknown serialization key '%s'", key); | |
856 | ||
857 | return 0; | |
858 | } | |
859 | ||
e537352b LP |
860 | static UnitActiveState mount_active_state(Unit *u) { |
861 | assert(u); | |
862 | ||
863 | return state_translation_table[MOUNT(u)->state]; | |
864 | } | |
865 | ||
10a94420 LP |
866 | static const char *mount_sub_state_to_string(Unit *u) { |
867 | assert(u); | |
868 | ||
a16e1123 | 869 | return mount_state_to_string(MOUNT(u)->state); |
10a94420 LP |
870 | } |
871 | ||
701cc384 LP |
872 | static bool mount_check_gc(Unit *u) { |
873 | Mount *m = MOUNT(u); | |
874 | ||
875 | assert(m); | |
876 | ||
877 | return m->from_etc_fstab || m->from_proc_self_mountinfo; | |
878 | } | |
879 | ||
e537352b LP |
880 | static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
881 | Mount *m = MOUNT(u); | |
882 | bool success; | |
883 | ||
884 | assert(m); | |
885 | assert(pid >= 0); | |
886 | ||
887 | success = code == CLD_EXITED && status == 0; | |
888 | m->failure = m->failure || !success; | |
889 | ||
890 | assert(m->control_pid == pid); | |
e537352b LP |
891 | m->control_pid = 0; |
892 | ||
a16e1123 LP |
893 | if (m->control_command) { |
894 | exec_status_fill(&m->control_command->exec_status, pid, code, status); | |
895 | m->control_command = NULL; | |
896 | m->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
897 | } | |
898 | ||
9e2f7c11 | 899 | log_debug("%s control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); |
e537352b LP |
900 | |
901 | /* Note that mount(8) returning and the kernel sending us a | |
902 | * mount table change event might happen out-of-order. If an | |
903 | * operation succeed we assume the kernel will follow soon too | |
904 | * and already change into the resulting state. If it fails | |
905 | * we check if the kernel still knows about the mount. and | |
906 | * change state accordingly. */ | |
907 | ||
908 | switch (m->state) { | |
909 | ||
910 | case MOUNT_MOUNTING: | |
911 | case MOUNT_MOUNTING_DONE: | |
912 | case MOUNT_MOUNTING_SIGKILL: | |
913 | case MOUNT_MOUNTING_SIGTERM: | |
914 | case MOUNT_REMOUNTING: | |
915 | case MOUNT_REMOUNTING_SIGKILL: | |
916 | case MOUNT_REMOUNTING_SIGTERM: | |
917 | ||
918 | if (success && m->from_proc_self_mountinfo) | |
919 | mount_enter_mounted(m, true); | |
920 | else if (m->from_proc_self_mountinfo) | |
921 | mount_enter_mounted(m, false); | |
922 | else | |
923 | mount_enter_dead(m, false); | |
924 | break; | |
925 | ||
926 | case MOUNT_UNMOUNTING: | |
927 | case MOUNT_UNMOUNTING_SIGKILL: | |
928 | case MOUNT_UNMOUNTING_SIGTERM: | |
929 | ||
930 | if (success) | |
931 | mount_enter_dead(m, true); | |
932 | else if (m->from_proc_self_mountinfo) | |
933 | mount_enter_mounted(m, false); | |
934 | else | |
935 | mount_enter_dead(m, false); | |
936 | break; | |
937 | ||
938 | default: | |
939 | assert_not_reached("Uh, control process died at wrong time."); | |
940 | } | |
941 | } | |
942 | ||
943 | static void mount_timer_event(Unit *u, uint64_t elapsed, Watch *w) { | |
944 | Mount *m = MOUNT(u); | |
945 | ||
946 | assert(m); | |
947 | assert(elapsed == 1); | |
948 | assert(w == &m->timer_watch); | |
949 | ||
950 | switch (m->state) { | |
951 | ||
952 | case MOUNT_MOUNTING: | |
953 | case MOUNT_MOUNTING_DONE: | |
9e2f7c11 | 954 | log_warning("%s mounting timed out. Stopping.", u->meta.id); |
e537352b LP |
955 | mount_enter_signal(m, MOUNT_MOUNTING_SIGTERM, false); |
956 | break; | |
957 | ||
958 | case MOUNT_REMOUNTING: | |
9e2f7c11 | 959 | log_warning("%s remounting timed out. Stopping.", u->meta.id); |
e537352b LP |
960 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGTERM, false); |
961 | break; | |
962 | ||
963 | case MOUNT_UNMOUNTING: | |
9e2f7c11 | 964 | log_warning("%s unmounting timed out. Stopping.", u->meta.id); |
e537352b LP |
965 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGTERM, false); |
966 | break; | |
967 | ||
968 | case MOUNT_MOUNTING_SIGTERM: | |
9e2f7c11 | 969 | log_warning("%s mounting timed out. Killing.", u->meta.id); |
e537352b LP |
970 | mount_enter_signal(m, MOUNT_MOUNTING_SIGKILL, false); |
971 | break; | |
972 | ||
973 | case MOUNT_REMOUNTING_SIGTERM: | |
9e2f7c11 | 974 | log_warning("%s remounting timed out. Killing.", u->meta.id); |
e537352b LP |
975 | mount_enter_signal(m, MOUNT_REMOUNTING_SIGKILL, false); |
976 | break; | |
977 | ||
978 | case MOUNT_UNMOUNTING_SIGTERM: | |
9e2f7c11 | 979 | log_warning("%s unmounting timed out. Killing.", u->meta.id); |
e537352b LP |
980 | mount_enter_signal(m, MOUNT_UNMOUNTING_SIGKILL, false); |
981 | break; | |
982 | ||
983 | case MOUNT_MOUNTING_SIGKILL: | |
984 | case MOUNT_REMOUNTING_SIGKILL: | |
985 | case MOUNT_UNMOUNTING_SIGKILL: | |
9e2f7c11 | 986 | log_warning("%s mount process still around after SIGKILL. Ignoring.", u->meta.id); |
e537352b LP |
987 | |
988 | if (m->from_proc_self_mountinfo) | |
989 | mount_enter_mounted(m, false); | |
990 | else | |
991 | mount_enter_dead(m, false); | |
992 | break; | |
993 | ||
994 | default: | |
995 | assert_not_reached("Timeout at wrong time."); | |
996 | } | |
997 | } | |
998 | ||
999 | static int mount_add_one( | |
1000 | Manager *m, | |
1001 | const char *what, | |
1002 | const char *where, | |
1003 | const char *options, | |
1004 | const char *fstype, | |
1005 | bool from_proc_self_mountinfo, | |
1006 | bool set_flags) { | |
b08d03ff LP |
1007 | int r; |
1008 | Unit *u; | |
1009 | bool delete; | |
e537352b LP |
1010 | char *e, *w = NULL, *o = NULL, *f = NULL; |
1011 | MountParameters *mp; | |
b08d03ff | 1012 | |
f50e0a01 | 1013 | assert(m); |
b08d03ff LP |
1014 | assert(what); |
1015 | assert(where); | |
e537352b LP |
1016 | assert(options); |
1017 | assert(fstype); | |
1018 | ||
1019 | assert(!set_flags || from_proc_self_mountinfo); | |
1020 | ||
1021 | /* Ignore API mount points. They should never be referenced in | |
1022 | * dependencies ever. */ | |
1023 | if (mount_point_is_api(where)) | |
1024 | return 0; | |
b08d03ff | 1025 | |
8d567588 LP |
1026 | if (streq(fstype, "autofs")) |
1027 | return 0; | |
1028 | ||
b08d03ff LP |
1029 | /* probably some kind of swap, which we don't cover for now */ |
1030 | if (where[0] != '/') | |
1031 | return 0; | |
1032 | ||
a16e1123 | 1033 | if (!(e = unit_name_from_path(where, ".mount"))) |
b08d03ff LP |
1034 | return -ENOMEM; |
1035 | ||
1036 | if (!(u = manager_get_unit(m, e))) { | |
1037 | delete = true; | |
1038 | ||
1039 | if (!(u = unit_new(m))) { | |
1040 | free(e); | |
1041 | return -ENOMEM; | |
1042 | } | |
1043 | ||
1044 | r = unit_add_name(u, e); | |
1045 | free(e); | |
1046 | ||
1047 | if (r < 0) | |
1048 | goto fail; | |
1049 | ||
e537352b | 1050 | if (!(MOUNT(u)->where = strdup(where))) { |
07b0b134 ML |
1051 | r = -ENOMEM; |
1052 | goto fail; | |
1053 | } | |
f50e0a01 LP |
1054 | |
1055 | if ((r = unit_set_description(u, where)) < 0) | |
b08d03ff | 1056 | goto fail; |
ef734fd6 | 1057 | |
f94ea366 | 1058 | unit_add_to_load_queue(u); |
b08d03ff LP |
1059 | } else { |
1060 | delete = false; | |
1061 | free(e); | |
1062 | } | |
1063 | ||
e537352b LP |
1064 | if (!(w = strdup(what)) || |
1065 | !(o = strdup(options)) || | |
1066 | !(f = strdup(fstype))) { | |
1067 | r = -ENOMEM; | |
1068 | goto fail; | |
1069 | } | |
1070 | ||
1071 | if (from_proc_self_mountinfo) { | |
1072 | mp = &MOUNT(u)->parameters_proc_self_mountinfo; | |
1073 | ||
1074 | if (set_flags) { | |
1075 | MOUNT(u)->is_mounted = true; | |
1076 | MOUNT(u)->just_mounted = !MOUNT(u)->from_proc_self_mountinfo; | |
1077 | MOUNT(u)->just_changed = !streq_ptr(MOUNT(u)->parameters_proc_self_mountinfo.options, o); | |
1078 | } | |
ef734fd6 | 1079 | |
f50e0a01 | 1080 | MOUNT(u)->from_proc_self_mountinfo = true; |
e537352b | 1081 | |
ef734fd6 | 1082 | } else { |
e537352b LP |
1083 | mp = &MOUNT(u)->parameters_etc_fstab; |
1084 | ||
f50e0a01 | 1085 | MOUNT(u)->from_etc_fstab = true; |
ef734fd6 | 1086 | } |
f50e0a01 | 1087 | |
e537352b LP |
1088 | free(mp->what); |
1089 | mp->what = w; | |
b08d03ff | 1090 | |
e537352b LP |
1091 | free(mp->options); |
1092 | mp->options = o; | |
1093 | ||
1094 | free(mp->fstype); | |
1095 | mp->fstype = f; | |
b08d03ff | 1096 | |
c1e1601e LP |
1097 | unit_add_to_dbus_queue(u); |
1098 | ||
b08d03ff LP |
1099 | return 0; |
1100 | ||
1101 | fail: | |
e537352b LP |
1102 | free(w); |
1103 | free(o); | |
1104 | free(f); | |
1105 | ||
b08d03ff LP |
1106 | if (delete && u) |
1107 | unit_free(u); | |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | static char *fstab_node_to_udev_node(char *p) { | |
1113 | char *dn, *t; | |
1114 | int r; | |
1115 | ||
1116 | /* FIXME: to follow udev's logic 100% we need to leave valid | |
1117 | * UTF8 chars unescaped */ | |
1118 | ||
1119 | if (startswith(p, "LABEL=")) { | |
1120 | ||
f50e0a01 | 1121 | if (!(t = xescape(p+6, "/ "))) |
b08d03ff LP |
1122 | return NULL; |
1123 | ||
f50e0a01 | 1124 | r = asprintf(&dn, "/dev/disk/by-label/%s", t); |
b08d03ff LP |
1125 | free(t); |
1126 | ||
1127 | if (r < 0) | |
1128 | return NULL; | |
1129 | ||
1130 | return dn; | |
1131 | } | |
1132 | ||
1133 | if (startswith(p, "UUID=")) { | |
1134 | ||
f50e0a01 | 1135 | if (!(t = xescape(p+5, "/ "))) |
b08d03ff LP |
1136 | return NULL; |
1137 | ||
f50e0a01 | 1138 | r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t)); |
b08d03ff LP |
1139 | free(t); |
1140 | ||
1141 | if (r < 0) | |
1142 | return NULL; | |
1143 | ||
1144 | return dn; | |
1145 | } | |
1146 | ||
1147 | return strdup(p); | |
1148 | } | |
1149 | ||
07b0b134 ML |
1150 | static int mount_find_pri(char *options) { |
1151 | char *end, *pri; | |
1152 | unsigned long r; | |
1153 | ||
1154 | if (!(pri = mount_test_option(options, "pri="))) | |
1155 | return 0; | |
1156 | ||
1157 | pri += 4; | |
1158 | ||
1159 | errno = 0; | |
1160 | r = strtoul(pri, &end, 10); | |
1161 | ||
1162 | if (errno != 0) | |
1163 | return -errno; | |
1164 | ||
1165 | if (end == pri || (*end != ',' && *end != 0)) | |
1166 | return -EINVAL; | |
1167 | ||
1168 | return (int) r; | |
1169 | } | |
1170 | ||
e537352b | 1171 | static int mount_load_etc_fstab(Manager *m) { |
b08d03ff LP |
1172 | FILE *f; |
1173 | int r; | |
1174 | struct mntent* me; | |
1175 | ||
1176 | assert(m); | |
1177 | ||
1178 | errno = 0; | |
1179 | if (!(f = setmntent("/etc/fstab", "r"))) | |
1180 | return -errno; | |
1181 | ||
1182 | while ((me = getmntent(f))) { | |
1183 | char *where, *what; | |
1184 | ||
1185 | if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) { | |
1186 | r = -ENOMEM; | |
1187 | goto finish; | |
1188 | } | |
1189 | ||
1190 | if (!(where = strdup(me->mnt_dir))) { | |
1191 | free(what); | |
1192 | r = -ENOMEM; | |
1193 | goto finish; | |
1194 | } | |
1195 | ||
1196 | if (what[0] == '/') | |
1197 | path_kill_slashes(what); | |
1198 | ||
1199 | if (where[0] == '/') | |
1200 | path_kill_slashes(where); | |
1201 | ||
07b0b134 ML |
1202 | if (streq(me->mnt_type, "swap")) { |
1203 | int pri; | |
1204 | ||
1205 | if ((pri = mount_find_pri(me->mnt_opts)) < 0) | |
1206 | r = pri; | |
1207 | else | |
1208 | r = swap_add_one(m, | |
1209 | what, | |
1210 | !!mount_test_option(me->mnt_opts, MNTOPT_NOAUTO), | |
1211 | pri, | |
1212 | false); | |
1213 | } else | |
1214 | r = mount_add_one(m, what, where, me->mnt_opts, me->mnt_type, false, false); | |
1215 | ||
b08d03ff LP |
1216 | free(what); |
1217 | free(where); | |
1218 | ||
1219 | if (r < 0) | |
1220 | goto finish; | |
1221 | } | |
1222 | ||
1223 | r = 0; | |
1224 | finish: | |
1225 | ||
1226 | endmntent(f); | |
1227 | return r; | |
1228 | } | |
1229 | ||
ef734fd6 | 1230 | static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) { |
b08d03ff | 1231 | int r; |
e537352b | 1232 | char *device, *path, *options, *fstype, *d, *p; |
b08d03ff LP |
1233 | |
1234 | assert(m); | |
1235 | ||
ef734fd6 | 1236 | rewind(m->proc_self_mountinfo); |
b08d03ff LP |
1237 | |
1238 | for (;;) { | |
1239 | int k; | |
e537352b LP |
1240 | |
1241 | device = path = options = fstype = d = p = NULL; | |
b08d03ff | 1242 | |
ef734fd6 | 1243 | if ((k = fscanf(m->proc_self_mountinfo, |
b08d03ff LP |
1244 | "%*s " /* (1) mount id */ |
1245 | "%*s " /* (2) parent id */ | |
1246 | "%*s " /* (3) major:minor */ | |
1247 | "%*s " /* (4) root */ | |
1248 | "%ms " /* (5) mount point */ | |
e537352b | 1249 | "%ms" /* (6) mount options */ |
b08d03ff LP |
1250 | "%*[^-]" /* (7) optional fields */ |
1251 | "- " /* (8) seperator */ | |
e537352b | 1252 | "%ms " /* (9) file system type */ |
ef734fd6 LP |
1253 | "%ms" /* (10) mount source */ |
1254 | "%*[^\n]", /* some rubbish at the end */ | |
b08d03ff | 1255 | &path, |
e537352b LP |
1256 | &options, |
1257 | &fstype, | |
1258 | &device)) != 4) { | |
b08d03ff | 1259 | |
ef734fd6 LP |
1260 | if (k == EOF) |
1261 | break; | |
b08d03ff | 1262 | |
e537352b LP |
1263 | r = -EBADMSG; |
1264 | goto finish; | |
b08d03ff LP |
1265 | } |
1266 | ||
e537352b LP |
1267 | if (!(d = cunescape(device)) || |
1268 | !(p = cunescape(path))) { | |
1269 | r = -ENOMEM; | |
1270 | goto finish; | |
b08d03ff | 1271 | } |
b08d03ff | 1272 | |
e537352b LP |
1273 | if ((r = mount_add_one(m, d, p, options, fstype, true, set_flags)) < 0) |
1274 | goto finish; | |
b08d03ff | 1275 | |
e537352b LP |
1276 | free(device); |
1277 | free(path); | |
1278 | free(options); | |
1279 | free(fstype); | |
b08d03ff LP |
1280 | free(d); |
1281 | free(p); | |
b08d03ff LP |
1282 | } |
1283 | ||
e537352b LP |
1284 | r = 0; |
1285 | ||
1286 | finish: | |
1287 | free(device); | |
1288 | free(path); | |
1289 | free(options); | |
1290 | free(fstype); | |
1291 | free(d); | |
1292 | free(p); | |
1293 | ||
1294 | return r; | |
1295 | } | |
1296 | ||
1297 | static void mount_shutdown(Manager *m) { | |
1298 | assert(m); | |
1299 | ||
a16e1123 | 1300 | if (m->proc_self_mountinfo) { |
e537352b | 1301 | fclose(m->proc_self_mountinfo); |
a16e1123 LP |
1302 | m->proc_self_mountinfo = NULL; |
1303 | } | |
b08d03ff LP |
1304 | } |
1305 | ||
1306 | static int mount_enumerate(Manager *m) { | |
1307 | int r; | |
ef734fd6 | 1308 | struct epoll_event ev; |
b08d03ff LP |
1309 | assert(m); |
1310 | ||
a16e1123 LP |
1311 | if (!m->proc_self_mountinfo) { |
1312 | if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"))) | |
1313 | return -errno; | |
ef734fd6 | 1314 | |
a16e1123 LP |
1315 | m->mount_watch.type = WATCH_MOUNT; |
1316 | m->mount_watch.fd = fileno(m->proc_self_mountinfo); | |
ef734fd6 | 1317 | |
a16e1123 LP |
1318 | zero(ev); |
1319 | ev.events = EPOLLERR; | |
1320 | ev.data.ptr = &m->mount_watch; | |
ef734fd6 | 1321 | |
a16e1123 LP |
1322 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0) |
1323 | return -errno; | |
1324 | } | |
ef734fd6 | 1325 | |
e537352b | 1326 | if ((r = mount_load_etc_fstab(m)) < 0) |
b08d03ff LP |
1327 | goto fail; |
1328 | ||
ef734fd6 | 1329 | if ((r = mount_load_proc_self_mountinfo(m, false)) < 0) |
b08d03ff LP |
1330 | goto fail; |
1331 | ||
1332 | return 0; | |
1333 | ||
1334 | fail: | |
1335 | mount_shutdown(m); | |
1336 | return r; | |
5cb5a6ff LP |
1337 | } |
1338 | ||
ef734fd6 LP |
1339 | void mount_fd_event(Manager *m, int events) { |
1340 | Meta *meta; | |
1341 | int r; | |
1342 | ||
1343 | assert(m); | |
f94ea366 | 1344 | assert(events == EPOLLERR); |
ef734fd6 LP |
1345 | |
1346 | /* The manager calls this for every fd event happening on the | |
1347 | * /proc/self/mountinfo file, which informs us about mounting | |
1348 | * table changes */ | |
1349 | ||
1350 | if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) { | |
1351 | log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno)); | |
e537352b LP |
1352 | |
1353 | /* Reset flags, just in case, for later calls */ | |
1354 | LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) { | |
1355 | Mount *mount = (Mount*) meta; | |
1356 | ||
1357 | mount->is_mounted = mount->just_mounted = mount->just_changed = false; | |
1358 | } | |
1359 | ||
ef734fd6 LP |
1360 | return; |
1361 | } | |
1362 | ||
1363 | manager_dispatch_load_queue(m); | |
1364 | ||
1365 | LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) { | |
1366 | Mount *mount = (Mount*) meta; | |
1367 | ||
e537352b LP |
1368 | if (!mount->is_mounted) { |
1369 | /* This has just been unmounted. */ | |
1370 | ||
ef734fd6 | 1371 | mount->from_proc_self_mountinfo = false; |
e537352b LP |
1372 | |
1373 | switch (mount->state) { | |
1374 | ||
1375 | case MOUNT_MOUNTED: | |
1376 | mount_enter_dead(mount, true); | |
1377 | break; | |
1378 | ||
1379 | default: | |
1380 | mount_set_state(mount, mount->state); | |
1381 | break; | |
1382 | ||
1383 | } | |
1384 | ||
1385 | } else if (mount->just_mounted || mount->just_changed) { | |
1386 | ||
1387 | /* New or changed entrymount */ | |
1388 | ||
1389 | switch (mount->state) { | |
1390 | ||
1391 | case MOUNT_DEAD: | |
1392 | case MOUNT_MAINTAINANCE: | |
1393 | mount_enter_mounted(mount, true); | |
1394 | break; | |
1395 | ||
1396 | case MOUNT_MOUNTING: | |
8d567588 | 1397 | mount_enter_mounting_done(mount); |
e537352b LP |
1398 | break; |
1399 | ||
1400 | default: | |
1401 | /* Nothing really changed, but let's | |
1402 | * issue an notification call | |
1403 | * nonetheless, in case somebody is | |
1404 | * waiting for this. (e.g. file system | |
1405 | * ro/rw remounts.) */ | |
1406 | mount_set_state(mount, mount->state); | |
1407 | break; | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | /* Reset the flags for later calls */ | |
1412 | mount->is_mounted = mount->just_mounted = mount->just_changed = false; | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | int mount_path_is_mounted(Manager *m, const char* path) { | |
1417 | char *t; | |
1418 | int r; | |
1419 | ||
1420 | assert(m); | |
1421 | assert(path); | |
1422 | ||
1423 | if (path[0] != '/') | |
1424 | return 1; | |
1425 | ||
1426 | if (!(t = strdup(path))) | |
1427 | return -ENOMEM; | |
1428 | ||
1429 | path_kill_slashes(t); | |
1430 | ||
1431 | for (;;) { | |
1432 | char *e, *slash; | |
1433 | Unit *u; | |
1434 | ||
a16e1123 | 1435 | if (!(e = unit_name_from_path(t, ".mount"))) { |
e537352b LP |
1436 | r = -ENOMEM; |
1437 | goto finish; | |
1438 | } | |
1439 | ||
1440 | u = manager_get_unit(m, e); | |
1441 | free(e); | |
1442 | ||
1443 | if (u && | |
1444 | (MOUNT(u)->from_etc_fstab || MOUNT(u)->from_fragment) && | |
1445 | MOUNT(u)->state != MOUNT_MOUNTED) { | |
1446 | r = 0; | |
1447 | goto finish; | |
ef734fd6 LP |
1448 | } |
1449 | ||
e537352b LP |
1450 | assert_se(slash = strrchr(t, '/')); |
1451 | ||
1452 | if (slash == t) { | |
1453 | r = 1; | |
1454 | goto finish; | |
1455 | } | |
1456 | ||
1457 | *slash = 0; | |
ef734fd6 | 1458 | } |
e537352b LP |
1459 | |
1460 | r = 1; | |
1461 | ||
1462 | finish: | |
1463 | free(t); | |
1464 | return r; | |
ef734fd6 LP |
1465 | } |
1466 | ||
a16e1123 LP |
1467 | static const char* const mount_state_table[_MOUNT_STATE_MAX] = { |
1468 | [MOUNT_DEAD] = "dead", | |
1469 | [MOUNT_MOUNTING] = "mounting", | |
1470 | [MOUNT_MOUNTING_DONE] = "mounting-done", | |
1471 | [MOUNT_MOUNTED] = "mounted", | |
1472 | [MOUNT_REMOUNTING] = "remounting", | |
1473 | [MOUNT_UNMOUNTING] = "unmounting", | |
1474 | [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm", | |
1475 | [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill", | |
1476 | [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm", | |
1477 | [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill", | |
1478 | [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm", | |
1479 | [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill", | |
1480 | [MOUNT_MAINTAINANCE] = "maintainance" | |
1481 | }; | |
1482 | ||
1483 | DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState); | |
1484 | ||
1485 | static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = { | |
1486 | [MOUNT_EXEC_MOUNT] = "ExecMount", | |
1487 | [MOUNT_EXEC_UNMOUNT] = "ExecUnmount", | |
1488 | [MOUNT_EXEC_REMOUNT] = "ExecRemount", | |
1489 | }; | |
1490 | ||
1491 | DEFINE_STRING_TABLE_LOOKUP(mount_exec_command, MountExecCommand); | |
1492 | ||
87f0e418 | 1493 | const UnitVTable mount_vtable = { |
5cb5a6ff LP |
1494 | .suffix = ".mount", |
1495 | ||
e537352b | 1496 | .no_alias = true, |
9e2f7c11 | 1497 | .no_instances = true, |
c497c7a9 | 1498 | .no_isolate = true, |
e537352b LP |
1499 | |
1500 | .init = mount_init, | |
1501 | .load = mount_load, | |
034c6ed7 | 1502 | .done = mount_done, |
e537352b | 1503 | |
f50e0a01 LP |
1504 | .coldplug = mount_coldplug, |
1505 | ||
034c6ed7 | 1506 | .dump = mount_dump, |
5cb5a6ff | 1507 | |
e537352b LP |
1508 | .start = mount_start, |
1509 | .stop = mount_stop, | |
1510 | .reload = mount_reload, | |
1511 | ||
a16e1123 LP |
1512 | .serialize = mount_serialize, |
1513 | .deserialize_item = mount_deserialize_item, | |
1514 | ||
f50e0a01 | 1515 | .active_state = mount_active_state, |
10a94420 | 1516 | .sub_state_to_string = mount_sub_state_to_string, |
b08d03ff | 1517 | |
701cc384 LP |
1518 | .check_gc = mount_check_gc, |
1519 | ||
e537352b LP |
1520 | .sigchld_event = mount_sigchld_event, |
1521 | .timer_event = mount_timer_event, | |
1522 | ||
4139c1b2 LP |
1523 | .bus_message_handler = bus_mount_message_handler, |
1524 | ||
f50e0a01 LP |
1525 | .enumerate = mount_enumerate, |
1526 | .shutdown = mount_shutdown | |
5cb5a6ff | 1527 | }; |