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