]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.c
Fix console infinite loop
[mirror_lxc.git] / src / lxc / utils.c
CommitLineData
e3642c43
DL
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
d983b93c 24#define _GNU_SOURCE
e3642c43
DL
25#include <errno.h>
26#include <unistd.h>
d983b93c
MN
27#include <stdlib.h>
28#include <stddef.h>
e3642c43
DL
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/mman.h>
d983b93c 32#include <sys/param.h>
6e4bb2e0 33#include <sys/mount.h>
d983b93c
MN
34#include <dirent.h>
35#include <fcntl.h>
e3642c43
DL
36
37#include "log.h"
38
39lxc_log_define(lxc_utils, lxc);
40
41int lxc_copy_file(const char *srcfile, const char *dstfile)
42{
43 void *srcaddr = NULL, *dstaddr;
44 struct stat stat;
45 int srcfd, dstfd, ret = -1;
46 char c = '\0';
47
48 dstfd = open(dstfile, O_CREAT | O_EXCL | O_RDWR, 0600);
49 if (dstfd < 0) {
50 SYSERROR("failed to creat '%s'", dstfile);
51 goto out;
52 }
53
54 srcfd = open(srcfile, O_RDONLY);
55 if (srcfd < 0) {
56 SYSERROR("failed to open '%s'", srcfile);
57 goto err;
58 }
59
60 if (fstat(srcfd, &stat)) {
61 SYSERROR("failed to stat '%s'", srcfile);
62 goto err;
63 }
64
65 if (!stat.st_size) {
66 INFO("copy '%s' which is an empty file", srcfile);
67 ret = 0;
68 goto out_close;
69 }
70
71 if (lseek(dstfd, stat.st_size - 1, SEEK_SET) < 0) {
72 SYSERROR("failed to seek dest file '%s'", dstfile);
73 goto err;
74 }
75
76 /* fixup length */
77 if (write(dstfd, &c, 1) < 0) {
78 SYSERROR("failed to write to '%s'", dstfile);
79 goto err;
80 }
81
82 srcaddr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, srcfd, 0L);
83 if (srcaddr == MAP_FAILED) {
84 SYSERROR("failed to mmap '%s'", srcfile);
85 goto err;
86 }
87
88 dstaddr = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, dstfd, 0L);
89 if (dstaddr == MAP_FAILED) {
90 SYSERROR("failed to mmap '%s'", dstfile);
91 goto err;
92 }
93
94 ret = 0;
95
96 memcpy(dstaddr, srcaddr, stat.st_size);
97
98 munmap(dstaddr, stat.st_size);
99out_mmap:
100 if (srcaddr)
101 munmap(srcaddr, stat.st_size);
102out_close:
103 close(dstfd);
104 close(srcfd);
105out:
106 return ret;
107err:
108 unlink(dstfile);
109 goto out_mmap;
110}
d983b93c 111
6e4bb2e0
MN
112static int mount_fs(const char *source, const char *target, const char *type)
113{
114 /* the umount may fail */
115 if (umount(target))
116 WARN("failed to unmount %s : %s", target, strerror(errno));
117
118 if (mount(source, target, type, 0, NULL)) {
119 ERROR("failed to mount %s : %s", target, strerror(errno));
120 return -1;
121 }
122
123 DEBUG("'%s' mounted on '%s'", source, target);
124
125 return 0;
126}
127
128extern int lxc_setup_fs(void)
129{
130 if (mount_fs("proc", "/proc", "proc"))
131 return -1;
132
133 if (mount_fs("shmfs", "/dev/shm", "tmpfs"))
134 return -1;
135
136 /* If we were able to mount /dev/shm, then /dev exists */
137 if (access("/dev/mqueue", F_OK) && mkdir("/dev/mqueue", 0666)) {
138 SYSERROR("failed to create '/dev/mqueue'");
139 return -1;
140 }
141
142 if (mount_fs("mqueue", "/dev/mqueue", "mqueue"))
143 return -1;
144
145 return 0;
146}
9ddaf3bf
JHS
147
148/* borrowed from iproute2 */
149extern int get_u16(ushort *val, const char *arg, int base)
150{
151 unsigned long res;
152 char *ptr;
153
154 if (!arg || !*arg)
155 return -1;
156
157 res = strtoul(arg, &ptr, base);
158 if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
159 return -1;
160
161 *val = res;
162
163 return 0;
164}
165