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