]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/utils.c
implement backend drivers and container clone API (v3)
[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:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
e3642c43
DL
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>
1b09f2c0 36#include <libgen.h>
9be53773
SH
37#include <sys/types.h>
38#include <sys/wait.h>
e3642c43
DL
39
40#include "log.h"
41
42lxc_log_define(lxc_utils, lxc);
43
6e4bb2e0
MN
44static int mount_fs(const char *source, const char *target, const char *type)
45{
46 /* the umount may fail */
47 if (umount(target))
48 WARN("failed to unmount %s : %s", target, strerror(errno));
49
50 if (mount(source, target, type, 0, NULL)) {
51 ERROR("failed to mount %s : %s", target, strerror(errno));
52 return -1;
53 }
54
55 DEBUG("'%s' mounted on '%s'", source, target);
56
57 return 0;
58}
59
60extern int lxc_setup_fs(void)
61{
62 if (mount_fs("proc", "/proc", "proc"))
63 return -1;
64
721d262c 65 /* if we can't mount /dev/shm, continue anyway */
6e4bb2e0 66 if (mount_fs("shmfs", "/dev/shm", "tmpfs"))
3283db09 67 INFO("failed to mount /dev/shm");
6e4bb2e0
MN
68
69 /* If we were able to mount /dev/shm, then /dev exists */
b91b1cd7 70 /* Sure, but it's read-only per config :) */
6e4bb2e0 71 if (access("/dev/mqueue", F_OK) && mkdir("/dev/mqueue", 0666)) {
b91b1cd7
SH
72 DEBUG("failed to create '/dev/mqueue'");
73 return 0;
6e4bb2e0
MN
74 }
75
76 if (mount_fs("mqueue", "/dev/mqueue", "mqueue"))
77 return -1;
78
79 return 0;
80}
9ddaf3bf
JHS
81
82/* borrowed from iproute2 */
7c11d57a 83extern int get_u16(unsigned short *val, const char *arg, int base)
9ddaf3bf
JHS
84{
85 unsigned long res;
86 char *ptr;
87
88 if (!arg || !*arg)
89 return -1;
90
91 res = strtoul(arg, &ptr, base);
92 if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
93 return -1;
94
95 *val = res;
96
97 return 0;
98}
99
1b09f2c0
DL
100extern int mkdir_p(char *dir, mode_t mode)
101{
860fc865
RW
102 char *tmp = dir;
103 char *orig = dir;
104 char *makeme;
105
106 do {
107 dir = tmp + strspn(tmp, "/");
108 tmp = dir + strcspn(dir, "/");
109 makeme = strndupa(orig, dir - orig);
110 if (*makeme) {
111 if (mkdir(makeme, mode) && errno != EEXIST) {
112 SYSERROR("failed to create directory '%s'\n", makeme);
113 return -1;
114 }
115 }
116 } while(tmp != dir);
1b09f2c0 117
98663823 118 return 0;
1b09f2c0 119}
2a59a681
SH
120
121static char *copypath(char *p)
122{
123 int len = strlen(p);
124 char *retbuf;
125
126 if (len < 1)
127 return NULL;
128 if (p[len-1] == '\n') {
129 p[len-1] = '\0';
130 len--;
131 }
132 retbuf = malloc(len+1);
133 if (!retbuf)
134 return NULL;
135 strcpy(retbuf, p);
136 return retbuf;
137}
138
67e571de
SG
139char *default_lxcpath;
140
141const char *default_lxc_path(void)
2a59a681 142{
67e571de 143 char buf[1024], *p;
2a59a681
SH
144 FILE *fin;
145
67e571de
SG
146 if (default_lxcpath)
147 return default_lxcpath;
148
2a59a681
SH
149 fin = fopen(LXC_GLOBAL_CONF, "r");
150 if (fin) {
151 while (fgets(buf, 1024, fin)) {
152 if (buf[0] == '#')
153 continue;
154 p = strstr(buf, "lxcpath");
155 if (!p)
156 continue;
157 p = strchr(p, '=');
158 if (!p)
159 continue;
160 p++;
161 while (*p && (*p == ' ' || *p == '\t')) p++;
162 if (!*p)
163 continue;
67e571de 164 default_lxcpath = copypath(p);
2a59a681
SH
165 goto out;
166 }
167 }
168 /* we couldn't open the file, or didn't find a lxcpath
169 * entry there. Return @LXCPATH@ */
67e571de 170 default_lxcpath = LXCPATH;
2a59a681
SH
171
172out:
173 if (fin)
174 fclose(fin);
67e571de 175 return default_lxcpath;
2a59a681 176}
9be53773
SH
177
178int wait_for_pid(pid_t pid)
179{
180 int status, ret;
181
182again:
183 ret = waitpid(pid, &status, 0);
184 if (ret == -1) {
185 if (errno == -EINTR)
186 goto again;
187 return -1;
188 }
189 if (ret != pid)
190 goto again;
191 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
192 return -1;
193 return 0;
194}