]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
Merge pull request #2493 from brauner/2018-07-26/bugfixes
[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 #define _GNU_SOURCE
25 #include <stdio.h>
26 #undef _GNU_SOURCE
27 #include <dirent.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32
33 #include "parse.h"
34 #include "config.h"
35 #include "utils.h"
36 #include "log.h"
37
38 lxc_log_define(parse, lxc);
39
40 void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
41 off_t offset)
42 {
43 void *tmp = NULL, *overlap = NULL;
44
45 /* We establish an anonymous mapping that is one byte larger than the
46 * underlying file. The pages handed to us are zero filled. */
47 tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
48 if (tmp == MAP_FAILED)
49 return tmp;
50
51 /* Now we establish a fixed-address mapping starting at the address we
52 * received from our anonymous mapping and replace all bytes excluding
53 * the additional \0-byte with the file. This allows us to use normal
54 * string-handling functions. */
55 overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset);
56 if (overlap == MAP_FAILED)
57 munmap(tmp, length + 1);
58
59 return overlap;
60 }
61
62 int lxc_strmunmap(void *addr, size_t length)
63 {
64 return munmap(addr, length + 1);
65 }
66
67 int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
68 void *data)
69 {
70 int fd;
71 char *buf, *line;
72 struct stat st;
73 int ret = 0;
74
75 fd = open(file, O_RDONLY | O_CLOEXEC);
76 if (fd < 0)
77 return -1;
78
79 ret = fstat(fd, &st);
80 if (ret < 0) {
81 close(fd);
82 return -1;
83 }
84
85 if (st.st_size == 0) {
86 close(fd);
87 return 0;
88 }
89
90 buf = lxc_strmmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
91 if (buf == MAP_FAILED) {
92 close(fd);
93 return -1;
94 }
95
96 lxc_iterate_parts(line, buf, "\n\0") {
97 ret = callback(line, data);
98 if (ret) {
99 /* Callback rv > 0 means stop here callback rv < 0 means
100 * error.
101 */
102 if (ret < 0)
103 ERROR("Failed to parse config: %s", line);
104 break;
105 }
106 }
107
108 lxc_strmunmap(buf, st.st_size);
109 close(fd);
110 return ret;
111 }
112
113 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
114 {
115 FILE *f;
116 int err = 0;
117 char *line = NULL;
118 size_t len = 0;
119
120 f = fopen(file, "r");
121 if (!f) {
122 SYSERROR("failed to open %s", file);
123 return -1;
124 }
125
126 while (getline(&line, &len, f) != -1) {
127 err = callback(line, data);
128 if (err) {
129 /* Callback rv > 0 means stop here callback rv < 0 means
130 * error.
131 */
132 if (err < 0)
133 ERROR("Failed to parse config: %s", line);
134 break;
135 }
136 }
137
138 free(line);
139 fclose(f);
140 return err;
141 }
142
143 int lxc_char_left_gc(const char *buffer, size_t len)
144 {
145 size_t i;
146
147 for (i = 0; i < len; i++) {
148 if (buffer[i] == ' ' ||
149 buffer[i] == '\t')
150 continue;
151
152 return i;
153 }
154
155 return 0;
156 }
157
158 int lxc_char_right_gc(const char *buffer, size_t len)
159 {
160 int i;
161
162 for (i = len - 1; i >= 0; i--) {
163 if (buffer[i] == ' ' ||
164 buffer[i] == '\t' ||
165 buffer[i] == '\n' ||
166 buffer[i] == '\0')
167 continue;
168
169 return i + 1;
170 }
171
172 return 0;
173 }
174
175 char *lxc_trim_whitespace_in_place(char *buffer)
176 {
177 buffer += lxc_char_left_gc(buffer, strlen(buffer));
178 buffer[lxc_char_right_gc(buffer, strlen(buffer))] = '\0';
179 return buffer;
180 }
181
182 int lxc_is_line_empty(const char *line)
183 {
184 int i;
185 size_t len = strlen(line);
186
187 for (i = 0; i < len; i++)
188 if (line[i] != ' ' && line[i] != '\t' &&
189 line[i] != '\n' && line[i] != '\r' &&
190 line[i] != '\f' && line[i] != '\0')
191 return 0;
192 return 1;
193 }