2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
40 #include <sys/mount.h>
41 #include <sys/param.h>
42 #include <sys/prctl.h>
44 #include <sys/types.h>
49 #include "namespace.h"
54 #define O_PATH 010000000
58 #define O_NOFOLLOW 00400000
61 lxc_log_define(lxc_utils
, lxc
);
64 * if path is btrfs, tries to remove it and any subvolumes beneath it
66 extern bool btrfs_try_remove_subvol(const char *path
);
68 static int _recursive_rmdir(const char *dirname
, dev_t pdev
,
69 const char *exclude
, int level
, bool onedev
)
71 struct dirent
*direntp
;
74 char pathname
[MAXPATHLEN
];
75 bool hadexclude
= false;
77 dir
= opendir(dirname
);
79 ERROR("failed to open %s", dirname
);
83 while ((direntp
= readdir(dir
))) {
90 if (!strcmp(direntp
->d_name
, ".") ||
91 !strcmp(direntp
->d_name
, ".."))
94 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
95 if (rc
< 0 || rc
>= MAXPATHLEN
) {
96 ERROR("pathname too long");
101 if (!level
&& exclude
&& !strcmp(direntp
->d_name
, exclude
)) {
102 ret
= rmdir(pathname
);
106 INFO("Not deleting snapshot %s", pathname
);
110 ret
= unlink(pathname
);
112 INFO("Failed to remove %s", pathname
);
115 SYSERROR("Failed to rmdir %s", pathname
);
123 ret
= lstat(pathname
, &mystat
);
125 ERROR("Failed to stat %s", pathname
);
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
);
136 if (S_ISDIR(mystat
.st_mode
)) {
137 if (_recursive_rmdir(pathname
, pdev
, exclude
, level
+1, onedev
) < 0)
140 if (unlink(pathname
) < 0) {
141 SYSERROR("Failed to delete %s", pathname
);
147 if (rmdir(dirname
) < 0 && !btrfs_try_remove_subvol(dirname
) && !hadexclude
) {
148 ERROR("Failed to delete %s", dirname
);
154 ERROR("Failed to close directory %s", dirname
);
158 return failed
? -1 : 0;
161 /* We have two different magic values for overlayfs, yay. */
162 #ifndef OVERLAYFS_SUPER_MAGIC
163 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
166 #ifndef OVERLAY_SUPER_MAGIC
167 #define OVERLAY_SUPER_MAGIC 0x794c7630
170 /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
173 static bool is_native_overlayfs(const char *path
)
175 if (has_fs_type(path
, OVERLAY_SUPER_MAGIC
) ||
176 has_fs_type(path
, OVERLAYFS_SUPER_MAGIC
))
182 /* returns 0 on success, -1 if there were any failures */
183 extern int lxc_rmdir_onedev(const char *path
, const char *exclude
)
188 if (is_native_overlayfs(path
))
191 if (lstat(path
, &mystat
) < 0) {
195 ERROR("Failed to stat %s", path
);
199 return _recursive_rmdir(path
, mystat
.st_dev
, exclude
, 0, onedev
);
202 /* borrowed from iproute2 */
203 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
212 res
= strtoul(arg
, &ptr
, base
);
213 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
221 extern int mkdir_p(const char *dir
, mode_t mode
)
223 const char *tmp
= dir
;
224 const char *orig
= dir
;
228 dir
= tmp
+ strspn(tmp
, "/");
229 tmp
= dir
+ strcspn(dir
, "/");
230 makeme
= strndup(orig
, dir
- orig
);
232 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
233 SYSERROR("failed to create directory '%s'", makeme
);
250 if (stat(RUNTIME_PATH
, &sb
) < 0) {
254 if (geteuid() == sb
.st_uid
|| getegid() == sb
.st_gid
) {
255 rundir
= strdup(RUNTIME_PATH
);
259 rundir
= getenv("XDG_RUNTIME_DIR");
261 rundir
= strdup(rundir
);
265 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
266 homedir
= getenv("HOME");
268 ERROR("HOME isn't set in the environment.");
272 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
273 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
278 int wait_for_pid(pid_t pid
)
283 ret
= waitpid(pid
, &status
, 0);
291 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
296 int lxc_wait_for_pid_status(pid_t pid
)
301 ret
= waitpid(pid
, &status
, 0);
312 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
316 ret
= write(fd
, buf
, count
);
317 if (ret
< 0 && errno
== EINTR
)
322 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
326 ret
= read(fd
, buf
, count
);
327 if (ret
< 0 && errno
== EINTR
)
332 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
335 ret
= lxc_read_nointr(fd
, buf
, count
);
338 if ((size_t)ret
!= count
)
340 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
348 #include <gnutls/gnutls.h>
349 #include <gnutls/crypto.h>
351 __attribute__((constructor
))
352 static void gnutls_lxc_init(void)
354 gnutls_global_init();
357 int sha1sum_file(char *fnam
, unsigned char *digest
)
366 f
= fopen_cloexec(fnam
, "r");
368 SYSERROR("Error opening template");
371 if (fseek(f
, 0, SEEK_END
) < 0) {
372 SYSERROR("Error seeking to end of template");
376 if ((flen
= ftell(f
)) < 0) {
377 SYSERROR("Error telling size of template");
381 if (fseek(f
, 0, SEEK_SET
) < 0) {
382 SYSERROR("Error seeking to start of template");
386 if ((buf
= malloc(flen
+1)) == NULL
) {
387 SYSERROR("Out of memory");
391 if (fread(buf
, 1, flen
, f
) != flen
) {
392 SYSERROR("Failure reading template");
398 SYSERROR("Failre closing template");
403 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
409 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
412 size_t count
= 1 + skip
;
415 /* first determine size of argument list, we don't want to reallocate
420 char* arg
= va_arg(ap2
, char*);
427 result
= calloc(count
, sizeof(char*));
432 char* arg
= va_arg(ap
, char*);
435 arg
= do_strdup
? strdup(arg
) : arg
;
438 result
[count
++] = arg
;
441 /* calloc has already set last element to NULL*/
449 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
451 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
454 struct lxc_popen_FILE
*lxc_popen(const char *command
)
459 struct lxc_popen_FILE
*fp
= NULL
;
461 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
474 /* duplicate stdout */
475 if (pipe_fds
[1] != STDOUT_FILENO
)
476 ret
= dup2(pipe_fds
[1], STDOUT_FILENO
);
478 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
484 /* duplicate stderr */
485 if (pipe_fds
[1] != STDERR_FILENO
)
486 ret
= dup2(pipe_fds
[1], STDERR_FILENO
);
488 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
493 /* unblock all signals */
494 ret
= sigfillset(&mask
);
498 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
502 execl("/bin/sh", "sh", "-c", command
, (char *)NULL
);
509 fp
= malloc(sizeof(*fp
));
512 memset(fp
, 0, sizeof(*fp
));
514 fp
->child_pid
= child_pid
;
515 fp
->pipe
= pipe_fds
[0];
517 /* From now on, closing fp->f will also close fp->pipe. So only ever
518 * call fclose(fp->f).
520 fp
->f
= fdopen(pipe_fds
[0], "r");
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.
531 if (fp
&& !fp
->f
&& pipe_fds
[0] >= 0)
534 if (pipe_fds
[1] >= 0)
546 int lxc_pclose(struct lxc_popen_FILE
*fp
)
555 wait_pid
= waitpid(fp
->child_pid
, &wstatus
, 0);
556 } while (wait_pid
< 0 && errno
== EINTR
);
567 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
569 ssize_t len
= -1, saved_len
= -1;
571 size_t replacement_len
= strlen(replacement
);
572 size_t needle_len
= strlen(needle
);
574 /* should be executed exactly twice */
575 while (len
== -1 || result
== NULL
) {
581 result
= calloc(1, len
+ 1);
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
);
594 if (result
&& replacement_len
> 0)
595 memcpy(&result
[len
], replacement
, replacement_len
);
596 len
+= replacement_len
;
599 part_len
= strlen(last_p
);
600 if (result
&& part_len
> 0)
601 memcpy(&result
[len
], last_p
, part_len
);
605 /* make sure we did the same thing twice,
606 * once for calculating length, the other
607 * time for copying data */
608 if (saved_len
!= len
) {
612 /* make sure we didn't overwrite any buffer,
613 * due to calloc the string should be 0-terminated */
614 if (result
[len
] != '\0') {
622 bool lxc_string_in_array(const char *needle
, const char **haystack
)
624 for (; haystack
&& *haystack
; haystack
++)
625 if (!strcmp(needle
, *haystack
))
630 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
634 size_t sep_len
= strlen(sep
);
635 size_t result_len
= use_as_prefix
* sep_len
;
637 /* calculate new string length */
638 for (p
= (char **)parts
; *p
; p
++)
639 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
641 result
= calloc(result_len
+ 1, 1);
647 for (p
= (char **)parts
; *p
; p
++) {
648 if (p
> (char **)parts
)
656 char **lxc_normalize_path(const char *path
)
660 size_t components_len
= 0;
663 components
= lxc_string_split(path
, '/');
666 for (p
= components
; *p
; p
++)
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
));
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
));
691 char *lxc_deslashify(const char *path
)
701 parts
= lxc_normalize_path(dup
);
707 /* We'll end up here if path == "///" or path == "". */
711 lxc_free_array((void **)parts
, free
);
714 n
= strcspn(dup
, "/");
717 lxc_free_array((void **)parts
, free
);
727 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
729 lxc_free_array((void **)parts
, free
);
733 char *lxc_append_paths(const char *first
, const char *second
)
738 const char *pattern
= "%s%s";
740 len
= strlen(first
) + strlen(second
) + 1;
741 if (second
[0] != '/') {
746 result
= calloc(1, len
);
750 ret
= snprintf(result
, len
, pattern
, first
, second
);
751 if (ret
< 0 || (size_t)ret
>= len
) {
759 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
761 char *token
, *str
, *saveptr
= NULL
;
762 char sep
[2] = { _sep
, '\0' };
764 if (!haystack
|| !needle
)
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)
777 char **lxc_string_split(const char *string
, char _sep
)
779 char *token
, *str
, *saveptr
= NULL
;
780 char sep
[2] = {_sep
, '\0'};
781 char **tmp
= NULL
, **result
= NULL
;
782 size_t result_capacity
= 0;
783 size_t result_count
= 0;
787 return calloc(1, sizeof(char *));
789 str
= alloca(strlen(string
) + 1);
791 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
792 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
795 result
[result_count
] = strdup(token
);
796 if (!result
[result_count
])
801 /* if we allocated too much, reduce it */
802 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
806 /* Make sure we don't return uninitialized memory. */
807 if (result_count
== 0)
812 lxc_free_array((void **)result
, free
);
817 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
, size_t *cnt
)
821 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
824 (*result
)[*cnt
] = strndup(start
, end
- start
);
825 if (!(*result
)[*cnt
])
833 * Given a a string 'one two "three four"', split into three words,
834 * one, two, and "three four"
836 char **lxc_string_split_quoted(char *string
)
838 char *nextword
= string
, *p
, state
;
839 char **result
= NULL
;
840 size_t result_capacity
= 0;
841 size_t result_count
= 0;
843 if (!string
|| !*string
)
844 return calloc(1, sizeof(char *));
846 // TODO I'm *not* handling escaped quote
848 for (p
= string
; *p
; p
++) {
853 else if (*p
== '"' || *p
== '\'') {
863 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
871 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
880 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
882 return realloc(result
, (result_count
+ 1) * sizeof(char *));
885 char **lxc_string_split_and_trim(const char *string
, char _sep
)
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;
896 return calloc(1, sizeof(char *));
898 str
= alloca(strlen(string
)+1);
900 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
901 while (token
[0] == ' ' || token
[0] == '\t')
904 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
908 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
911 result
[result_count
] = strdup(token
);
912 if (!result
[result_count
])
917 /* if we allocated too much, reduce it */
918 return realloc(result
, (result_count
+ 1) * sizeof(char *));
921 lxc_free_array((void **)result
, free
);
926 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
929 for (p
= array
; p
&& *p
; p
++)
934 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
939 /* first time around, catch some trivial mistakes of the user
940 * only initializing one of these */
941 if (!*array
|| !*capacity
) {
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 *));
954 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
956 *capacity
= new_capacity
;
959 /* array has sufficient elements */
963 size_t lxc_array_len(void **array
)
968 for (p
= array
; p
&& *p
; p
++)
974 int lxc_write_to_file(const char *filename
, const void *buf
, size_t count
,
975 bool add_newline
, mode_t mode
)
980 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, mode
);
983 ret
= lxc_write_nointr(fd
, buf
, count
);
986 if ((size_t)ret
!= count
)
989 ret
= lxc_write_nointr(fd
, "\n", 1);
1003 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
1005 int fd
= -1, saved_errno
;
1008 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1012 if (!buf
|| !count
) {
1015 while ((ret
= read(fd
, buf2
, 100)) > 0)
1020 memset(buf
, 0, count
);
1021 ret
= read(fd
, buf
, count
);
1025 ERROR("read %s: %s", filename
, strerror(errno
));
1027 saved_errno
= errno
;
1029 errno
= saved_errno
;
1033 void **lxc_append_null_to_array(void **array
, size_t count
)
1037 /* Append NULL to the array */
1039 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1042 for (i
= 0; i
< count
; i
++)
1048 array
[count
] = NULL
;
1053 int randseed(bool srand_it
)
1056 srand pre-seed function based on /dev/urandom
1058 unsigned int seed
= time(NULL
) + getpid();
1061 f
= fopen("/dev/urandom", "r");
1063 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1065 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1075 uid_t
get_ns_uid(uid_t orig
)
1079 uid_t nsid
, hostid
, range
;
1080 FILE *f
= fopen("/proc/self/uid_map", "r");
1084 while (getline(&line
, &sz
, f
) != -1) {
1085 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1087 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1088 nsid
+= orig
- hostid
;
1100 bool dir_exists(const char *path
)
1105 ret
= stat(path
, &sb
);
1107 /* Could be something other than eexist, just say "no". */
1109 return S_ISDIR(sb
.st_mode
);
1112 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1113 * FNV has good anti collision properties and we're not worried
1114 * about pre-image resistance or one-way-ness, we're just trying to make
1115 * the name unique in the 108 bytes of space we have.
1117 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1121 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1123 /* xor the bottom with the current octet */
1124 hval
^= (uint64_t)*bp
;
1127 * multiply by the 64 bit FNV magic prime mod 2^64
1129 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1130 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1137 * Detect whether / is mounted MS_SHARED. The only way I know of to
1138 * check that is through /proc/self/mountinfo.
1139 * I'm only checking for /. If the container rootfs or mount location
1140 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1141 * out would be too much work to be worth it.
1143 int detect_shared_rootfs(void)
1145 char buf
[LXC_LINELEN
], *p
;
1150 f
= fopen("/proc/self/mountinfo", "r");
1153 while (fgets(buf
, LXC_LINELEN
, f
)) {
1154 for (p
= buf
, i
= 0; p
&& i
< 4; i
++)
1155 p
= strchr(p
+ 1, ' ');
1158 p2
= strchr(p
+ 1, ' ');
1162 if (strcmp(p
+ 1, "/") == 0) {
1163 /* This is '/'. Is it shared? */
1164 p
= strchr(p2
+ 1, ' ');
1165 if (p
&& strstr(p
, "shared:")) {
1175 bool switch_to_ns(pid_t pid
, const char *ns
) {
1177 char nspath
[MAXPATHLEN
];
1179 /* Switch to new ns */
1180 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1181 if (ret
< 0 || ret
>= MAXPATHLEN
)
1184 fd
= open(nspath
, O_RDONLY
);
1186 SYSERROR("failed to open %s", nspath
);
1192 SYSERROR("failed to set process %d to %s of %d.", pid
, ns
, fd
);
1201 * looking at fs/proc_namespace.c, it appears we can
1202 * actually expect the rootfs entry to very specifically contain
1203 * " - rootfs rootfs "
1204 * IIUC, so long as we've chrooted so that rootfs is not our root,
1205 * the rootfs entry should always be skipped in mountinfo contents.
1207 bool detect_ramfs_rootfs(void)
1215 f
= fopen("/proc/self/mountinfo", "r");
1219 while (getline(&line
, &len
, f
) != -1) {
1220 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
1221 p
= strchr(p
+ 1, ' ');
1224 p2
= strchr(p
+ 1, ' ');
1228 if (strcmp(p
+ 1, "/") == 0) {
1229 /* This is '/'. Is it the ramfs? */
1230 p
= strchr(p2
+ 1, '-');
1231 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1243 char *on_path(const char *cmd
, const char *rootfs
) {
1246 char *saveptr
= NULL
;
1247 char cmdpath
[MAXPATHLEN
];
1250 path
= getenv("PATH");
1254 path
= strdup(path
);
1258 entry
= strtok_r(path
, ":", &saveptr
);
1261 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1263 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1265 if (ret
< 0 || ret
>= MAXPATHLEN
)
1268 if (access(cmdpath
, X_OK
) == 0) {
1270 return strdup(cmdpath
);
1274 entry
= strtok_r(NULL
, ":", &saveptr
);
1281 bool file_exists(const char *f
)
1283 struct stat statbuf
;
1285 return stat(f
, &statbuf
) == 0;
1288 bool cgns_supported(void)
1290 return file_exists("/proc/self/ns/cgroup");
1293 /* historically lxc-init has been under /usr/lib/lxc and under
1294 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1296 char *choose_init(const char *rootfs
)
1299 const char *empty
= "",
1301 int ret
, env_set
= 0;
1303 if (!getenv("PATH")) {
1304 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1305 SYSERROR("Failed to setenv");
1309 retv
= on_path("init.lxc", rootfs
);
1312 if (unsetenv("PATH"))
1313 SYSERROR("Failed to unsetenv");
1319 retv
= malloc(PATH_MAX
);
1328 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1329 if (ret
< 0 || ret
>= PATH_MAX
) {
1330 ERROR("pathname too long");
1333 if (access(retv
, X_OK
) == 0)
1336 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1337 if (ret
< 0 || ret
>= PATH_MAX
) {
1338 ERROR("pathname too long");
1341 if (access(retv
, X_OK
) == 0)
1344 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1345 if (ret
< 0 || ret
>= PATH_MAX
) {
1346 ERROR("pathname too long");
1349 if (access(retv
, X_OK
) == 0)
1352 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1353 if (ret
< 0 || ret
>= PATH_MAX
) {
1354 ERROR("pathname too long");
1357 if (access(retv
, X_OK
) == 0)
1361 * Last resort, look for the statically compiled init.lxc which we
1362 * hopefully bind-mounted in.
1363 * If we are called during container setup, and we get to this point,
1364 * then the init.lxc.static from the host will need to be bind-mounted
1365 * in. So we return NULL here to indicate that.
1370 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1371 if (ret
< 0 || ret
>= PATH_MAX
) {
1372 WARN("Nonsense - name /lxc.init.static too long");
1375 if (access(retv
, X_OK
) == 0)
1383 int print_to_file(const char *file
, const char *content
)
1388 f
= fopen(file
, "w");
1391 if (fprintf(f
, "%s", content
) != strlen(content
))
1397 int is_dir(const char *path
)
1399 struct stat statbuf
;
1400 int ret
= stat(path
, &statbuf
);
1401 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1407 * Given the '-t' template option to lxc-create, figure out what to
1408 * do. If the template is a full executable path, use that. If it
1409 * is something like 'sshd', then return $templatepath/lxc-sshd.
1410 * On success return the template, on error return NULL.
1412 char *get_template_path(const char *t
)
1417 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1422 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1423 tpath
= malloc(len
);
1426 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1427 if (ret
< 0 || ret
>= len
) {
1431 if (access(tpath
, X_OK
) < 0) {
1432 SYSERROR("bad template: %s", t
);
1441 * @path: a pathname where / replaced with '\0'.
1442 * @offsetp: pointer to int showing which path segment was last seen.
1443 * Updated on return to reflect the next segment.
1444 * @fulllen: full original path length.
1445 * Returns a pointer to the next path segment, or NULL if done.
1447 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1449 int offset
= *offsetp
;
1451 if (offset
>= fulllen
)
1454 while (path
[offset
] != '\0' && offset
< fulllen
)
1456 while (path
[offset
] == '\0' && offset
< fulllen
)
1460 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1464 * Check that @subdir is a subdir of @dir. @len is the length of
1465 * @dir (to avoid having to recalculate it).
1467 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1469 size_t subdirlen
= strlen(subdir
);
1471 if (subdirlen
< len
)
1473 if (strncmp(subdir
, dir
, len
) != 0)
1475 if (dir
[len
-1] == '/')
1477 if (subdir
[len
] == '/' || subdirlen
== len
)
1483 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1484 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1486 static int check_symlink(int fd
)
1489 int ret
= fstat(fd
, &sb
);
1492 if (S_ISLNK(sb
.st_mode
))
1498 * Open a file or directory, provided that it contains no symlinks.
1500 * CAVEAT: This function must not be used for other purposes than container
1501 * setup before executing the container's init
1503 static int open_if_safe(int dirfd
, const char *nextpath
)
1505 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1506 if (newfd
>= 0) /* Was not a symlink, all good. */
1512 if (errno
== EPERM
|| errno
== EACCES
) {
1513 /* We're not root (cause we got EPERM) so try opening with
1516 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1518 /* O_PATH will return an fd for symlinks. We know
1519 * nextpath wasn't a symlink at last openat, so if fd is
1520 * now a link, then something * fishy is going on.
1522 int ret
= check_symlink(newfd
);
1534 * Open a path intending for mounting, ensuring that the final path
1535 * is inside the container's rootfs.
1537 * CAVEAT: This function must not be used for other purposes than container
1538 * setup before executing the container's init
1540 * @target: path to be opened
1541 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1542 * would be the container's rootfs.
1544 * Return an open fd for the path, or <0 on error.
1546 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1548 int curlen
= 0, dirfd
, fulllen
, i
;
1551 fulllen
= strlen(target
);
1553 /* make sure prefix-skip makes sense */
1554 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1555 curlen
= strlen(prefix_skip
);
1556 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1557 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1558 target
, prefix_skip
);
1562 * get_nextpath() expects the curlen argument to be
1563 * on a (turned into \0) / or before it, so decrement
1564 * curlen to make sure that happens
1573 /* Make a copy of target which we can hack up, and tokenize it */
1574 if ((dup
= strdup(target
)) == NULL
) {
1575 SYSERROR("Out of memory checking for symbolic link");
1578 for (i
= 0; i
< fulllen
; i
++) {
1583 dirfd
= open(prefix_skip
, O_RDONLY
);
1587 int newfd
, saved_errno
;
1590 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1592 newfd
= open_if_safe(dirfd
, nextpath
);
1593 saved_errno
= errno
;
1597 errno
= saved_errno
;
1599 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1610 * Safely mount a path into a container, ensuring that the mount target
1611 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1612 * uses the host's /)
1614 * CAVEAT: This function must not be used for other purposes than container
1615 * setup before executing the container's init
1617 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1618 unsigned long flags
, const void *data
, const char *rootfs
)
1620 int destfd
, ret
, saved_errno
;
1621 /* Only needs enough for /proc/self/fd/<fd>. */
1622 char srcbuf
[50], destbuf
[50];
1624 const char *mntsrc
= src
;
1629 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1630 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1631 INFO("this is a relative bind mount");
1632 srcfd
= open_without_symlink(src
, NULL
);
1635 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1636 if (ret
< 0 || ret
> 50) {
1638 ERROR("Out of memory");
1644 destfd
= open_without_symlink(dest
, rootfs
);
1647 saved_errno
= errno
;
1649 errno
= saved_errno
;
1654 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1655 if (ret
< 0 || ret
> 50) {
1659 ERROR("Out of memory");
1663 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1664 saved_errno
= errno
;
1669 errno
= saved_errno
;
1670 SYSERROR("Failed to mount %s onto %s", src
? src
: "(null)", dest
);
1678 * Mount a proc under @rootfs if proc self points to a pid other than
1679 * my own. This is needed to have a known-good proc mount for setting
1680 * up LSMs both at container startup and attach.
1682 * @rootfs : the rootfs where proc should be mounted
1684 * Returns < 0 on failure, 0 if the correct proc was already mounted
1685 * and 1 if a new proc was mounted.
1687 * NOTE: not to be called from inside the container namespace!
1689 int lxc_mount_proc_if_needed(const char *rootfs
)
1691 char path
[MAXPATHLEN
];
1692 int link_to_pid
, linklen
, mypid
, ret
;
1693 char link
[LXC_NUMSTRLEN64
] = {0};
1695 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1696 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1697 SYSERROR("proc path name too long");
1701 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1703 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1704 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1705 SYSERROR("proc path name too long");
1709 /* /proc not mounted */
1711 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1714 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1715 link
[linklen
- 1] = '\0';
1716 ERROR("readlink returned truncated content: \"%s\"", link
);
1720 mypid
= lxc_raw_getpid();
1721 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1723 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1726 /* correct procfs is already mounted */
1727 if (link_to_pid
== mypid
)
1730 ret
= umount2(path
, MNT_DETACH
);
1732 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1735 /* rootfs is NULL */
1736 if (!strcmp(rootfs
, ""))
1737 ret
= mount("proc", path
, "proc", 0, NULL
);
1739 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1743 INFO("mounted /proc in container for security transition");
1747 int open_devnull(void)
1749 int fd
= open("/dev/null", O_RDWR
);
1752 SYSERROR("Can't open /dev/null");
1757 int set_stdfds(int fd
)
1764 ret
= dup2(fd
, STDIN_FILENO
);
1768 ret
= dup2(fd
, STDOUT_FILENO
);
1772 ret
= dup2(fd
, STDERR_FILENO
);
1779 int null_stdfds(void)
1782 int fd
= open_devnull();
1785 ret
= set_stdfds(fd
);
1793 * Return the number of lines in file @fn, or -1 on error
1795 int lxc_count_file_lines(const char *fn
)
1802 f
= fopen_cloexec(fn
, "r");
1806 while (getline(&line
, &sz
, f
) != -1) {
1814 /* Check whether a signal is blocked by a process. */
1815 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1816 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
1817 bool task_blocking_signal(pid_t pid
, int signal
)
1821 long unsigned int sigblk
= 0;
1826 char status
[__PROC_STATUS_LEN
];
1828 ret
= snprintf(status
, __PROC_STATUS_LEN
, "/proc/%d/status", pid
);
1829 if (ret
< 0 || ret
>= __PROC_STATUS_LEN
)
1832 f
= fopen(status
, "r");
1836 while (getline(&line
, &n
, f
) != -1) {
1837 if (strncmp(line
, "SigBlk:\t", 8))
1840 if (sscanf(line
+ 8, "%lx", &sigblk
) != 1)
1844 if (sigblk
& (1LU << (signal
- 1)))
1853 static int lxc_append_null_to_list(void ***list
)
1859 for (; (*list
)[newentry
]; newentry
++) {
1863 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
1868 (*list
)[newentry
+ 1] = NULL
;
1873 int lxc_append_string(char ***list
, char *entry
)
1878 newentry
= lxc_append_null_to_list((void ***)list
);
1882 copy
= strdup(entry
);
1886 (*list
)[newentry
] = copy
;
1891 int lxc_preserve_ns(const int pid
, const char *ns
)
1894 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
1895 #define __NS_PATH_LEN 50
1896 char path
[__NS_PATH_LEN
];
1898 /* This way we can use this function to also check whether namespaces
1899 * are supported by the kernel by passing in the NULL or the empty
1902 ret
= snprintf(path
, __NS_PATH_LEN
, "/proc/%d/ns%s%s", pid
,
1903 !ns
|| strcmp(ns
, "") == 0 ? "" : "/",
1904 !ns
|| strcmp(ns
, "") == 0 ? "" : ns
);
1906 if (ret
< 0 || (size_t)ret
>= __NS_PATH_LEN
)
1909 return open(path
, O_RDONLY
| O_CLOEXEC
);
1912 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
1915 unsigned long int uli
;
1917 while (isspace(*numstr
))
1924 uli
= strtoul(numstr
, &err
, 0);
1925 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
1928 if (err
== numstr
|| *err
!= '\0')
1934 *converted
= (unsigned int)uli
;
1938 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
1941 unsigned long int uli
;
1943 while (isspace(*numstr
))
1950 uli
= strtoul(numstr
, &err
, 0);
1951 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
1954 if (err
== numstr
|| *err
!= '\0')
1961 int lxc_safe_int(const char *numstr
, int *converted
)
1964 signed long int sli
;
1967 sli
= strtol(numstr
, &err
, 0);
1968 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
1971 if (errno
!= 0 && sli
== 0)
1974 if (err
== numstr
|| *err
!= '\0')
1977 if (sli
> INT_MAX
|| sli
< INT_MIN
)
1980 *converted
= (int)sli
;
1984 int lxc_safe_long(const char *numstr
, long int *converted
)
1987 signed long int sli
;
1990 sli
= strtol(numstr
, &err
, 0);
1991 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
1994 if (errno
!= 0 && sli
== 0)
1997 if (err
== numstr
|| *err
!= '\0')
2004 int lxc_safe_long_long(const char *numstr
, long long int *converted
)
2007 signed long long int sli
;
2010 sli
= strtoll(numstr
, &err
, 0);
2011 if (errno
== ERANGE
&& (sli
== LLONG_MAX
|| sli
== LLONG_MIN
))
2014 if (errno
!= 0 && sli
== 0)
2017 if (err
== numstr
|| *err
!= '\0')
2024 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2026 if (setgid(gid
) < 0) {
2027 SYSERROR("Failed to switch to gid %d.", gid
);
2030 NOTICE("Switched to gid %d.", gid
);
2032 if (setuid(uid
) < 0) {
2033 SYSERROR("Failed to switch to uid %d.", uid
);
2036 NOTICE("Switched to uid %d.", uid
);
2041 /* Simple covenience function which enables uniform logging. */
2042 int lxc_setgroups(int size
, gid_t list
[])
2044 if (setgroups(size
, list
) < 0) {
2045 SYSERROR("Failed to setgroups().");
2048 NOTICE("Dropped additional groups.");
2053 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2056 struct loop_info64 lo64
;
2058 int dfd
= -1, fd
= -1, ret
= -1;
2060 dir
= opendir("/dev");
2064 while ((dp
= readdir(dir
))) {
2068 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2075 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2079 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2081 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2089 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2090 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2107 static int lxc_get_unused_loop_dev(char *name_loop
)
2110 int fd_ctl
= -1, fd_tmp
= -1;
2112 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2116 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2120 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2121 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2124 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2133 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2136 struct loop_info64 lo64
;
2137 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2139 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2141 if (fd_loop
== -ENODEV
)
2142 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2147 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2151 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2155 memset(&lo64
, 0, sizeof(lo64
));
2156 lo64
.lo_flags
= flags
;
2158 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2168 if (fret
< 0 && fd_loop
>= 0) {
2176 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2182 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2184 /* We consider anything else than EINVAL deadly to prevent going
2185 * into an infinite loop. (The other alternative is constantly
2186 * parsing /proc/self/mountinfo which is yucky and probably
2189 if (errno
!= EINVAL
)
2192 /* Just stop counting when this happens. That'd just be so
2193 * stupid that we won't even bother trying to report back the
2194 * correct value anymore.
2196 if (umounts
!= INT_MAX
)
2198 /* We succeeded in umounting. Make sure that there's no other
2199 * mountpoint stacked underneath.
2207 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2210 int ret
, fret
, pipefd
[2];
2213 /* Make sure our callers do not receive unitialized memory. */
2214 if (buf_size
> 0 && buf
)
2217 if (pipe(pipefd
) < 0) {
2218 SYSERROR("failed to create pipe");
2222 child
= lxc_raw_clone(0);
2226 SYSERROR("failed to create new process");
2231 /* Close the read-end of the pipe. */
2234 /* Redirect std{err,out} to write-end of the
2237 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2239 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2241 /* Close the write-end of the pipe. */
2245 SYSERROR("failed to duplicate std{err,out} file descriptor");
2246 _exit(EXIT_FAILURE
);
2249 /* Does not return. */
2251 ERROR("failed to exec command");
2252 _exit(EXIT_FAILURE
);
2255 /* close the write-end of the pipe */
2258 if (buf
&& buf_size
> 0) {
2259 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2261 buf
[bytes
- 1] = '\0';
2264 fret
= wait_for_pid(child
);
2265 /* close the read-end of the pipe */
2271 char *must_make_path(const char *first
, ...)
2275 size_t full_len
= strlen(first
);
2277 dest
= must_copy_string(first
);
2279 va_start(args
, first
);
2280 while ((cur
= va_arg(args
, char *)) != NULL
) {
2281 full_len
+= strlen(cur
);
2284 dest
= must_realloc(dest
, full_len
+ 1);
2294 char *must_append_path(char *first
, ...)
2301 full_len
= strlen(first
);
2302 va_start(args
, first
);
2303 while ((cur
= va_arg(args
, char *)) != NULL
) {
2304 full_len
+= strlen(cur
);
2309 dest
= must_realloc(dest
, full_len
+ 1);
2321 char *must_copy_string(const char *entry
)
2328 ret
= strdup(entry
);
2334 void *must_realloc(void *orig
, size_t sz
)
2339 ret
= realloc(orig
, sz
);
2345 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2347 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2350 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2356 ret
= statfs(path
, &sb
);
2360 has_type
= is_fs_type(&sb
, magic_val
);
2361 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2362 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2367 bool lxc_nic_exists(char *nic
)
2369 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2370 char path
[__LXC_SYS_CLASS_NET_LEN
];
2374 if (!strcmp(nic
, "none"))
2377 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2378 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2381 ret
= stat(path
, &sb
);
2388 int lxc_make_tmpfile(char *template, bool rm
)
2392 fd
= mkstemp(template);
2399 ret
= unlink(template);
2408 int parse_byte_size_string(const char *s
, int64_t *converted
)
2410 int ret
, suffix_len
;
2412 int64_t mltpl
, overflow
;
2414 char dup
[LXC_NUMSTRLEN64
+ 2];
2415 char suffix
[3] = {0};
2417 if (!s
|| !strcmp(s
, ""))
2420 end
= stpncpy(dup
, s
, sizeof(dup
) - 1);
2424 if (isdigit(*(end
- 1)))
2426 else if (isalpha(*(end
- 1)))
2431 if (suffix_len
> 0 && (end
- 2) == dup
&& !isdigit(*(end
- 2)))
2434 if (suffix_len
> 0 && isalpha(*(end
- 2)))
2437 if (suffix_len
> 0) {
2438 memcpy(suffix
, end
- suffix_len
, suffix_len
);
2439 *(suffix
+ suffix_len
) = '\0';
2440 *(end
- suffix_len
) = '\0';
2442 dup
[lxc_char_right_gc(dup
, strlen(dup
))] = '\0';
2444 ret
= lxc_safe_long_long(dup
, &conv
);
2448 if (suffix_len
!= 2) {
2453 if (!strcasecmp(suffix
, "KB"))
2455 else if (!strcasecmp(suffix
, "MB"))
2456 mltpl
= 1024 * 1024;
2457 else if (!strcasecmp(suffix
, "GB"))
2458 mltpl
= 1024 * 1024 * 1024;
2462 overflow
= conv
* mltpl
;
2463 if (conv
!= 0 && (overflow
/ conv
) != mltpl
)
2466 *converted
= overflow
;
2470 uint64_t lxc_find_next_power2(uint64_t n
)
2472 /* 0 is not valid input. We return 0 to the caller since 0 is not a
2473 * valid power of two.
2488 int lxc_set_death_signal(int signal
)
2493 ret
= prctl(PR_SET_PDEATHSIG
, signal
, 0, 0, 0);
2495 /* Check whether we have been orphaned. */
2496 ppid
= (pid_t
)syscall(SYS_getppid
);
2500 self
= lxc_raw_getpid();
2501 ret
= kill(self
, SIGKILL
);
2507 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal
);