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