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