]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
3c25aa534c9b2f17f8170cc23ea04a2498de8d52
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Kay Sievers
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "alloc-util.h"
30 #include "base-filesystem.h"
34 #include "string-util.h"
35 #include "umask-util.h"
36 #include "user-util.h"
39 typedef struct BaseFilesystem
{
47 static const BaseFilesystem table
[] = {
48 { "bin", 0, "usr/bin\0", NULL
},
49 { "lib", 0, "usr/lib\0", NULL
},
50 { "root", 0755, NULL
, NULL
, true },
51 { "sbin", 0, "usr/sbin\0", NULL
},
52 { "usr", 0755, NULL
, NULL
},
53 { "var", 0755, NULL
, NULL
},
54 { "etc", 0755, NULL
, NULL
},
55 { "proc", 0755, NULL
, NULL
, true },
56 { "sys", 0755, NULL
, NULL
, true },
57 { "dev", 0755, NULL
, NULL
, true },
58 #if defined(__i386__) || defined(__x86_64__)
59 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
60 "usr/lib64\0", "ld-linux-x86-64.so.2" },
64 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
65 _cleanup_close_
int fd
= -1;
69 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
71 return log_error_errno(errno
, "Failed to open root file system: %m");
73 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
74 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
77 if (table
[i
].target
) {
78 const char *target
= NULL
, *s
;
80 /* check if one of the targets exists */
81 NULSTR_FOREACH(s
, table
[i
].target
) {
82 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
85 /* check if a specific file exists at the target path */
86 if (table
[i
].exists
) {
87 _cleanup_free_
char *p
= NULL
;
89 p
= strjoin(s
, "/", table
[i
].exists
);
93 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
104 r
= symlinkat(target
, fd
, table
[i
].dir
);
105 if (r
< 0 && errno
!= EEXIST
)
106 return log_error_errno(errno
, "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
108 if (uid_is_valid(uid
) || gid_is_valid(gid
)) {
109 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
110 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
117 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
118 if (r
< 0 && errno
!= EEXIST
) {
119 log_full_errno(table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
120 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
122 if (!table
[i
].ignore_failure
)
128 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
129 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
130 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);