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