]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "string-util.h"
31 #include "base-filesystem.h"
34 typedef struct BaseFilesystem
{
42 static const BaseFilesystem table
[] = {
43 { "bin", 0, "usr/bin\0", NULL
},
44 { "lib", 0, "usr/lib\0", NULL
},
45 { "root", 0755, NULL
, NULL
, true },
46 { "sbin", 0, "usr/sbin\0", NULL
},
47 { "usr", 0755, NULL
, NULL
},
48 { "var", 0755, NULL
, NULL
},
49 { "etc", 0755, NULL
, NULL
},
50 #if defined(__i386__) || defined(__x86_64__)
51 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
52 "usr/lib64\0", "ld-linux-x86-64.so.2" },
56 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
57 _cleanup_close_
int fd
= -1;
61 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
63 return log_error_errno(errno
, "Failed to open root file system: %m");
65 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
66 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
69 if (table
[i
].target
) {
70 const char *target
= NULL
, *s
;
72 /* check if one of the targets exists */
73 NULSTR_FOREACH(s
, table
[i
].target
) {
74 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
77 /* check if a specific file exists at the target path */
78 if (table
[i
].exists
) {
79 _cleanup_free_
char *p
= NULL
;
81 p
= strjoin(s
, "/", table
[i
].exists
, NULL
);
85 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
96 r
= symlinkat(target
, fd
, table
[i
].dir
);
97 if (r
< 0 && errno
!= EEXIST
)
98 return log_error_errno(errno
, "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
100 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
101 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
102 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
109 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
110 if (r
< 0 && errno
!= EEXIST
) {
111 log_full_errno(table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
112 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
114 if (!table
[i
].ignore_failure
)
118 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
119 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
120 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);