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