]>
Commit | Line | Data |
---|---|---|
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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
e3642c43 DL |
22 | */ |
23 | ||
052616eb ÇO |
24 | #include "config.h" |
25 | ||
7935833c | 26 | #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ |
643c1984 | 27 | #include <ctype.h> |
a1e5280d | 28 | #include <dirent.h> |
e3642c43 | 29 | #include <errno.h> |
a1e5280d | 30 | #include <fcntl.h> |
dbaf55a3 | 31 | #include <grp.h> |
7935833c | 32 | #include <inttypes.h> |
a1e5280d | 33 | #include <libgen.h> |
d983b93c | 34 | #include <stddef.h> |
a1e5280d CB |
35 | #include <stdio.h> |
36 | #include <stdlib.h> | |
61a1d519 | 37 | #include <string.h> |
981f6029 | 38 | #include <unistd.h> |
e3642c43 | 39 | #include <sys/mman.h> |
6e4bb2e0 | 40 | #include <sys/mount.h> |
a1e5280d CB |
41 | #include <sys/param.h> |
42 | #include <sys/prctl.h> | |
43 | #include <sys/stat.h> | |
9be53773 SH |
44 | #include <sys/types.h> |
45 | #include <sys/wait.h> | |
e3642c43 DL |
46 | |
47 | #include "log.h" | |
025ed0f3 | 48 | #include "lxclock.h" |
51d0854c | 49 | #include "namespace.h" |
e3db0162 | 50 | #include "parse.h" |
981f6029 | 51 | #include "utils.h" |
e3642c43 | 52 | |
4928c718 SG |
53 | #ifndef O_PATH |
54 | #define O_PATH 010000000 | |
55 | #endif | |
56 | ||
57 | #ifndef O_NOFOLLOW | |
58 | #define O_NOFOLLOW 00400000 | |
59 | #endif | |
60 | ||
e3642c43 DL |
61 | lxc_log_define(lxc_utils, lxc); |
62 | ||
4295c5de SH |
63 | /* |
64 | * if path is btrfs, tries to remove it and any subvolumes beneath it | |
65 | */ | |
66 | extern bool btrfs_try_remove_subvol(const char *path); | |
67 | ||
41dc7155 | 68 | static int _recursive_rmdir(const char *dirname, dev_t pdev, |
0cc417b2 | 69 | const char *exclude, int level, bool onedev) |
60bf62d4 | 70 | { |
74f96976 | 71 | struct dirent *direntp; |
60bf62d4 SH |
72 | DIR *dir; |
73 | int ret, failed=0; | |
74 | char pathname[MAXPATHLEN]; | |
18aa217b | 75 | bool hadexclude = false; |
60bf62d4 SH |
76 | |
77 | dir = opendir(dirname); | |
78 | if (!dir) { | |
b103ceac | 79 | ERROR("failed to open %s", dirname); |
4355ab5f | 80 | return -1; |
60bf62d4 SH |
81 | } |
82 | ||
74f96976 | 83 | while ((direntp = readdir(dir))) { |
60bf62d4 SH |
84 | struct stat mystat; |
85 | int rc; | |
86 | ||
87 | if (!direntp) | |
88 | break; | |
89 | ||
90 | if (!strcmp(direntp->d_name, ".") || | |
91 | !strcmp(direntp->d_name, "..")) | |
92 | continue; | |
93 | ||
94 | rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name); | |
95 | if (rc < 0 || rc >= MAXPATHLEN) { | |
96 | ERROR("pathname too long"); | |
97 | failed=1; | |
98 | continue; | |
99 | } | |
18aa217b SH |
100 | |
101 | if (!level && exclude && !strcmp(direntp->d_name, exclude)) { | |
102 | ret = rmdir(pathname); | |
103 | if (ret < 0) { | |
104 | switch(errno) { | |
105 | case ENOTEMPTY: | |
0cc417b2 | 106 | INFO("Not deleting snapshot %s", pathname); |
18aa217b SH |
107 | hadexclude = true; |
108 | break; | |
109 | case ENOTDIR: | |
110 | ret = unlink(pathname); | |
111 | if (ret) | |
b103ceac | 112 | INFO("Failed to remove %s", pathname); |
18aa217b SH |
113 | break; |
114 | default: | |
b103ceac | 115 | SYSERROR("Failed to rmdir %s", pathname); |
18aa217b SH |
116 | failed = 1; |
117 | break; | |
118 | } | |
119 | } | |
120 | continue; | |
121 | } | |
122 | ||
60bf62d4 SH |
123 | ret = lstat(pathname, &mystat); |
124 | if (ret) { | |
b103ceac | 125 | ERROR("Failed to stat %s", pathname); |
4295c5de | 126 | failed = 1; |
60bf62d4 SH |
127 | continue; |
128 | } | |
4295c5de SH |
129 | if (onedev && mystat.st_dev != pdev) { |
130 | /* TODO should we be checking /proc/self/mountinfo for | |
131 | * pathname and not doing this if found? */ | |
132 | if (btrfs_try_remove_subvol(pathname)) | |
133 | INFO("Removed btrfs subvolume at %s\n", pathname); | |
60bf62d4 | 134 | continue; |
4295c5de | 135 | } |
60bf62d4 | 136 | if (S_ISDIR(mystat.st_mode)) { |
0cc417b2 | 137 | if (_recursive_rmdir(pathname, pdev, exclude, level+1, onedev) < 0) |
60bf62d4 SH |
138 | failed=1; |
139 | } else { | |
140 | if (unlink(pathname) < 0) { | |
b103ceac | 141 | SYSERROR("Failed to delete %s", pathname); |
60bf62d4 SH |
142 | failed=1; |
143 | } | |
144 | } | |
145 | } | |
146 | ||
4295c5de | 147 | if (rmdir(dirname) < 0 && !btrfs_try_remove_subvol(dirname) && !hadexclude) { |
b103ceac | 148 | ERROR("Failed to delete %s", dirname); |
4295c5de | 149 | failed=1; |
60bf62d4 SH |
150 | } |
151 | ||
025ed0f3 | 152 | ret = closedir(dir); |
025ed0f3 | 153 | if (ret) { |
b103ceac | 154 | ERROR("Failed to close directory %s", dirname); |
60bf62d4 SH |
155 | failed=1; |
156 | } | |
157 | ||
4355ab5f | 158 | return failed ? -1 : 0; |
60bf62d4 SH |
159 | } |
160 | ||
29a11a7f CB |
161 | /* We have two different magic values for overlayfs, yay. */ |
162 | #ifndef OVERLAYFS_SUPER_MAGIC | |
0cc417b2 | 163 | #define OVERLAYFS_SUPER_MAGIC 0x794c764f |
29a11a7f CB |
164 | #endif |
165 | ||
166 | #ifndef OVERLAY_SUPER_MAGIC | |
0cc417b2 | 167 | #define OVERLAY_SUPER_MAGIC 0x794c7630 |
29a11a7f CB |
168 | #endif |
169 | ||
170 | /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the | |
171 | * lxc_rmdir_onedev() | |
0cc417b2 SH |
172 | */ |
173 | static bool is_native_overlayfs(const char *path) | |
174 | { | |
29a11a7f CB |
175 | if (has_fs_type(path, OVERLAY_SUPER_MAGIC) || |
176 | has_fs_type(path, OVERLAYFS_SUPER_MAGIC)) | |
0cc417b2 | 177 | return true; |
29a11a7f | 178 | |
0cc417b2 SH |
179 | return false; |
180 | } | |
181 | ||
4355ab5f | 182 | /* returns 0 on success, -1 if there were any failures */ |
41dc7155 | 183 | extern int lxc_rmdir_onedev(const char *path, const char *exclude) |
60bf62d4 SH |
184 | { |
185 | struct stat mystat; | |
0cc417b2 SH |
186 | bool onedev = true; |
187 | ||
41dc7155 | 188 | if (is_native_overlayfs(path)) |
0cc417b2 | 189 | onedev = false; |
60bf62d4 SH |
190 | |
191 | if (lstat(path, &mystat) < 0) { | |
067650d0 SH |
192 | if (errno == ENOENT) |
193 | return 0; | |
41dc7155 | 194 | |
b103ceac | 195 | ERROR("Failed to stat %s", path); |
4355ab5f | 196 | return -1; |
60bf62d4 SH |
197 | } |
198 | ||
0cc417b2 | 199 | return _recursive_rmdir(path, mystat.st_dev, exclude, 0, onedev); |
60bf62d4 SH |
200 | } |
201 | ||
9ddaf3bf | 202 | /* borrowed from iproute2 */ |
7c11d57a | 203 | extern int get_u16(unsigned short *val, const char *arg, int base) |
9ddaf3bf JHS |
204 | { |
205 | unsigned long res; | |
206 | char *ptr; | |
207 | ||
208 | if (!arg || !*arg) | |
209 | return -1; | |
210 | ||
09bbd745 | 211 | errno = 0; |
9ddaf3bf | 212 | res = strtoul(arg, &ptr, base); |
09bbd745 | 213 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0) |
9ddaf3bf JHS |
214 | return -1; |
215 | ||
216 | *val = res; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
3ce74686 | 221 | extern int mkdir_p(const char *dir, mode_t mode) |
1b09f2c0 | 222 | { |
3ce74686 SH |
223 | const char *tmp = dir; |
224 | const char *orig = dir; | |
860fc865 RW |
225 | char *makeme; |
226 | ||
227 | do { | |
228 | dir = tmp + strspn(tmp, "/"); | |
229 | tmp = dir + strcspn(dir, "/"); | |
d74325c4 | 230 | makeme = strndup(orig, dir - orig); |
860fc865 RW |
231 | if (*makeme) { |
232 | if (mkdir(makeme, mode) && errno != EEXIST) { | |
959aee9c | 233 | SYSERROR("failed to create directory '%s'", makeme); |
d74325c4 | 234 | free(makeme); |
860fc865 RW |
235 | return -1; |
236 | } | |
237 | } | |
d74325c4 | 238 | free(makeme); |
860fc865 | 239 | } while(tmp != dir); |
1b09f2c0 | 240 | |
98663823 | 241 | return 0; |
1b09f2c0 | 242 | } |
2a59a681 | 243 | |
44b9ae4b | 244 | char *get_rundir() |
9e60f51d | 245 | { |
97a696c6 SG |
246 | char *rundir; |
247 | const char *homedir; | |
9650c735 | 248 | struct stat sb; |
9e60f51d | 249 | |
9650c735 TA |
250 | if (stat(RUNTIME_PATH, &sb) < 0) { |
251 | return NULL; | |
252 | } | |
253 | ||
254 | if (geteuid() == sb.st_uid || getegid() == sb.st_gid) { | |
c580b8d2 | 255 | rundir = strdup(RUNTIME_PATH); |
d6470e71 SG |
256 | return rundir; |
257 | } | |
97a696c6 SG |
258 | |
259 | rundir = getenv("XDG_RUNTIME_DIR"); | |
44b9ae4b SG |
260 | if (rundir) { |
261 | rundir = strdup(rundir); | |
262 | return rundir; | |
263 | } | |
97a696c6 | 264 | |
44b9ae4b SG |
265 | INFO("XDG_RUNTIME_DIR isn't set in the environment."); |
266 | homedir = getenv("HOME"); | |
267 | if (!homedir) { | |
268 | ERROR("HOME isn't set in the environment."); | |
269 | return NULL; | |
97a696c6 SG |
270 | } |
271 | ||
44b9ae4b SG |
272 | rundir = malloc(sizeof(char) * (17 + strlen(homedir))); |
273 | sprintf(rundir, "%s/.cache/lxc/run/", homedir); | |
274 | ||
9e60f51d DE |
275 | return rundir; |
276 | } | |
277 | ||
9be53773 SH |
278 | int wait_for_pid(pid_t pid) |
279 | { | |
280 | int status, ret; | |
281 | ||
282 | again: | |
283 | ret = waitpid(pid, &status, 0); | |
284 | if (ret == -1) { | |
71b9b8ed | 285 | if (errno == EINTR) |
9be53773 SH |
286 | goto again; |
287 | return -1; | |
288 | } | |
289 | if (ret != pid) | |
290 | goto again; | |
291 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) | |
292 | return -1; | |
293 | return 0; | |
294 | } | |
c797a220 CS |
295 | |
296 | int lxc_wait_for_pid_status(pid_t pid) | |
297 | { | |
298 | int status, ret; | |
299 | ||
300 | again: | |
301 | ret = waitpid(pid, &status, 0); | |
302 | if (ret == -1) { | |
303 | if (errno == EINTR) | |
304 | goto again; | |
305 | return -1; | |
306 | } | |
307 | if (ret != pid) | |
308 | goto again; | |
309 | return status; | |
310 | } | |
92f023dc | 311 | |
650468bb | 312 | ssize_t lxc_write_nointr(int fd, const void* buf, size_t count) |
92f023dc | 313 | { |
650468bb | 314 | ssize_t ret; |
92f023dc CS |
315 | again: |
316 | ret = write(fd, buf, count); | |
317 | if (ret < 0 && errno == EINTR) | |
318 | goto again; | |
319 | return ret; | |
320 | } | |
321 | ||
650468bb | 322 | ssize_t lxc_read_nointr(int fd, void* buf, size_t count) |
92f023dc | 323 | { |
650468bb | 324 | ssize_t ret; |
92f023dc CS |
325 | again: |
326 | ret = read(fd, buf, count); | |
327 | if (ret < 0 && errno == EINTR) | |
328 | goto again; | |
329 | return ret; | |
330 | } | |
331 | ||
650468bb | 332 | ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf) |
92f023dc | 333 | { |
650468bb | 334 | ssize_t ret; |
92f023dc CS |
335 | ret = lxc_read_nointr(fd, buf, count); |
336 | if (ret <= 0) | |
337 | return ret; | |
650468bb | 338 | if ((size_t)ret != count) |
92f023dc CS |
339 | return -1; |
340 | if (expected_buf && memcmp(buf, expected_buf, count) != 0) { | |
341 | errno = EINVAL; | |
342 | return -1; | |
343 | } | |
344 | return ret; | |
345 | } | |
3ce74686 SH |
346 | |
347 | #if HAVE_LIBGNUTLS | |
348 | #include <gnutls/gnutls.h> | |
349 | #include <gnutls/crypto.h> | |
41246cee DE |
350 | |
351 | __attribute__((constructor)) | |
352 | static void gnutls_lxc_init(void) | |
353 | { | |
354 | gnutls_global_init(); | |
355 | } | |
356 | ||
3ce74686 SH |
357 | int sha1sum_file(char *fnam, unsigned char *digest) |
358 | { | |
359 | char *buf; | |
360 | int ret; | |
361 | FILE *f; | |
362 | long flen; | |
363 | ||
364 | if (!fnam) | |
365 | return -1; | |
025ed0f3 | 366 | f = fopen_cloexec(fnam, "r"); |
7be677a8 | 367 | if (!f) { |
3ce74686 SH |
368 | SYSERROR("Error opening template"); |
369 | return -1; | |
370 | } | |
371 | if (fseek(f, 0, SEEK_END) < 0) { | |
372 | SYSERROR("Error seeking to end of template"); | |
dd1d77f9 | 373 | fclose(f); |
3ce74686 SH |
374 | return -1; |
375 | } | |
376 | if ((flen = ftell(f)) < 0) { | |
377 | SYSERROR("Error telling size of template"); | |
dd1d77f9 | 378 | fclose(f); |
3ce74686 SH |
379 | return -1; |
380 | } | |
381 | if (fseek(f, 0, SEEK_SET) < 0) { | |
382 | SYSERROR("Error seeking to start of template"); | |
dd1d77f9 | 383 | fclose(f); |
3ce74686 SH |
384 | return -1; |
385 | } | |
386 | if ((buf = malloc(flen+1)) == NULL) { | |
387 | SYSERROR("Out of memory"); | |
dd1d77f9 | 388 | fclose(f); |
3ce74686 SH |
389 | return -1; |
390 | } | |
391 | if (fread(buf, 1, flen, f) != flen) { | |
392 | SYSERROR("Failure reading template"); | |
393 | free(buf); | |
dd1d77f9 | 394 | fclose(f); |
3ce74686 SH |
395 | return -1; |
396 | } | |
dd1d77f9 | 397 | if (fclose(f) < 0) { |
3ce74686 SH |
398 | SYSERROR("Failre closing template"); |
399 | free(buf); | |
400 | return -1; | |
401 | } | |
402 | buf[flen] = '\0'; | |
403 | ret = gnutls_hash_fast(GNUTLS_DIG_SHA1, buf, flen, (void *)digest); | |
404 | free(buf); | |
405 | return ret; | |
406 | } | |
407 | #endif | |
61a1d519 CS |
408 | |
409 | char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup) | |
410 | { | |
411 | va_list ap2; | |
412 | size_t count = 1 + skip; | |
413 | char **result; | |
414 | ||
415 | /* first determine size of argument list, we don't want to reallocate | |
416 | * constantly... | |
417 | */ | |
418 | va_copy(ap2, ap); | |
419 | while (1) { | |
420 | char* arg = va_arg(ap2, char*); | |
421 | if (!arg) | |
422 | break; | |
423 | count++; | |
424 | } | |
425 | va_end(ap2); | |
426 | ||
427 | result = calloc(count, sizeof(char*)); | |
428 | if (!result) | |
429 | return NULL; | |
430 | count = skip; | |
431 | while (1) { | |
432 | char* arg = va_arg(ap, char*); | |
433 | if (!arg) | |
434 | break; | |
435 | arg = do_strdup ? strdup(arg) : arg; | |
436 | if (!arg) | |
437 | goto oom; | |
438 | result[count++] = arg; | |
439 | } | |
440 | ||
441 | /* calloc has already set last element to NULL*/ | |
442 | return result; | |
443 | ||
444 | oom: | |
445 | free(result); | |
446 | return NULL; | |
447 | } | |
448 | ||
449 | const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip) | |
450 | { | |
451 | return (const char**)lxc_va_arg_list_to_argv(ap, skip, 0); | |
452 | } | |
db27c8d7 | 453 | |
8bd8018e | 454 | struct lxc_popen_FILE *lxc_popen(const char *command) |
ebec9176 | 455 | { |
3f323207 | 456 | int ret; |
ebec9176 AM |
457 | int pipe_fds[2]; |
458 | pid_t child_pid; | |
8bd8018e | 459 | struct lxc_popen_FILE *fp = NULL; |
ebec9176 | 460 | |
8bd8018e CB |
461 | ret = pipe2(pipe_fds, O_CLOEXEC); |
462 | if (ret < 0) | |
ebec9176 | 463 | return NULL; |
ebec9176 AM |
464 | |
465 | child_pid = fork(); | |
8bd8018e CB |
466 | if (child_pid < 0) |
467 | goto on_error; | |
468 | ||
469 | if (!child_pid) { | |
470 | sigset_t mask; | |
471 | ||
472 | close(pipe_fds[0]); | |
473 | ||
474 | /* duplicate stdout */ | |
475 | if (pipe_fds[1] != STDOUT_FILENO) | |
476 | ret = dup2(pipe_fds[1], STDOUT_FILENO); | |
477 | else | |
478 | ret = fcntl(pipe_fds[1], F_SETFD, 0); | |
479 | if (ret < 0) { | |
480 | close(pipe_fds[1]); | |
481 | exit(EXIT_FAILURE); | |
3f323207 CB |
482 | } |
483 | ||
8bd8018e CB |
484 | /* duplicate stderr */ |
485 | if (pipe_fds[1] != STDERR_FILENO) | |
486 | ret = dup2(pipe_fds[1], STDERR_FILENO); | |
487 | else | |
488 | ret = fcntl(pipe_fds[1], F_SETFD, 0); | |
489 | close(pipe_fds[1]); | |
490 | if (ret < 0) | |
491 | exit(EXIT_FAILURE); | |
492 | ||
493 | /* unblock all signals */ | |
494 | ret = sigfillset(&mask); | |
495 | if (ret < 0) | |
496 | exit(EXIT_FAILURE); | |
497 | ||
498 | ret = sigprocmask(SIG_UNBLOCK, &mask, NULL); | |
499 | if (ret < 0) | |
500 | exit(EXIT_FAILURE); | |
501 | ||
502 | execl("/bin/sh", "sh", "-c", command, (char *)NULL); | |
ebec9176 AM |
503 | exit(127); |
504 | } | |
505 | ||
8bd8018e CB |
506 | close(pipe_fds[1]); |
507 | pipe_fds[1] = -1; | |
ebec9176 | 508 | |
8bd8018e CB |
509 | fp = malloc(sizeof(*fp)); |
510 | if (!fp) | |
511 | goto on_error; | |
7e50ec0b | 512 | memset(fp, 0, sizeof(*fp)); |
ebec9176 AM |
513 | |
514 | fp->child_pid = child_pid; | |
8bd8018e | 515 | fp->pipe = pipe_fds[0]; |
ebec9176 | 516 | |
7e50ec0b CB |
517 | /* From now on, closing fp->f will also close fp->pipe. So only ever |
518 | * call fclose(fp->f). | |
519 | */ | |
8bd8018e CB |
520 | fp->f = fdopen(pipe_fds[0], "r"); |
521 | if (!fp->f) | |
522 | goto on_error; | |
ebec9176 | 523 | |
8bd8018e | 524 | return fp; |
ebec9176 | 525 | |
8bd8018e | 526 | on_error: |
7e50ec0b CB |
527 | /* We can only close pipe_fds[0] if fdopen() didn't succeed or wasn't |
528 | * called yet. Otherwise the fd belongs to the file opened by fdopen() | |
529 | * since it isn't dup()ed. | |
530 | */ | |
531 | if (fp && !fp->f && pipe_fds[0] >= 0) | |
8bd8018e CB |
532 | close(pipe_fds[0]); |
533 | ||
534 | if (pipe_fds[1] >= 0) | |
535 | close(pipe_fds[1]); | |
ebec9176 | 536 | |
7e50ec0b CB |
537 | if (fp && fp->f) |
538 | fclose(fp->f); | |
539 | ||
540 | if (fp) | |
541 | free(fp); | |
542 | ||
ebec9176 AM |
543 | return NULL; |
544 | } | |
545 | ||
8bd8018e | 546 | int lxc_pclose(struct lxc_popen_FILE *fp) |
ebec9176 | 547 | { |
ebec9176 | 548 | pid_t wait_pid; |
8bd8018e | 549 | int wstatus = 0; |
ebec9176 | 550 | |
8bd8018e | 551 | if (!fp) |
ebec9176 | 552 | return -1; |
ebec9176 AM |
553 | |
554 | do { | |
8bd8018e CB |
555 | wait_pid = waitpid(fp->child_pid, &wstatus, 0); |
556 | } while (wait_pid < 0 && errno == EINTR); | |
ebec9176 | 557 | |
8bd8018e CB |
558 | fclose(fp->f); |
559 | free(fp); | |
560 | ||
561 | if (wait_pid < 0) | |
ebec9176 | 562 | return -1; |
ebec9176 AM |
563 | |
564 | return wstatus; | |
565 | } | |
566 | ||
502657d5 CS |
567 | char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack) |
568 | { | |
569 | ssize_t len = -1, saved_len = -1; | |
570 | char *result = NULL; | |
571 | size_t replacement_len = strlen(replacement); | |
572 | size_t needle_len = strlen(needle); | |
573 | ||
574 | /* should be executed exactly twice */ | |
575 | while (len == -1 || result == NULL) { | |
576 | char *p; | |
577 | char *last_p; | |
578 | ssize_t part_len; | |
579 | ||
580 | if (len != -1) { | |
581 | result = calloc(1, len + 1); | |
582 | if (!result) | |
583 | return NULL; | |
584 | saved_len = len; | |
585 | } | |
586 | ||
587 | len = 0; | |
588 | ||
589 | for (last_p = (char *)haystack, p = strstr(last_p, needle); p; last_p = p, p = strstr(last_p, needle)) { | |
590 | part_len = (ssize_t)(p - last_p); | |
591 | if (result && part_len > 0) | |
592 | memcpy(&result[len], last_p, part_len); | |
593 | len += part_len; | |
594 | if (result && replacement_len > 0) | |
595 | memcpy(&result[len], replacement, replacement_len); | |
596 | len += replacement_len; | |
597 | p += needle_len; | |
598 | } | |
599 | part_len = strlen(last_p); | |
600 | if (result && part_len > 0) | |
601 | memcpy(&result[len], last_p, part_len); | |
602 | len += part_len; | |
603 | } | |
604 | ||
605 | /* make sure we did the same thing twice, | |
606 | * once for calculating length, the other | |
607 | * time for copying data */ | |
97bc2422 CB |
608 | if (saved_len != len) { |
609 | free(result); | |
610 | return NULL; | |
611 | } | |
502657d5 CS |
612 | /* make sure we didn't overwrite any buffer, |
613 | * due to calloc the string should be 0-terminated */ | |
97bc2422 CB |
614 | if (result[len] != '\0') { |
615 | free(result); | |
616 | return NULL; | |
617 | } | |
502657d5 CS |
618 | |
619 | return result; | |
620 | } | |
621 | ||
622 | bool lxc_string_in_array(const char *needle, const char **haystack) | |
623 | { | |
624 | for (; haystack && *haystack; haystack++) | |
625 | if (!strcmp(needle, *haystack)) | |
626 | return true; | |
627 | return false; | |
628 | } | |
629 | ||
630 | char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix) | |
631 | { | |
632 | char *result; | |
633 | char **p; | |
634 | size_t sep_len = strlen(sep); | |
635 | size_t result_len = use_as_prefix * sep_len; | |
636 | ||
637 | /* calculate new string length */ | |
638 | for (p = (char **)parts; *p; p++) | |
639 | result_len += (p > (char **)parts) * sep_len + strlen(*p); | |
640 | ||
641 | result = calloc(result_len + 1, 1); | |
642 | if (!result) | |
643 | return NULL; | |
644 | ||
645 | if (use_as_prefix) | |
646 | strcpy(result, sep); | |
647 | for (p = (char **)parts; *p; p++) { | |
648 | if (p > (char **)parts) | |
649 | strcat(result, sep); | |
650 | strcat(result, *p); | |
651 | } | |
652 | ||
653 | return result; | |
654 | } | |
655 | ||
656 | char **lxc_normalize_path(const char *path) | |
657 | { | |
658 | char **components; | |
659 | char **p; | |
660 | size_t components_len = 0; | |
661 | size_t pos = 0; | |
662 | ||
663 | components = lxc_string_split(path, '/'); | |
664 | if (!components) | |
665 | return NULL; | |
666 | for (p = components; *p; p++) | |
667 | components_len++; | |
668 | ||
669 | /* resolve '.' and '..' */ | |
670 | for (pos = 0; pos < components_len; ) { | |
671 | if (!strcmp(components[pos], ".") || (!strcmp(components[pos], "..") && pos == 0)) { | |
672 | /* eat this element */ | |
673 | free(components[pos]); | |
674 | memmove(&components[pos], &components[pos+1], sizeof(char *) * (components_len - pos)); | |
675 | components_len--; | |
676 | } else if (!strcmp(components[pos], "..")) { | |
677 | /* eat this and the previous element */ | |
678 | free(components[pos - 1]); | |
679 | free(components[pos]); | |
680 | memmove(&components[pos-1], &components[pos+1], sizeof(char *) * (components_len - pos)); | |
681 | components_len -= 2; | |
682 | pos--; | |
683 | } else { | |
684 | pos++; | |
685 | } | |
686 | } | |
687 | ||
688 | return components; | |
689 | } | |
690 | ||
eda0afd4 | 691 | char *lxc_deslashify(const char *path) |
aeb3682f | 692 | { |
eda0afd4 | 693 | char *dup, *p; |
c56a9652 CB |
694 | char **parts = NULL; |
695 | size_t n, len; | |
aeb3682f | 696 | |
eda0afd4 CB |
697 | dup = strdup(path); |
698 | if (!dup) | |
699 | return NULL; | |
700 | ||
701 | parts = lxc_normalize_path(dup); | |
702 | if (!parts) { | |
703 | free(dup); | |
704 | return NULL; | |
705 | } | |
aeb3682f | 706 | |
c56a9652 CB |
707 | /* We'll end up here if path == "///" or path == "". */ |
708 | if (!*parts) { | |
eda0afd4 | 709 | len = strlen(dup); |
f85b16a1 | 710 | if (!len) { |
eda0afd4 CB |
711 | lxc_free_array((void **)parts, free); |
712 | return dup; | |
f85b16a1 | 713 | } |
eda0afd4 | 714 | n = strcspn(dup, "/"); |
c56a9652 | 715 | if (n == len) { |
eda0afd4 CB |
716 | free(dup); |
717 | lxc_free_array((void **)parts, free); | |
718 | ||
c56a9652 CB |
719 | p = strdup("/"); |
720 | if (!p) | |
eda0afd4 CB |
721 | return NULL; |
722 | ||
723 | return p; | |
c56a9652 CB |
724 | } |
725 | } | |
726 | ||
eda0afd4 CB |
727 | p = lxc_string_join("/", (const char **)parts, *dup == '/'); |
728 | free(dup); | |
f85b16a1 | 729 | lxc_free_array((void **)parts, free); |
eda0afd4 | 730 | return p; |
aeb3682f TA |
731 | } |
732 | ||
24b51482 CS |
733 | char *lxc_append_paths(const char *first, const char *second) |
734 | { | |
e8eb3956 CB |
735 | int ret; |
736 | size_t len; | |
24b51482 | 737 | char *result = NULL; |
e8eb3956 | 738 | const char *pattern = "%s%s"; |
24b51482 | 739 | |
e8eb3956 | 740 | len = strlen(first) + strlen(second) + 1; |
24b51482 CS |
741 | if (second[0] != '/') { |
742 | len += 1; | |
743 | pattern = "%s/%s"; | |
744 | } | |
745 | ||
746 | result = calloc(1, len); | |
747 | if (!result) | |
748 | return NULL; | |
749 | ||
e8eb3956 CB |
750 | ret = snprintf(result, len, pattern, first, second); |
751 | if (ret < 0 || (size_t)ret >= len) { | |
752 | free(result); | |
753 | return NULL; | |
754 | } | |
755 | ||
24b51482 CS |
756 | return result; |
757 | } | |
758 | ||
502657d5 CS |
759 | bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) |
760 | { | |
761 | char *token, *str, *saveptr = NULL; | |
762 | char sep[2] = { _sep, '\0' }; | |
763 | ||
764 | if (!haystack || !needle) | |
765 | return 0; | |
766 | ||
767 | str = alloca(strlen(haystack)+1); | |
768 | strcpy(str, haystack); | |
769 | for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { | |
770 | if (strcmp(needle, token) == 0) | |
771 | return 1; | |
772 | } | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | char **lxc_string_split(const char *string, char _sep) | |
778 | { | |
779 | char *token, *str, *saveptr = NULL; | |
605ea1f7 CB |
780 | char sep[2] = {_sep, '\0'}; |
781 | char **tmp = NULL, **result = NULL; | |
502657d5 CS |
782 | size_t result_capacity = 0; |
783 | size_t result_count = 0; | |
784 | int r, saved_errno; | |
785 | ||
786 | if (!string) | |
787 | return calloc(1, sizeof(char *)); | |
788 | ||
605ea1f7 | 789 | str = alloca(strlen(string) + 1); |
502657d5 CS |
790 | strcpy(str, string); |
791 | for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { | |
792 | r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); | |
793 | if (r < 0) | |
794 | goto error_out; | |
795 | result[result_count] = strdup(token); | |
796 | if (!result[result_count]) | |
797 | goto error_out; | |
798 | result_count++; | |
799 | } | |
800 | ||
801 | /* if we allocated too much, reduce it */ | |
605ea1f7 CB |
802 | tmp = realloc(result, (result_count + 1) * sizeof(char *)); |
803 | if (!tmp) | |
804 | goto error_out; | |
805 | result = tmp; | |
806 | /* Make sure we don't return uninitialized memory. */ | |
807 | if (result_count == 0) | |
808 | *result = NULL; | |
809 | return result; | |
502657d5 CS |
810 | error_out: |
811 | saved_errno = errno; | |
812 | lxc_free_array((void **)result, free); | |
813 | errno = saved_errno; | |
814 | return NULL; | |
815 | } | |
816 | ||
3dca1af0 SH |
817 | static bool complete_word(char ***result, char *start, char *end, size_t *cap, size_t *cnt) |
818 | { | |
819 | int r; | |
820 | ||
821 | r = lxc_grow_array((void ***)result, cap, 2 + *cnt, 16); | |
822 | if (r < 0) | |
823 | return false; | |
824 | (*result)[*cnt] = strndup(start, end - start); | |
825 | if (!(*result)[*cnt]) | |
826 | return false; | |
827 | (*cnt)++; | |
828 | ||
829 | return true; | |
830 | } | |
831 | ||
832 | /* | |
833 | * Given a a string 'one two "three four"', split into three words, | |
834 | * one, two, and "three four" | |
835 | */ | |
836 | char **lxc_string_split_quoted(char *string) | |
837 | { | |
838 | char *nextword = string, *p, state; | |
839 | char **result = NULL; | |
840 | size_t result_capacity = 0; | |
841 | size_t result_count = 0; | |
842 | ||
843 | if (!string || !*string) | |
844 | return calloc(1, sizeof(char *)); | |
845 | ||
846 | // TODO I'm *not* handling escaped quote | |
847 | state = ' '; | |
848 | for (p = string; *p; p++) { | |
849 | switch(state) { | |
850 | case ' ': | |
851 | if (isspace(*p)) | |
852 | continue; | |
853 | else if (*p == '"' || *p == '\'') { | |
854 | nextword = p; | |
855 | state = *p; | |
856 | continue; | |
857 | } | |
858 | nextword = p; | |
859 | state = 'a'; | |
860 | continue; | |
861 | case 'a': | |
862 | if (isspace(*p)) { | |
863 | complete_word(&result, nextword, p, &result_capacity, &result_count); | |
864 | state = ' '; | |
865 | continue; | |
866 | } | |
867 | continue; | |
868 | case '"': | |
869 | case '\'': | |
870 | if (*p == state) { | |
871 | complete_word(&result, nextword+1, p, &result_capacity, &result_count); | |
872 | state = ' '; | |
873 | continue; | |
874 | } | |
875 | continue; | |
876 | } | |
877 | } | |
878 | ||
879 | if (state == 'a') | |
880 | complete_word(&result, nextword, p, &result_capacity, &result_count); | |
881 | ||
882 | return realloc(result, (result_count + 1) * sizeof(char *)); | |
883 | } | |
884 | ||
502657d5 CS |
885 | char **lxc_string_split_and_trim(const char *string, char _sep) |
886 | { | |
887 | char *token, *str, *saveptr = NULL; | |
888 | char sep[2] = { _sep, '\0' }; | |
889 | char **result = NULL; | |
890 | size_t result_capacity = 0; | |
891 | size_t result_count = 0; | |
892 | int r, saved_errno; | |
893 | size_t i = 0; | |
894 | ||
895 | if (!string) | |
896 | return calloc(1, sizeof(char *)); | |
897 | ||
898 | str = alloca(strlen(string)+1); | |
899 | strcpy(str, string); | |
900 | for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) { | |
901 | while (token[0] == ' ' || token[0] == '\t') | |
902 | token++; | |
903 | i = strlen(token); | |
904 | while (i > 0 && (token[i - 1] == ' ' || token[i - 1] == '\t')) { | |
905 | token[i - 1] = '\0'; | |
906 | i--; | |
907 | } | |
908 | r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); | |
909 | if (r < 0) | |
910 | goto error_out; | |
911 | result[result_count] = strdup(token); | |
912 | if (!result[result_count]) | |
913 | goto error_out; | |
914 | result_count++; | |
915 | } | |
916 | ||
917 | /* if we allocated too much, reduce it */ | |
918 | return realloc(result, (result_count + 1) * sizeof(char *)); | |
919 | error_out: | |
920 | saved_errno = errno; | |
921 | lxc_free_array((void **)result, free); | |
922 | errno = saved_errno; | |
923 | return NULL; | |
924 | } | |
925 | ||
926 | void lxc_free_array(void **array, lxc_free_fn element_free_fn) | |
927 | { | |
928 | void **p; | |
929 | for (p = array; p && *p; p++) | |
930 | element_free_fn(*p); | |
931 | free((void*)array); | |
932 | } | |
933 | ||
934 | int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment) | |
935 | { | |
936 | size_t new_capacity; | |
937 | void **new_array; | |
938 | ||
939 | /* first time around, catch some trivial mistakes of the user | |
940 | * only initializing one of these */ | |
941 | if (!*array || !*capacity) { | |
942 | *array = NULL; | |
943 | *capacity = 0; | |
944 | } | |
945 | ||
946 | new_capacity = *capacity; | |
947 | while (new_size + 1 > new_capacity) | |
948 | new_capacity += capacity_increment; | |
949 | if (new_capacity != *capacity) { | |
950 | /* we have to reallocate */ | |
951 | new_array = realloc(*array, new_capacity * sizeof(void *)); | |
952 | if (!new_array) | |
953 | return -1; | |
954 | memset(&new_array[*capacity], 0, (new_capacity - (*capacity)) * sizeof(void *)); | |
955 | *array = new_array; | |
956 | *capacity = new_capacity; | |
957 | } | |
958 | ||
959 | /* array has sufficient elements */ | |
960 | return 0; | |
961 | } | |
962 | ||
963 | size_t lxc_array_len(void **array) | |
964 | { | |
965 | void **p; | |
966 | size_t result = 0; | |
967 | ||
968 | for (p = array; p && *p; p++) | |
969 | result++; | |
970 | ||
971 | return result; | |
972 | } | |
973 | ||
0e95426b CS |
974 | int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline) |
975 | { | |
976 | int fd, saved_errno; | |
977 | ssize_t ret; | |
978 | ||
979 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666); | |
980 | if (fd < 0) | |
981 | return -1; | |
982 | ret = lxc_write_nointr(fd, buf, count); | |
983 | if (ret < 0) | |
799f29ab | 984 | goto out_error; |
0e95426b CS |
985 | if ((size_t)ret != count) |
986 | goto out_error; | |
987 | if (add_newline) { | |
988 | ret = lxc_write_nointr(fd, "\n", 1); | |
989 | if (ret != 1) | |
990 | goto out_error; | |
991 | } | |
992 | close(fd); | |
993 | return 0; | |
994 | ||
995 | out_error: | |
996 | saved_errno = errno; | |
997 | close(fd); | |
998 | errno = saved_errno; | |
999 | return -1; | |
1000 | } | |
1001 | ||
1002 | int lxc_read_from_file(const char *filename, void* buf, size_t count) | |
1003 | { | |
1004 | int fd = -1, saved_errno; | |
1005 | ssize_t ret; | |
1006 | ||
1007 | fd = open(filename, O_RDONLY | O_CLOEXEC); | |
1008 | if (fd < 0) | |
1009 | return -1; | |
1010 | ||
1011 | if (!buf || !count) { | |
1012 | char buf2[100]; | |
1013 | size_t count2 = 0; | |
1014 | while ((ret = read(fd, buf2, 100)) > 0) | |
1015 | count2 += ret; | |
1016 | if (ret >= 0) | |
1017 | ret = count2; | |
1018 | } else { | |
1019 | memset(buf, 0, count); | |
1020 | ret = read(fd, buf, count); | |
1021 | } | |
1022 | ||
1023 | if (ret < 0) | |
1024 | ERROR("read %s: %s", filename, strerror(errno)); | |
1025 | ||
1026 | saved_errno = errno; | |
1027 | close(fd); | |
1028 | errno = saved_errno; | |
1029 | return ret; | |
1030 | } | |
799f29ab ÇO |
1031 | |
1032 | void **lxc_append_null_to_array(void **array, size_t count) | |
1033 | { | |
1034 | void **temp; | |
1035 | ||
1036 | /* Append NULL to the array */ | |
1037 | if (count) { | |
1038 | temp = realloc(array, (count + 1) * sizeof(*array)); | |
1039 | if (!temp) { | |
84760c11 | 1040 | size_t i; |
799f29ab ÇO |
1041 | for (i = 0; i < count; i++) |
1042 | free(array[i]); | |
1043 | free(array); | |
1044 | return NULL; | |
1045 | } | |
1046 | array = temp; | |
1047 | array[count] = NULL; | |
1048 | } | |
1049 | return array; | |
1050 | } | |
508c263e SH |
1051 | |
1052 | int randseed(bool srand_it) | |
1053 | { | |
1054 | /* | |
1055 | srand pre-seed function based on /dev/urandom | |
1056 | */ | |
091045f8 | 1057 | unsigned int seed = time(NULL) + getpid(); |
508c263e SH |
1058 | |
1059 | FILE *f; | |
1060 | f = fopen("/dev/urandom", "r"); | |
1061 | if (f) { | |
1062 | int ret = fread(&seed, sizeof(seed), 1, f); | |
1063 | if (ret != 1) | |
1064 | DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno)); | |
1065 | fclose(f); | |
1066 | } | |
1067 | ||
1068 | if (srand_it) | |
1069 | srand(seed); | |
1070 | ||
1071 | return seed; | |
1072 | } | |
5d897655 SH |
1073 | |
1074 | uid_t get_ns_uid(uid_t orig) | |
1075 | { | |
1076 | char *line = NULL; | |
1077 | size_t sz = 0; | |
1078 | uid_t nsid, hostid, range; | |
1079 | FILE *f = fopen("/proc/self/uid_map", "r"); | |
1080 | if (!f) | |
1081 | return 0; | |
1082 | ||
1083 | while (getline(&line, &sz, f) != -1) { | |
1084 | if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3) | |
1085 | continue; | |
1086 | if (hostid <= orig && hostid + range > orig) { | |
1087 | nsid += orig - hostid; | |
1088 | goto found; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | nsid = 0; | |
1093 | found: | |
1094 | fclose(f); | |
1095 | free(line); | |
1096 | return nsid; | |
1097 | } | |
c476bdce SH |
1098 | |
1099 | bool dir_exists(const char *path) | |
1100 | { | |
1101 | struct stat sb; | |
1102 | int ret; | |
1103 | ||
1104 | ret = stat(path, &sb); | |
1105 | if (ret < 0) | |
1a0e70ac | 1106 | /* Could be something other than eexist, just say "no". */ |
c476bdce SH |
1107 | return false; |
1108 | return S_ISDIR(sb.st_mode); | |
1109 | } | |
93c379f0 ÇO |
1110 | |
1111 | /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS. | |
1112 | * FNV has good anti collision properties and we're not worried | |
1113 | * about pre-image resistance or one-way-ness, we're just trying to make | |
1114 | * the name unique in the 108 bytes of space we have. | |
1115 | */ | |
1116 | uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval) | |
1117 | { | |
1118 | unsigned char *bp; | |
1119 | ||
1120 | for(bp = buf; bp < (unsigned char *)buf + len; bp++) | |
1121 | { | |
1122 | /* xor the bottom with the current octet */ | |
1123 | hval ^= (uint64_t)*bp; | |
1124 | ||
1125 | /* gcc optimised: | |
1126 | * multiply by the 64 bit FNV magic prime mod 2^64 | |
1127 | */ | |
1128 | hval += (hval << 1) + (hval << 4) + (hval << 5) + | |
1129 | (hval << 7) + (hval << 8) + (hval << 40); | |
1130 | } | |
1131 | ||
1132 | return hval; | |
1133 | } | |
2c6f3fc9 SH |
1134 | |
1135 | /* | |
1136 | * Detect whether / is mounted MS_SHARED. The only way I know of to | |
1137 | * check that is through /proc/self/mountinfo. | |
1138 | * I'm only checking for /. If the container rootfs or mount location | |
1139 | * is MS_SHARED, but not '/', then you're out of luck - figuring that | |
1140 | * out would be too much work to be worth it. | |
1141 | */ | |
2c6f3fc9 SH |
1142 | int detect_shared_rootfs(void) |
1143 | { | |
eab15c1e | 1144 | char buf[LXC_LINELEN], *p; |
2c6f3fc9 SH |
1145 | FILE *f; |
1146 | int i; | |
1147 | char *p2; | |
1148 | ||
1149 | f = fopen("/proc/self/mountinfo", "r"); | |
1150 | if (!f) | |
1151 | return 0; | |
eab15c1e CB |
1152 | while (fgets(buf, LXC_LINELEN, f)) { |
1153 | for (p = buf, i = 0; p && i < 4; i++) | |
1154 | p = strchr(p + 1, ' '); | |
2c6f3fc9 SH |
1155 | if (!p) |
1156 | continue; | |
eab15c1e | 1157 | p2 = strchr(p + 1, ' '); |
2c6f3fc9 SH |
1158 | if (!p2) |
1159 | continue; | |
1160 | *p2 = '\0'; | |
eab15c1e | 1161 | if (strcmp(p + 1, "/") == 0) { |
1a0e70ac | 1162 | /* This is '/'. Is it shared? */ |
eab15c1e | 1163 | p = strchr(p2 + 1, ' '); |
2c6f3fc9 SH |
1164 | if (p && strstr(p, "shared:")) { |
1165 | fclose(f); | |
1166 | return 1; | |
1167 | } | |
1168 | } | |
1169 | } | |
1170 | fclose(f); | |
1171 | return 0; | |
1172 | } | |
0e6e3a41 | 1173 | |
51d0854c DY |
1174 | bool switch_to_ns(pid_t pid, const char *ns) { |
1175 | int fd, ret; | |
1176 | char nspath[MAXPATHLEN]; | |
1177 | ||
1178 | /* Switch to new ns */ | |
1179 | ret = snprintf(nspath, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns); | |
1180 | if (ret < 0 || ret >= MAXPATHLEN) | |
1181 | return false; | |
1182 | ||
1183 | fd = open(nspath, O_RDONLY); | |
1184 | if (fd < 0) { | |
1185 | SYSERROR("failed to open %s", nspath); | |
1186 | return false; | |
1187 | } | |
1188 | ||
1189 | ret = setns(fd, 0); | |
1190 | if (ret) { | |
1191 | SYSERROR("failed to set process %d to %s of %d.", pid, ns, fd); | |
1192 | close(fd); | |
1193 | return false; | |
1194 | } | |
1195 | close(fd); | |
1196 | return true; | |
1197 | } | |
1198 | ||
b7f954bb SH |
1199 | /* |
1200 | * looking at fs/proc_namespace.c, it appears we can | |
1201 | * actually expect the rootfs entry to very specifically contain | |
1202 | * " - rootfs rootfs " | |
1203 | * IIUC, so long as we've chrooted so that rootfs is not our root, | |
1204 | * the rootfs entry should always be skipped in mountinfo contents. | |
1205 | */ | |
fa454c8e | 1206 | bool detect_ramfs_rootfs(void) |
b7f954bb | 1207 | { |
b7f954bb | 1208 | FILE *f; |
fa454c8e CB |
1209 | char *p, *p2; |
1210 | char *line = NULL; | |
1211 | size_t len = 0; | |
b7f954bb | 1212 | int i; |
b7f954bb SH |
1213 | |
1214 | f = fopen("/proc/self/mountinfo", "r"); | |
1215 | if (!f) | |
fa454c8e CB |
1216 | return false; |
1217 | ||
1218 | while (getline(&line, &len, f) != -1) { | |
1219 | for (p = line, i = 0; p && i < 4; i++) | |
1220 | p = strchr(p + 1, ' '); | |
b7f954bb SH |
1221 | if (!p) |
1222 | continue; | |
fa454c8e | 1223 | p2 = strchr(p + 1, ' '); |
b7f954bb SH |
1224 | if (!p2) |
1225 | continue; | |
1226 | *p2 = '\0'; | |
fa454c8e | 1227 | if (strcmp(p + 1, "/") == 0) { |
1a0e70ac | 1228 | /* This is '/'. Is it the ramfs? */ |
fa454c8e | 1229 | p = strchr(p2 + 1, '-'); |
b7f954bb | 1230 | if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) { |
fa454c8e | 1231 | free(line); |
b7f954bb | 1232 | fclose(f); |
fa454c8e | 1233 | return true; |
b7f954bb SH |
1234 | } |
1235 | } | |
1236 | } | |
fa454c8e | 1237 | free(line); |
b7f954bb | 1238 | fclose(f); |
fa454c8e | 1239 | return false; |
b7f954bb SH |
1240 | } |
1241 | ||
df6a2945 | 1242 | char *on_path(const char *cmd, const char *rootfs) { |
0e6e3a41 SG |
1243 | char *path = NULL; |
1244 | char *entry = NULL; | |
1245 | char *saveptr = NULL; | |
1246 | char cmdpath[MAXPATHLEN]; | |
1247 | int ret; | |
1248 | ||
1249 | path = getenv("PATH"); | |
1250 | if (!path) | |
8afb3e61 | 1251 | return NULL; |
0e6e3a41 SG |
1252 | |
1253 | path = strdup(path); | |
1254 | if (!path) | |
8afb3e61 | 1255 | return NULL; |
0e6e3a41 SG |
1256 | |
1257 | entry = strtok_r(path, ":", &saveptr); | |
1258 | while (entry) { | |
9d9c111c SH |
1259 | if (rootfs) |
1260 | ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd); | |
1261 | else | |
1262 | ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd); | |
0e6e3a41 SG |
1263 | |
1264 | if (ret < 0 || ret >= MAXPATHLEN) | |
1265 | goto next_loop; | |
1266 | ||
1267 | if (access(cmdpath, X_OK) == 0) { | |
1268 | free(path); | |
8afb3e61 | 1269 | return strdup(cmdpath); |
0e6e3a41 SG |
1270 | } |
1271 | ||
1272 | next_loop: | |
b707e368 | 1273 | entry = strtok_r(NULL, ":", &saveptr); |
0e6e3a41 SG |
1274 | } |
1275 | ||
1276 | free(path); | |
8afb3e61 | 1277 | return NULL; |
0e6e3a41 | 1278 | } |
76a26f55 SH |
1279 | |
1280 | bool file_exists(const char *f) | |
1281 | { | |
1282 | struct stat statbuf; | |
1283 | ||
1284 | return stat(f, &statbuf) == 0; | |
1285 | } | |
9d9c111c | 1286 | |
12983ba4 SH |
1287 | bool cgns_supported(void) |
1288 | { | |
1289 | return file_exists("/proc/self/ns/cgroup"); | |
1290 | } | |
1291 | ||
9d9c111c SH |
1292 | /* historically lxc-init has been under /usr/lib/lxc and under |
1293 | * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc. | |
1294 | */ | |
1295 | char *choose_init(const char *rootfs) | |
1296 | { | |
1297 | char *retv = NULL; | |
370ec268 SF |
1298 | const char *empty = "", |
1299 | *tmp; | |
9d9c111c | 1300 | int ret, env_set = 0; |
9d9c111c SH |
1301 | |
1302 | if (!getenv("PATH")) { | |
1303 | if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0)) | |
1304 | SYSERROR("Failed to setenv"); | |
1305 | env_set = 1; | |
1306 | } | |
1307 | ||
1308 | retv = on_path("init.lxc", rootfs); | |
1309 | ||
1310 | if (env_set) { | |
1311 | if (unsetenv("PATH")) | |
1312 | SYSERROR("Failed to unsetenv"); | |
1313 | } | |
1314 | ||
1315 | if (retv) | |
1316 | return retv; | |
1317 | ||
1318 | retv = malloc(PATH_MAX); | |
1319 | if (!retv) | |
1320 | return NULL; | |
1321 | ||
1322 | if (rootfs) | |
370ec268 | 1323 | tmp = rootfs; |
9d9c111c | 1324 | else |
370ec268 SF |
1325 | tmp = empty; |
1326 | ||
1327 | ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc"); | |
9d9c111c SH |
1328 | if (ret < 0 || ret >= PATH_MAX) { |
1329 | ERROR("pathname too long"); | |
1330 | goto out1; | |
1331 | } | |
e57cd7e9 | 1332 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1333 | return retv; |
1334 | ||
370ec268 | 1335 | ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init"); |
9d9c111c SH |
1336 | if (ret < 0 || ret >= PATH_MAX) { |
1337 | ERROR("pathname too long"); | |
1338 | goto out1; | |
1339 | } | |
e57cd7e9 | 1340 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1341 | return retv; |
1342 | ||
370ec268 | 1343 | ret = snprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp); |
9d9c111c SH |
1344 | if (ret < 0 || ret >= PATH_MAX) { |
1345 | ERROR("pathname too long"); | |
1346 | goto out1; | |
1347 | } | |
e57cd7e9 | 1348 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1349 | return retv; |
1350 | ||
370ec268 | 1351 | ret = snprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp); |
9d9c111c SH |
1352 | if (ret < 0 || ret >= PATH_MAX) { |
1353 | ERROR("pathname too long"); | |
1354 | goto out1; | |
1355 | } | |
e57cd7e9 | 1356 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1357 | return retv; |
1358 | ||
1359 | /* | |
1360 | * Last resort, look for the statically compiled init.lxc which we | |
1361 | * hopefully bind-mounted in. | |
1362 | * If we are called during container setup, and we get to this point, | |
1363 | * then the init.lxc.static from the host will need to be bind-mounted | |
1364 | * in. So we return NULL here to indicate that. | |
1365 | */ | |
1366 | if (rootfs) | |
1367 | goto out1; | |
1368 | ||
1369 | ret = snprintf(retv, PATH_MAX, "/init.lxc.static"); | |
1370 | if (ret < 0 || ret >= PATH_MAX) { | |
1371 | WARN("Nonsense - name /lxc.init.static too long"); | |
1372 | goto out1; | |
1373 | } | |
e57cd7e9 | 1374 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1375 | return retv; |
1376 | ||
1377 | out1: | |
1378 | free(retv); | |
1379 | return NULL; | |
1380 | } | |
735f2c6e TA |
1381 | |
1382 | int print_to_file(const char *file, const char *content) | |
1383 | { | |
1384 | FILE *f; | |
1385 | int ret = 0; | |
1386 | ||
1387 | f = fopen(file, "w"); | |
1388 | if (!f) | |
1389 | return -1; | |
1390 | if (fprintf(f, "%s", content) != strlen(content)) | |
1391 | ret = -1; | |
1392 | fclose(f); | |
1393 | return ret; | |
1394 | } | |
e1daebd9 SH |
1395 | |
1396 | int is_dir(const char *path) | |
1397 | { | |
1398 | struct stat statbuf; | |
1399 | int ret = stat(path, &statbuf); | |
1400 | if (ret == 0 && S_ISDIR(statbuf.st_mode)) | |
1401 | return 1; | |
1402 | return 0; | |
1403 | } | |
6010a416 SG |
1404 | |
1405 | /* | |
1406 | * Given the '-t' template option to lxc-create, figure out what to | |
1407 | * do. If the template is a full executable path, use that. If it | |
1408 | * is something like 'sshd', then return $templatepath/lxc-sshd. | |
1409 | * On success return the template, on error return NULL. | |
1410 | */ | |
1411 | char *get_template_path(const char *t) | |
1412 | { | |
1413 | int ret, len; | |
1414 | char *tpath; | |
1415 | ||
1416 | if (t[0] == '/' && access(t, X_OK) == 0) { | |
1417 | tpath = strdup(t); | |
1418 | return tpath; | |
1419 | } | |
1420 | ||
1421 | len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; | |
1422 | tpath = malloc(len); | |
1423 | if (!tpath) | |
1424 | return NULL; | |
1425 | ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); | |
1426 | if (ret < 0 || ret >= len) { | |
1427 | free(tpath); | |
1428 | return NULL; | |
1429 | } | |
1430 | if (access(tpath, X_OK) < 0) { | |
1431 | SYSERROR("bad template: %s", t); | |
1432 | free(tpath); | |
1433 | return NULL; | |
1434 | } | |
1435 | ||
1436 | return tpath; | |
1437 | } | |
0a4be28d | 1438 | |
592fd47a SH |
1439 | /* |
1440 | * @path: a pathname where / replaced with '\0'. | |
1441 | * @offsetp: pointer to int showing which path segment was last seen. | |
1442 | * Updated on return to reflect the next segment. | |
1443 | * @fulllen: full original path length. | |
1444 | * Returns a pointer to the next path segment, or NULL if done. | |
1445 | */ | |
1446 | static char *get_nextpath(char *path, int *offsetp, int fulllen) | |
1447 | { | |
1448 | int offset = *offsetp; | |
1449 | ||
1450 | if (offset >= fulllen) | |
1451 | return NULL; | |
1452 | ||
1453 | while (path[offset] != '\0' && offset < fulllen) | |
1454 | offset++; | |
1455 | while (path[offset] == '\0' && offset < fulllen) | |
1456 | offset++; | |
1457 | ||
1458 | *offsetp = offset; | |
1459 | return (offset < fulllen) ? &path[offset] : NULL; | |
1460 | } | |
1461 | ||
1462 | /* | |
1463 | * Check that @subdir is a subdir of @dir. @len is the length of | |
1464 | * @dir (to avoid having to recalculate it). | |
1465 | */ | |
1466 | static bool is_subdir(const char *subdir, const char *dir, size_t len) | |
1467 | { | |
1468 | size_t subdirlen = strlen(subdir); | |
1469 | ||
1470 | if (subdirlen < len) | |
1471 | return false; | |
1472 | if (strncmp(subdir, dir, len) != 0) | |
1473 | return false; | |
1474 | if (dir[len-1] == '/') | |
1475 | return true; | |
1476 | if (subdir[len] == '/' || subdirlen == len) | |
1477 | return true; | |
1478 | return false; | |
1479 | } | |
1480 | ||
1481 | /* | |
1482 | * Check if the open fd is a symlink. Return -ELOOP if it is. Return | |
1483 | * -ENOENT if we couldn't fstat. Return 0 if the fd is ok. | |
1484 | */ | |
1485 | static int check_symlink(int fd) | |
1486 | { | |
1487 | struct stat sb; | |
1488 | int ret = fstat(fd, &sb); | |
1489 | if (ret < 0) | |
1490 | return -ENOENT; | |
1491 | if (S_ISLNK(sb.st_mode)) | |
1492 | return -ELOOP; | |
1493 | return 0; | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * Open a file or directory, provided that it contains no symlinks. | |
1498 | * | |
1499 | * CAVEAT: This function must not be used for other purposes than container | |
1500 | * setup before executing the container's init | |
1501 | */ | |
1502 | static int open_if_safe(int dirfd, const char *nextpath) | |
1503 | { | |
1504 | int newfd = openat(dirfd, nextpath, O_RDONLY | O_NOFOLLOW); | |
1a0e70ac | 1505 | if (newfd >= 0) /* Was not a symlink, all good. */ |
592fd47a SH |
1506 | return newfd; |
1507 | ||
1508 | if (errno == ELOOP) | |
1509 | return newfd; | |
1510 | ||
1511 | if (errno == EPERM || errno == EACCES) { | |
1a0e70ac CB |
1512 | /* We're not root (cause we got EPERM) so try opening with |
1513 | * O_PATH. | |
1514 | */ | |
592fd47a SH |
1515 | newfd = openat(dirfd, nextpath, O_PATH | O_NOFOLLOW); |
1516 | if (newfd >= 0) { | |
1a0e70ac CB |
1517 | /* O_PATH will return an fd for symlinks. We know |
1518 | * nextpath wasn't a symlink at last openat, so if fd is | |
1519 | * now a link, then something * fishy is going on. | |
592fd47a SH |
1520 | */ |
1521 | int ret = check_symlink(newfd); | |
1522 | if (ret < 0) { | |
1523 | close(newfd); | |
1524 | newfd = ret; | |
1525 | } | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | return newfd; | |
1530 | } | |
1531 | ||
1532 | /* | |
1533 | * Open a path intending for mounting, ensuring that the final path | |
1534 | * is inside the container's rootfs. | |
1535 | * | |
1536 | * CAVEAT: This function must not be used for other purposes than container | |
1537 | * setup before executing the container's init | |
1538 | * | |
1539 | * @target: path to be opened | |
1540 | * @prefix_skip: a part of @target in which to ignore symbolic links. This | |
1541 | * would be the container's rootfs. | |
1542 | * | |
1543 | * Return an open fd for the path, or <0 on error. | |
1544 | */ | |
1545 | static int open_without_symlink(const char *target, const char *prefix_skip) | |
1546 | { | |
1547 | int curlen = 0, dirfd, fulllen, i; | |
1548 | char *dup = NULL; | |
1549 | ||
1550 | fulllen = strlen(target); | |
1551 | ||
1552 | /* make sure prefix-skip makes sense */ | |
01074e5b | 1553 | if (prefix_skip && strlen(prefix_skip) > 0) { |
592fd47a SH |
1554 | curlen = strlen(prefix_skip); |
1555 | if (!is_subdir(target, prefix_skip, curlen)) { | |
1556 | ERROR("WHOA there - target '%s' didn't start with prefix '%s'", | |
1557 | target, prefix_skip); | |
1558 | return -EINVAL; | |
1559 | } | |
1560 | /* | |
1561 | * get_nextpath() expects the curlen argument to be | |
1562 | * on a (turned into \0) / or before it, so decrement | |
1563 | * curlen to make sure that happens | |
1564 | */ | |
1565 | if (curlen) | |
1566 | curlen--; | |
1567 | } else { | |
1568 | prefix_skip = "/"; | |
1569 | curlen = 0; | |
1570 | } | |
1571 | ||
1572 | /* Make a copy of target which we can hack up, and tokenize it */ | |
1573 | if ((dup = strdup(target)) == NULL) { | |
1574 | SYSERROR("Out of memory checking for symbolic link"); | |
1575 | return -ENOMEM; | |
1576 | } | |
1577 | for (i = 0; i < fulllen; i++) { | |
1578 | if (dup[i] == '/') | |
1579 | dup[i] = '\0'; | |
1580 | } | |
1581 | ||
1582 | dirfd = open(prefix_skip, O_RDONLY); | |
1583 | if (dirfd < 0) | |
1584 | goto out; | |
1585 | while (1) { | |
1586 | int newfd, saved_errno; | |
1587 | char *nextpath; | |
1588 | ||
1589 | if ((nextpath = get_nextpath(dup, &curlen, fulllen)) == NULL) | |
1590 | goto out; | |
1591 | newfd = open_if_safe(dirfd, nextpath); | |
1592 | saved_errno = errno; | |
1593 | close(dirfd); | |
1594 | dirfd = newfd; | |
1595 | if (newfd < 0) { | |
1596 | errno = saved_errno; | |
1597 | if (errno == ELOOP) | |
1598 | SYSERROR("%s in %s was a symbolic link!", nextpath, target); | |
592fd47a SH |
1599 | goto out; |
1600 | } | |
1601 | } | |
1602 | ||
1603 | out: | |
1604 | free(dup); | |
1605 | return dirfd; | |
1606 | } | |
1607 | ||
1608 | /* | |
1609 | * Safely mount a path into a container, ensuring that the mount target | |
1610 | * is under the container's @rootfs. (If @rootfs is NULL, then the container | |
1611 | * uses the host's /) | |
1612 | * | |
1613 | * CAVEAT: This function must not be used for other purposes than container | |
1614 | * setup before executing the container's init | |
1615 | */ | |
1616 | int safe_mount(const char *src, const char *dest, const char *fstype, | |
1617 | unsigned long flags, const void *data, const char *rootfs) | |
1618 | { | |
1a0e70ac CB |
1619 | int destfd, ret, saved_errno; |
1620 | /* Only needs enough for /proc/self/fd/<fd>. */ | |
1621 | char srcbuf[50], destbuf[50]; | |
1622 | int srcfd = -1; | |
592fd47a SH |
1623 | const char *mntsrc = src; |
1624 | ||
1625 | if (!rootfs) | |
1626 | rootfs = ""; | |
1627 | ||
1628 | /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */ | |
1629 | if (flags & MS_BIND && src && src[0] != '/') { | |
1630 | INFO("this is a relative bind mount"); | |
1631 | srcfd = open_without_symlink(src, NULL); | |
1632 | if (srcfd < 0) | |
1633 | return srcfd; | |
1634 | ret = snprintf(srcbuf, 50, "/proc/self/fd/%d", srcfd); | |
1635 | if (ret < 0 || ret > 50) { | |
1636 | close(srcfd); | |
1637 | ERROR("Out of memory"); | |
1638 | return -EINVAL; | |
1639 | } | |
1640 | mntsrc = srcbuf; | |
1641 | } | |
1642 | ||
1643 | destfd = open_without_symlink(dest, rootfs); | |
1644 | if (destfd < 0) { | |
88e078ba CB |
1645 | if (srcfd != -1) { |
1646 | saved_errno = errno; | |
592fd47a | 1647 | close(srcfd); |
88e078ba CB |
1648 | errno = saved_errno; |
1649 | } | |
592fd47a SH |
1650 | return destfd; |
1651 | } | |
1652 | ||
1653 | ret = snprintf(destbuf, 50, "/proc/self/fd/%d", destfd); | |
1654 | if (ret < 0 || ret > 50) { | |
1655 | if (srcfd != -1) | |
1656 | close(srcfd); | |
1657 | close(destfd); | |
1658 | ERROR("Out of memory"); | |
1659 | return -EINVAL; | |
1660 | } | |
1661 | ||
1662 | ret = mount(mntsrc, destbuf, fstype, flags, data); | |
1663 | saved_errno = errno; | |
1664 | if (srcfd != -1) | |
1665 | close(srcfd); | |
1666 | close(destfd); | |
1667 | if (ret < 0) { | |
1668 | errno = saved_errno; | |
0103eb53 | 1669 | SYSERROR("Failed to mount %s onto %s", src ? src : "(null)", dest); |
592fd47a SH |
1670 | return ret; |
1671 | } | |
1672 | ||
1673 | return 0; | |
1674 | } | |
1675 | ||
ced03a01 SH |
1676 | /* |
1677 | * Mount a proc under @rootfs if proc self points to a pid other than | |
1678 | * my own. This is needed to have a known-good proc mount for setting | |
1679 | * up LSMs both at container startup and attach. | |
1680 | * | |
1681 | * @rootfs : the rootfs where proc should be mounted | |
1682 | * | |
1683 | * Returns < 0 on failure, 0 if the correct proc was already mounted | |
1684 | * and 1 if a new proc was mounted. | |
f267d666 BP |
1685 | * |
1686 | * NOTE: not to be called from inside the container namespace! | |
ced03a01 | 1687 | */ |
943144d9 | 1688 | int lxc_mount_proc_if_needed(const char *rootfs) |
ced03a01 SH |
1689 | { |
1690 | char path[MAXPATHLEN]; | |
6b1ba5d6 CB |
1691 | int link_to_pid, linklen, mypid, ret; |
1692 | char link[LXC_NUMSTRLEN64] = {0}; | |
ced03a01 SH |
1693 | |
1694 | ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs); | |
1695 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1696 | SYSERROR("proc path name too long"); | |
1697 | return -1; | |
1698 | } | |
fc2ad9dc | 1699 | |
6b1ba5d6 | 1700 | linklen = readlink(path, link, LXC_NUMSTRLEN64); |
fc2ad9dc | 1701 | |
ced03a01 | 1702 | ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs); |
d539a2b2 CB |
1703 | if (ret < 0 || ret >= MAXPATHLEN) { |
1704 | SYSERROR("proc path name too long"); | |
1705 | return -1; | |
1706 | } | |
fc2ad9dc CB |
1707 | |
1708 | /* /proc not mounted */ | |
1709 | if (linklen < 0) { | |
1710 | if (mkdir(path, 0755) && errno != EEXIST) | |
1711 | return -1; | |
ced03a01 | 1712 | goto domount; |
6b1ba5d6 CB |
1713 | } else if (linklen >= LXC_NUMSTRLEN64) { |
1714 | link[linklen - 1] = '\0'; | |
1715 | ERROR("readlink returned truncated content: \"%s\"", link); | |
1716 | return -1; | |
fc2ad9dc CB |
1717 | } |
1718 | ||
0059379f | 1719 | mypid = lxc_raw_getpid(); |
6b1ba5d6 CB |
1720 | INFO("I am %d, /proc/self points to \"%s\"", mypid, link); |
1721 | ||
2d036cca CB |
1722 | if (lxc_safe_int(link, &link_to_pid) < 0) |
1723 | return -1; | |
fc2ad9dc | 1724 | |
6b1ba5d6 CB |
1725 | /* correct procfs is already mounted */ |
1726 | if (link_to_pid == mypid) | |
1727 | return 0; | |
fc2ad9dc | 1728 | |
6b1ba5d6 CB |
1729 | ret = umount2(path, MNT_DETACH); |
1730 | if (ret < 0) | |
1731 | WARN("failed to umount \"%s\" with MNT_DETACH", path); | |
ced03a01 SH |
1732 | |
1733 | domount: | |
fc2ad9dc | 1734 | /* rootfs is NULL */ |
6b1ba5d6 | 1735 | if (!strcmp(rootfs, "")) |
f267d666 BP |
1736 | ret = mount("proc", path, "proc", 0, NULL); |
1737 | else | |
1738 | ret = safe_mount("proc", path, "proc", 0, NULL, rootfs); | |
f267d666 | 1739 | if (ret < 0) |
ced03a01 | 1740 | return -1; |
f267d666 | 1741 | |
fc2ad9dc | 1742 | INFO("mounted /proc in container for security transition"); |
ced03a01 SH |
1743 | return 1; |
1744 | } | |
69aeabac | 1745 | |
f8dd0275 | 1746 | int open_devnull(void) |
69aeabac | 1747 | { |
f8dd0275 AM |
1748 | int fd = open("/dev/null", O_RDWR); |
1749 | ||
1750 | if (fd < 0) | |
1751 | SYSERROR("Can't open /dev/null"); | |
1752 | ||
1753 | return fd; | |
1754 | } | |
69aeabac | 1755 | |
f8dd0275 AM |
1756 | int set_stdfds(int fd) |
1757 | { | |
bbbf65ee CB |
1758 | int ret; |
1759 | ||
69aeabac TA |
1760 | if (fd < 0) |
1761 | return -1; | |
1762 | ||
bbbf65ee CB |
1763 | ret = dup2(fd, STDIN_FILENO); |
1764 | if (ret < 0) | |
f8dd0275 | 1765 | return -1; |
bbbf65ee CB |
1766 | |
1767 | ret = dup2(fd, STDOUT_FILENO); | |
1768 | if (ret < 0) | |
f8dd0275 | 1769 | return -1; |
bbbf65ee CB |
1770 | |
1771 | ret = dup2(fd, STDERR_FILENO); | |
1772 | if (ret < 0) | |
f8dd0275 AM |
1773 | return -1; |
1774 | ||
1775 | return 0; | |
1776 | } | |
1777 | ||
1778 | int null_stdfds(void) | |
1779 | { | |
1780 | int ret = -1; | |
1781 | int fd = open_devnull(); | |
1782 | ||
1783 | if (fd >= 0) { | |
1784 | ret = set_stdfds(fd); | |
1785 | close(fd); | |
1786 | } | |
69aeabac | 1787 | |
69aeabac TA |
1788 | return ret; |
1789 | } | |
ccb4cabe SH |
1790 | |
1791 | /* | |
1792 | * Return the number of lines in file @fn, or -1 on error | |
1793 | */ | |
1794 | int lxc_count_file_lines(const char *fn) | |
1795 | { | |
1796 | FILE *f; | |
1797 | char *line = NULL; | |
1798 | size_t sz = 0; | |
1799 | int n = 0; | |
1800 | ||
1801 | f = fopen_cloexec(fn, "r"); | |
1802 | if (!f) | |
1803 | return -1; | |
1804 | ||
1805 | while (getline(&line, &sz, f) != -1) { | |
1806 | n++; | |
1807 | } | |
1808 | free(line); | |
1809 | fclose(f); | |
1810 | return n; | |
1811 | } | |
1adbd020 | 1812 | |
25086a5f CB |
1813 | void *lxc_strmmap(void *addr, size_t length, int prot, int flags, int fd, |
1814 | off_t offset) | |
1adbd020 CB |
1815 | { |
1816 | void *tmp = NULL, *overlap = NULL; | |
1817 | ||
1818 | /* We establish an anonymous mapping that is one byte larger than the | |
1819 | * underlying file. The pages handed to us are zero filled. */ | |
1820 | tmp = mmap(addr, length + 1, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
1821 | if (tmp == MAP_FAILED) | |
a1e5280d | 1822 | return tmp; |
1adbd020 CB |
1823 | |
1824 | /* Now we establish a fixed-address mapping starting at the address we | |
1825 | * received from our anonymous mapping and replace all bytes excluding | |
1826 | * the additional \0-byte with the file. This allows us to use normal | |
a1e5280d | 1827 | * string-handling functions. */ |
1adbd020 CB |
1828 | overlap = mmap(tmp, length, prot, MAP_FIXED | flags, fd, offset); |
1829 | if (overlap == MAP_FAILED) | |
a1e5280d | 1830 | munmap(tmp, length + 1); |
1adbd020 | 1831 | |
1adbd020 CB |
1832 | return overlap; |
1833 | } | |
1834 | ||
25086a5f | 1835 | int lxc_strmunmap(void *addr, size_t length) |
1adbd020 CB |
1836 | { |
1837 | return munmap(addr, length + 1); | |
1838 | } | |
330ae3d3 CB |
1839 | |
1840 | /* Check whether a signal is blocked by a process. */ | |
de3c491b | 1841 | /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */ |
eab15c1e | 1842 | #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1) |
330ae3d3 CB |
1843 | bool task_blocking_signal(pid_t pid, int signal) |
1844 | { | |
1845 | bool bret = false; | |
1846 | char *line = NULL; | |
1847 | long unsigned int sigblk = 0; | |
1848 | size_t n = 0; | |
1849 | int ret; | |
1850 | FILE *f; | |
1851 | ||
de3c491b | 1852 | char status[__PROC_STATUS_LEN]; |
330ae3d3 | 1853 | |
de3c491b CB |
1854 | ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid); |
1855 | if (ret < 0 || ret >= __PROC_STATUS_LEN) | |
330ae3d3 CB |
1856 | return bret; |
1857 | ||
1858 | f = fopen(status, "r"); | |
1859 | if (!f) | |
1860 | return bret; | |
1861 | ||
1862 | while (getline(&line, &n, f) != -1) { | |
6fbcbe3b CB |
1863 | if (strncmp(line, "SigBlk:\t", 8)) |
1864 | continue; | |
1865 | ||
1866 | if (sscanf(line + 8, "%lx", &sigblk) != 1) | |
1867 | goto out; | |
330ae3d3 CB |
1868 | } |
1869 | ||
6fbcbe3b | 1870 | if (sigblk & (1LU << (signal - 1))) |
330ae3d3 CB |
1871 | bret = true; |
1872 | ||
1873 | out: | |
1874 | free(line); | |
1875 | fclose(f); | |
1876 | return bret; | |
1877 | } | |
000dfda7 CB |
1878 | |
1879 | static int lxc_append_null_to_list(void ***list) | |
1880 | { | |
1881 | int newentry = 0; | |
1882 | void **tmp; | |
1883 | ||
1884 | if (*list) | |
1885 | for (; (*list)[newentry]; newentry++) { | |
1886 | ; | |
1887 | } | |
1888 | ||
1889 | tmp = realloc(*list, (newentry + 2) * sizeof(void **)); | |
1890 | if (!tmp) | |
1891 | return -1; | |
1892 | ||
1893 | *list = tmp; | |
1894 | (*list)[newentry + 1] = NULL; | |
1895 | ||
1896 | return newentry; | |
1897 | } | |
1898 | ||
1899 | int lxc_append_string(char ***list, char *entry) | |
1900 | { | |
000dfda7 | 1901 | char *copy; |
a54694f8 CB |
1902 | int newentry; |
1903 | ||
1904 | newentry = lxc_append_null_to_list((void ***)list); | |
1905 | if (newentry < 0) | |
1906 | return -1; | |
000dfda7 CB |
1907 | |
1908 | copy = strdup(entry); | |
1909 | if (!copy) | |
1910 | return -1; | |
1911 | ||
1912 | (*list)[newentry] = copy; | |
1913 | ||
1914 | return 0; | |
1915 | } | |
a687256f CB |
1916 | |
1917 | int lxc_preserve_ns(const int pid, const char *ns) | |
1918 | { | |
1919 | int ret; | |
a052913d CB |
1920 | /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */ |
1921 | #define __NS_PATH_LEN 50 | |
1922 | char path[__NS_PATH_LEN]; | |
a687256f | 1923 | |
4d8ac866 CB |
1924 | /* This way we can use this function to also check whether namespaces |
1925 | * are supported by the kernel by passing in the NULL or the empty | |
1926 | * string. | |
1927 | */ | |
a052913d | 1928 | ret = snprintf(path, __NS_PATH_LEN, "/proc/%d/ns%s%s", pid, |
4d8ac866 CB |
1929 | !ns || strcmp(ns, "") == 0 ? "" : "/", |
1930 | !ns || strcmp(ns, "") == 0 ? "" : ns); | |
134284c3 | 1931 | errno = EFBIG; |
a052913d | 1932 | if (ret < 0 || (size_t)ret >= __NS_PATH_LEN) |
134284c3 | 1933 | return -EFBIG; |
a687256f CB |
1934 | |
1935 | return open(path, O_RDONLY | O_CLOEXEC); | |
1936 | } | |
6bc2eafe CB |
1937 | |
1938 | int lxc_safe_uint(const char *numstr, unsigned int *converted) | |
1939 | { | |
1940 | char *err = NULL; | |
1941 | unsigned long int uli; | |
1942 | ||
643c1984 CB |
1943 | while (isspace(*numstr)) |
1944 | numstr++; | |
1945 | ||
1946 | if (*numstr == '-') | |
1947 | return -EINVAL; | |
1948 | ||
6bc2eafe CB |
1949 | errno = 0; |
1950 | uli = strtoul(numstr, &err, 0); | |
643c1984 | 1951 | if (errno == ERANGE && uli == ULONG_MAX) |
ff0e49c7 | 1952 | return -ERANGE; |
6bc2eafe | 1953 | |
643c1984 | 1954 | if (err == numstr || *err != '\0') |
6bc2eafe CB |
1955 | return -EINVAL; |
1956 | ||
1957 | if (uli > UINT_MAX) | |
1958 | return -ERANGE; | |
1959 | ||
8c57d930 | 1960 | *converted = (unsigned int)uli; |
6bc2eafe CB |
1961 | return 0; |
1962 | } | |
b5f845e7 | 1963 | |
681188c1 CB |
1964 | int lxc_safe_ulong(const char *numstr, unsigned long *converted) |
1965 | { | |
1966 | char *err = NULL; | |
1967 | unsigned long int uli; | |
1968 | ||
1969 | while (isspace(*numstr)) | |
1970 | numstr++; | |
1971 | ||
1972 | if (*numstr == '-') | |
1973 | return -EINVAL; | |
1974 | ||
1975 | errno = 0; | |
1976 | uli = strtoul(numstr, &err, 0); | |
1977 | if (errno == ERANGE && uli == ULONG_MAX) | |
1978 | return -ERANGE; | |
1979 | ||
1980 | if (err == numstr || *err != '\0') | |
1981 | return -EINVAL; | |
1982 | ||
1983 | *converted = uli; | |
1984 | return 0; | |
1985 | } | |
1986 | ||
b5f845e7 CB |
1987 | int lxc_safe_int(const char *numstr, int *converted) |
1988 | { | |
1989 | char *err = NULL; | |
1990 | signed long int sli; | |
1991 | ||
1992 | errno = 0; | |
1993 | sli = strtol(numstr, &err, 0); | |
643c1984 | 1994 | if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN)) |
ff0e49c7 | 1995 | return -ERANGE; |
643c1984 CB |
1996 | |
1997 | if (errno != 0 && sli == 0) | |
ff0e49c7 | 1998 | return -EINVAL; |
b5f845e7 | 1999 | |
643c1984 | 2000 | if (err == numstr || *err != '\0') |
b5f845e7 CB |
2001 | return -EINVAL; |
2002 | ||
643c1984 | 2003 | if (sli > INT_MAX || sli < INT_MIN) |
b5f845e7 CB |
2004 | return -ERANGE; |
2005 | ||
2006 | *converted = (int)sli; | |
2007 | return 0; | |
2008 | } | |
8c57d930 CB |
2009 | |
2010 | int lxc_safe_long(const char *numstr, long int *converted) | |
2011 | { | |
2012 | char *err = NULL; | |
2013 | signed long int sli; | |
2014 | ||
2015 | errno = 0; | |
2016 | sli = strtol(numstr, &err, 0); | |
643c1984 | 2017 | if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN)) |
ff0e49c7 | 2018 | return -ERANGE; |
8c57d930 | 2019 | |
643c1984 | 2020 | if (errno != 0 && sli == 0) |
ff0e49c7 | 2021 | return -EINVAL; |
8c57d930 | 2022 | |
643c1984 CB |
2023 | if (err == numstr || *err != '\0') |
2024 | return -EINVAL; | |
8c57d930 CB |
2025 | |
2026 | *converted = sli; | |
2027 | return 0; | |
2028 | } | |
dbaf55a3 | 2029 | |
b037bc67 CB |
2030 | int lxc_safe_long_long(const char *numstr, long long int *converted) |
2031 | { | |
2032 | char *err = NULL; | |
2033 | signed long long int sli; | |
2034 | ||
2035 | errno = 0; | |
2036 | sli = strtoll(numstr, &err, 0); | |
2037 | if (errno == ERANGE && (sli == LLONG_MAX || sli == LLONG_MIN)) | |
2038 | return -ERANGE; | |
2039 | ||
2040 | if (errno != 0 && sli == 0) | |
2041 | return -EINVAL; | |
2042 | ||
2043 | if (err == numstr || *err != '\0') | |
2044 | return -EINVAL; | |
2045 | ||
2046 | *converted = sli; | |
2047 | return 0; | |
2048 | } | |
2049 | ||
dbaf55a3 CB |
2050 | int lxc_switch_uid_gid(uid_t uid, gid_t gid) |
2051 | { | |
2052 | if (setgid(gid) < 0) { | |
2053 | SYSERROR("Failed to switch to gid %d.", gid); | |
2054 | return -errno; | |
2055 | } | |
2056 | NOTICE("Switched to gid %d.", gid); | |
2057 | ||
2058 | if (setuid(uid) < 0) { | |
2059 | SYSERROR("Failed to switch to uid %d.", uid); | |
2060 | return -errno; | |
2061 | } | |
2062 | NOTICE("Switched to uid %d.", uid); | |
2063 | ||
2064 | return 0; | |
2065 | } | |
2066 | ||
2067 | /* Simple covenience function which enables uniform logging. */ | |
2068 | int lxc_setgroups(int size, gid_t list[]) | |
2069 | { | |
2070 | if (setgroups(size, list) < 0) { | |
2071 | SYSERROR("Failed to setgroups()."); | |
2072 | return -errno; | |
2073 | } | |
2074 | NOTICE("Dropped additional groups."); | |
2075 | ||
2076 | return 0; | |
2077 | } | |
c6868a1f CB |
2078 | |
2079 | static int lxc_get_unused_loop_dev_legacy(char *loop_name) | |
2080 | { | |
2081 | struct dirent *dp; | |
2082 | struct loop_info64 lo64; | |
2083 | DIR *dir; | |
2084 | int dfd = -1, fd = -1, ret = -1; | |
2085 | ||
2086 | dir = opendir("/dev"); | |
2087 | if (!dir) | |
2088 | return -1; | |
2089 | ||
2090 | while ((dp = readdir(dir))) { | |
2091 | if (!dp) | |
2092 | break; | |
2093 | ||
2094 | if (strncmp(dp->d_name, "loop", 4) != 0) | |
2095 | continue; | |
2096 | ||
2097 | dfd = dirfd(dir); | |
2098 | if (dfd < 0) | |
2099 | continue; | |
2100 | ||
2101 | fd = openat(dfd, dp->d_name, O_RDWR); | |
2102 | if (fd < 0) | |
2103 | continue; | |
2104 | ||
2105 | ret = ioctl(fd, LOOP_GET_STATUS64, &lo64); | |
2106 | if (ret < 0) { | |
2107 | if (ioctl(fd, LOOP_GET_STATUS64, &lo64) == 0 || | |
2108 | errno != ENXIO) { | |
2109 | close(fd); | |
2110 | fd = -1; | |
2111 | continue; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name); | |
2116 | if (ret < 0 || ret >= LO_NAME_SIZE) { | |
2117 | close(fd); | |
2118 | fd = -1; | |
2119 | continue; | |
2120 | } | |
2121 | ||
2122 | break; | |
2123 | } | |
2124 | ||
2125 | closedir(dir); | |
2126 | ||
2127 | if (fd < 0) | |
2128 | return -1; | |
2129 | ||
2130 | return fd; | |
2131 | } | |
2132 | ||
2133 | static int lxc_get_unused_loop_dev(char *name_loop) | |
2134 | { | |
2135 | int loop_nr, ret; | |
2136 | int fd_ctl = -1, fd_tmp = -1; | |
2137 | ||
2138 | fd_ctl = open("/dev/loop-control", O_RDWR | O_CLOEXEC); | |
2139 | if (fd_ctl < 0) | |
2140 | return -ENODEV; | |
2141 | ||
2142 | loop_nr = ioctl(fd_ctl, LOOP_CTL_GET_FREE); | |
2143 | if (loop_nr < 0) | |
2144 | goto on_error; | |
2145 | ||
2146 | ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr); | |
2147 | if (ret < 0 || ret >= LO_NAME_SIZE) | |
2148 | goto on_error; | |
2149 | ||
2150 | fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC); | |
2151 | if (fd_tmp < 0) | |
2152 | goto on_error; | |
2153 | ||
2154 | on_error: | |
2155 | close(fd_ctl); | |
2156 | return fd_tmp; | |
2157 | } | |
2158 | ||
2159 | int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags) | |
2160 | { | |
2161 | int ret; | |
2162 | struct loop_info64 lo64; | |
2163 | int fd_img = -1, fret = -1, fd_loop = -1; | |
2164 | ||
2165 | fd_loop = lxc_get_unused_loop_dev(loop_dev); | |
2166 | if (fd_loop < 0) { | |
2167 | if (fd_loop == -ENODEV) | |
2168 | fd_loop = lxc_get_unused_loop_dev_legacy(loop_dev); | |
2169 | else | |
2170 | goto on_error; | |
2171 | } | |
2172 | ||
2173 | fd_img = open(source, O_RDWR | O_CLOEXEC); | |
2174 | if (fd_img < 0) | |
2175 | goto on_error; | |
2176 | ||
2177 | ret = ioctl(fd_loop, LOOP_SET_FD, fd_img); | |
2178 | if (ret < 0) | |
2179 | goto on_error; | |
2180 | ||
2181 | memset(&lo64, 0, sizeof(lo64)); | |
2182 | lo64.lo_flags = flags; | |
2183 | ||
2184 | ret = ioctl(fd_loop, LOOP_SET_STATUS64, &lo64); | |
2185 | if (ret < 0) | |
2186 | goto on_error; | |
2187 | ||
2188 | fret = 0; | |
2189 | ||
2190 | on_error: | |
2191 | if (fd_img >= 0) | |
2192 | close(fd_img); | |
2193 | ||
2194 | if (fret < 0 && fd_loop >= 0) { | |
2195 | close(fd_loop); | |
2196 | fd_loop = -1; | |
2197 | } | |
2198 | ||
2199 | return fd_loop; | |
2200 | } | |
74251e49 CB |
2201 | |
2202 | int lxc_unstack_mountpoint(const char *path, bool lazy) | |
2203 | { | |
2204 | int ret; | |
2205 | int umounts = 0; | |
2206 | ||
2207 | pop_stack: | |
2208 | ret = umount2(path, lazy ? MNT_DETACH : 0); | |
2209 | if (ret < 0) { | |
2210 | /* We consider anything else than EINVAL deadly to prevent going | |
2211 | * into an infinite loop. (The other alternative is constantly | |
2212 | * parsing /proc/self/mountinfo which is yucky and probably | |
2213 | * racy.) | |
2214 | */ | |
2215 | if (errno != EINVAL) | |
2216 | return -errno; | |
2217 | } else { | |
b4a40f7b CB |
2218 | /* Just stop counting when this happens. That'd just be so |
2219 | * stupid that we won't even bother trying to report back the | |
2220 | * correct value anymore. | |
2221 | */ | |
2222 | if (umounts != INT_MAX) | |
2223 | umounts++; | |
74251e49 CB |
2224 | /* We succeeded in umounting. Make sure that there's no other |
2225 | * mountpoint stacked underneath. | |
2226 | */ | |
74251e49 CB |
2227 | goto pop_stack; |
2228 | } | |
2229 | ||
2230 | return umounts; | |
2231 | } | |
ea3a694f CB |
2232 | |
2233 | int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args) | |
2234 | { | |
2235 | pid_t child; | |
2236 | int ret, fret, pipefd[2]; | |
2237 | ssize_t bytes; | |
2238 | ||
2239 | /* Make sure our callers do not receive unitialized memory. */ | |
2240 | if (buf_size > 0 && buf) | |
2241 | buf[0] = '\0'; | |
2242 | ||
2243 | if (pipe(pipefd) < 0) { | |
2244 | SYSERROR("failed to create pipe"); | |
2245 | return -1; | |
2246 | } | |
2247 | ||
2d728b2f | 2248 | child = lxc_raw_clone(0); |
ea3a694f CB |
2249 | if (child < 0) { |
2250 | close(pipefd[0]); | |
2251 | close(pipefd[1]); | |
2252 | SYSERROR("failed to create new process"); | |
2253 | return -1; | |
2254 | } | |
2255 | ||
2256 | if (child == 0) { | |
2257 | /* Close the read-end of the pipe. */ | |
2258 | close(pipefd[0]); | |
2259 | ||
2260 | /* Redirect std{err,out} to write-end of the | |
2261 | * pipe. | |
2262 | */ | |
2263 | ret = dup2(pipefd[1], STDOUT_FILENO); | |
2264 | if (ret >= 0) | |
2265 | ret = dup2(pipefd[1], STDERR_FILENO); | |
2266 | ||
2267 | /* Close the write-end of the pipe. */ | |
2268 | close(pipefd[1]); | |
2269 | ||
2270 | if (ret < 0) { | |
2271 | SYSERROR("failed to duplicate std{err,out} file descriptor"); | |
2272 | exit(EXIT_FAILURE); | |
2273 | } | |
2274 | ||
2275 | /* Does not return. */ | |
2276 | child_fn(args); | |
2277 | ERROR("failed to exec command"); | |
2278 | exit(EXIT_FAILURE); | |
2279 | } | |
2280 | ||
2281 | /* close the write-end of the pipe */ | |
2282 | close(pipefd[1]); | |
2283 | ||
7a643c7c CB |
2284 | if (buf && buf_size > 0) { |
2285 | bytes = read(pipefd[0], buf, buf_size - 1); | |
2286 | if (bytes > 0) | |
2287 | buf[bytes - 1] = '\0'; | |
2288 | } | |
ea3a694f CB |
2289 | |
2290 | fret = wait_for_pid(child); | |
2291 | /* close the read-end of the pipe */ | |
2292 | close(pipefd[0]); | |
2293 | ||
2294 | return fret; | |
2295 | } | |
04ad7ffe CB |
2296 | |
2297 | char *must_make_path(const char *first, ...) | |
2298 | { | |
2299 | va_list args; | |
2300 | char *cur, *dest; | |
2301 | size_t full_len = strlen(first); | |
2302 | ||
2303 | dest = must_copy_string(first); | |
2304 | ||
2305 | va_start(args, first); | |
2306 | while ((cur = va_arg(args, char *)) != NULL) { | |
2307 | full_len += strlen(cur); | |
2308 | if (cur[0] != '/') | |
2309 | full_len++; | |
2310 | dest = must_realloc(dest, full_len + 1); | |
2311 | if (cur[0] != '/') | |
2312 | strcat(dest, "/"); | |
2313 | strcat(dest, cur); | |
2314 | } | |
2315 | va_end(args); | |
2316 | ||
2317 | return dest; | |
2318 | } | |
2319 | ||
0c3deb94 CB |
2320 | char *must_append_path(char *first, ...) |
2321 | { | |
2322 | char *cur; | |
2323 | size_t full_len; | |
2324 | va_list args; | |
2325 | char *dest = first; | |
2326 | ||
2327 | full_len = strlen(first); | |
2328 | va_start(args, first); | |
2329 | while ((cur = va_arg(args, char *)) != NULL) { | |
2330 | full_len += strlen(cur); | |
2331 | ||
2332 | if (cur[0] != '/') | |
2333 | full_len++; | |
2334 | ||
2335 | dest = must_realloc(dest, full_len + 1); | |
2336 | ||
2337 | if (cur[0] != '/') | |
2338 | strcat(dest, "/"); | |
2339 | ||
2340 | strcat(dest, cur); | |
2341 | } | |
2342 | va_end(args); | |
2343 | ||
2344 | return dest; | |
2345 | } | |
2346 | ||
04ad7ffe CB |
2347 | char *must_copy_string(const char *entry) |
2348 | { | |
2349 | char *ret; | |
2350 | ||
2351 | if (!entry) | |
2352 | return NULL; | |
2353 | do { | |
2354 | ret = strdup(entry); | |
2355 | } while (!ret); | |
2356 | ||
2357 | return ret; | |
2358 | } | |
2359 | ||
2360 | void *must_realloc(void *orig, size_t sz) | |
2361 | { | |
2362 | void *ret; | |
2363 | ||
2364 | do { | |
2365 | ret = realloc(orig, sz); | |
2366 | } while (!ret); | |
2367 | ||
2368 | return ret; | |
2369 | } | |
a035c53a CB |
2370 | |
2371 | bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val) | |
2372 | { | |
2373 | return (fs->f_type == (fs_type_magic)magic_val); | |
2374 | } | |
2375 | ||
2376 | bool has_fs_type(const char *path, fs_type_magic magic_val) | |
2377 | { | |
2378 | bool has_type; | |
2379 | int ret; | |
2380 | struct statfs sb; | |
2381 | ||
2382 | ret = statfs(path, &sb); | |
2383 | if (ret < 0) | |
2384 | return false; | |
2385 | ||
2386 | has_type = is_fs_type(&sb, magic_val); | |
2387 | if (!has_type && magic_val == RAMFS_MAGIC) | |
2388 | WARN("When the ramfs it a tmpfs statfs() might report tmpfs"); | |
2389 | ||
2390 | return has_type; | |
2391 | } | |
d75c14e2 CB |
2392 | |
2393 | bool lxc_nic_exists(char *nic) | |
2394 | { | |
2395 | #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1 | |
2396 | char path[__LXC_SYS_CLASS_NET_LEN]; | |
2397 | int ret; | |
2398 | struct stat sb; | |
2399 | ||
2400 | if (!strcmp(nic, "none")) | |
2401 | return true; | |
2402 | ||
2403 | ret = snprintf(path, __LXC_SYS_CLASS_NET_LEN, "/sys/class/net/%s", nic); | |
2404 | if (ret < 0 || (size_t)ret >= __LXC_SYS_CLASS_NET_LEN) | |
2405 | return false; | |
2406 | ||
2407 | ret = stat(path, &sb); | |
2408 | if (ret < 0) | |
2409 | return false; | |
2410 | ||
2411 | return true; | |
2412 | } | |
127c6e70 CB |
2413 | |
2414 | int lxc_make_tmpfile(char *template, bool rm) | |
2415 | { | |
2416 | int fd, ret; | |
2417 | ||
2418 | fd = mkstemp(template); | |
2419 | if (fd < 0) | |
2420 | return -1; | |
2421 | ||
2422 | if (!rm) | |
2423 | return fd; | |
2424 | ||
2425 | ret = unlink(template); | |
2426 | if (ret < 0) { | |
2427 | close(fd); | |
2428 | return -1; | |
2429 | } | |
2430 | ||
2431 | return fd; | |
2432 | } | |
e4636123 | 2433 | |
e3db0162 CB |
2434 | int parse_byte_size_string(const char *s, int64_t *converted) |
2435 | { | |
2436 | int ret, suffix_len; | |
2437 | long long int conv; | |
2438 | int64_t mltpl, overflow; | |
2439 | char *end; | |
2440 | char dup[LXC_NUMSTRLEN64 + 2]; | |
a13560af | 2441 | char suffix[3] = {0}; |
e3db0162 CB |
2442 | |
2443 | if (!s || !strcmp(s, "")) | |
2444 | return -EINVAL; | |
2445 | ||
2446 | end = stpncpy(dup, s, sizeof(dup)); | |
2447 | if (*end != '\0') | |
2448 | return -EINVAL; | |
2449 | ||
2450 | if (isdigit(*(end - 1))) | |
2451 | suffix_len = 0; | |
2452 | else if (isalpha(*(end - 1))) | |
2453 | suffix_len = 1; | |
2454 | else | |
2455 | return -EINVAL; | |
2456 | ||
c2229b24 | 2457 | if (suffix_len > 0 && (end - 2) == dup && !isdigit(*(end - 2))) |
e3db0162 CB |
2458 | return -EINVAL; |
2459 | ||
358b8c81 CB |
2460 | if (suffix_len > 0 && isalpha(*(end - 2))) |
2461 | suffix_len++; | |
e3db0162 CB |
2462 | |
2463 | if (suffix_len > 0) { | |
2464 | memcpy(suffix, end - suffix_len, suffix_len); | |
2465 | *(suffix + suffix_len) = '\0'; | |
2466 | *(end - suffix_len) = '\0'; | |
2467 | } | |
2468 | dup[lxc_char_right_gc(dup, strlen(dup))] = '\0'; | |
2469 | ||
2470 | ret = lxc_safe_long_long(dup, &conv); | |
2471 | if (ret < 0) | |
2472 | return -ret; | |
2473 | ||
2474 | if (suffix_len != 2) { | |
2475 | *converted = conv; | |
2476 | return 0; | |
2477 | } | |
2478 | ||
2479 | if (!strcmp(suffix, "kB")) | |
2480 | mltpl = 1024; | |
2481 | else if (!strcmp(suffix, "MB")) | |
2482 | mltpl = 1024 * 1024; | |
2483 | else if (!strcmp(suffix, "GB")) | |
2484 | mltpl = 1024 * 1024 * 1024; | |
2485 | else | |
2486 | return -EINVAL; | |
2487 | ||
2488 | overflow = conv * mltpl; | |
2489 | if (conv != 0 && (overflow / conv) != mltpl) | |
2490 | return -ERANGE; | |
2491 | ||
2492 | *converted = overflow; | |
2493 | return 0; | |
2494 | } | |
6222c3f4 CB |
2495 | |
2496 | uint64_t lxc_find_next_power2(uint64_t n) | |
2497 | { | |
2498 | /* 0 is not valid input. We return 0 to the caller since 0 is not a | |
2499 | * valid power of two. | |
2500 | */ | |
2501 | if (n == 0) | |
2502 | return 0; | |
2503 | ||
2504 | if (!(n & (n - 1))) | |
2505 | return n; | |
2506 | ||
2507 | while (n & (n - 1)) | |
2508 | n = n & (n - 1); | |
2509 | ||
2510 | n = n << 1; | |
2511 | return n; | |
2512 | } | |
1fd0f41e CB |
2513 | |
2514 | int lxc_set_death_signal(int signal) | |
2515 | { | |
2516 | int ret; | |
2517 | pid_t ppid; | |
2518 | ||
2519 | ret = prctl(PR_SET_PDEATHSIG, signal, 0, 0, 0); | |
2520 | ||
2521 | /* Check whether we have been orphaned. */ | |
2522 | ppid = (pid_t)syscall(SYS_getppid); | |
2523 | if (ppid == 1) { | |
2524 | pid_t self; | |
2525 | ||
2526 | self = lxc_raw_getpid(); | |
2527 | ret = kill(self, SIGKILL); | |
2528 | if (ret < 0) | |
2529 | return -1; | |
2530 | } | |
2531 | ||
2532 | if (ret < 0) { | |
2533 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal); | |
2534 | return -1; | |
2535 | } | |
2536 | ||
2537 | return 0; | |
2538 | } |