1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
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/>.
22 #include <sys/prctl.h>
27 #include "btrfs-util.h"
28 #include "capability.h"
29 #include "import-common.h"
31 int import_make_read_only_fd(int fd) {
36 /* First, let's make this a read-only subvolume if it refers
38 r = btrfs_subvol_set_read_only_fd(fd, true);
39 if (r == -ENOTTY || r == -ENOTDIR || r == -EINVAL) {
42 /* This doesn't refer to a subvolume, or the file
43 * system isn't even btrfs. In that, case fall back to
48 return log_error_errno(errno, "Failed to stat temporary image: %m");
51 if (fchmod(fd, st.st_mode & 07555) < 0)
52 return log_error_errno(errno, "Failed to chmod() final image: %m");
57 return log_error_errno(r, "Failed to make subvolume read-only: %m");
62 int import_make_read_only(const char *path) {
63 _cleanup_close_ int fd = 1;
65 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
67 return log_error_errno(errno, "Failed to open %s: %m", path);
69 return import_make_read_only_fd(fd);
72 int import_fork_tar(const char *path, pid_t *ret) {
73 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
80 if (pipe2(pipefd, O_CLOEXEC) < 0)
81 return log_error_errno(errno, "Failed to create pipe for tar: %m");
85 return log_error_errno(errno, "Failed to fork off tar: %m");
91 (1ULL << CAP_FOWNER) |
92 (1ULL << CAP_FSETID) |
94 (1ULL << CAP_SETFCAP) |
95 (1ULL << CAP_DAC_OVERRIDE);
99 reset_all_signal_handlers();
101 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
103 pipefd[1] = safe_close(pipefd[1]);
105 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
106 log_error_errno(errno, "Failed to dup2() fd: %m");
110 if (pipefd[0] != STDIN_FILENO)
111 pipefd[0] = safe_close(pipefd[0]);
113 null_fd = open("/dev/null", O_WRONLY|O_NOCTTY);
115 log_error_errno(errno, "Failed to open /dev/null: %m");
119 if (dup2(null_fd, STDOUT_FILENO) != STDOUT_FILENO) {
120 log_error_errno(errno, "Failed to dup2() fd: %m");
124 if (null_fd != STDOUT_FILENO)
125 null_fd = safe_close(null_fd);
127 fd_cloexec(STDIN_FILENO, false);
128 fd_cloexec(STDOUT_FILENO, false);
129 fd_cloexec(STDERR_FILENO, false);
131 if (unshare(CLONE_NEWNET) < 0)
132 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
134 r = capability_bounding_set_drop(~retain, true);
136 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
138 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", NULL);
139 log_error_errno(errno, "Failed to execute tar: %m");
143 pipefd[0] = safe_close(pipefd[0]);