chiark / gitweb /
Classify processes from sessions into cgroups
[elogind.git] / src / shared / base-filesystem.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Kay Sievers
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <sys/stat.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "base-filesystem.h"
28 #include "log.h"
29 #include "macro.h"
30 #include "util.h"
31
32 typedef struct BaseFilesystem {
33         const char *dir;
34         mode_t mode;
35         const char *target;
36         const char *exists;
37 } BaseFilesystem;
38
39 static const BaseFilesystem table[] = {
40         { "bin",      0, "usr/bin\0",                  NULL },
41         { "lib",      0, "usr/lib\0",                  NULL },
42         { "root",  0755, NULL,                         NULL },
43         { "sbin",     0, "usr/sbin\0",                 NULL },
44         { "usr",   0755, NULL,                         NULL },
45         { "var",   0755, NULL,                         NULL },
46         { "etc",   0755, NULL,                         NULL },
47 #if defined(__i386__) || defined(__x86_64__)
48         { "lib64",    0, "usr/lib/x86_64-linux-gnu\0"
49                          "usr/lib64\0",                "ld-linux-x86-64.so.2" },
50 #endif
51 };
52
53 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
54         _cleanup_close_ int fd = -1;
55         unsigned i;
56         int r = 0;
57
58         fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
59         if (fd < 0)
60                 return log_error_errno(errno, "Failed to open root file system: %m");
61
62         for (i = 0; i < ELEMENTSOF(table); i ++) {
63                 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
64                         continue;
65
66                 if (table[i].target) {
67                         const char *target = NULL, *s;
68
69                         /* check if one of the targets exists */
70                         NULSTR_FOREACH(s, table[i].target) {
71                                 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
72                                         continue;
73
74                                 /* check if a specific file exists at the target path */
75                                 if (table[i].exists) {
76                                         _cleanup_free_ char *p = NULL;
77
78                                         p = strjoin(s, "/", table[i].exists, NULL);
79                                         if (!p)
80                                                 return log_oom();
81
82                                         if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
83                                                 continue;
84                                 }
85
86                                 target = s;
87                                 break;
88                         }
89
90                         if (!target)
91                                 continue;
92
93                         r = symlinkat(target, fd, table[i].dir);
94                         if (r < 0 && errno != EEXIST)
95                                 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
96
97                         if (uid != UID_INVALID || gid != UID_INVALID) {
98                                 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
99                                         return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
100                         }
101
102                         continue;
103                 }
104
105                 RUN_WITH_UMASK(0000)
106                         r = mkdirat(fd, table[i].dir, table[i].mode);
107                 if (r < 0 && errno != EEXIST)
108                         return log_error_errno(errno, "Failed to create directory at %s/%s: %m", root, table[i].dir);
109
110                 if (uid != UID_INVALID || gid != UID_INVALID) {
111                         if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
112                                 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
113                 }
114         }
115
116         return 0;
117 }