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