]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "base-filesystem.h"
16 #include "nulstr-util.h"
17 #include "string-util.h"
18 #include "umask-util.h"
19 #include "user-util.h"
21 typedef struct BaseFilesystem
{
29 static const BaseFilesystem table
[] = {
30 { "bin", 0, "usr/bin\0", NULL
},
31 { "lib", 0, "usr/lib\0", NULL
},
32 { "root", 0755, NULL
, NULL
, true },
33 { "sbin", 0, "usr/sbin\0", NULL
},
34 { "usr", 0755, NULL
, NULL
},
35 { "var", 0755, NULL
, NULL
},
36 { "etc", 0755, NULL
, NULL
},
37 { "proc", 0755, NULL
, NULL
, true },
38 { "sys", 0755, NULL
, NULL
, true },
39 { "dev", 0755, NULL
, NULL
, true },
40 #if defined(__i386__) || defined(__x86_64__)
41 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
42 "usr/lib64\0", "ld-linux-x86-64.so.2" },
46 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
47 _cleanup_close_
int fd
= -1;
51 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
53 return log_error_errno(errno
, "Failed to open root file system: %m");
55 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
56 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
59 if (table
[i
].target
) {
60 const char *target
= NULL
, *s
;
62 /* check if one of the targets exists */
63 NULSTR_FOREACH(s
, table
[i
].target
) {
64 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
67 /* check if a specific file exists at the target path */
68 if (table
[i
].exists
) {
69 _cleanup_free_
char *p
= NULL
;
71 p
= strjoin(s
, "/", table
[i
].exists
);
75 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
86 r
= symlinkat(target
, fd
, table
[i
].dir
);
87 if (r
< 0 && errno
!= EEXIST
)
88 return log_error_errno(errno
, "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
90 if (uid_is_valid(uid
) || gid_is_valid(gid
)) {
91 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
92 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
99 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
100 if (r
< 0 && errno
!= EEXIST
) {
101 log_full_errno(table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
102 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
104 if (!table
[i
].ignore_failure
)
110 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
111 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
112 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);