]>
Commit | Line | Data |
---|---|---|
37ef15bb CB |
1 | /* liblxcapi |
2 | * | |
8d01f531 CB |
3 | * Copyright © 2019 Christian Brauner <christian.brauner@ubuntu.com>. |
4 | * Copyright © 2019 Canonical Ltd. | |
37ef15bb | 5 | * |
8d01f531 CB |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | * This library is distributed in the hope that it will be useful, | |
37ef15bb | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8d01f531 CB |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. | |
15 | ||
16 | * You should have received a copy of the GNU Lesser General Public License | |
17 | * along with this library; if not, write to the Free Software Foundation, | |
18 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
37ef15bb CB |
19 | */ |
20 | ||
d38dd64a CB |
21 | #ifndef _GNU_SOURCE |
22 | #define _GNU_SOURCE 1 | |
23 | #endif | |
37ef15bb CB |
24 | #include <errno.h> |
25 | #include <fcntl.h> | |
26 | #include <linux/magic.h> | |
27 | #include <stdlib.h> | |
28 | #include <sys/stat.h> | |
7c4d9466 | 29 | #include <sys/sendfile.h> |
37ef15bb CB |
30 | #include <sys/types.h> |
31 | ||
d38dd64a | 32 | #include "config.h" |
37ef15bb | 33 | #include "file_utils.h" |
37ef15bb | 34 | #include "macro.h" |
4aa90f60 | 35 | #include "memory_utils.h" |
6400238d | 36 | #include "string_utils.h" |
4aa90f60 | 37 | #include "utils.h" |
37ef15bb | 38 | |
37ef15bb CB |
39 | int lxc_write_to_file(const char *filename, const void *buf, size_t count, |
40 | bool add_newline, mode_t mode) | |
41 | { | |
42 | int fd, saved_errno; | |
43 | ssize_t ret; | |
44 | ||
45 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); | |
46 | if (fd < 0) | |
47 | return -1; | |
48 | ||
49 | ret = lxc_write_nointr(fd, buf, count); | |
50 | if (ret < 0) | |
51 | goto out_error; | |
52 | ||
53 | if ((size_t)ret != count) | |
54 | goto out_error; | |
55 | ||
56 | if (add_newline) { | |
57 | ret = lxc_write_nointr(fd, "\n", 1); | |
58 | if (ret != 1) | |
59 | goto out_error; | |
60 | } | |
61 | ||
62 | close(fd); | |
63 | return 0; | |
64 | ||
65 | out_error: | |
66 | saved_errno = errno; | |
67 | close(fd); | |
68 | errno = saved_errno; | |
69 | return -1; | |
70 | } | |
71 | ||
72 | int lxc_read_from_file(const char *filename, void *buf, size_t count) | |
73 | { | |
74 | int fd = -1, saved_errno; | |
75 | ssize_t ret; | |
76 | ||
77 | fd = open(filename, O_RDONLY | O_CLOEXEC); | |
78 | if (fd < 0) | |
79 | return -1; | |
80 | ||
81 | if (!buf || !count) { | |
82 | char buf2[100]; | |
83 | size_t count2 = 0; | |
84 | ||
85 | while ((ret = lxc_read_nointr(fd, buf2, 100)) > 0) | |
86 | count2 += ret; | |
87 | ||
88 | if (ret >= 0) | |
89 | ret = count2; | |
90 | } else { | |
91 | memset(buf, 0, count); | |
92 | ret = lxc_read_nointr(fd, buf, count); | |
93 | } | |
94 | ||
95 | saved_errno = errno; | |
96 | close(fd); | |
97 | errno = saved_errno; | |
98 | return ret; | |
99 | } | |
100 | ||
101 | ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) | |
102 | { | |
103 | ssize_t ret; | |
104 | again: | |
105 | ret = write(fd, buf, count); | |
106 | if (ret < 0 && errno == EINTR) | |
107 | goto again; | |
108 | ||
109 | return ret; | |
110 | } | |
111 | ||
28143f88 CB |
112 | ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags) |
113 | { | |
114 | ssize_t ret; | |
115 | again: | |
116 | ret = send(sockfd, buf, len, flags); | |
117 | if (ret < 0 && errno == EINTR) | |
118 | goto again; | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
37ef15bb CB |
123 | ssize_t lxc_read_nointr(int fd, void *buf, size_t count) |
124 | { | |
125 | ssize_t ret; | |
126 | again: | |
127 | ret = read(fd, buf, count); | |
128 | if (ret < 0 && errno == EINTR) | |
129 | goto again; | |
130 | ||
131 | return ret; | |
132 | } | |
133 | ||
de69edd1 CB |
134 | ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags) |
135 | { | |
136 | ssize_t ret; | |
137 | again: | |
138 | ret = recv(sockfd, buf, len, flags); | |
139 | if (ret < 0 && errno == EINTR) | |
140 | goto again; | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
37ef15bb CB |
145 | ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf) |
146 | { | |
147 | ssize_t ret; | |
148 | ||
149 | ret = lxc_read_nointr(fd, buf, count); | |
6509154d | 150 | if (ret < 0) |
37ef15bb CB |
151 | return ret; |
152 | ||
153 | if ((size_t)ret != count) | |
154 | return -1; | |
155 | ||
156 | if (expected_buf && memcmp(buf, expected_buf, count) != 0) { | |
157 | errno = EINVAL; | |
158 | return -1; | |
159 | } | |
160 | ||
6509154d | 161 | return 0; |
162 | } | |
163 | ||
164 | ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count, const void *expected_buf) | |
165 | { | |
166 | __do_close_prot_errno int fd = -EBADF; | |
167 | ||
168 | fd = open(path, O_RDONLY | O_CLOEXEC); | |
169 | if (fd < 0) | |
170 | return -1; | |
171 | ||
172 | return lxc_read_nointr_expect(fd, buf, count, expected_buf); | |
37ef15bb CB |
173 | } |
174 | ||
175 | bool file_exists(const char *f) | |
176 | { | |
177 | struct stat statbuf; | |
178 | ||
179 | return stat(f, &statbuf) == 0; | |
180 | } | |
181 | ||
182 | int print_to_file(const char *file, const char *content) | |
183 | { | |
184 | FILE *f; | |
185 | int ret = 0; | |
186 | ||
187 | f = fopen(file, "w"); | |
188 | if (!f) | |
189 | return -1; | |
190 | ||
191 | if (fprintf(f, "%s", content) != strlen(content)) | |
192 | ret = -1; | |
193 | ||
194 | fclose(f); | |
195 | return ret; | |
196 | } | |
197 | ||
198 | int is_dir(const char *path) | |
199 | { | |
200 | struct stat statbuf; | |
201 | int ret; | |
202 | ||
203 | ret = stat(path, &statbuf); | |
204 | if (ret == 0 && S_ISDIR(statbuf.st_mode)) | |
205 | return 1; | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | /* | |
211 | * Return the number of lines in file @fn, or -1 on error | |
212 | */ | |
213 | int lxc_count_file_lines(const char *fn) | |
214 | { | |
215 | FILE *f; | |
216 | char *line = NULL; | |
217 | size_t sz = 0; | |
218 | int n = 0; | |
219 | ||
220 | f = fopen_cloexec(fn, "r"); | |
221 | if (!f) | |
222 | return -1; | |
223 | ||
224 | while (getline(&line, &sz, f) != -1) { | |
225 | n++; | |
226 | } | |
227 | ||
228 | free(line); | |
229 | fclose(f); | |
230 | return n; | |
231 | } | |
232 | ||
233 | int lxc_make_tmpfile(char *template, bool rm) | |
234 | { | |
4aa90f60 CB |
235 | __do_close_prot_errno int fd = -EBADF; |
236 | int ret; | |
37ef15bb CB |
237 | mode_t msk; |
238 | ||
239 | msk = umask(0022); | |
240 | fd = mkstemp(template); | |
241 | umask(msk); | |
242 | if (fd < 0) | |
243 | return -1; | |
244 | ||
4aa90f60 CB |
245 | if (lxc_set_cloexec(fd)) |
246 | return -1; | |
247 | ||
37ef15bb | 248 | if (!rm) |
240fecd0 | 249 | return move_fd(fd); |
37ef15bb CB |
250 | |
251 | ret = unlink(template); | |
4aa90f60 | 252 | if (ret < 0) |
37ef15bb | 253 | return -1; |
37ef15bb | 254 | |
240fecd0 | 255 | return move_fd(fd); |
37ef15bb CB |
256 | } |
257 | ||
37ef15bb CB |
258 | bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val) |
259 | { | |
260 | return (fs->f_type == (fs_type_magic)magic_val); | |
261 | } | |
262 | ||
263 | bool has_fs_type(const char *path, fs_type_magic magic_val) | |
264 | { | |
37ef15bb CB |
265 | int ret; |
266 | struct statfs sb; | |
267 | ||
268 | ret = statfs(path, &sb); | |
269 | if (ret < 0) | |
270 | return false; | |
271 | ||
272 | return is_fs_type(&sb, magic_val); | |
273 | } | |
274 | ||
275 | bool fhas_fs_type(int fd, fs_type_magic magic_val) | |
276 | { | |
277 | int ret; | |
278 | struct statfs sb; | |
279 | ||
280 | ret = fstatfs(fd, &sb); | |
281 | if (ret < 0) | |
282 | return false; | |
283 | ||
284 | return is_fs_type(&sb, magic_val); | |
285 | } | |
286 | ||
287 | FILE *fopen_cloexec(const char *path, const char *mode) | |
288 | { | |
289 | int open_mode = 0; | |
290 | int step = 0; | |
291 | int fd; | |
292 | int saved_errno = 0; | |
293 | FILE *ret; | |
294 | ||
295 | if (!strncmp(mode, "r+", 2)) { | |
296 | open_mode = O_RDWR; | |
297 | step = 2; | |
298 | } else if (!strncmp(mode, "r", 1)) { | |
299 | open_mode = O_RDONLY; | |
300 | step = 1; | |
301 | } else if (!strncmp(mode, "w+", 2)) { | |
302 | open_mode = O_RDWR | O_TRUNC | O_CREAT; | |
303 | step = 2; | |
304 | } else if (!strncmp(mode, "w", 1)) { | |
305 | open_mode = O_WRONLY | O_TRUNC | O_CREAT; | |
306 | step = 1; | |
307 | } else if (!strncmp(mode, "a+", 2)) { | |
308 | open_mode = O_RDWR | O_CREAT | O_APPEND; | |
309 | step = 2; | |
310 | } else if (!strncmp(mode, "a", 1)) { | |
311 | open_mode = O_WRONLY | O_CREAT | O_APPEND; | |
312 | step = 1; | |
313 | } | |
314 | for (; mode[step]; step++) | |
315 | if (mode[step] == 'x') | |
316 | open_mode |= O_EXCL; | |
317 | open_mode |= O_CLOEXEC; | |
318 | ||
3e9bde7e | 319 | fd = open(path, open_mode, 0660); |
37ef15bb CB |
320 | if (fd < 0) |
321 | return NULL; | |
322 | ||
323 | ret = fdopen(fd, mode); | |
324 | saved_errno = errno; | |
325 | if (!ret) | |
326 | close(fd); | |
327 | errno = saved_errno; | |
328 | return ret; | |
329 | } | |
7c4d9466 CB |
330 | |
331 | ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count) | |
332 | { | |
333 | ssize_t ret; | |
334 | ||
335 | again: | |
336 | ret = sendfile(out_fd, in_fd, offset, count); | |
337 | if (ret < 0) { | |
338 | if (errno == EINTR) | |
339 | goto again; | |
340 | ||
341 | return -1; | |
342 | } | |
343 | ||
344 | return ret; | |
345 | } | |
6400238d CB |
346 | |
347 | char *file_to_buf(char *path, size_t *length) | |
348 | { | |
349 | int fd; | |
350 | char buf[PATH_MAX]; | |
351 | char *copy = NULL; | |
352 | ||
353 | if (!length) | |
354 | return NULL; | |
355 | ||
356 | fd = open(path, O_RDONLY | O_CLOEXEC); | |
357 | if (fd < 0) | |
358 | return NULL; | |
359 | ||
360 | *length = 0; | |
361 | for (;;) { | |
362 | int n; | |
363 | char *old = copy; | |
364 | ||
365 | n = lxc_read_nointr(fd, buf, sizeof(buf)); | |
366 | if (n < 0) | |
367 | goto on_error; | |
368 | if (!n) | |
369 | break; | |
370 | ||
371 | copy = must_realloc(old, (*length + n) * sizeof(*old)); | |
372 | memcpy(copy + *length, buf, n); | |
373 | *length += n; | |
374 | } | |
375 | ||
376 | close(fd); | |
377 | return copy; | |
378 | ||
379 | on_error: | |
380 | close(fd); | |
381 | free(copy); | |
382 | ||
383 | return NULL; | |
384 | } | |
4aa90f60 CB |
385 | |
386 | int fd_to_fd(int from, int to) | |
387 | { | |
388 | for (;;) { | |
389 | uint8_t buf[PATH_MAX]; | |
390 | uint8_t *p = buf; | |
391 | ssize_t bytes_to_write; | |
392 | ssize_t bytes_read; | |
393 | ||
394 | bytes_read = lxc_read_nointr(from, buf, sizeof buf); | |
395 | if (bytes_read < 0) | |
396 | return -1; | |
397 | if (bytes_read == 0) | |
398 | break; | |
399 | ||
400 | bytes_to_write = (size_t)bytes_read; | |
401 | do { | |
402 | ssize_t bytes_written; | |
403 | ||
404 | bytes_written = lxc_write_nointr(to, p, bytes_to_write); | |
405 | if (bytes_written < 0) | |
406 | return -1; | |
407 | ||
408 | bytes_to_write -= bytes_written; | |
409 | p += bytes_written; | |
410 | } while (bytes_to_write > 0); | |
411 | } | |
412 | ||
413 | return 0; | |
414 | } |