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