]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
2 This file is part of systemd.
4 Copyright 2014 Kay Sievers
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.
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.
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/>.
28 #include "alloc-util.h"
29 #include "base-filesystem.h"
33 #include "string-util.h"
34 #include "umask-util.h"
35 #include "user-util.h"
38 typedef struct BaseFilesystem
{
46 static const BaseFilesystem table
[] = {
47 { "bin", 0, "usr/bin\0", NULL
},
48 { "lib", 0, "usr/lib\0", NULL
},
49 { "root", 0755, NULL
, NULL
, true },
50 { "sbin", 0, "usr/sbin\0", NULL
},
51 { "usr", 0755, NULL
, NULL
},
52 { "var", 0755, NULL
, NULL
},
53 { "etc", 0755, NULL
, NULL
},
54 { "proc", 0755, NULL
, NULL
, true },
55 { "sys", 0755, NULL
, NULL
, true },
56 { "dev", 0755, NULL
, NULL
, true },
57 #if defined(__i386__) || defined(__x86_64__)
58 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
59 "usr/lib64\0", "ld-linux-x86-64.so.2" },
63 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
64 _cleanup_close_
int fd
= -1;
68 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
70 return log_error_errno(errno
, "Failed to open root file system: %m");
72 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
73 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
76 if (table
[i
].target
) {
77 const char *target
= NULL
, *s
;
79 /* check if one of the targets exists */
80 NULSTR_FOREACH(s
, table
[i
].target
) {
81 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
84 /* check if a specific file exists at the target path */
85 if (table
[i
].exists
) {
86 _cleanup_free_
char *p
= NULL
;
88 p
= strjoin(s
, "/", table
[i
].exists
);
92 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
103 r
= symlinkat(target
, fd
, table
[i
].dir
);
104 if (r
< 0 && errno
!= EEXIST
)
105 return log_error_errno(errno
, "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
107 if (uid_is_valid(uid
) || gid_is_valid(gid
)) {
108 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
109 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
116 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
117 if (r
< 0 && errno
!= EEXIST
) {
118 log_full_errno(table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
119 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
121 if (!table
[i
].ignore_failure
)
127 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
128 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
129 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);