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