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