]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
Merge pull request #2702 from 2xsec/asan1
[mirror_lxc.git] / src / lxc / parse.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <dirent.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/mman.h>
33 #include <sys/sendfile.h>
34
35 #include "config.h"
36 #include "file_utils.h"
37 #include "log.h"
38 #include "macro.h"
39 #include "parse.h"
40 #include "syscall_wrappers.h"
41 #include "utils.h"
42
43 lxc_log_define(parse, lxc);
44
45 void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
46 off_t offset)
47 {
48 void *tmp = NULL, *overlap = NULL;
49
50 /* We establish an anonymous mapping that is one byte larger than the
51 * underlying file. The pages handed to us are zero filled. */
52 tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
53 if (tmp == MAP_FAILED)
54 return tmp;
55
56 /* Now we establish a fixed-address mapping starting at the address we
57 * received from our anonymous mapping and replace all bytes excluding
58 * the additional \0-byte with the file. This allows us to use normal
59 * string-handling functions. */
60 overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset);
61 if (overlap == MAP_FAILED)
62 munmap(tmp, length + 1);
63
64 return overlap;
65 }
66
67 int lxc_strmunmap(void *addr, size_t length)
68 {
69 return munmap(addr, length + 1);
70 }
71
72 int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *data)
73 {
74 int saved_errno;
75 ssize_t ret = -1, bytes_sent;
76 char *line;
77 int fd = -1, memfd = -1;
78 char *buf = NULL;
79
80 memfd = memfd_create(".lxc_config_file", MFD_CLOEXEC);
81 if (memfd < 0) {
82 char template[] = P_tmpdir "/.lxc_config_file_XXXXXX";
83
84 if (errno != ENOSYS) {
85 SYSERROR("Failed to create memory file");
86 goto on_error;
87 }
88
89 TRACE("Failed to create in-memory file. Falling back to "
90 "temporary file");
91 memfd = lxc_make_tmpfile(template, true);
92 if (memfd < 0) {
93 SYSERROR("Failed to create temporary file \"%s\"", template);
94 goto on_error;
95 }
96 }
97
98 fd = open(file, O_RDONLY | O_CLOEXEC);
99 if (fd < 0) {
100 SYSERROR("Failed to open file \"%s\"", file);
101 return -1;
102 }
103
104
105 /* sendfile() handles up to 2GB. No config file should be that big. */
106 bytes_sent = lxc_sendfile_nointr(memfd, fd, NULL, LXC_SENDFILE_MAX);
107 if (bytes_sent < 0) {
108 SYSERROR("Failed to sendfile \"%s\"", file);
109 goto on_error;
110 }
111
112 ret = lxc_write_nointr(memfd, "\0", 1);
113 if (ret < 0) {
114 SYSERROR("Failed to append zero byte");
115 goto on_error;
116 }
117 bytes_sent++;
118
119 ret = lseek(memfd, 0, SEEK_SET);
120 if (ret < 0) {
121 SYSERROR("Failed to lseek");
122 goto on_error;
123 }
124
125 ret = -1;
126 buf = mmap(NULL, bytes_sent, PROT_READ | PROT_WRITE,
127 MAP_SHARED | MAP_POPULATE, memfd, 0);
128 if (buf == MAP_FAILED) {
129 buf = NULL;
130 SYSERROR("Failed to mmap");
131 goto on_error;
132 }
133
134 ret = 0;
135 lxc_iterate_parts(line, buf, "\n\0") {
136 ret = callback(line, data);
137 if (ret) {
138 /* Callback rv > 0 means stop here callback rv < 0 means
139 * error.
140 */
141 if (ret < 0)
142 ERROR("Failed to parse config file \"%s\" at "
143 "line \"%s\"", file, line);
144 break;
145 }
146 }
147
148 on_error:
149 saved_errno = errno;
150 if (fd >= 0)
151 close(fd);
152 if (memfd >= 0)
153 close(memfd);
154 if (buf && munmap(buf, bytes_sent)) {
155 SYSERROR("Failed to unmap");
156 if (ret == 0)
157 ret = -1;
158 }
159 errno = saved_errno;
160
161 return ret;
162 }
163
164 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
165 {
166 FILE *f;
167 int err = 0;
168 char *line = NULL;
169 size_t len = 0;
170
171 f = fopen(file, "r");
172 if (!f) {
173 SYSERROR("failed to open %s", file);
174 return -1;
175 }
176
177 while (getline(&line, &len, f) != -1) {
178 err = callback(line, data);
179 if (err) {
180 /* Callback rv > 0 means stop here callback rv < 0 means
181 * error.
182 */
183 if (err < 0)
184 ERROR("Failed to parse config: %s", line);
185 break;
186 }
187 }
188
189 free(line);
190 fclose(f);
191 return err;
192 }