]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/parse.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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
38 lxc_log_define(parse
, lxc
);
40 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
43 void *tmp
= NULL
, *overlap
= NULL
;
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
)
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);
62 int lxc_strmunmap(void *addr
, size_t length
)
64 return munmap(addr
, length
+ 1);
67 int lxc_file_for_each_line_mmap(const char *file
, lxc_file_cb callback
,
75 fd
= open(file
, O_RDONLY
| O_CLOEXEC
);
85 if (st
.st_size
== 0) {
90 buf
= lxc_strmmap(NULL
, st
.st_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
91 if (buf
== MAP_FAILED
) {
96 lxc_iterate_parts(line
, buf
, "\n\0") {
97 ret
= callback(line
, data
);
99 /* Callback rv > 0 means stop here callback rv < 0 means
103 ERROR("Failed to parse config: %s", line
);
108 lxc_strmunmap(buf
, st
.st_size
);
113 int lxc_file_for_each_line(const char *file
, lxc_file_cb callback
, void *data
)
120 f
= fopen(file
, "r");
122 SYSERROR("failed to open %s", file
);
126 while (getline(&line
, &len
, f
) != -1) {
127 err
= callback(line
, data
);
129 /* Callback rv > 0 means stop here callback rv < 0 means
133 ERROR("Failed to parse config: %s", line
);