2
* lxc: linux Container library
4
* (C) Copyright IBM Corp. 2007, 2008
7
* Daniel Lezcano <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
#include <sys/types.h>
32
#include <sys/param.h>
33
#include <sys/mount.h>
40
lxc_log_define(lxc_utils, lxc);
42
int lxc_copy_file(const char *srcfile, const char *dstfile)
44
void *srcaddr = NULL, *dstaddr;
46
int srcfd, dstfd, ret = -1;
49
dstfd = open(dstfile, O_CREAT | O_EXCL | O_RDWR, 0600);
51
SYSERROR("failed to creat '%s'", dstfile);
55
srcfd = open(srcfile, O_RDONLY);
57
SYSERROR("failed to open '%s'", srcfile);
61
if (fstat(srcfd, &stat)) {
62
SYSERROR("failed to stat '%s'", srcfile);
67
INFO("copy '%s' which is an empty file", srcfile);
72
if (lseek(dstfd, stat.st_size - 1, SEEK_SET) < 0) {
73
SYSERROR("failed to seek dest file '%s'", dstfile);
78
if (write(dstfd, &c, 1) < 0) {
79
SYSERROR("failed to write to '%s'", dstfile);
83
srcaddr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, srcfd, 0L);
84
if (srcaddr == MAP_FAILED) {
85
SYSERROR("failed to mmap '%s'", srcfile);
89
dstaddr = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, dstfd, 0L);
90
if (dstaddr == MAP_FAILED) {
91
SYSERROR("failed to mmap '%s'", dstfile);
97
memcpy(dstaddr, srcaddr, stat.st_size);
99
munmap(dstaddr, stat.st_size);
102
munmap(srcaddr, stat.st_size);
113
static int mount_fs(const char *source, const char *target, const char *type)
115
/* the umount may fail */
117
WARN("failed to unmount %s : %s", target, strerror(errno));
119
if (mount(source, target, type, 0, NULL)) {
120
ERROR("failed to mount %s : %s", target, strerror(errno));
124
DEBUG("'%s' mounted on '%s'", source, target);
129
extern int lxc_setup_fs(void)
131
if (mount_fs("proc", "/proc", "proc"))
134
if (mount_fs("shmfs", "/dev/shm", "tmpfs"))
137
/* If we were able to mount /dev/shm, then /dev exists */
138
if (access("/dev/mqueue", F_OK) && mkdir("/dev/mqueue", 0666)) {
139
SYSERROR("failed to create '/dev/mqueue'");
143
if (mount_fs("mqueue", "/dev/mqueue", "mqueue"))
149
/* borrowed from iproute2 */
150
extern int get_u16(ushort *val, const char *arg, int base)
158
res = strtoul(arg, &ptr, base);
159
if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
167
extern int mkdir_p(char *dir, mode_t mode)
172
if (!strcmp(dir, "/"))
179
ret = mkdir_p(dirname(d), mode);
184
if (!access(dir, F_OK))
187
if (mkdir(dir, mode)) {
188
SYSERROR("failed to create directory '%s'\n", dir);