]>
Commit | Line | Data |
---|---|---|
8e274523 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
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 | ||
22 | #include <sys/mount.h> | |
23 | #include <errno.h> | |
24 | #include <sys/stat.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <libgen.h> | |
28 | #include <assert.h> | |
29 | ||
30 | #include "mount-setup.h" | |
31 | #include "log.h" | |
32 | ||
33 | enum { | |
34 | MOUNT_WHAT, | |
35 | MOUNT_WHERE, | |
36 | MOUNT_TYPE, | |
37 | MOUNT_OPTIONS, | |
38 | MOUNT_SKIP | |
39 | }; | |
40 | ||
41 | static const char *table[] = { | |
055ba4d6 LP |
42 | "proc", "/proc", "proc", "rw", |
43 | "sysfs", "/sys", "sysfs", "rw", | |
44 | "devtmps", "/dev", "devtmpfs", "mode=755", | |
45 | "tmpfs", "/dev/shm", "tmpfs", "mode=1777", | |
46 | "devpts", "/dev/pts", "devpts", "rw", | |
47 | "cgroup", "/cgroup/debug", "cgroup", "debug", | |
8e274523 LP |
48 | NULL |
49 | }; | |
50 | ||
51 | static int is_mount_point(const char *t) { | |
52 | struct stat a, b; | |
53 | char *copy; | |
54 | ||
55 | if (lstat(t, &a) < 0) { | |
56 | ||
57 | if (errno == ENOENT) | |
58 | return 0; | |
59 | ||
60 | return -errno; | |
61 | } | |
62 | ||
63 | if (!(copy = strdup(t))) | |
64 | return -ENOMEM; | |
65 | ||
66 | if (lstat(dirname(copy), &b) < 0) { | |
67 | free(copy); | |
68 | return -errno; | |
69 | } | |
70 | ||
71 | free(copy); | |
72 | ||
73 | return a.st_dev != b.st_dev; | |
74 | ||
75 | } | |
76 | ||
77 | static int mount_one(const char *t[]) { | |
78 | int r; | |
79 | ||
80 | assert(t); | |
81 | ||
82 | if ((r = is_mount_point(t[MOUNT_WHERE])) < 0) | |
83 | return r; | |
84 | ||
85 | if (r > 0) | |
86 | return 0; | |
87 | ||
88 | log_debug("Mounting %s to %s of type %s with options %s.", | |
89 | t[MOUNT_WHAT], | |
90 | t[MOUNT_WHERE], | |
91 | t[MOUNT_TYPE], | |
92 | t[MOUNT_OPTIONS]); | |
93 | ||
94 | if (mount(t[MOUNT_WHAT], | |
95 | t[MOUNT_WHERE], | |
96 | t[MOUNT_TYPE], | |
97 | 0, | |
98 | t[MOUNT_OPTIONS]) < 0) { | |
99 | log_error("Failed to mount %s: %s", t[MOUNT_WHERE], strerror(errno)); | |
100 | return -errno; | |
101 | } | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | int mount_setup(void) { | |
107 | int r; | |
108 | const char **t; | |
109 | ||
110 | for (t = table; *t; t += MOUNT_SKIP) | |
111 | if ((r = mount_one(t)) < 0) | |
112 | return r; | |
113 | ||
114 | return 0; | |
115 | } |