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