]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include <sys/sendfile.h>
14 #include "file_utils.h"
18 #include "syscall_wrappers.h"
21 lxc_log_define(parse
, lxc
);
23 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
26 void *tmp
= NULL
, *overlap
= NULL
;
28 /* We establish an anonymous mapping that is one byte larger than the
29 * underlying file. The pages handed to us are zero filled. */
30 tmp
= mmap(addr
, length
+ 1, PROT_READ
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
31 if (tmp
== MAP_FAILED
)
34 /* Now we establish a fixed-address mapping starting at the address we
35 * received from our anonymous mapping and replace all bytes excluding
36 * the additional \0-byte with the file. This allows us to use normal
37 * string-handling functions. */
38 overlap
= mmap(tmp
, length
, prot
, MAP_FIXED
| flags
, fd
, offset
);
39 if (overlap
== MAP_FAILED
)
40 munmap(tmp
, length
+ 1);
45 int lxc_strmunmap(void *addr
, size_t length
)
47 return munmap(addr
, length
+ 1);
50 int lxc_file_for_each_line_mmap(const char *file
, lxc_file_cb callback
, void *data
)
52 __do_close
int fd
= -EBADF
, memfd
= -EBADF
;
59 memfd
= memfd_create(".lxc_config_file", MFD_CLOEXEC
);
61 char template[] = P_tmpdir
"/.lxc_config_file_XXXXXX";
63 if (errno
!= ENOSYS
) {
64 SYSERROR("Failed to create memory file");
68 TRACE("Failed to create in-memory file. Falling back to temporary file");
69 memfd
= lxc_make_tmpfile(template, true);
71 SYSERROR("Failed to create temporary file \"%s\"", template);
76 fd
= open(file
, O_RDONLY
| O_CLOEXEC
);
78 SYSERROR("Failed to open file \"%s\"", file
);
84 SYSERROR("Failed to stat file \"%s\"", file
);
88 if (st
.st_size
> INT_MAX
) {
89 SYSERROR("Excessively large config file \"%s\"", file
);
94 bytes
= __fd_to_fd(fd
, memfd
);
96 SYSERROR("Failed to copy config file \"%s\"", file
);
100 ret
= lxc_write_nointr(memfd
, "\0", 1);
102 SYSERROR("Failed to append zero byte");
107 ret
= lseek(memfd
, 0, SEEK_SET
);
109 SYSERROR("Failed to lseek");
114 buf
= mmap(NULL
, bytes
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_POPULATE
, memfd
, 0);
115 if (buf
== MAP_FAILED
) {
117 SYSERROR("Failed to mmap");
122 lxc_iterate_parts(line
, buf
, "\r\n\0") {
123 ret
= callback(line
, data
);
125 /* Callback rv > 0 means stop here callback rv < 0 means
129 ERROR("Failed to parse config file \"%s\" at line \"%s\"",
136 if (buf
&& munmap(buf
, bytes
)) {
137 SYSERROR("Failed to unmap");
145 int lxc_file_for_each_line(const char *file
, lxc_file_cb callback
, void *data
)
147 __do_fclose
FILE *f
= NULL
;
148 __do_free
char *line
= NULL
;
152 f
= fopen(file
, "re");
154 SYSERROR("Failed to open \"%s\"", file
);
158 while (getline(&line
, &len
, f
) != -1) {
159 err
= callback(line
, data
);
161 /* Callback rv > 0 means stop here callback rv < 0 means
165 ERROR("Failed to parse config: \"%s\"", line
);