]>
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" | |
c9af1080 LP |
32 | #include "macro.h" |
33 | #include "util.h" | |
8e274523 | 34 | |
ca714c0e LP |
35 | typedef struct MountPoint { |
36 | const char *what; | |
37 | const char *where; | |
38 | const char *type; | |
39 | const char *options; | |
40 | unsigned long flags; | |
2076ca54 | 41 | bool fatal; |
ca714c0e LP |
42 | } MountPoint; |
43 | ||
44 | static const MountPoint mount_table[] = { | |
f7d01394 LP |
45 | { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true }, |
46 | { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true }, | |
47 | { "devtmps", "/dev", "devtmpfs", "mode=755", MS_NOSUID, true }, | |
48 | { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NOEXEC|MS_NODEV, true }, | |
49 | { "devpts", "/dev/pts", "devpts", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false }, | |
50 | { "cgroup", "/cgroup/debug", "cgroup", "debug", MS_NOSUID|MS_NOEXEC|MS_NODEV, true }, | |
51 | { "debugfs", "/sys/kernel/debug", "debugfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false }, | |
52 | { "binfmt_misc", "/proc/sys/fs/binfmt_misc", "binfmt_misc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, false }, | |
8e274523 LP |
53 | }; |
54 | ||
dad08730 LP |
55 | bool mount_point_is_api(const char *path) { |
56 | unsigned i; | |
57 | ||
58 | /* Checks if this mount point is considered "API", and hence | |
59 | * should be ignored */ | |
60 | ||
ca714c0e LP |
61 | for (i = 0; i < ELEMENTSOF(mount_table); i ++) |
62 | if (path_startswith(path, mount_table[i].where)) | |
dad08730 LP |
63 | return true; |
64 | ||
ac86f50d | 65 | return path_startswith(path, "/cgroup/"); |
dad08730 LP |
66 | } |
67 | ||
ca714c0e | 68 | static int mount_one(const MountPoint *p) { |
8e274523 LP |
69 | int r; |
70 | ||
ca714c0e | 71 | assert(p); |
8e274523 | 72 | |
8d567588 | 73 | if ((r = path_is_mount_point(p->where)) < 0) |
8e274523 LP |
74 | return r; |
75 | ||
76 | if (r > 0) | |
77 | return 0; | |
78 | ||
a04f58d6 LP |
79 | /* The access mode here doesn't really matter too much, since |
80 | * the mounted file system will take precedence anyway. */ | |
ca714c0e | 81 | mkdir_p(p->where, 0755); |
a04f58d6 | 82 | |
8e274523 | 83 | log_debug("Mounting %s to %s of type %s with options %s.", |
ca714c0e LP |
84 | p->what, |
85 | p->where, | |
86 | p->type, | |
87 | strna(p->options)); | |
88 | ||
89 | if (mount(p->what, | |
90 | p->where, | |
91 | p->type, | |
92 | p->flags, | |
93 | p->options) < 0) { | |
94 | log_error("Failed to mount %s: %s", p->where, strerror(errno)); | |
2076ca54 | 95 | return p->fatal ? -errno : 0; |
8e274523 LP |
96 | } |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
2076ca54 LP |
101 | static int mount_cgroup_controllers(void) { |
102 | int r; | |
103 | FILE *f; | |
104 | char buf [256]; | |
105 | ||
106 | /* Mount all available cgroup controllers. */ | |
107 | ||
108 | if (!(f = fopen("/proc/cgroups", "re"))) | |
109 | return -ENOENT; | |
110 | ||
111 | /* Ignore the header line */ | |
112 | fgets(buf, sizeof(buf), f); | |
113 | ||
114 | for (;;) { | |
115 | MountPoint p; | |
116 | char *controller, *where; | |
117 | ||
118 | if (fscanf(f, "%ms %*i %*i %*i", &controller) != 1) { | |
119 | ||
120 | if (feof(f)) | |
121 | break; | |
122 | ||
123 | log_error("Failed to parse /proc/cgroups."); | |
124 | r = -EIO; | |
125 | goto finish; | |
126 | } | |
127 | ||
128 | if (asprintf(&where, "/cgroup/%s", controller) < 0) { | |
129 | free(controller); | |
130 | r = -ENOMEM; | |
131 | goto finish; | |
132 | } | |
133 | ||
134 | zero(p); | |
135 | p.what = "cgroup"; | |
136 | p.where = where; | |
137 | p.type = "cgroup"; | |
138 | p.options = controller; | |
139 | p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV; | |
140 | p.fatal = false; | |
141 | ||
142 | r = mount_one(&p); | |
143 | free(controller); | |
144 | free(where); | |
145 | ||
146 | if (r < 0) | |
147 | goto finish; | |
148 | } | |
149 | ||
150 | r = 0; | |
151 | ||
152 | finish: | |
153 | fclose(f); | |
154 | ||
155 | return r; | |
156 | } | |
157 | ||
8e274523 LP |
158 | int mount_setup(void) { |
159 | int r; | |
dad08730 | 160 | unsigned i; |
8e274523 | 161 | |
ca714c0e LP |
162 | for (i = 0; i < ELEMENTSOF(mount_table); i ++) |
163 | if ((r = mount_one(mount_table+i)) < 0) | |
8e274523 LP |
164 | return r; |
165 | ||
2076ca54 | 166 | return mount_cgroup_controllers(); |
8e274523 | 167 | } |