]>
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 #if defined(__i386__) || defined(__x86_64__)
55 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
56 "usr/lib64\0", "ld-linux-x86-64.so.2" },
60 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
61 _cleanup_close_
int fd
= -1;
65 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
67 return log_error_errno(errno
, "Failed to open root file system: %m");
69 for (i
= 0; i
< ELEMENTSOF(table
); i
++) {
70 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
73 if (table
[i
].target
) {
74 const char *target
= NULL
, *s
;
76 /* check if one of the targets exists */
77 NULSTR_FOREACH(s
, table
[i
].target
) {
78 if (faccessat(fd
, s
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
81 /* check if a specific file exists at the target path */
82 if (table
[i
].exists
) {
83 _cleanup_free_
char *p
= NULL
;
85 p
= strjoin(s
, "/", table
[i
].exists
, NULL
);
89 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
100 r
= symlinkat(target
, fd
, table
[i
].dir
);
101 if (r
< 0 && errno
!= EEXIST
)
102 return log_error_errno(errno
, "Failed to create symlink at %s/%s: %m", root
, table
[i
].dir
);
104 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
105 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
106 return log_error_errno(errno
, "Failed to chown symlink at %s/%s: %m", root
, table
[i
].dir
);
113 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
114 if (r
< 0 && errno
!= EEXIST
) {
115 log_full_errno(table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
116 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
118 if (!table
[i
].ignore_failure
)
122 if (uid
!= UID_INVALID
|| gid
!= UID_INVALID
) {
123 if (fchownat(fd
, table
[i
].dir
, uid
, gid
, AT_SYMLINK_NOFOLLOW
) < 0)
124 return log_error_errno(errno
, "Failed to chown directory at %s/%s: %m", root
, table
[i
].dir
);