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(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(char *path
, const char *exclude
)
188 if (is_native_overlayfs(path
)) {
192 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
);
249 if (geteuid() == 0) {
250 rundir
= strdup(RUNTIME_PATH
);
254 rundir
= getenv("XDG_RUNTIME_DIR");
256 rundir
= strdup(rundir
);
260 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
261 homedir
= getenv("HOME");
263 ERROR("HOME isn't set in the environment.");
267 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
268 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
273 int wait_for_pid(pid_t pid
)
278 ret
= waitpid(pid
, &status
, 0);
286 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
291 int lxc_wait_for_pid_status(pid_t pid
)
296 ret
= waitpid(pid
, &status
, 0);
307 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
311 ret
= write(fd
, buf
, count
);
312 if (ret
< 0 && errno
== EINTR
)
317 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
321 ret
= read(fd
, buf
, count
);
322 if (ret
< 0 && errno
== EINTR
)
327 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
330 ret
= lxc_read_nointr(fd
, buf
, count
);
333 if ((size_t)ret
!= count
)
335 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
343 #include <gnutls/gnutls.h>
344 #include <gnutls/crypto.h>
346 __attribute__((constructor
))
347 static void gnutls_lxc_init(void)
349 gnutls_global_init();
352 int sha1sum_file(char *fnam
, unsigned char *digest
)
361 f
= fopen_cloexec(fnam
, "r");
363 SYSERROR("Error opening template");
366 if (fseek(f
, 0, SEEK_END
) < 0) {
367 SYSERROR("Error seeking to end of template");
371 if ((flen
= ftell(f
)) < 0) {
372 SYSERROR("Error telling size of template");
376 if (fseek(f
, 0, SEEK_SET
) < 0) {
377 SYSERROR("Error seeking to start of template");
381 if ((buf
= malloc(flen
+1)) == NULL
) {
382 SYSERROR("Out of memory");
386 if (fread(buf
, 1, flen
, f
) != flen
) {
387 SYSERROR("Failure reading template");
393 SYSERROR("Failre closing template");
398 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
404 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
407 size_t count
= 1 + skip
;
410 /* first determine size of argument list, we don't want to reallocate
415 char* arg
= va_arg(ap2
, char*);
422 result
= calloc(count
, sizeof(char*));
427 char* arg
= va_arg(ap
, char*);
430 arg
= do_strdup
? strdup(arg
) : arg
;
433 result
[count
++] = arg
;
436 /* calloc has already set last element to NULL*/
444 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
446 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
449 struct lxc_popen_FILE
*lxc_popen(const char *command
)
454 struct lxc_popen_FILE
*fp
= NULL
;
456 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
469 /* duplicate stdout */
470 if (pipe_fds
[1] != STDOUT_FILENO
)
471 ret
= dup2(pipe_fds
[1], STDOUT_FILENO
);
473 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
479 /* duplicate stderr */
480 if (pipe_fds
[1] != STDERR_FILENO
)
481 ret
= dup2(pipe_fds
[1], STDERR_FILENO
);
483 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
488 /* unblock all signals */
489 ret
= sigfillset(&mask
);
493 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
497 execl("/bin/sh", "sh", "-c", command
, (char *)NULL
);
504 fp
= malloc(sizeof(*fp
));
508 fp
->child_pid
= child_pid
;
509 fp
->pipe
= pipe_fds
[0];
511 fp
->f
= fdopen(pipe_fds
[0], "r");
521 if (pipe_fds
[0] >= 0)
524 if (pipe_fds
[1] >= 0)
530 int lxc_pclose(struct lxc_popen_FILE
*fp
)
539 wait_pid
= waitpid(fp
->child_pid
, &wstatus
, 0);
540 } while (wait_pid
< 0 && errno
== EINTR
);
552 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
554 ssize_t len
= -1, saved_len
= -1;
556 size_t replacement_len
= strlen(replacement
);
557 size_t needle_len
= strlen(needle
);
559 /* should be executed exactly twice */
560 while (len
== -1 || result
== NULL
) {
566 result
= calloc(1, len
+ 1);
574 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
575 part_len
= (ssize_t
)(p
- last_p
);
576 if (result
&& part_len
> 0)
577 memcpy(&result
[len
], last_p
, part_len
);
579 if (result
&& replacement_len
> 0)
580 memcpy(&result
[len
], replacement
, replacement_len
);
581 len
+= replacement_len
;
584 part_len
= strlen(last_p
);
585 if (result
&& part_len
> 0)
586 memcpy(&result
[len
], last_p
, part_len
);
590 /* make sure we did the same thing twice,
591 * once for calculating length, the other
592 * time for copying data */
593 if (saved_len
!= len
) {
597 /* make sure we didn't overwrite any buffer,
598 * due to calloc the string should be 0-terminated */
599 if (result
[len
] != '\0') {
607 bool lxc_string_in_array(const char *needle
, const char **haystack
)
609 for (; haystack
&& *haystack
; haystack
++)
610 if (!strcmp(needle
, *haystack
))
615 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
619 size_t sep_len
= strlen(sep
);
620 size_t result_len
= use_as_prefix
* sep_len
;
622 /* calculate new string length */
623 for (p
= (char **)parts
; *p
; p
++)
624 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
626 result
= calloc(result_len
+ 1, 1);
632 for (p
= (char **)parts
; *p
; p
++) {
633 if (p
> (char **)parts
)
641 char **lxc_normalize_path(const char *path
)
645 size_t components_len
= 0;
648 components
= lxc_string_split(path
, '/');
651 for (p
= components
; *p
; p
++)
654 /* resolve '.' and '..' */
655 for (pos
= 0; pos
< components_len
; ) {
656 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
657 /* eat this element */
658 free(components
[pos
]);
659 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
661 } else if (!strcmp(components
[pos
], "..")) {
662 /* eat this and the previous element */
663 free(components
[pos
- 1]);
664 free(components
[pos
]);
665 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
676 char *lxc_deslashify(const char *path
)
686 parts
= lxc_normalize_path(dup
);
692 /* We'll end up here if path == "///" or path == "". */
696 lxc_free_array((void **)parts
, free
);
699 n
= strcspn(dup
, "/");
702 lxc_free_array((void **)parts
, free
);
712 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
714 lxc_free_array((void **)parts
, free
);
718 char *lxc_append_paths(const char *first
, const char *second
)
720 size_t len
= strlen(first
) + strlen(second
) + 1;
721 const char *pattern
= "%s%s";
724 if (second
[0] != '/') {
729 result
= calloc(1, len
);
733 snprintf(result
, len
, pattern
, first
, second
);
737 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
739 char *token
, *str
, *saveptr
= NULL
;
740 char sep
[2] = { _sep
, '\0' };
742 if (!haystack
|| !needle
)
745 str
= alloca(strlen(haystack
)+1);
746 strcpy(str
, haystack
);
747 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
748 if (strcmp(needle
, token
) == 0)
755 char **lxc_string_split(const char *string
, char _sep
)
757 char *token
, *str
, *saveptr
= NULL
;
758 char sep
[2] = {_sep
, '\0'};
759 char **tmp
= NULL
, **result
= NULL
;
760 size_t result_capacity
= 0;
761 size_t result_count
= 0;
765 return calloc(1, sizeof(char *));
767 str
= alloca(strlen(string
) + 1);
769 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
770 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
773 result
[result_count
] = strdup(token
);
774 if (!result
[result_count
])
779 /* if we allocated too much, reduce it */
780 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
784 /* Make sure we don't return uninitialized memory. */
785 if (result_count
== 0)
790 lxc_free_array((void **)result
, free
);
795 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
, size_t *cnt
)
799 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
802 (*result
)[*cnt
] = strndup(start
, end
- start
);
803 if (!(*result
)[*cnt
])
811 * Given a a string 'one two "three four"', split into three words,
812 * one, two, and "three four"
814 char **lxc_string_split_quoted(char *string
)
816 char *nextword
= string
, *p
, state
;
817 char **result
= NULL
;
818 size_t result_capacity
= 0;
819 size_t result_count
= 0;
821 if (!string
|| !*string
)
822 return calloc(1, sizeof(char *));
824 // TODO I'm *not* handling escaped quote
826 for (p
= string
; *p
; p
++) {
831 else if (*p
== '"' || *p
== '\'') {
841 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
849 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
858 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
860 return realloc(result
, (result_count
+ 1) * sizeof(char *));
863 char **lxc_string_split_and_trim(const char *string
, char _sep
)
865 char *token
, *str
, *saveptr
= NULL
;
866 char sep
[2] = { _sep
, '\0' };
867 char **result
= NULL
;
868 size_t result_capacity
= 0;
869 size_t result_count
= 0;
874 return calloc(1, sizeof(char *));
876 str
= alloca(strlen(string
)+1);
878 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
879 while (token
[0] == ' ' || token
[0] == '\t')
882 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
886 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
889 result
[result_count
] = strdup(token
);
890 if (!result
[result_count
])
895 /* if we allocated too much, reduce it */
896 return realloc(result
, (result_count
+ 1) * sizeof(char *));
899 lxc_free_array((void **)result
, free
);
904 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
907 for (p
= array
; p
&& *p
; p
++)
912 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
917 /* first time around, catch some trivial mistakes of the user
918 * only initializing one of these */
919 if (!*array
|| !*capacity
) {
924 new_capacity
= *capacity
;
925 while (new_size
+ 1 > new_capacity
)
926 new_capacity
+= capacity_increment
;
927 if (new_capacity
!= *capacity
) {
928 /* we have to reallocate */
929 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
932 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
934 *capacity
= new_capacity
;
937 /* array has sufficient elements */
941 size_t lxc_array_len(void **array
)
946 for (p
= array
; p
&& *p
; p
++)
952 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
957 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
960 ret
= lxc_write_nointr(fd
, buf
, count
);
963 if ((size_t)ret
!= count
)
966 ret
= lxc_write_nointr(fd
, "\n", 1);
980 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
982 int fd
= -1, saved_errno
;
985 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
989 if (!buf
|| !count
) {
992 while ((ret
= read(fd
, buf2
, 100)) > 0)
997 memset(buf
, 0, count
);
998 ret
= read(fd
, buf
, count
);
1002 ERROR("read %s: %s", filename
, strerror(errno
));
1004 saved_errno
= errno
;
1006 errno
= saved_errno
;
1010 void **lxc_append_null_to_array(void **array
, size_t count
)
1014 /* Append NULL to the array */
1016 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1019 for (i
= 0; i
< count
; i
++)
1025 array
[count
] = NULL
;
1030 int randseed(bool srand_it
)
1033 srand pre-seed function based on /dev/urandom
1035 unsigned int seed
= time(NULL
) + getpid();
1038 f
= fopen("/dev/urandom", "r");
1040 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1042 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1052 uid_t
get_ns_uid(uid_t orig
)
1056 uid_t nsid
, hostid
, range
;
1057 FILE *f
= fopen("/proc/self/uid_map", "r");
1061 while (getline(&line
, &sz
, f
) != -1) {
1062 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1064 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1065 nsid
+= orig
- hostid
;
1077 bool dir_exists(const char *path
)
1082 ret
= stat(path
, &sb
);
1084 /* Could be something other than eexist, just say "no". */
1086 return S_ISDIR(sb
.st_mode
);
1089 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1090 * FNV has good anti collision properties and we're not worried
1091 * about pre-image resistance or one-way-ness, we're just trying to make
1092 * the name unique in the 108 bytes of space we have.
1094 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1098 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1100 /* xor the bottom with the current octet */
1101 hval
^= (uint64_t)*bp
;
1104 * multiply by the 64 bit FNV magic prime mod 2^64
1106 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1107 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1114 * Detect whether / is mounted MS_SHARED. The only way I know of to
1115 * check that is through /proc/self/mountinfo.
1116 * I'm only checking for /. If the container rootfs or mount location
1117 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1118 * out would be too much work to be worth it.
1120 int detect_shared_rootfs(void)
1122 char buf
[LXC_LINELEN
], *p
;
1127 f
= fopen("/proc/self/mountinfo", "r");
1130 while (fgets(buf
, LXC_LINELEN
, f
)) {
1131 for (p
= buf
, i
= 0; p
&& i
< 4; i
++)
1132 p
= strchr(p
+ 1, ' ');
1135 p2
= strchr(p
+ 1, ' ');
1139 if (strcmp(p
+ 1, "/") == 0) {
1140 /* This is '/'. Is it shared? */
1141 p
= strchr(p2
+ 1, ' ');
1142 if (p
&& strstr(p
, "shared:")) {
1152 bool switch_to_ns(pid_t pid
, const char *ns
) {
1154 char nspath
[MAXPATHLEN
];
1156 /* Switch to new ns */
1157 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1158 if (ret
< 0 || ret
>= MAXPATHLEN
)
1161 fd
= open(nspath
, O_RDONLY
);
1163 SYSERROR("failed to open %s", nspath
);
1169 SYSERROR("failed to set process %d to %s of %d.", pid
, ns
, fd
);
1178 * looking at fs/proc_namespace.c, it appears we can
1179 * actually expect the rootfs entry to very specifically contain
1180 * " - rootfs rootfs "
1181 * IIUC, so long as we've chrooted so that rootfs is not our root,
1182 * the rootfs entry should always be skipped in mountinfo contents.
1184 bool detect_ramfs_rootfs(void)
1192 f
= fopen("/proc/self/mountinfo", "r");
1196 while (getline(&line
, &len
, f
) != -1) {
1197 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
1198 p
= strchr(p
+ 1, ' ');
1201 p2
= strchr(p
+ 1, ' ');
1205 if (strcmp(p
+ 1, "/") == 0) {
1206 /* This is '/'. Is it the ramfs? */
1207 p
= strchr(p2
+ 1, '-');
1208 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1220 char *on_path(const char *cmd
, const char *rootfs
) {
1223 char *saveptr
= NULL
;
1224 char cmdpath
[MAXPATHLEN
];
1227 path
= getenv("PATH");
1231 path
= strdup(path
);
1235 entry
= strtok_r(path
, ":", &saveptr
);
1238 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1240 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1242 if (ret
< 0 || ret
>= MAXPATHLEN
)
1245 if (access(cmdpath
, X_OK
) == 0) {
1247 return strdup(cmdpath
);
1251 entry
= strtok_r(NULL
, ":", &saveptr
);
1258 bool file_exists(const char *f
)
1260 struct stat statbuf
;
1262 return stat(f
, &statbuf
) == 0;
1265 bool cgns_supported(void)
1267 return file_exists("/proc/self/ns/cgroup");
1270 /* historically lxc-init has been under /usr/lib/lxc and under
1271 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1273 char *choose_init(const char *rootfs
)
1276 const char *empty
= "",
1278 int ret
, env_set
= 0;
1280 if (!getenv("PATH")) {
1281 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1282 SYSERROR("Failed to setenv");
1286 retv
= on_path("init.lxc", rootfs
);
1289 if (unsetenv("PATH"))
1290 SYSERROR("Failed to unsetenv");
1296 retv
= malloc(PATH_MAX
);
1305 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1306 if (ret
< 0 || ret
>= PATH_MAX
) {
1307 ERROR("pathname too long");
1310 if (access(retv
, X_OK
) == 0)
1313 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1314 if (ret
< 0 || ret
>= PATH_MAX
) {
1315 ERROR("pathname too long");
1318 if (access(retv
, X_OK
) == 0)
1321 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1322 if (ret
< 0 || ret
>= PATH_MAX
) {
1323 ERROR("pathname too long");
1326 if (access(retv
, X_OK
) == 0)
1329 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1330 if (ret
< 0 || ret
>= PATH_MAX
) {
1331 ERROR("pathname too long");
1334 if (access(retv
, X_OK
) == 0)
1338 * Last resort, look for the statically compiled init.lxc which we
1339 * hopefully bind-mounted in.
1340 * If we are called during container setup, and we get to this point,
1341 * then the init.lxc.static from the host will need to be bind-mounted
1342 * in. So we return NULL here to indicate that.
1347 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1348 if (ret
< 0 || ret
>= PATH_MAX
) {
1349 WARN("Nonsense - name /lxc.init.static too long");
1352 if (access(retv
, X_OK
) == 0)
1360 int print_to_file(const char *file
, const char *content
)
1365 f
= fopen(file
, "w");
1368 if (fprintf(f
, "%s", content
) != strlen(content
))
1374 int is_dir(const char *path
)
1376 struct stat statbuf
;
1377 int ret
= stat(path
, &statbuf
);
1378 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1384 * Given the '-t' template option to lxc-create, figure out what to
1385 * do. If the template is a full executable path, use that. If it
1386 * is something like 'sshd', then return $templatepath/lxc-sshd.
1387 * On success return the template, on error return NULL.
1389 char *get_template_path(const char *t
)
1394 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1399 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1400 tpath
= malloc(len
);
1403 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1404 if (ret
< 0 || ret
>= len
) {
1408 if (access(tpath
, X_OK
) < 0) {
1409 SYSERROR("bad template: %s", t
);
1418 * @path: a pathname where / replaced with '\0'.
1419 * @offsetp: pointer to int showing which path segment was last seen.
1420 * Updated on return to reflect the next segment.
1421 * @fulllen: full original path length.
1422 * Returns a pointer to the next path segment, or NULL if done.
1424 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1426 int offset
= *offsetp
;
1428 if (offset
>= fulllen
)
1431 while (path
[offset
] != '\0' && offset
< fulllen
)
1433 while (path
[offset
] == '\0' && offset
< fulllen
)
1437 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1441 * Check that @subdir is a subdir of @dir. @len is the length of
1442 * @dir (to avoid having to recalculate it).
1444 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1446 size_t subdirlen
= strlen(subdir
);
1448 if (subdirlen
< len
)
1450 if (strncmp(subdir
, dir
, len
) != 0)
1452 if (dir
[len
-1] == '/')
1454 if (subdir
[len
] == '/' || subdirlen
== len
)
1460 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1461 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1463 static int check_symlink(int fd
)
1466 int ret
= fstat(fd
, &sb
);
1469 if (S_ISLNK(sb
.st_mode
))
1475 * Open a file or directory, provided that it contains no symlinks.
1477 * CAVEAT: This function must not be used for other purposes than container
1478 * setup before executing the container's init
1480 static int open_if_safe(int dirfd
, const char *nextpath
)
1482 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1483 if (newfd
>= 0) /* Was not a symlink, all good. */
1489 if (errno
== EPERM
|| errno
== EACCES
) {
1490 /* We're not root (cause we got EPERM) so try opening with
1493 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1495 /* O_PATH will return an fd for symlinks. We know
1496 * nextpath wasn't a symlink at last openat, so if fd is
1497 * now a link, then something * fishy is going on.
1499 int ret
= check_symlink(newfd
);
1511 * Open a path intending for mounting, ensuring that the final path
1512 * is inside the container's rootfs.
1514 * CAVEAT: This function must not be used for other purposes than container
1515 * setup before executing the container's init
1517 * @target: path to be opened
1518 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1519 * would be the container's rootfs.
1521 * Return an open fd for the path, or <0 on error.
1523 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1525 int curlen
= 0, dirfd
, fulllen
, i
;
1528 fulllen
= strlen(target
);
1530 /* make sure prefix-skip makes sense */
1531 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1532 curlen
= strlen(prefix_skip
);
1533 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1534 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1535 target
, prefix_skip
);
1539 * get_nextpath() expects the curlen argument to be
1540 * on a (turned into \0) / or before it, so decrement
1541 * curlen to make sure that happens
1550 /* Make a copy of target which we can hack up, and tokenize it */
1551 if ((dup
= strdup(target
)) == NULL
) {
1552 SYSERROR("Out of memory checking for symbolic link");
1555 for (i
= 0; i
< fulllen
; i
++) {
1560 dirfd
= open(prefix_skip
, O_RDONLY
);
1564 int newfd
, saved_errno
;
1567 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1569 newfd
= open_if_safe(dirfd
, nextpath
);
1570 saved_errno
= errno
;
1574 errno
= saved_errno
;
1576 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1587 * Safely mount a path into a container, ensuring that the mount target
1588 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1589 * uses the host's /)
1591 * CAVEAT: This function must not be used for other purposes than container
1592 * setup before executing the container's init
1594 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1595 unsigned long flags
, const void *data
, const char *rootfs
)
1597 int destfd
, ret
, saved_errno
;
1598 /* Only needs enough for /proc/self/fd/<fd>. */
1599 char srcbuf
[50], destbuf
[50];
1601 const char *mntsrc
= src
;
1606 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1607 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1608 INFO("this is a relative bind mount");
1609 srcfd
= open_without_symlink(src
, NULL
);
1612 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1613 if (ret
< 0 || ret
> 50) {
1615 ERROR("Out of memory");
1621 destfd
= open_without_symlink(dest
, rootfs
);
1624 saved_errno
= errno
;
1626 errno
= saved_errno
;
1631 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1632 if (ret
< 0 || ret
> 50) {
1636 ERROR("Out of memory");
1640 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1641 saved_errno
= errno
;
1646 errno
= saved_errno
;
1647 SYSERROR("Failed to mount %s onto %s", src
, dest
);
1655 * Mount a proc under @rootfs if proc self points to a pid other than
1656 * my own. This is needed to have a known-good proc mount for setting
1657 * up LSMs both at container startup and attach.
1659 * @rootfs : the rootfs where proc should be mounted
1661 * Returns < 0 on failure, 0 if the correct proc was already mounted
1662 * and 1 if a new proc was mounted.
1664 * NOTE: not to be called from inside the container namespace!
1666 int lxc_mount_proc_if_needed(const char *rootfs
)
1668 char path
[MAXPATHLEN
];
1669 int link_to_pid
, linklen
, mypid
, ret
;
1670 char link
[LXC_NUMSTRLEN64
] = {0};
1672 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1673 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1674 SYSERROR("proc path name too long");
1678 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1680 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1681 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1682 SYSERROR("proc path name too long");
1686 /* /proc not mounted */
1688 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1691 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1692 link
[linklen
- 1] = '\0';
1693 ERROR("readlink returned truncated content: \"%s\"", link
);
1698 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1700 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1703 /* correct procfs is already mounted */
1704 if (link_to_pid
== mypid
)
1707 ret
= umount2(path
, MNT_DETACH
);
1709 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1712 /* rootfs is NULL */
1713 if (!strcmp(rootfs
, ""))
1714 ret
= mount("proc", path
, "proc", 0, NULL
);
1716 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1720 INFO("mounted /proc in container for security transition");
1724 int open_devnull(void)
1726 int fd
= open("/dev/null", O_RDWR
);
1729 SYSERROR("Can't open /dev/null");
1734 int set_stdfds(int fd
)
1741 ret
= dup2(fd
, STDIN_FILENO
);
1745 ret
= dup2(fd
, STDOUT_FILENO
);
1749 ret
= dup2(fd
, STDERR_FILENO
);
1756 int null_stdfds(void)
1759 int fd
= open_devnull();
1762 ret
= set_stdfds(fd
);
1770 * Return the number of lines in file @fn, or -1 on error
1772 int lxc_count_file_lines(const char *fn
)
1779 f
= fopen_cloexec(fn
, "r");
1783 while (getline(&line
, &sz
, f
) != -1) {
1791 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
1794 void *tmp
= NULL
, *overlap
= NULL
;
1796 /* We establish an anonymous mapping that is one byte larger than the
1797 * underlying file. The pages handed to us are zero filled. */
1798 tmp
= mmap(addr
, length
+ 1, PROT_READ
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1799 if (tmp
== MAP_FAILED
)
1802 /* Now we establish a fixed-address mapping starting at the address we
1803 * received from our anonymous mapping and replace all bytes excluding
1804 * the additional \0-byte with the file. This allows us to use normal
1805 * string-handling functions. */
1806 overlap
= mmap(tmp
, length
, prot
, MAP_FIXED
| flags
, fd
, offset
);
1807 if (overlap
== MAP_FAILED
)
1808 munmap(tmp
, length
+ 1);
1813 int lxc_strmunmap(void *addr
, size_t length
)
1815 return munmap(addr
, length
+ 1);
1818 /* Check whether a signal is blocked by a process. */
1819 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1820 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
1821 bool task_blocking_signal(pid_t pid
, int signal
)
1825 long unsigned int sigblk
= 0;
1830 char status
[__PROC_STATUS_LEN
];
1832 ret
= snprintf(status
, __PROC_STATUS_LEN
, "/proc/%d/status", pid
);
1833 if (ret
< 0 || ret
>= __PROC_STATUS_LEN
)
1836 f
= fopen(status
, "r");
1840 while (getline(&line
, &n
, f
) != -1) {
1841 if (strncmp(line
, "SigBlk:\t", 8))
1844 if (sscanf(line
+ 8, "%lx", &sigblk
) != 1)
1848 if (sigblk
& (1LU << (signal
- 1)))
1857 static int lxc_append_null_to_list(void ***list
)
1863 for (; (*list
)[newentry
]; newentry
++) {
1867 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
1872 (*list
)[newentry
+ 1] = NULL
;
1877 int lxc_append_string(char ***list
, char *entry
)
1882 newentry
= lxc_append_null_to_list((void ***)list
);
1886 copy
= strdup(entry
);
1890 (*list
)[newentry
] = copy
;
1895 int lxc_preserve_ns(const int pid
, const char *ns
)
1898 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
1899 #define __NS_PATH_LEN 50
1900 char path
[__NS_PATH_LEN
];
1902 /* This way we can use this function to also check whether namespaces
1903 * are supported by the kernel by passing in the NULL or the empty
1906 ret
= snprintf(path
, __NS_PATH_LEN
, "/proc/%d/ns%s%s", pid
,
1907 !ns
|| strcmp(ns
, "") == 0 ? "" : "/",
1908 !ns
|| strcmp(ns
, "") == 0 ? "" : ns
);
1909 if (ret
< 0 || (size_t)ret
>= __NS_PATH_LEN
)
1912 return open(path
, O_RDONLY
| O_CLOEXEC
);
1915 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
1918 unsigned long int uli
;
1920 while (isspace(*numstr
))
1927 uli
= strtoul(numstr
, &err
, 0);
1928 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
1931 if (err
== numstr
|| *err
!= '\0')
1937 *converted
= (unsigned int)uli
;
1941 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
1944 unsigned long int uli
;
1946 while (isspace(*numstr
))
1953 uli
= strtoul(numstr
, &err
, 0);
1954 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
1957 if (err
== numstr
|| *err
!= '\0')
1964 int lxc_safe_int(const char *numstr
, int *converted
)
1967 signed long int sli
;
1970 sli
= strtol(numstr
, &err
, 0);
1971 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
1974 if (errno
!= 0 && sli
== 0)
1977 if (err
== numstr
|| *err
!= '\0')
1980 if (sli
> INT_MAX
|| sli
< INT_MIN
)
1983 *converted
= (int)sli
;
1987 int lxc_safe_long(const char *numstr
, long int *converted
)
1990 signed long int sli
;
1993 sli
= strtol(numstr
, &err
, 0);
1994 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
1997 if (errno
!= 0 && sli
== 0)
2000 if (err
== numstr
|| *err
!= '\0')
2007 int lxc_safe_long_long(const char *numstr
, long long int *converted
)
2010 signed long long int sli
;
2013 sli
= strtoll(numstr
, &err
, 0);
2014 if (errno
== ERANGE
&& (sli
== LLONG_MAX
|| sli
== LLONG_MIN
))
2017 if (errno
!= 0 && sli
== 0)
2020 if (err
== numstr
|| *err
!= '\0')
2027 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2029 if (setgid(gid
) < 0) {
2030 SYSERROR("Failed to switch to gid %d.", gid
);
2033 NOTICE("Switched to gid %d.", gid
);
2035 if (setuid(uid
) < 0) {
2036 SYSERROR("Failed to switch to uid %d.", uid
);
2039 NOTICE("Switched to uid %d.", uid
);
2044 /* Simple covenience function which enables uniform logging. */
2045 int lxc_setgroups(int size
, gid_t list
[])
2047 if (setgroups(size
, list
) < 0) {
2048 SYSERROR("Failed to setgroups().");
2051 NOTICE("Dropped additional groups.");
2056 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2059 struct loop_info64 lo64
;
2061 int dfd
= -1, fd
= -1, ret
= -1;
2063 dir
= opendir("/dev");
2067 while ((dp
= readdir(dir
))) {
2071 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2078 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2082 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2084 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2092 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2093 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2110 static int lxc_get_unused_loop_dev(char *name_loop
)
2113 int fd_ctl
= -1, fd_tmp
= -1;
2115 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2119 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2123 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2124 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2127 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2136 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2139 struct loop_info64 lo64
;
2140 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2142 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2144 if (fd_loop
== -ENODEV
)
2145 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2150 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2154 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2158 memset(&lo64
, 0, sizeof(lo64
));
2159 lo64
.lo_flags
= flags
;
2161 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2171 if (fret
< 0 && fd_loop
>= 0) {
2179 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2185 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2187 /* We consider anything else than EINVAL deadly to prevent going
2188 * into an infinite loop. (The other alternative is constantly
2189 * parsing /proc/self/mountinfo which is yucky and probably
2192 if (errno
!= EINVAL
)
2195 /* Just stop counting when this happens. That'd just be so
2196 * stupid that we won't even bother trying to report back the
2197 * correct value anymore.
2199 if (umounts
!= INT_MAX
)
2201 /* We succeeded in umounting. Make sure that there's no other
2202 * mountpoint stacked underneath.
2210 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2213 int ret
, fret
, pipefd
[2];
2216 /* Make sure our callers do not receive unitialized memory. */
2217 if (buf_size
> 0 && buf
)
2220 if (pipe(pipefd
) < 0) {
2221 SYSERROR("failed to create pipe");
2229 SYSERROR("failed to create new process");
2234 /* Close the read-end of the pipe. */
2237 /* Redirect std{err,out} to write-end of the
2240 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2242 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2244 /* Close the write-end of the pipe. */
2248 SYSERROR("failed to duplicate std{err,out} file descriptor");
2252 /* Does not return. */
2254 ERROR("failed to exec command");
2258 /* close the write-end of the pipe */
2261 if (buf
&& buf_size
> 0) {
2262 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2264 buf
[bytes
- 1] = '\0';
2267 fret
= wait_for_pid(child
);
2268 /* close the read-end of the pipe */
2274 char *must_make_path(const char *first
, ...)
2278 size_t full_len
= strlen(first
);
2280 dest
= must_copy_string(first
);
2282 va_start(args
, first
);
2283 while ((cur
= va_arg(args
, char *)) != NULL
) {
2284 full_len
+= strlen(cur
);
2287 dest
= must_realloc(dest
, full_len
+ 1);
2297 char *must_copy_string(const char *entry
)
2304 ret
= strdup(entry
);
2310 void *must_realloc(void *orig
, size_t sz
)
2315 ret
= realloc(orig
, sz
);
2321 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2323 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2326 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2332 ret
= statfs(path
, &sb
);
2336 has_type
= is_fs_type(&sb
, magic_val
);
2337 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2338 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2343 bool lxc_nic_exists(char *nic
)
2345 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2346 char path
[__LXC_SYS_CLASS_NET_LEN
];
2350 if (!strcmp(nic
, "none"))
2353 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2354 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2357 ret
= stat(path
, &sb
);
2364 int lxc_make_tmpfile(char *template, bool rm
)
2368 fd
= mkstemp(template);
2375 ret
= unlink(template);
2384 uint64_t lxc_getpagesize(void)
2388 pgsz
= sysconf(_SC_PAGESIZE
);
2395 int parse_byte_size_string(const char *s
, int64_t *converted
)
2397 int ret
, suffix_len
;
2399 int64_t mltpl
, overflow
;
2401 char dup
[LXC_NUMSTRLEN64
+ 2];
2404 if (!s
|| !strcmp(s
, ""))
2407 end
= stpncpy(dup
, s
, sizeof(dup
));
2411 if (isdigit(*(end
- 1)))
2413 else if (isalpha(*(end
- 1)))
2418 if ((end
- 2) == dup
&& !isdigit(*(end
- 2)))
2421 if (isalpha(*(end
- 2))) {
2422 if (suffix_len
== 1)
2428 if (suffix_len
> 0) {
2429 memcpy(suffix
, end
- suffix_len
, suffix_len
);
2430 *(suffix
+ suffix_len
) = '\0';
2431 *(end
- suffix_len
) = '\0';
2433 dup
[lxc_char_right_gc(dup
, strlen(dup
))] = '\0';
2435 ret
= lxc_safe_long_long(dup
, &conv
);
2439 if (suffix_len
!= 2) {
2444 if (!strcmp(suffix
, "kB"))
2446 else if (!strcmp(suffix
, "MB"))
2447 mltpl
= 1024 * 1024;
2448 else if (!strcmp(suffix
, "GB"))
2449 mltpl
= 1024 * 1024 * 1024;
2453 overflow
= conv
* mltpl
;
2454 if (conv
!= 0 && (overflow
/ conv
) != mltpl
)
2457 *converted
= overflow
;