]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
Merge pull request #5420 from OpenDZ/tixxdz/namespace-fixes-v2
[thirdparty/systemd.git] / src / shared / base-filesystem.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Kay Sievers
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <syslog.h>
26 #include <unistd.h>
27
28 #include "alloc-util.h"
29 #include "base-filesystem.h"
30 #include "fd-util.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "string-util.h"
34 #include "umask-util.h"
35 #include "user-util.h"
36 #include "util.h"
37
38 typedef struct BaseFilesystem {
39 const char *dir;
40 mode_t mode;
41 const char *target;
42 const char *exists;
43 bool ignore_failure;
44 } BaseFilesystem;
45
46 static const BaseFilesystem table[] = {
47 { "bin", 0, "usr/bin\0", NULL },
48 { "lib", 0, "usr/lib\0", NULL },
49 { "root", 0755, NULL, NULL, true },
50 { "sbin", 0, "usr/sbin\0", NULL },
51 { "usr", 0755, NULL, NULL },
52 { "var", 0755, NULL, NULL },
53 { "etc", 0755, NULL, NULL },
54 { "proc", 0755, NULL, NULL, true },
55 { "sys", 0755, NULL, NULL, true },
56 { "dev", 0755, NULL, NULL, true },
57 #if defined(__i386__) || defined(__x86_64__)
58 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
59 "usr/lib64\0", "ld-linux-x86-64.so.2" },
60 #endif
61 };
62
63 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
64 _cleanup_close_ int fd = -1;
65 unsigned i;
66 int r = 0;
67
68 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
69 if (fd < 0)
70 return log_error_errno(errno, "Failed to open root file system: %m");
71
72 for (i = 0; i < ELEMENTSOF(table); i ++) {
73 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
74 continue;
75
76 if (table[i].target) {
77 const char *target = NULL, *s;
78
79 /* check if one of the targets exists */
80 NULSTR_FOREACH(s, table[i].target) {
81 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
82 continue;
83
84 /* check if a specific file exists at the target path */
85 if (table[i].exists) {
86 _cleanup_free_ char *p = NULL;
87
88 p = strjoin(s, "/", table[i].exists);
89 if (!p)
90 return log_oom();
91
92 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
93 continue;
94 }
95
96 target = s;
97 break;
98 }
99
100 if (!target)
101 continue;
102
103 r = symlinkat(target, fd, table[i].dir);
104 if (r < 0 && errno != EEXIST)
105 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
106
107 if (uid_is_valid(uid) || gid_is_valid(gid)) {
108 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
109 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
110 }
111
112 continue;
113 }
114
115 RUN_WITH_UMASK(0000)
116 r = mkdirat(fd, table[i].dir, table[i].mode);
117 if (r < 0 && errno != EEXIST) {
118 log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
119 "Failed to create directory at %s/%s: %m", root, table[i].dir);
120
121 if (!table[i].ignore_failure)
122 return -errno;
123
124 continue;
125 }
126
127 if (uid != UID_INVALID || gid != UID_INVALID) {
128 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
129 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
130 }
131 }
132
133 return 0;
134 }