chiark / gitweb /
systemctl: sort case-insensitively
[elogind.git] / src / mount-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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         { "devtmpfs", "/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,          false },
50         { "tmpfs",    "/cgroup",         "tmpfs",    "mode=755",          MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
51         { "cgroup",   "/cgroup/systemd", "cgroup",   "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
52 };
53
54 /* These are API file systems that might be mounted by other software,
55  * we just list them here so that we know that we should igore them */
56
57 static const char * const ignore_paths[] = {
58         "/selinux",
59         "/proc/bus/usb",
60         "/var/lib/nfs/rpc_pipefs",
61         "/proc/fs/nfsd"
62 };
63
64 bool mount_point_is_api(const char *path) {
65         unsigned i;
66
67         /* Checks if this mount point is considered "API", and hence
68          * should be ignored */
69
70         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
71                 if (path_startswith(path, mount_table[i].where))
72                         return true;
73
74         for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
75                 if (path_startswith(path, ignore_paths[i]))
76                         return true;
77
78         return path_startswith(path, "/cgroup/");
79 }
80
81 static int mount_one(const MountPoint *p) {
82         int r;
83
84         assert(p);
85
86         if ((r = path_is_mount_point(p->where)) < 0)
87                 return r;
88
89         if (r > 0)
90                 return 0;
91
92         /* The access mode here doesn't really matter too much, since
93          * the mounted file system will take precedence anyway. */
94         mkdir_p(p->where, 0755);
95
96         log_debug("Mounting %s to %s of type %s with options %s.",
97                   p->what,
98                   p->where,
99                   p->type,
100                   strna(p->options));
101
102         if (mount(p->what,
103                   p->where,
104                   p->type,
105                   p->flags,
106                   p->options) < 0) {
107                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
108                 return p->fatal ? -errno : 0;
109         }
110
111         return 0;
112 }
113
114 static int mount_cgroup_controllers(void) {
115         int r;
116         FILE *f;
117         char buf [256];
118
119         /* Mount all available cgroup controllers that are built into the kernel. */
120
121         if (!(f = fopen("/proc/cgroups", "re")))
122                 return -ENOENT;
123
124         /* Ignore the header line */
125         (void) fgets(buf, sizeof(buf), f);
126
127         for (;;) {
128                 MountPoint p;
129                 char *controller, *where;
130
131                 if (fscanf(f, "%ms %*i %*i %*i", &controller) != 1) {
132
133                         if (feof(f))
134                                 break;
135
136                         log_error("Failed to parse /proc/cgroups.");
137                         r = -EIO;
138                         goto finish;
139                 }
140
141                 if (asprintf(&where, "/cgroup/%s", controller) < 0) {
142                         free(controller);
143                         r = -ENOMEM;
144                         goto finish;
145                 }
146
147                 zero(p);
148                 p.what = "cgroup";
149                 p.where = where;
150                 p.type = "cgroup";
151                 p.options = controller;
152                 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
153                 p.fatal = false;
154
155                 r = mount_one(&p);
156                 free(controller);
157                 free(where);
158
159                 if (r < 0)
160                         goto finish;
161         }
162
163         r = 0;
164
165 finish:
166         fclose(f);
167
168         return r;
169 }
170
171 int mount_setup(void) {
172         int r;
173         unsigned i;
174
175         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
176                 if ((r = mount_one(mount_table+i)) < 0)
177                         return r;
178
179         return mount_cgroup_controllers();
180 }