]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-common.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
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/>.
22 #include <sys/prctl.h>
26 #include "btrfs-util.h"
27 #include "capability-util.h"
29 #include "import-common.h"
30 #include "process-util.h"
31 #include "signal-util.h"
34 int import_make_read_only_fd(int fd
) {
39 /* First, let's make this a read-only subvolume if it refers
41 r
= btrfs_subvol_set_read_only_fd(fd
, true);
42 if (IN_SET(r
, -ENOTTY
, -ENOTDIR
, -EINVAL
)) {
45 /* This doesn't refer to a subvolume, or the file
46 * system isn't even btrfs. In that, case fall back to
51 return log_error_errno(errno
, "Failed to stat temporary image: %m");
54 if (fchmod(fd
, st
.st_mode
& 07555) < 0)
55 return log_error_errno(errno
, "Failed to chmod() final image: %m");
60 return log_error_errno(r
, "Failed to make subvolume read-only: %m");
65 int import_make_read_only(const char *path
) {
66 _cleanup_close_
int fd
= 1;
68 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
70 return log_error_errno(errno
, "Failed to open %s: %m", path
);
72 return import_make_read_only_fd(fd
);
75 int import_fork_tar_x(const char *path
, pid_t
*ret
) {
76 _cleanup_close_pair_
int pipefd
[2] = { -1, -1 };
83 if (pipe2(pipefd
, O_CLOEXEC
) < 0)
84 return log_error_errno(errno
, "Failed to create pipe for tar: %m");
86 r
= safe_fork("(tar)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
92 (1ULL << CAP_FOWNER
) |
93 (1ULL << CAP_FSETID
) |
95 (1ULL << CAP_SETFCAP
) |
96 (1ULL << CAP_DAC_OVERRIDE
);
100 pipefd
[1] = safe_close(pipefd
[1]);
102 r
= rearrange_stdio(pipefd
[0], -1, STDERR_FILENO
);
104 log_error_errno(r
, "Failed to rearrange stdin/stdout: %m");
108 if (unshare(CLONE_NEWNET
) < 0)
109 log_error_errno(errno
, "Failed to lock tar into network namespace, ignoring: %m");
111 r
= capability_bounding_set_drop(retain
, true);
113 log_error_errno(r
, "Failed to drop capabilities, ignoring: %m");
115 execlp("tar", "tar", "--numeric-owner", "-C", path
, "-px", "--xattrs", "--xattrs-include=*", NULL
);
116 log_error_errno(errno
, "Failed to execute tar: %m");
120 pipefd
[0] = safe_close(pipefd
[0]);
129 int import_fork_tar_c(const char *path
, pid_t
*ret
) {
130 _cleanup_close_pair_
int pipefd
[2] = { -1, -1 };
137 if (pipe2(pipefd
, O_CLOEXEC
) < 0)
138 return log_error_errno(errno
, "Failed to create pipe for tar: %m");
140 r
= safe_fork("(tar)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
144 uint64_t retain
= (1ULL << CAP_DAC_OVERRIDE
);
148 pipefd
[0] = safe_close(pipefd
[0]);
150 r
= rearrange_stdio(-1, pipefd
[1], STDERR_FILENO
);
152 log_error_errno(r
, "Failed to rearrange stdin/stdout: %m");
156 if (unshare(CLONE_NEWNET
) < 0)
157 log_error_errno(errno
, "Failed to lock tar into network namespace, ignoring: %m");
159 r
= capability_bounding_set_drop(retain
, true);
161 log_error_errno(r
, "Failed to drop capabilities, ignoring: %m");
163 execlp("tar", "tar", "-C", path
, "-c", "--xattrs", "--xattrs-include=*", ".", NULL
);
164 log_error_errno(errno
, "Failed to execute tar: %m");
168 pipefd
[1] = safe_close(pipefd
[1]);