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