]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "alloc-util.h"
12 #include "base-filesystem.h"
16 #include "nulstr-util.h"
17 #include "path-util.h"
18 #include "string-util.h"
19 #include "umask-util.h"
20 #include "user-util.h"
22 typedef struct BaseFilesystem
{
30 static const BaseFilesystem table
[] = {
31 { "bin", 0, "usr/bin\0", NULL
},
32 { "lib", 0, "usr/lib\0", NULL
},
33 { "root", 0755, NULL
, NULL
, true },
34 { "sbin", 0, "usr/sbin\0", NULL
},
35 { "usr", 0755, NULL
, NULL
},
36 { "var", 0755, NULL
, NULL
},
37 { "etc", 0755, NULL
, NULL
},
38 { "proc", 0755, NULL
, NULL
, true },
39 { "sys", 0755, NULL
, NULL
, true },
40 { "dev", 0755, NULL
, NULL
, true },
41 #if defined(__i386__) || defined(__x86_64__)
42 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
43 "usr/lib64\0", "ld-linux-x86-64.so.2" },
47 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
48 _cleanup_close_
int fd
= -1;
52 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
54 return log_error_errno(errno
, "Failed to open root file system: %m");
56 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
57 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
60 if (table
[i
].target
) {
61 const char *target
= NULL
, *s
;
63 /* check if one of the targets exists */
64 NULSTR_FOREACH(s
, table
[i
].target
) {
65 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
68 /* check if a specific file exists at the target path */
69 if (table
[i
].exists
) {
70 _cleanup_free_
char *p
= NULL
;
72 p
= path_join(s
, table
[i
].exists
);
76 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
87 if (symlinkat(target
, fd
, table
[i
].dir
) < 0) {
88 log_full_errno(IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
89 "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
91 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
97 if (uid_is_valid(uid
) || gid_is_valid(gid
)) {
98 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
99 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
106 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
108 log_full_errno(IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
109 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
111 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
117 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
118 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
119 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);