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