chiark / gitweb /
load-fragment: simplify fragment loading code by using macros
[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 } MountPoint;
42
43 static const MountPoint mount_table[] = {
44         { "proc",    "/proc",             "proc",     NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV },
45         { "sysfs",   "/sys",              "sysfs",    NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV },
46         { "devtmps", "/dev",              "devtmpfs", "mode=755",  MS_NOSUID },
47         { "tmpfs",   "/dev/shm",          "tmpfs",    "mode=1777", MS_NOSUID|MS_NOEXEC|MS_NODEV },
48         { "devpts",  "/dev/pts",          "devpts",   NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV },
49         { "cgroup",  "/cgroup/debug",     "cgroup",   "debug",     MS_NOSUID|MS_NOEXEC|MS_NODEV },
50         { "debugfs", "/sys/kernel/debug", "debugfs",  NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV }
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 false;
64 }
65
66 static int is_mount_point(const char *t) {
67         struct stat a, b;
68         char *copy;
69
70         if (lstat(t, &a) < 0) {
71
72                 if (errno == ENOENT)
73                         return 0;
74
75                 return -errno;
76         }
77
78         if (!(copy = strdup(t)))
79                 return -ENOMEM;
80
81         if (lstat(dirname(copy), &b) < 0) {
82                 free(copy);
83                 return -errno;
84         }
85
86         free(copy);
87
88         return a.st_dev != b.st_dev;
89 }
90
91 static int mount_one(const MountPoint *p) {
92         int r;
93
94         assert(p);
95
96         if ((r = is_mount_point(p->where)) < 0)
97                 return r;
98
99         if (r > 0)
100                 return 0;
101
102         /* The access mode here doesn't really matter too much, since
103          * the mounted file system will take precedence anyway. */
104         mkdir_p(p->where, 0755);
105
106         log_debug("Mounting %s to %s of type %s with options %s.",
107                   p->what,
108                   p->where,
109                   p->type,
110                   strna(p->options));
111
112         if (mount(p->what,
113                   p->where,
114                   p->type,
115                   p->flags,
116                   p->options) < 0) {
117                 log_error("Failed to mount %s: %s", p->where, strerror(errno));
118                 return -errno;
119         }
120
121         return 0;
122 }
123
124 int mount_setup(void) {
125         int r;
126         unsigned i;
127
128         for (i = 0; i < ELEMENTSOF(mount_table); i ++)
129                 if ((r = mount_one(mount_table+i)) < 0)
130                         return r;
131
132         return 0;
133 }