]>
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
33 #include <sys/sendfile.h>
36 #include "file_utils.h"
40 #include "syscall_wrappers.h"
43 lxc_log_define(parse
, lxc
);
45 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
48 void *tmp
= NULL
, *overlap
= NULL
;
50 /* We establish an anonymous mapping that is one byte larger than the
51 * underlying file. The pages handed to us are zero filled. */
52 tmp
= mmap(addr
, length
+ 1, PROT_READ
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
53 if (tmp
== MAP_FAILED
)
56 /* Now we establish a fixed-address mapping starting at the address we
57 * received from our anonymous mapping and replace all bytes excluding
58 * the additional \0-byte with the file. This allows us to use normal
59 * string-handling functions. */
60 overlap
= mmap(tmp
, length
, prot
, MAP_FIXED
| flags
, fd
, offset
);
61 if (overlap
== MAP_FAILED
)
62 munmap(tmp
, length
+ 1);
67 int lxc_strmunmap(void *addr
, size_t length
)
69 return munmap(addr
, length
+ 1);
72 int lxc_file_for_each_line_mmap(const char *file
, lxc_file_cb callback
, void *data
)
75 ssize_t ret
= -1, bytes_sent
;
77 int fd
= -1, memfd
= -1;
80 memfd
= memfd_create(".lxc_config_file", MFD_CLOEXEC
);
82 char template[] = P_tmpdir
"/.lxc_config_file_XXXXXX";
84 if (errno
!= ENOSYS
) {
85 SYSERROR("Failed to create memory file");
89 TRACE("Failed to create in-memory file. Falling back to "
91 memfd
= lxc_make_tmpfile(template, true);
93 SYSERROR("Failed to create temporary file \"%s\"", template);
98 fd
= open(file
, O_RDONLY
| O_CLOEXEC
);
100 SYSERROR("Failed to open file \"%s\"", file
);
104 /* sendfile() handles up to 2GB. No config file should be that big. */
105 bytes_sent
= lxc_sendfile_nointr(memfd
, fd
, NULL
, LXC_SENDFILE_MAX
);
106 if (bytes_sent
< 0) {
107 SYSERROR("Failed to sendfile \"%s\"", file
);
111 ret
= lxc_write_nointr(memfd
, "\0", 1);
113 SYSERROR("Failed to append zero byte");
118 ret
= lseek(memfd
, 0, SEEK_SET
);
120 SYSERROR("Failed to lseek");
125 buf
= mmap(NULL
, bytes_sent
, PROT_READ
| PROT_WRITE
,
126 MAP_SHARED
| MAP_POPULATE
, memfd
, 0);
127 if (buf
== MAP_FAILED
) {
129 SYSERROR("Failed to mmap");
134 lxc_iterate_parts(line
, buf
, "\n\0") {
135 ret
= callback(line
, data
);
137 /* Callback rv > 0 means stop here callback rv < 0 means
141 ERROR("Failed to parse config file \"%s\" at "
142 "line \"%s\"", file
, line
);
153 if (buf
&& munmap(buf
, bytes_sent
)) {
154 SYSERROR("Failed to unmap");
163 int lxc_file_for_each_line(const char *file
, lxc_file_cb callback
, void *data
)
170 f
= fopen(file
, "r");
172 SYSERROR("Failed to open \"%s\"", file
);
176 while (getline(&line
, &len
, f
) != -1) {
177 err
= callback(line
, data
);
179 /* Callback rv > 0 means stop here callback rv < 0 means
183 ERROR("Failed to parse config: \"%s\"", line
);