]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/import-common.c
CODING_STYLE: document order in which to #include headers
[thirdparty/systemd.git] / src / import / import-common.c
CommitLineData
b6e676ce
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <sys/prctl.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#include "util.h"
27#include "btrfs-util.h"
28#include "capability.h"
29#include "import-common.h"
30
31int import_make_read_only_fd(int fd) {
32 int r;
33
34 assert(fd >= 0);
35
36 /* First, let's make this a read-only subvolume if it refers
37 * to a subvolume */
38 r = btrfs_subvol_set_read_only_fd(fd, true);
39 if (r == -ENOTTY || r == -ENOTDIR || r == -EINVAL) {
40 struct stat st;
41
42 /* This doesn't refer to a subvolume, or the file
43 * system isn't even btrfs. In that, case fall back to
44 * chmod()ing */
45
46 r = fstat(fd, &st);
47 if (r < 0)
48 return log_error_errno(errno, "Failed to stat temporary image: %m");
49
50 /* Drop "w" flag */
51 if (fchmod(fd, st.st_mode & 07555) < 0)
52 return log_error_errno(errno, "Failed to chmod() final image: %m");
53
54 return 0;
55
56 } else if (r < 0)
57 return log_error_errno(r, "Failed to make subvolume read-only: %m");
58
59 return 0;
60}
61
62int import_make_read_only(const char *path) {
63 _cleanup_close_ int fd = 1;
64
65 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
66 if (fd < 0)
67 return log_error_errno(errno, "Failed to open %s: %m", path);
68
69 return import_make_read_only_fd(fd);
70}
71
587fec42 72int import_fork_tar_x(const char *path, pid_t *ret) {
b6e676ce
LP
73 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
74 pid_t pid;
75 int r;
76
77 assert(path);
78 assert(ret);
79
80 if (pipe2(pipefd, O_CLOEXEC) < 0)
81 return log_error_errno(errno, "Failed to create pipe for tar: %m");
82
83 pid = fork();
84 if (pid < 0)
85 return log_error_errno(errno, "Failed to fork off tar: %m");
86
87 if (pid == 0) {
88 int null_fd;
89 uint64_t retain =
90 (1ULL << CAP_CHOWN) |
91 (1ULL << CAP_FOWNER) |
92 (1ULL << CAP_FSETID) |
93 (1ULL << CAP_MKNOD) |
94 (1ULL << CAP_SETFCAP) |
95 (1ULL << CAP_DAC_OVERRIDE);
96
97 /* Child */
98
99 reset_all_signal_handlers();
100 reset_signal_mask();
101 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
102
103 pipefd[1] = safe_close(pipefd[1]);
104
105 if (dup2(pipefd[0], STDIN_FILENO) != STDIN_FILENO) {
106 log_error_errno(errno, "Failed to dup2() fd: %m");
107 _exit(EXIT_FAILURE);
108 }
109
110 if (pipefd[0] != STDIN_FILENO)
111 pipefd[0] = safe_close(pipefd[0]);
112
113 null_fd = open("/dev/null", O_WRONLY|O_NOCTTY);
114 if (null_fd < 0) {
115 log_error_errno(errno, "Failed to open /dev/null: %m");
116 _exit(EXIT_FAILURE);
117 }
118
119 if (dup2(null_fd, STDOUT_FILENO) != STDOUT_FILENO) {
120 log_error_errno(errno, "Failed to dup2() fd: %m");
121 _exit(EXIT_FAILURE);
122 }
123
124 if (null_fd != STDOUT_FILENO)
125 null_fd = safe_close(null_fd);
126
127 fd_cloexec(STDIN_FILENO, false);
128 fd_cloexec(STDOUT_FILENO, false);
129 fd_cloexec(STDERR_FILENO, false);
130
131 if (unshare(CLONE_NEWNET) < 0)
132 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
133
134 r = capability_bounding_set_drop(~retain, true);
135 if (r < 0)
136 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
137
138 execlp("tar", "tar", "--numeric-owner", "-C", path, "-px", NULL);
139 log_error_errno(errno, "Failed to execute tar: %m");
140 _exit(EXIT_FAILURE);
141 }
142
143 pipefd[0] = safe_close(pipefd[0]);
144 r = pipefd[1];
145 pipefd[1] = -1;
146
147 *ret = pid;
148
149 return r;
150}
587fec42
LP
151
152int import_fork_tar_c(const char *path, pid_t *ret) {
153 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
154 pid_t pid;
155 int r;
156
157 assert(path);
158 assert(ret);
159
160 if (pipe2(pipefd, O_CLOEXEC) < 0)
161 return log_error_errno(errno, "Failed to create pipe for tar: %m");
162
163 pid = fork();
164 if (pid < 0)
165 return log_error_errno(errno, "Failed to fork off tar: %m");
166
167 if (pid == 0) {
168 int null_fd;
169 uint64_t retain = (1ULL << CAP_DAC_OVERRIDE);
170
171 /* Child */
172
173 reset_all_signal_handlers();
174 reset_signal_mask();
175 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
176
177 pipefd[0] = safe_close(pipefd[0]);
178
179 if (dup2(pipefd[1], STDOUT_FILENO) != STDOUT_FILENO) {
180 log_error_errno(errno, "Failed to dup2() fd: %m");
181 _exit(EXIT_FAILURE);
182 }
183
184 if (pipefd[1] != STDOUT_FILENO)
185 pipefd[1] = safe_close(pipefd[1]);
186
187 null_fd = open("/dev/null", O_RDONLY|O_NOCTTY);
188 if (null_fd < 0) {
189 log_error_errno(errno, "Failed to open /dev/null: %m");
190 _exit(EXIT_FAILURE);
191 }
192
193 if (dup2(null_fd, STDIN_FILENO) != STDIN_FILENO) {
194 log_error_errno(errno, "Failed to dup2() fd: %m");
195 _exit(EXIT_FAILURE);
196 }
197
198 if (null_fd != STDIN_FILENO)
199 null_fd = safe_close(null_fd);
200
201 fd_cloexec(STDIN_FILENO, false);
202 fd_cloexec(STDOUT_FILENO, false);
203 fd_cloexec(STDERR_FILENO, false);
204
205 if (unshare(CLONE_NEWNET) < 0)
206 log_error_errno(errno, "Failed to lock tar into network namespace, ignoring: %m");
207
208 r = capability_bounding_set_drop(~retain, true);
209 if (r < 0)
210 log_error_errno(r, "Failed to drop capabilities, ignoring: %m");
211
212 execlp("tar", "tar", "--sparse", "-C", path, "-c", ".", NULL);
213 log_error_errno(errno, "Failed to execute tar: %m");
214 _exit(EXIT_FAILURE);
215 }
216
217 pipefd[1] = safe_close(pipefd[1]);
218 r = pipefd[0];
219 pipefd[0] = -1;
220
221 *ret = pid;
222
223 return r;
224}