chiark / gitweb /
unit: introduce unit_load_related_unit()
[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 enum {
36         MOUNT_WHAT,
37         MOUNT_WHERE,
38         MOUNT_TYPE,
39         MOUNT_OPTIONS,
40         MOUNT_SKIP
41 };
42
43 static const char *table[] = {
44         "proc",    "/proc",             "proc",     NULL,
45         "sysfs",   "/sys",              "sysfs",    NULL,
46         "devtmps", "/dev",              "devtmpfs", "mode=755",
47         "tmpfs",   "/dev/shm",          "tmpfs",    "mode=1777",
48         "devpts",  "/dev/pts",          "devpts",   NULL,
49         "cgroup",  "/cgroup/debug",     "cgroup",   "debug",
50         "debugfs", "/sys/kernel/debug", "debugfs",  NULL,
51         NULL
52 };
53
54 static int is_mount_point(const char *t) {
55         struct stat a, b;
56         char *copy;
57
58         if (lstat(t, &a) < 0) {
59
60                 if (errno == ENOENT)
61                         return 0;
62
63                 return -errno;
64         }
65
66         if (!(copy = strdup(t)))
67                 return -ENOMEM;
68
69         if (lstat(dirname(copy), &b) < 0) {
70                 free(copy);
71                 return -errno;
72         }
73
74         free(copy);
75
76         return a.st_dev != b.st_dev;
77
78 }
79
80 static int mount_one(const char *t[]) {
81         int r;
82
83         assert(t);
84
85         if ((r = is_mount_point(t[MOUNT_WHERE])) < 0)
86                 return r;
87
88         if (r > 0)
89                 return 0;
90
91         /* The access mode here doesn't really matter too much, since
92          * the mounted file system will take precedence anyway. */
93         mkdir(t[MOUNT_WHERE], 0755);
94
95         log_debug("Mounting %s to %s of type %s with options %s.",
96                   t[MOUNT_WHAT],
97                   t[MOUNT_WHERE],
98                   t[MOUNT_TYPE],
99                   strna(t[MOUNT_OPTIONS]));
100
101         if (mount(t[MOUNT_WHAT],
102                   t[MOUNT_WHERE],
103                   t[MOUNT_TYPE],
104                   0,
105                   t[MOUNT_OPTIONS]) < 0) {
106                 log_error("Failed to mount %s: %s", t[MOUNT_WHERE], strerror(errno));
107                 return -errno;
108         }
109
110         return 0;
111 }
112
113 int mount_setup(void) {
114         int r;
115         const char **t;
116
117         for (t = table; *t; t += MOUNT_SKIP)
118                 if ((r = mount_one(t)) < 0)
119                         return r;
120
121         return 0;
122 }