]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
parse: rework config parsing routine
[mirror_lxc.git] / src / lxc / parse.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <dirent.h>
7 #include <errno.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <sys/sendfile.h>
14
15 #include "config.h"
16 #include "file_utils.h"
17 #include "log.h"
18 #include "macro.h"
19 #include "parse.h"
20 #include "syscall_wrappers.h"
21 #include "utils.h"
22
23 lxc_log_define(parse, lxc);
24
25 void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
26 off_t offset)
27 {
28 void *tmp = NULL, *overlap = NULL;
29
30 /* We establish an anonymous mapping that is one byte larger than the
31 * underlying file. The pages handed to us are zero filled. */
32 tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
33 if (tmp == MAP_FAILED)
34 return tmp;
35
36 /* Now we establish a fixed-address mapping starting at the address we
37 * received from our anonymous mapping and replace all bytes excluding
38 * the additional \0-byte with the file. This allows us to use normal
39 * string-handling functions. */
40 overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset);
41 if (overlap == MAP_FAILED)
42 munmap(tmp, length + 1);
43
44 return overlap;
45 }
46
47 int lxc_strmunmap(void *addr, size_t length)
48 {
49 return munmap(addr, length + 1);
50 }
51
52 int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *data)
53 {
54 __do_close int fd = -EBADF, memfd = -EBADF;
55 ssize_t ret = -1;
56 char *buf = NULL;
57 struct stat st = {};
58 ssize_t bytes;
59 char *line;
60
61 memfd = memfd_create(".lxc_config_file", MFD_CLOEXEC);
62 if (memfd < 0) {
63 char template[] = P_tmpdir "/.lxc_config_file_XXXXXX";
64
65 if (errno != ENOSYS) {
66 SYSERROR("Failed to create memory file");
67 goto on_error;
68 }
69
70 TRACE("Failed to create in-memory file. Falling back to temporary file");
71 memfd = lxc_make_tmpfile(template, true);
72 if (memfd < 0) {
73 SYSERROR("Failed to create temporary file \"%s\"", template);
74 goto on_error;
75 }
76 }
77
78 fd = open(file, O_RDONLY | O_CLOEXEC);
79 if (fd < 0) {
80 SYSERROR("Failed to open file \"%s\"", file);
81 goto on_error;
82 }
83
84 ret = fstat(fd, &st);
85 if (ret) {
86 SYSERROR("Failed to stat file \"%s\"", file);
87 goto on_error;
88 }
89
90 if (st.st_size > INT_MAX) {
91 SYSERROR("Excessively large config file \"%s\"", file);
92 goto on_error;
93 }
94
95
96 bytes = __fd_to_fd(fd, memfd);
97 if (bytes < 0) {
98 SYSERROR("Failed to copy config file \"%s\"", file);
99 goto on_error;
100 }
101
102 ret = lxc_write_nointr(memfd, "\0", 1);
103 if (ret < 0) {
104 SYSERROR("Failed to append zero byte");
105 goto on_error;
106 }
107 bytes++;
108
109 ret = lseek(memfd, 0, SEEK_SET);
110 if (ret < 0) {
111 SYSERROR("Failed to lseek");
112 goto on_error;
113 }
114
115 ret = -1;
116 buf = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE, memfd, 0);
117 if (buf == MAP_FAILED) {
118 buf = NULL;
119 SYSERROR("Failed to mmap");
120 goto on_error;
121 }
122
123 ret = 0;
124 lxc_iterate_parts(line, buf, "\r\n\0") {
125 ret = callback(line, data);
126 if (ret) {
127 /* Callback rv > 0 means stop here callback rv < 0 means
128 * error.
129 */
130 if (ret < 0)
131 ERROR("Failed to parse config file \"%s\" at line \"%s\"",
132 file, line);
133 break;
134 }
135 }
136
137 on_error:
138 if (buf && munmap(buf, bytes)) {
139 SYSERROR("Failed to unmap");
140 if (ret == 0)
141 ret = -1;
142 }
143
144 return ret;
145 }
146
147 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
148 {
149 __do_fclose FILE *f = NULL;
150 __do_free char *line = NULL;
151 int err = 0;
152 size_t len = 0;
153
154 f = fopen(file, "re");
155 if (!f) {
156 SYSERROR("Failed to open \"%s\"", file);
157 return -1;
158 }
159
160 while (getline(&line, &len, f) != -1) {
161 err = callback(line, data);
162 if (err) {
163 /* Callback rv > 0 means stop here callback rv < 0 means
164 * error.
165 */
166 if (err < 0)
167 ERROR("Failed to parse config: \"%s\"", line);
168 break;
169 }
170 }
171
172 return err;
173 }