]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/base-filesystem.c
nspawn: split out VolatileMode definitions
[thirdparty/systemd.git] / src / shared / base-filesystem.c
CommitLineData
3577de7a
KS
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>
a8fbdf54
TA
21#include <fcntl.h>
22#include <stdbool.h>
3577de7a 23#include <stdlib.h>
07630cea 24#include <sys/stat.h>
a8fbdf54 25#include <syslog.h>
3577de7a
KS
26#include <unistd.h>
27
b5efdb8a 28#include "alloc-util.h"
affb60b1
LP
29#include "base-filesystem.h"
30#include "fd-util.h"
3577de7a
KS
31#include "log.h"
32#include "macro.h"
07630cea 33#include "string-util.h"
affb60b1 34#include "umask-util.h"
ee104e11 35#include "user-util.h"
3577de7a 36#include "util.h"
3577de7a
KS
37
38typedef struct BaseFilesystem {
39 const char *dir;
40 mode_t mode;
41 const char *target;
3fd165e5 42 const char *exists;
6404ecc8 43 bool ignore_failure;
3577de7a
KS
44} BaseFilesystem;
45
46static const BaseFilesystem table[] = {
30d7c9c4
HH
47 { "bin", 0, "usr/bin\0", NULL },
48 { "lib", 0, "usr/lib\0", NULL },
6404ecc8 49 { "root", 0755, NULL, NULL, true },
30d7c9c4 50 { "sbin", 0, "usr/sbin\0", NULL },
03cfe0d5
LP
51 { "usr", 0755, NULL, NULL },
52 { "var", 0755, NULL, NULL },
53 { "etc", 0755, NULL, NULL },
e1ae9755 54#if defined(__i386__) || defined(__x86_64__)
30d7c9c4
HH
55 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
56 "usr/lib64\0", "ld-linux-x86-64.so.2" },
e1ae9755 57#endif
3577de7a
KS
58};
59
03cfe0d5 60int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
3577de7a
KS
61 _cleanup_close_ int fd = -1;
62 unsigned i;
a7f7d1bd 63 int r = 0;
3577de7a
KS
64
65 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
4a62c710
MS
66 if (fd < 0)
67 return log_error_errno(errno, "Failed to open root file system: %m");
3577de7a
KS
68
69 for (i = 0; i < ELEMENTSOF(table); i ++) {
6f4f8056
HH
70 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
71 continue;
72
3577de7a 73 if (table[i].target) {
6dc2852c 74 const char *target = NULL, *s;
e1ae9755
KS
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
3fd165e5
KS
81 /* check if a specific file exists at the target path */
82 if (table[i].exists) {
83 _cleanup_free_ char *p = NULL;
84
605405c6 85 p = strjoin(s, "/", table[i].exists);
3fd165e5
KS
86 if (!p)
87 return log_oom();
88
89 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
90 continue;
91 }
92
e1ae9755
KS
93 target = s;
94 break;
95 }
96
97 if (!target)
3577de7a
KS
98 continue;
99
e1ae9755 100 r = symlinkat(target, fd, table[i].dir);
4a62c710
MS
101 if (r < 0 && errno != EEXIST)
102 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
03cfe0d5
LP
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
3577de7a
KS
109 continue;
110 }
111
112 RUN_WITH_UMASK(0000)
113 r = mkdirat(fd, table[i].dir, table[i].mode);
6404ecc8
LP
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 }
03cfe0d5
LP
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 }
3577de7a
KS
126 }
127
128 return 0;
129}