]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
Merge pull request #2569 from zonque/removals
[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 #if defined(__i386__) || defined(__x86_64__)
55 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
56 "usr/lib64\0", "ld-linux-x86-64.so.2" },
57 #endif
58 };
59
60 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
61 _cleanup_close_ int fd = -1;
62 unsigned i;
63 int r = 0;
64
65 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
66 if (fd < 0)
67 return log_error_errno(errno, "Failed to open root file system: %m");
68
69 for (i = 0; i < ELEMENTSOF(table); i ++) {
70 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
71 continue;
72
73 if (table[i].target) {
74 const char *target = NULL, *s;
75
76 /* check if one of the targets exists */
77 NULSTR_FOREACH(s, table[i].target) {
78 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
79 continue;
80
81 /* check if a specific file exists at the target path */
82 if (table[i].exists) {
83 _cleanup_free_ char *p = NULL;
84
85 p = strjoin(s, "/", table[i].exists, NULL);
86 if (!p)
87 return log_oom();
88
89 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
90 continue;
91 }
92
93 target = s;
94 break;
95 }
96
97 if (!target)
98 continue;
99
100 r = symlinkat(target, fd, table[i].dir);
101 if (r < 0 && errno != EEXIST)
102 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
103
104 if (uid != UID_INVALID || gid != UID_INVALID) {
105 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
106 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
107 }
108
109 continue;
110 }
111
112 RUN_WITH_UMASK(0000)
113 r = mkdirat(fd, table[i].dir, table[i].mode);
114 if (r < 0 && errno != EEXIST) {
115 log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
116 "Failed to create directory at %s/%s: %m", root, table[i].dir);
117
118 if (!table[i].ignore_failure)
119 return -errno;
120 }
121
122 if (uid != UID_INVALID || gid != UID_INVALID) {
123 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
124 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
125 }
126 }
127
128 return 0;
129 }