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