]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
Merge pull request #2629 from ssup2/master
[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
34 #include "config.h"
35 #include "log.h"
36 #include "parse.h"
37 #include "utils.h"
38
39 lxc_log_define(parse, lxc);
40
41 void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd,
42 off_t offset)
43 {
44 void *tmp = NULL, *overlap = NULL;
45
46 /* We establish an anonymous mapping that is one byte larger than the
47 * underlying file. The pages handed to us are zero filled. */
48 tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
49 if (tmp == MAP_FAILED)
50 return tmp;
51
52 /* Now we establish a fixed-address mapping starting at the address we
53 * received from our anonymous mapping and replace all bytes excluding
54 * the additional \0-byte with the file. This allows us to use normal
55 * string-handling functions. */
56 overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset);
57 if (overlap == MAP_FAILED)
58 munmap(tmp, length + 1);
59
60 return overlap;
61 }
62
63 int lxc_strmunmap(void *addr, size_t length)
64 {
65 return munmap(addr, length + 1);
66 }
67
68 int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback,
69 void *data)
70 {
71 int fd;
72 char *buf, *line;
73 struct stat st;
74 int ret = 0;
75
76 fd = open(file, O_RDONLY | O_CLOEXEC);
77 if (fd < 0)
78 return -1;
79
80 ret = fstat(fd, &st);
81 if (ret < 0) {
82 close(fd);
83 return -1;
84 }
85
86 if (st.st_size == 0) {
87 close(fd);
88 return 0;
89 }
90
91 buf = lxc_strmmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
92 if (buf == MAP_FAILED) {
93 close(fd);
94 return -1;
95 }
96
97 lxc_iterate_parts(line, buf, "\n\0") {
98 ret = callback(line, data);
99 if (ret) {
100 /* Callback rv > 0 means stop here callback rv < 0 means
101 * error.
102 */
103 if (ret < 0)
104 ERROR("Failed to parse config: %s", line);
105 break;
106 }
107 }
108
109 lxc_strmunmap(buf, st.st_size);
110 close(fd);
111 return ret;
112 }
113
114 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
115 {
116 FILE *f;
117 int err = 0;
118 char *line = NULL;
119 size_t len = 0;
120
121 f = fopen(file, "r");
122 if (!f) {
123 SYSERROR("failed to open %s", file);
124 return -1;
125 }
126
127 while (getline(&line, &len, f) != -1) {
128 err = callback(line, data);
129 if (err) {
130 /* Callback rv > 0 means stop here callback rv < 0 means
131 * error.
132 */
133 if (err < 0)
134 ERROR("Failed to parse config: %s", line);
135 break;
136 }
137 }
138
139 free(line);
140 fclose(f);
141 return err;
142 }