]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/file_utils.c
cgroup: check for non-empty conf
[mirror_lxc.git] / src / lxc / file_utils.c
1 /* liblxcapi
2 *
3 * Copyright © 2019 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2019 Canonical Ltd.
5 *
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,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
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
19 */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <linux/magic.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/sendfile.h>
30 #include <sys/types.h>
31
32 #include "config.h"
33 #include "file_utils.h"
34 #include "macro.h"
35 #include "memory_utils.h"
36 #include "string_utils.h"
37 #include "utils.h"
38
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
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
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
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
145 ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
146 int flags)
147 {
148 ssize_t ret;
149 struct msghdr msg;
150
151 memset(&msg, 0, sizeof(msg));
152 msg.msg_iov = iov;
153 msg.msg_iovlen = iovlen;
154
155 again:
156 ret = recvmsg(sockfd, &msg, flags);
157 if (ret < 0 && errno == EINTR)
158 goto again;
159
160 return ret;
161 }
162
163 ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
164 {
165 ssize_t ret;
166
167 ret = lxc_read_nointr(fd, buf, count);
168 if (ret < 0)
169 return ret;
170
171 if ((size_t)ret != count)
172 return -1;
173
174 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
175 errno = EINVAL;
176 return -1;
177 }
178
179 return 0;
180 }
181
182 ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count, const void *expected_buf)
183 {
184 __do_close_prot_errno int fd = -EBADF;
185
186 fd = open(path, O_RDONLY | O_CLOEXEC);
187 if (fd < 0)
188 return -1;
189
190 return lxc_read_nointr_expect(fd, buf, count, expected_buf);
191 }
192
193 bool file_exists(const char *f)
194 {
195 struct stat statbuf;
196
197 return stat(f, &statbuf) == 0;
198 }
199
200 int print_to_file(const char *file, const char *content)
201 {
202 FILE *f;
203 int ret = 0;
204
205 f = fopen(file, "w");
206 if (!f)
207 return -1;
208
209 if (fprintf(f, "%s", content) != strlen(content))
210 ret = -1;
211
212 fclose(f);
213 return ret;
214 }
215
216 int is_dir(const char *path)
217 {
218 struct stat statbuf;
219 int ret;
220
221 ret = stat(path, &statbuf);
222 if (ret == 0 && S_ISDIR(statbuf.st_mode))
223 return 1;
224
225 return 0;
226 }
227
228 /*
229 * Return the number of lines in file @fn, or -1 on error
230 */
231 int lxc_count_file_lines(const char *fn)
232 {
233 FILE *f;
234 char *line = NULL;
235 size_t sz = 0;
236 int n = 0;
237
238 f = fopen_cloexec(fn, "r");
239 if (!f)
240 return -1;
241
242 while (getline(&line, &sz, f) != -1) {
243 n++;
244 }
245
246 free(line);
247 fclose(f);
248 return n;
249 }
250
251 int lxc_make_tmpfile(char *template, bool rm)
252 {
253 __do_close_prot_errno int fd = -EBADF;
254 int ret;
255 mode_t msk;
256
257 msk = umask(0022);
258 fd = mkstemp(template);
259 umask(msk);
260 if (fd < 0)
261 return -1;
262
263 if (lxc_set_cloexec(fd))
264 return -1;
265
266 if (!rm)
267 return move_fd(fd);
268
269 ret = unlink(template);
270 if (ret < 0)
271 return -1;
272
273 return move_fd(fd);
274 }
275
276 bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
277 {
278 return (fs->f_type == (fs_type_magic)magic_val);
279 }
280
281 bool has_fs_type(const char *path, fs_type_magic magic_val)
282 {
283 int ret;
284 struct statfs sb;
285
286 ret = statfs(path, &sb);
287 if (ret < 0)
288 return false;
289
290 return is_fs_type(&sb, magic_val);
291 }
292
293 bool fhas_fs_type(int fd, fs_type_magic magic_val)
294 {
295 int ret;
296 struct statfs sb;
297
298 ret = fstatfs(fd, &sb);
299 if (ret < 0)
300 return false;
301
302 return is_fs_type(&sb, magic_val);
303 }
304
305 FILE *fopen_cloexec(const char *path, const char *mode)
306 {
307 int open_mode = 0;
308 int step = 0;
309 int fd;
310 int saved_errno = 0;
311 FILE *ret;
312
313 if (!strncmp(mode, "r+", 2)) {
314 open_mode = O_RDWR;
315 step = 2;
316 } else if (!strncmp(mode, "r", 1)) {
317 open_mode = O_RDONLY;
318 step = 1;
319 } else if (!strncmp(mode, "w+", 2)) {
320 open_mode = O_RDWR | O_TRUNC | O_CREAT;
321 step = 2;
322 } else if (!strncmp(mode, "w", 1)) {
323 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
324 step = 1;
325 } else if (!strncmp(mode, "a+", 2)) {
326 open_mode = O_RDWR | O_CREAT | O_APPEND;
327 step = 2;
328 } else if (!strncmp(mode, "a", 1)) {
329 open_mode = O_WRONLY | O_CREAT | O_APPEND;
330 step = 1;
331 }
332 for (; mode[step]; step++)
333 if (mode[step] == 'x')
334 open_mode |= O_EXCL;
335 open_mode |= O_CLOEXEC;
336
337 fd = open(path, open_mode, 0660);
338 if (fd < 0)
339 return NULL;
340
341 ret = fdopen(fd, mode);
342 saved_errno = errno;
343 if (!ret)
344 close(fd);
345 errno = saved_errno;
346 return ret;
347 }
348
349 ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
350 {
351 ssize_t ret;
352
353 again:
354 ret = sendfile(out_fd, in_fd, offset, count);
355 if (ret < 0) {
356 if (errno == EINTR)
357 goto again;
358
359 return -1;
360 }
361
362 return ret;
363 }
364
365 char *file_to_buf(char *path, size_t *length)
366 {
367 int fd;
368 char buf[PATH_MAX];
369 char *copy = NULL;
370
371 if (!length)
372 return NULL;
373
374 fd = open(path, O_RDONLY | O_CLOEXEC);
375 if (fd < 0)
376 return NULL;
377
378 *length = 0;
379 for (;;) {
380 int n;
381 char *old = copy;
382
383 n = lxc_read_nointr(fd, buf, sizeof(buf));
384 if (n < 0)
385 goto on_error;
386 if (!n)
387 break;
388
389 copy = must_realloc(old, (*length + n) * sizeof(*old));
390 memcpy(copy + *length, buf, n);
391 *length += n;
392 }
393
394 close(fd);
395 return copy;
396
397 on_error:
398 close(fd);
399 free(copy);
400
401 return NULL;
402 }
403
404 int fd_to_fd(int from, int to)
405 {
406 for (;;) {
407 uint8_t buf[PATH_MAX];
408 uint8_t *p = buf;
409 ssize_t bytes_to_write;
410 ssize_t bytes_read;
411
412 bytes_read = lxc_read_nointr(from, buf, sizeof buf);
413 if (bytes_read < 0)
414 return -1;
415 if (bytes_read == 0)
416 break;
417
418 bytes_to_write = (size_t)bytes_read;
419 do {
420 ssize_t bytes_written;
421
422 bytes_written = lxc_write_nointr(to, p, bytes_to_write);
423 if (bytes_written < 0)
424 return -1;
425
426 bytes_to_write -= bytes_written;
427 p += bytes_written;
428 } while (bytes_to_write > 0);
429 }
430
431 return 0;
432 }