]>
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
39 lxc_log_define(parse
, lxc
);
41 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
44 void *tmp
= NULL
, *overlap
= NULL
;
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
)
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);
63 int lxc_strmunmap(void *addr
, size_t length
)
65 return munmap(addr
, length
+ 1);
68 int lxc_file_for_each_line_mmap(const char *file
, lxc_file_cb callback
, void *data
)
75 fd
= open(file
, O_RDONLY
| O_CLOEXEC
);
77 SYSERROR("Failed to open config file \"%s\"", file
);
83 SYSERROR("Failed to stat config file \"%s\"", file
);
92 buf
= lxc_strmmap(NULL
, st
.st_size
, PROT_READ
| PROT_WRITE
,
93 MAP_PRIVATE
| MAP_POPULATE
, fd
, 0);
94 if (buf
== MAP_FAILED
) {
95 SYSERROR("Failed to map config file \"%s\"", file
);
99 lxc_iterate_parts(line
, buf
, "\n\0") {
100 ret
= callback(line
, data
);
102 /* Callback rv > 0 means stop here callback rv < 0 means
106 ERROR("Failed to parse config file \"%s\" at "
114 if (lxc_strmunmap(buf
, st
.st_size
) < 0) {
115 SYSERROR("Failed to unmap config file \"%s\"", file
);
128 int lxc_file_for_each_line(const char *file
, lxc_file_cb callback
, void *data
)
135 f
= fopen(file
, "r");
137 SYSERROR("failed to open %s", file
);
141 while (getline(&line
, &len
, f
) != -1) {
142 err
= callback(line
, data
);
144 /* Callback rv > 0 means stop here callback rv < 0 means
148 ERROR("Failed to parse config: %s", line
);