]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / shared / base-filesystem.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Kay Sievers
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <syslog.h>
12 #include <unistd.h>
13
14 #include "alloc-util.h"
15 #include "base-filesystem.h"
16 #include "fd-util.h"
17 #include "log.h"
18 #include "macro.h"
19 #include "string-util.h"
20 #include "umask-util.h"
21 #include "user-util.h"
22 #include "util.h"
23
24 typedef struct BaseFilesystem {
25 const char *dir;
26 mode_t mode;
27 const char *target;
28 const char *exists;
29 bool ignore_failure;
30 } BaseFilesystem;
31
32 static const BaseFilesystem table[] = {
33 { "bin", 0, "usr/bin\0", NULL },
34 { "lib", 0, "usr/lib\0", NULL },
35 { "root", 0755, NULL, NULL, true },
36 { "sbin", 0, "usr/sbin\0", NULL },
37 { "usr", 0755, NULL, NULL },
38 { "var", 0755, NULL, NULL },
39 { "etc", 0755, NULL, NULL },
40 { "proc", 0755, NULL, NULL, true },
41 { "sys", 0755, NULL, NULL, true },
42 { "dev", 0755, NULL, NULL, true },
43 #if defined(__i386__) || defined(__x86_64__)
44 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
45 "usr/lib64\0", "ld-linux-x86-64.so.2" },
46 #endif
47 };
48
49 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
50 _cleanup_close_ int fd = -1;
51 int r = 0;
52 size_t i;
53
54 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
55 if (fd < 0)
56 return log_error_errno(errno, "Failed to open root file system: %m");
57
58 for (i = 0; i < ELEMENTSOF(table); i ++) {
59 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
60 continue;
61
62 if (table[i].target) {
63 const char *target = NULL, *s;
64
65 /* check if one of the targets exists */
66 NULSTR_FOREACH(s, table[i].target) {
67 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
68 continue;
69
70 /* check if a specific file exists at the target path */
71 if (table[i].exists) {
72 _cleanup_free_ char *p = NULL;
73
74 p = strjoin(s, "/", table[i].exists);
75 if (!p)
76 return log_oom();
77
78 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
79 continue;
80 }
81
82 target = s;
83 break;
84 }
85
86 if (!target)
87 continue;
88
89 r = symlinkat(target, fd, table[i].dir);
90 if (r < 0 && errno != EEXIST)
91 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
92
93 if (uid_is_valid(uid) || gid_is_valid(gid)) {
94 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
95 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
96 }
97
98 continue;
99 }
100
101 RUN_WITH_UMASK(0000)
102 r = mkdirat(fd, table[i].dir, table[i].mode);
103 if (r < 0 && errno != EEXIST) {
104 log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
105 "Failed to create directory at %s/%s: %m", root, table[i].dir);
106
107 if (!table[i].ignore_failure)
108 return -errno;
109
110 continue;
111 }
112
113 if (uid != UID_INVALID || gid != UID_INVALID) {
114 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
115 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
116 }
117 }
118
119 return 0;
120 }