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. */
41 #include <sys/mount.h>
42 #include <sys/param.h>
43 #include <sys/prctl.h>
45 #include <sys/types.h>
50 #include "namespace.h"
55 #define O_PATH 010000000
59 #define O_NOFOLLOW 00400000
62 lxc_log_define(lxc_utils
, lxc
);
65 * if path is btrfs, tries to remove it and any subvolumes beneath it
67 extern bool btrfs_try_remove_subvol(const char *path
);
69 static int _recursive_rmdir(const char *dirname
, dev_t pdev
,
70 const char *exclude
, int level
, bool onedev
)
72 struct dirent
*direntp
;
75 char pathname
[MAXPATHLEN
];
76 bool hadexclude
= false;
78 dir
= opendir(dirname
);
80 ERROR("failed to open %s", dirname
);
84 while ((direntp
= readdir(dir
))) {
91 if (!strcmp(direntp
->d_name
, ".") ||
92 !strcmp(direntp
->d_name
, ".."))
95 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
96 if (rc
< 0 || rc
>= MAXPATHLEN
) {
97 ERROR("pathname too long");
102 if (!level
&& exclude
&& !strcmp(direntp
->d_name
, exclude
)) {
103 ret
= rmdir(pathname
);
107 INFO("Not deleting snapshot %s", pathname
);
111 ret
= unlink(pathname
);
113 INFO("Failed to remove %s", pathname
);
116 SYSERROR("Failed to rmdir %s", pathname
);
124 ret
= lstat(pathname
, &mystat
);
126 ERROR("Failed to stat %s", pathname
);
130 if (onedev
&& mystat
.st_dev
!= pdev
) {
131 /* TODO should we be checking /proc/self/mountinfo for
132 * pathname and not doing this if found? */
133 if (btrfs_try_remove_subvol(pathname
))
134 INFO("Removed btrfs subvolume at %s\n", pathname
);
137 if (S_ISDIR(mystat
.st_mode
)) {
138 if (_recursive_rmdir(pathname
, pdev
, exclude
, level
+1, onedev
) < 0)
141 if (unlink(pathname
) < 0) {
142 SYSERROR("Failed to delete %s", pathname
);
148 if (rmdir(dirname
) < 0 && !btrfs_try_remove_subvol(dirname
) && !hadexclude
) {
149 ERROR("Failed to delete %s", dirname
);
155 ERROR("Failed to close directory %s", dirname
);
159 return failed
? -1 : 0;
162 /* We have two different magic values for overlayfs, yay. */
163 #ifndef OVERLAYFS_SUPER_MAGIC
164 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
167 #ifndef OVERLAY_SUPER_MAGIC
168 #define OVERLAY_SUPER_MAGIC 0x794c7630
171 /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
174 static bool is_native_overlayfs(const char *path
)
176 if (has_fs_type(path
, OVERLAY_SUPER_MAGIC
) ||
177 has_fs_type(path
, OVERLAYFS_SUPER_MAGIC
))
183 /* returns 0 on success, -1 if there were any failures */
184 extern int lxc_rmdir_onedev(const char *path
, const char *exclude
)
189 if (is_native_overlayfs(path
))
192 if (lstat(path
, &mystat
) < 0) {
196 ERROR("Failed to stat %s", path
);
200 return _recursive_rmdir(path
, mystat
.st_dev
, exclude
, 0, onedev
);
203 /* borrowed from iproute2 */
204 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
213 res
= strtoul(arg
, &ptr
, base
);
214 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
222 extern int mkdir_p(const char *dir
, mode_t mode
)
224 const char *tmp
= dir
;
225 const char *orig
= dir
;
229 dir
= tmp
+ strspn(tmp
, "/");
230 tmp
= dir
+ strcspn(dir
, "/");
231 makeme
= strndup(orig
, dir
- orig
);
233 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
234 SYSERROR("failed to create directory '%s'", makeme
);
251 if (stat(RUNTIME_PATH
, &sb
) < 0) {
255 if (geteuid() == sb
.st_uid
|| getegid() == sb
.st_gid
) {
256 rundir
= strdup(RUNTIME_PATH
);
260 rundir
= getenv("XDG_RUNTIME_DIR");
262 rundir
= strdup(rundir
);
266 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
267 homedir
= getenv("HOME");
269 ERROR("HOME isn't set in the environment.");
273 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
274 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
279 int wait_for_pid(pid_t pid
)
284 ret
= waitpid(pid
, &status
, 0);
292 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
297 int lxc_wait_for_pid_status(pid_t pid
)
302 ret
= waitpid(pid
, &status
, 0);
313 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
317 ret
= write(fd
, buf
, count
);
318 if (ret
< 0 && errno
== EINTR
)
323 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
327 ret
= read(fd
, buf
, count
);
328 if (ret
< 0 && errno
== EINTR
)
333 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
336 ret
= lxc_read_nointr(fd
, buf
, count
);
339 if ((size_t)ret
!= count
)
341 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
349 #include <gnutls/gnutls.h>
350 #include <gnutls/crypto.h>
352 __attribute__((constructor
))
353 static void gnutls_lxc_init(void)
355 gnutls_global_init();
358 int sha1sum_file(char *fnam
, unsigned char *digest
)
367 f
= fopen_cloexec(fnam
, "r");
369 SYSERROR("Error opening template");
372 if (fseek(f
, 0, SEEK_END
) < 0) {
373 SYSERROR("Error seeking to end of template");
377 if ((flen
= ftell(f
)) < 0) {
378 SYSERROR("Error telling size of template");
382 if (fseek(f
, 0, SEEK_SET
) < 0) {
383 SYSERROR("Error seeking to start of template");
387 if ((buf
= malloc(flen
+1)) == NULL
) {
388 SYSERROR("Out of memory");
392 if (fread(buf
, 1, flen
, f
) != flen
) {
393 SYSERROR("Failure reading template");
399 SYSERROR("Failre closing template");
404 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
410 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
413 size_t count
= 1 + skip
;
416 /* first determine size of argument list, we don't want to reallocate
421 char* arg
= va_arg(ap2
, char*);
428 result
= calloc(count
, sizeof(char*));
433 char* arg
= va_arg(ap
, char*);
436 arg
= do_strdup
? strdup(arg
) : arg
;
439 result
[count
++] = arg
;
442 /* calloc has already set last element to NULL*/
450 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
452 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
455 struct lxc_popen_FILE
*lxc_popen(const char *command
)
460 struct lxc_popen_FILE
*fp
= NULL
;
462 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
475 /* duplicate stdout */
476 if (pipe_fds
[1] != STDOUT_FILENO
)
477 ret
= dup2(pipe_fds
[1], STDOUT_FILENO
);
479 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
485 /* duplicate stderr */
486 if (pipe_fds
[1] != STDERR_FILENO
)
487 ret
= dup2(pipe_fds
[1], STDERR_FILENO
);
489 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
494 /* unblock all signals */
495 ret
= sigfillset(&mask
);
499 ret
= pthread_sigmask(SIG_UNBLOCK
, &mask
, NULL
);
503 execl("/bin/sh", "sh", "-c", command
, (char *)NULL
);
510 fp
= malloc(sizeof(*fp
));
513 memset(fp
, 0, sizeof(*fp
));
515 fp
->child_pid
= child_pid
;
516 fp
->pipe
= pipe_fds
[0];
518 /* From now on, closing fp->f will also close fp->pipe. So only ever
519 * call fclose(fp->f).
521 fp
->f
= fdopen(pipe_fds
[0], "r");
528 /* We can only close pipe_fds[0] if fdopen() didn't succeed or wasn't
529 * called yet. Otherwise the fd belongs to the file opened by fdopen()
530 * since it isn't dup()ed.
532 if (fp
&& !fp
->f
&& pipe_fds
[0] >= 0)
535 if (pipe_fds
[1] >= 0)
547 int lxc_pclose(struct lxc_popen_FILE
*fp
)
556 wait_pid
= waitpid(fp
->child_pid
, &wstatus
, 0);
557 } while (wait_pid
< 0 && errno
== EINTR
);
568 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
570 ssize_t len
= -1, saved_len
= -1;
572 size_t replacement_len
= strlen(replacement
);
573 size_t needle_len
= strlen(needle
);
575 /* should be executed exactly twice */
576 while (len
== -1 || result
== NULL
) {
582 result
= calloc(1, len
+ 1);
590 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
591 part_len
= (ssize_t
)(p
- last_p
);
592 if (result
&& part_len
> 0)
593 memcpy(&result
[len
], last_p
, part_len
);
595 if (result
&& replacement_len
> 0)
596 memcpy(&result
[len
], replacement
, replacement_len
);
597 len
+= replacement_len
;
600 part_len
= strlen(last_p
);
601 if (result
&& part_len
> 0)
602 memcpy(&result
[len
], last_p
, part_len
);
606 /* make sure we did the same thing twice,
607 * once for calculating length, the other
608 * time for copying data */
609 if (saved_len
!= len
) {
613 /* make sure we didn't overwrite any buffer,
614 * due to calloc the string should be 0-terminated */
615 if (result
[len
] != '\0') {
623 bool lxc_string_in_array(const char *needle
, const char **haystack
)
625 for (; haystack
&& *haystack
; haystack
++)
626 if (!strcmp(needle
, *haystack
))
631 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
635 size_t sep_len
= strlen(sep
);
636 size_t result_len
= use_as_prefix
* sep_len
;
638 /* calculate new string length */
639 for (p
= (char **)parts
; *p
; p
++)
640 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
642 result
= calloc(result_len
+ 1, 1);
648 for (p
= (char **)parts
; *p
; p
++) {
649 if (p
> (char **)parts
)
657 char **lxc_normalize_path(const char *path
)
661 size_t components_len
= 0;
664 components
= lxc_string_split(path
, '/');
667 for (p
= components
; *p
; p
++)
670 /* resolve '.' and '..' */
671 for (pos
= 0; pos
< components_len
; ) {
672 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
673 /* eat this element */
674 free(components
[pos
]);
675 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
677 } else if (!strcmp(components
[pos
], "..")) {
678 /* eat this and the previous element */
679 free(components
[pos
- 1]);
680 free(components
[pos
]);
681 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
692 char *lxc_deslashify(const char *path
)
702 parts
= lxc_normalize_path(dup
);
708 /* We'll end up here if path == "///" or path == "". */
712 lxc_free_array((void **)parts
, free
);
715 n
= strcspn(dup
, "/");
718 lxc_free_array((void **)parts
, free
);
728 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
730 lxc_free_array((void **)parts
, free
);
734 char *lxc_append_paths(const char *first
, const char *second
)
739 const char *pattern
= "%s%s";
741 len
= strlen(first
) + strlen(second
) + 1;
742 if (second
[0] != '/') {
747 result
= calloc(1, len
);
751 ret
= snprintf(result
, len
, pattern
, first
, second
);
752 if (ret
< 0 || (size_t)ret
>= len
) {
760 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
762 char *token
, *str
, *saveptr
= NULL
;
763 char sep
[2] = { _sep
, '\0' };
765 if (!haystack
|| !needle
)
768 str
= alloca(strlen(haystack
)+1);
769 strcpy(str
, haystack
);
770 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
771 if (strcmp(needle
, token
) == 0)
778 char **lxc_string_split(const char *string
, char _sep
)
780 char *token
, *str
, *saveptr
= NULL
;
781 char sep
[2] = {_sep
, '\0'};
782 char **tmp
= NULL
, **result
= NULL
;
783 size_t result_capacity
= 0;
784 size_t result_count
= 0;
788 return calloc(1, sizeof(char *));
790 str
= alloca(strlen(string
) + 1);
792 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
793 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
796 result
[result_count
] = strdup(token
);
797 if (!result
[result_count
])
802 /* if we allocated too much, reduce it */
803 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
807 /* Make sure we don't return uninitialized memory. */
808 if (result_count
== 0)
813 lxc_free_array((void **)result
, free
);
818 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
, size_t *cnt
)
822 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
825 (*result
)[*cnt
] = strndup(start
, end
- start
);
826 if (!(*result
)[*cnt
])
834 * Given a a string 'one two "three four"', split into three words,
835 * one, two, and "three four"
837 char **lxc_string_split_quoted(char *string
)
839 char *nextword
= string
, *p
, state
;
840 char **result
= NULL
;
841 size_t result_capacity
= 0;
842 size_t result_count
= 0;
844 if (!string
|| !*string
)
845 return calloc(1, sizeof(char *));
847 // TODO I'm *not* handling escaped quote
849 for (p
= string
; *p
; p
++) {
854 else if (*p
== '"' || *p
== '\'') {
864 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
872 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
881 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
883 return realloc(result
, (result_count
+ 1) * sizeof(char *));
886 char **lxc_string_split_and_trim(const char *string
, char _sep
)
888 char *token
, *str
, *saveptr
= NULL
;
889 char sep
[2] = { _sep
, '\0' };
890 char **result
= NULL
;
891 size_t result_capacity
= 0;
892 size_t result_count
= 0;
897 return calloc(1, sizeof(char *));
899 str
= alloca(strlen(string
)+1);
901 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
902 while (token
[0] == ' ' || token
[0] == '\t')
905 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
909 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
912 result
[result_count
] = strdup(token
);
913 if (!result
[result_count
])
918 /* if we allocated too much, reduce it */
919 return realloc(result
, (result_count
+ 1) * sizeof(char *));
922 lxc_free_array((void **)result
, free
);
927 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
930 for (p
= array
; p
&& *p
; p
++)
935 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
940 /* first time around, catch some trivial mistakes of the user
941 * only initializing one of these */
942 if (!*array
|| !*capacity
) {
947 new_capacity
= *capacity
;
948 while (new_size
+ 1 > new_capacity
)
949 new_capacity
+= capacity_increment
;
950 if (new_capacity
!= *capacity
) {
951 /* we have to reallocate */
952 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
955 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
957 *capacity
= new_capacity
;
960 /* array has sufficient elements */
964 size_t lxc_array_len(void **array
)
969 for (p
= array
; p
&& *p
; p
++)
975 int lxc_write_to_file(const char *filename
, const void *buf
, size_t count
,
976 bool add_newline
, mode_t mode
)
981 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, mode
);
984 ret
= lxc_write_nointr(fd
, buf
, count
);
987 if ((size_t)ret
!= count
)
990 ret
= lxc_write_nointr(fd
, "\n", 1);
1000 errno
= saved_errno
;
1004 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
1006 int fd
= -1, saved_errno
;
1009 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1013 if (!buf
|| !count
) {
1016 while ((ret
= read(fd
, buf2
, 100)) > 0)
1021 memset(buf
, 0, count
);
1022 ret
= read(fd
, buf
, count
);
1026 ERROR("read %s: %s", filename
, strerror(errno
));
1028 saved_errno
= errno
;
1030 errno
= saved_errno
;
1034 void **lxc_append_null_to_array(void **array
, size_t count
)
1038 /* Append NULL to the array */
1040 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1043 for (i
= 0; i
< count
; i
++)
1049 array
[count
] = NULL
;
1054 int randseed(bool srand_it
)
1057 srand pre-seed function based on /dev/urandom
1059 unsigned int seed
= time(NULL
) + getpid();
1062 f
= fopen("/dev/urandom", "r");
1064 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1066 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1076 uid_t
get_ns_uid(uid_t orig
)
1080 uid_t nsid
, hostid
, range
;
1081 FILE *f
= fopen("/proc/self/uid_map", "r");
1085 while (getline(&line
, &sz
, f
) != -1) {
1086 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1088 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1089 nsid
+= orig
- hostid
;
1101 bool dir_exists(const char *path
)
1106 ret
= stat(path
, &sb
);
1108 /* Could be something other than eexist, just say "no". */
1110 return S_ISDIR(sb
.st_mode
);
1113 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1114 * FNV has good anti collision properties and we're not worried
1115 * about pre-image resistance or one-way-ness, we're just trying to make
1116 * the name unique in the 108 bytes of space we have.
1118 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1122 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1124 /* xor the bottom with the current octet */
1125 hval
^= (uint64_t)*bp
;
1128 * multiply by the 64 bit FNV magic prime mod 2^64
1130 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1131 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1138 * Detect whether / is mounted MS_SHARED. The only way I know of to
1139 * check that is through /proc/self/mountinfo.
1140 * I'm only checking for /. If the container rootfs or mount location
1141 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1142 * out would be too much work to be worth it.
1144 int detect_shared_rootfs(void)
1146 char buf
[LXC_LINELEN
], *p
;
1151 f
= fopen("/proc/self/mountinfo", "r");
1154 while (fgets(buf
, LXC_LINELEN
, f
)) {
1155 for (p
= buf
, i
= 0; p
&& i
< 4; i
++)
1156 p
= strchr(p
+ 1, ' ');
1159 p2
= strchr(p
+ 1, ' ');
1163 if (strcmp(p
+ 1, "/") == 0) {
1164 /* This is '/'. Is it shared? */
1165 p
= strchr(p2
+ 1, ' ');
1166 if (p
&& strstr(p
, "shared:")) {
1176 bool switch_to_ns(pid_t pid
, const char *ns
) {
1178 char nspath
[MAXPATHLEN
];
1180 /* Switch to new ns */
1181 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1182 if (ret
< 0 || ret
>= MAXPATHLEN
)
1185 fd
= open(nspath
, O_RDONLY
);
1187 SYSERROR("failed to open %s", nspath
);
1193 SYSERROR("failed to set process %d to %s of %d.", pid
, ns
, fd
);
1202 * looking at fs/proc_namespace.c, it appears we can
1203 * actually expect the rootfs entry to very specifically contain
1204 * " - rootfs rootfs "
1205 * IIUC, so long as we've chrooted so that rootfs is not our root,
1206 * the rootfs entry should always be skipped in mountinfo contents.
1208 bool detect_ramfs_rootfs(void)
1216 f
= fopen("/proc/self/mountinfo", "r");
1220 while (getline(&line
, &len
, f
) != -1) {
1221 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
1222 p
= strchr(p
+ 1, ' ');
1225 p2
= strchr(p
+ 1, ' ');
1229 if (strcmp(p
+ 1, "/") == 0) {
1230 /* This is '/'. Is it the ramfs? */
1231 p
= strchr(p2
+ 1, '-');
1232 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1244 char *on_path(const char *cmd
, const char *rootfs
) {
1247 char *saveptr
= NULL
;
1248 char cmdpath
[MAXPATHLEN
];
1251 path
= getenv("PATH");
1255 path
= strdup(path
);
1259 entry
= strtok_r(path
, ":", &saveptr
);
1262 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1264 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1266 if (ret
< 0 || ret
>= MAXPATHLEN
)
1269 if (access(cmdpath
, X_OK
) == 0) {
1271 return strdup(cmdpath
);
1275 entry
= strtok_r(NULL
, ":", &saveptr
);
1282 bool file_exists(const char *f
)
1284 struct stat statbuf
;
1286 return stat(f
, &statbuf
) == 0;
1289 bool cgns_supported(void)
1291 return file_exists("/proc/self/ns/cgroup");
1294 /* historically lxc-init has been under /usr/lib/lxc and under
1295 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1297 char *choose_init(const char *rootfs
)
1300 const char *empty
= "",
1302 int ret
, env_set
= 0;
1304 if (!getenv("PATH")) {
1305 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1306 SYSERROR("Failed to setenv");
1310 retv
= on_path("init.lxc", rootfs
);
1313 if (unsetenv("PATH"))
1314 SYSERROR("Failed to unsetenv");
1320 retv
= malloc(PATH_MAX
);
1329 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1330 if (ret
< 0 || ret
>= PATH_MAX
) {
1331 ERROR("pathname too long");
1334 if (access(retv
, X_OK
) == 0)
1337 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1338 if (ret
< 0 || ret
>= PATH_MAX
) {
1339 ERROR("pathname too long");
1342 if (access(retv
, X_OK
) == 0)
1345 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1346 if (ret
< 0 || ret
>= PATH_MAX
) {
1347 ERROR("pathname too long");
1350 if (access(retv
, X_OK
) == 0)
1353 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1354 if (ret
< 0 || ret
>= PATH_MAX
) {
1355 ERROR("pathname too long");
1358 if (access(retv
, X_OK
) == 0)
1362 * Last resort, look for the statically compiled init.lxc which we
1363 * hopefully bind-mounted in.
1364 * If we are called during container setup, and we get to this point,
1365 * then the init.lxc.static from the host will need to be bind-mounted
1366 * in. So we return NULL here to indicate that.
1371 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1372 if (ret
< 0 || ret
>= PATH_MAX
) {
1373 WARN("Nonsense - name /lxc.init.static too long");
1376 if (access(retv
, X_OK
) == 0)
1384 int print_to_file(const char *file
, const char *content
)
1389 f
= fopen(file
, "w");
1392 if (fprintf(f
, "%s", content
) != strlen(content
))
1398 int is_dir(const char *path
)
1400 struct stat statbuf
;
1401 int ret
= stat(path
, &statbuf
);
1402 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1408 * Given the '-t' template option to lxc-create, figure out what to
1409 * do. If the template is a full executable path, use that. If it
1410 * is something like 'sshd', then return $templatepath/lxc-sshd.
1411 * On success return the template, on error return NULL.
1413 char *get_template_path(const char *t
)
1418 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1423 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1424 tpath
= malloc(len
);
1427 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1428 if (ret
< 0 || ret
>= len
) {
1432 if (access(tpath
, X_OK
) < 0) {
1433 SYSERROR("bad template: %s", t
);
1442 * @path: a pathname where / replaced with '\0'.
1443 * @offsetp: pointer to int showing which path segment was last seen.
1444 * Updated on return to reflect the next segment.
1445 * @fulllen: full original path length.
1446 * Returns a pointer to the next path segment, or NULL if done.
1448 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1450 int offset
= *offsetp
;
1452 if (offset
>= fulllen
)
1455 while (path
[offset
] != '\0' && offset
< fulllen
)
1457 while (path
[offset
] == '\0' && offset
< fulllen
)
1461 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1465 * Check that @subdir is a subdir of @dir. @len is the length of
1466 * @dir (to avoid having to recalculate it).
1468 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1470 size_t subdirlen
= strlen(subdir
);
1472 if (subdirlen
< len
)
1474 if (strncmp(subdir
, dir
, len
) != 0)
1476 if (dir
[len
-1] == '/')
1478 if (subdir
[len
] == '/' || subdirlen
== len
)
1484 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1485 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1487 static int check_symlink(int fd
)
1490 int ret
= fstat(fd
, &sb
);
1493 if (S_ISLNK(sb
.st_mode
))
1499 * Open a file or directory, provided that it contains no symlinks.
1501 * CAVEAT: This function must not be used for other purposes than container
1502 * setup before executing the container's init
1504 static int open_if_safe(int dirfd
, const char *nextpath
)
1506 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1507 if (newfd
>= 0) /* Was not a symlink, all good. */
1513 if (errno
== EPERM
|| errno
== EACCES
) {
1514 /* We're not root (cause we got EPERM) so try opening with
1517 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1519 /* O_PATH will return an fd for symlinks. We know
1520 * nextpath wasn't a symlink at last openat, so if fd is
1521 * now a link, then something * fishy is going on.
1523 int ret
= check_symlink(newfd
);
1535 * Open a path intending for mounting, ensuring that the final path
1536 * is inside the container's rootfs.
1538 * CAVEAT: This function must not be used for other purposes than container
1539 * setup before executing the container's init
1541 * @target: path to be opened
1542 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1543 * would be the container's rootfs.
1545 * Return an open fd for the path, or <0 on error.
1547 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1549 int curlen
= 0, dirfd
, fulllen
, i
;
1552 fulllen
= strlen(target
);
1554 /* make sure prefix-skip makes sense */
1555 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1556 curlen
= strlen(prefix_skip
);
1557 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1558 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1559 target
, prefix_skip
);
1563 * get_nextpath() expects the curlen argument to be
1564 * on a (turned into \0) / or before it, so decrement
1565 * curlen to make sure that happens
1574 /* Make a copy of target which we can hack up, and tokenize it */
1575 if ((dup
= strdup(target
)) == NULL
) {
1576 SYSERROR("Out of memory checking for symbolic link");
1579 for (i
= 0; i
< fulllen
; i
++) {
1584 dirfd
= open(prefix_skip
, O_RDONLY
);
1588 int newfd
, saved_errno
;
1591 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1593 newfd
= open_if_safe(dirfd
, nextpath
);
1594 saved_errno
= errno
;
1598 errno
= saved_errno
;
1600 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1611 * Safely mount a path into a container, ensuring that the mount target
1612 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1613 * uses the host's /)
1615 * CAVEAT: This function must not be used for other purposes than container
1616 * setup before executing the container's init
1618 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1619 unsigned long flags
, const void *data
, const char *rootfs
)
1621 int destfd
, ret
, saved_errno
;
1622 /* Only needs enough for /proc/self/fd/<fd>. */
1623 char srcbuf
[50], destbuf
[50];
1625 const char *mntsrc
= src
;
1630 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1631 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1632 INFO("this is a relative bind mount");
1633 srcfd
= open_without_symlink(src
, NULL
);
1636 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1637 if (ret
< 0 || ret
> 50) {
1639 ERROR("Out of memory");
1645 destfd
= open_without_symlink(dest
, rootfs
);
1648 saved_errno
= errno
;
1650 errno
= saved_errno
;
1655 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1656 if (ret
< 0 || ret
> 50) {
1660 ERROR("Out of memory");
1664 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1665 saved_errno
= errno
;
1670 errno
= saved_errno
;
1671 SYSERROR("Failed to mount %s onto %s", src
? src
: "(null)", dest
);
1679 * Mount a proc under @rootfs if proc self points to a pid other than
1680 * my own. This is needed to have a known-good proc mount for setting
1681 * up LSMs both at container startup and attach.
1683 * @rootfs : the rootfs where proc should be mounted
1685 * Returns < 0 on failure, 0 if the correct proc was already mounted
1686 * and 1 if a new proc was mounted.
1688 * NOTE: not to be called from inside the container namespace!
1690 int lxc_mount_proc_if_needed(const char *rootfs
)
1692 char path
[MAXPATHLEN
];
1693 int link_to_pid
, linklen
, mypid
, ret
;
1694 char link
[LXC_NUMSTRLEN64
] = {0};
1696 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1697 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1698 SYSERROR("proc path name too long");
1702 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1704 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1705 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1706 SYSERROR("proc path name too long");
1710 /* /proc not mounted */
1712 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1715 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1716 link
[linklen
- 1] = '\0';
1717 ERROR("readlink returned truncated content: \"%s\"", link
);
1721 mypid
= lxc_raw_getpid();
1722 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1724 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1727 /* correct procfs is already mounted */
1728 if (link_to_pid
== mypid
)
1731 ret
= umount2(path
, MNT_DETACH
);
1733 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1736 /* rootfs is NULL */
1737 if (!strcmp(rootfs
, ""))
1738 ret
= mount("proc", path
, "proc", 0, NULL
);
1740 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1744 INFO("mounted /proc in container for security transition");
1748 int open_devnull(void)
1750 int fd
= open("/dev/null", O_RDWR
);
1753 SYSERROR("Can't open /dev/null");
1758 int set_stdfds(int fd
)
1765 ret
= dup2(fd
, STDIN_FILENO
);
1769 ret
= dup2(fd
, STDOUT_FILENO
);
1773 ret
= dup2(fd
, STDERR_FILENO
);
1780 int null_stdfds(void)
1783 int fd
= open_devnull();
1786 ret
= set_stdfds(fd
);
1794 * Return the number of lines in file @fn, or -1 on error
1796 int lxc_count_file_lines(const char *fn
)
1803 f
= fopen_cloexec(fn
, "r");
1807 while (getline(&line
, &sz
, f
) != -1) {
1815 /* Check whether a signal is blocked by a process. */
1816 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1817 #define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1)
1818 bool task_blocking_signal(pid_t pid
, int signal
)
1821 char status
[__PROC_STATUS_LEN
];
1823 long unsigned int sigblk
= 0;
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:", 7))
1840 if (sscanf(line
+ 7, "%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_uint64(const char *numstr
, uint64_t *converted
)
1966 while (isspace(*numstr
))
1973 u
= strtoull(numstr
, &err
, 0);
1974 if (errno
== ERANGE
&& u
== ULLONG_MAX
)
1977 if (err
== numstr
|| *err
!= '\0')
1984 int lxc_safe_int(const char *numstr
, 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')
2000 if (sli
> INT_MAX
|| sli
< INT_MIN
)
2003 *converted
= (int)sli
;
2007 int lxc_safe_long(const char *numstr
, long int *converted
)
2010 signed long int sli
;
2013 sli
= strtol(numstr
, &err
, 0);
2014 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2017 if (errno
!= 0 && sli
== 0)
2020 if (err
== numstr
|| *err
!= '\0')
2027 int lxc_safe_long_long(const char *numstr
, long long int *converted
)
2030 signed long long int sli
;
2033 sli
= strtoll(numstr
, &err
, 0);
2034 if (errno
== ERANGE
&& (sli
== LLONG_MAX
|| sli
== LLONG_MIN
))
2037 if (errno
!= 0 && sli
== 0)
2040 if (err
== numstr
|| *err
!= '\0')
2047 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2049 if (setgid(gid
) < 0) {
2050 SYSERROR("Failed to switch to gid %d.", gid
);
2053 NOTICE("Switched to gid %d.", gid
);
2055 if (setuid(uid
) < 0) {
2056 SYSERROR("Failed to switch to uid %d.", uid
);
2059 NOTICE("Switched to uid %d.", uid
);
2064 /* Simple covenience function which enables uniform logging. */
2065 int lxc_setgroups(int size
, gid_t list
[])
2067 if (setgroups(size
, list
) < 0) {
2068 SYSERROR("Failed to setgroups().");
2071 NOTICE("Dropped additional groups.");
2076 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2079 struct loop_info64 lo64
;
2081 int dfd
= -1, fd
= -1, ret
= -1;
2083 dir
= opendir("/dev");
2087 while ((dp
= readdir(dir
))) {
2091 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2098 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2102 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2104 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2112 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2113 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2130 static int lxc_get_unused_loop_dev(char *name_loop
)
2133 int fd_ctl
= -1, fd_tmp
= -1;
2135 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2139 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2143 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2144 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2147 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2156 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2159 struct loop_info64 lo64
;
2160 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2162 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2164 if (fd_loop
== -ENODEV
)
2165 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2170 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2174 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2178 memset(&lo64
, 0, sizeof(lo64
));
2179 lo64
.lo_flags
= flags
;
2181 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2191 if (fret
< 0 && fd_loop
>= 0) {
2199 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2205 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2207 /* We consider anything else than EINVAL deadly to prevent going
2208 * into an infinite loop. (The other alternative is constantly
2209 * parsing /proc/self/mountinfo which is yucky and probably
2212 if (errno
!= EINVAL
)
2215 /* Just stop counting when this happens. That'd just be so
2216 * stupid that we won't even bother trying to report back the
2217 * correct value anymore.
2219 if (umounts
!= INT_MAX
)
2221 /* We succeeded in umounting. Make sure that there's no other
2222 * mountpoint stacked underneath.
2230 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2233 int ret
, fret
, pipefd
[2];
2236 /* Make sure our callers do not receive unitialized memory. */
2237 if (buf_size
> 0 && buf
)
2240 if (pipe(pipefd
) < 0) {
2241 SYSERROR("failed to create pipe");
2245 child
= lxc_raw_clone(0);
2249 SYSERROR("failed to create new process");
2254 /* Close the read-end of the pipe. */
2257 /* Redirect std{err,out} to write-end of the
2260 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2262 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2264 /* Close the write-end of the pipe. */
2268 SYSERROR("failed to duplicate std{err,out} file descriptor");
2269 _exit(EXIT_FAILURE
);
2272 /* Does not return. */
2274 ERROR("failed to exec command");
2275 _exit(EXIT_FAILURE
);
2278 /* close the write-end of the pipe */
2281 if (buf
&& buf_size
> 0) {
2282 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2284 buf
[bytes
- 1] = '\0';
2287 fret
= wait_for_pid(child
);
2288 /* close the read-end of the pipe */
2294 char *must_make_path(const char *first
, ...)
2298 size_t full_len
= strlen(first
);
2300 dest
= must_copy_string(first
);
2302 va_start(args
, first
);
2303 while ((cur
= va_arg(args
, char *)) != NULL
) {
2304 full_len
+= strlen(cur
);
2307 dest
= must_realloc(dest
, full_len
+ 1);
2317 char *must_append_path(char *first
, ...)
2324 full_len
= strlen(first
);
2325 va_start(args
, first
);
2326 while ((cur
= va_arg(args
, char *)) != NULL
) {
2327 full_len
+= strlen(cur
);
2332 dest
= must_realloc(dest
, full_len
+ 1);
2344 char *must_copy_string(const char *entry
)
2351 ret
= strdup(entry
);
2357 void *must_realloc(void *orig
, size_t sz
)
2362 ret
= realloc(orig
, sz
);
2368 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2370 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2373 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2379 ret
= statfs(path
, &sb
);
2383 has_type
= is_fs_type(&sb
, magic_val
);
2384 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2385 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2390 bool lxc_nic_exists(char *nic
)
2392 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2393 char path
[__LXC_SYS_CLASS_NET_LEN
];
2397 if (!strcmp(nic
, "none"))
2400 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2401 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2404 ret
= stat(path
, &sb
);
2411 int lxc_make_tmpfile(char *template, bool rm
)
2415 fd
= mkstemp(template);
2422 ret
= unlink(template);
2431 int parse_byte_size_string(const char *s
, int64_t *converted
)
2433 int ret
, suffix_len
;
2435 int64_t mltpl
, overflow
;
2437 char dup
[LXC_NUMSTRLEN64
+ 2];
2438 char suffix
[3] = {0};
2440 if (!s
|| !strcmp(s
, ""))
2443 end
= stpncpy(dup
, s
, sizeof(dup
) - 1);
2447 if (isdigit(*(end
- 1)))
2449 else if (isalpha(*(end
- 1)))
2454 if (suffix_len
> 0 && (end
- 2) == dup
&& !isdigit(*(end
- 2)))
2457 if (suffix_len
> 0 && isalpha(*(end
- 2)))
2460 if (suffix_len
> 0) {
2461 memcpy(suffix
, end
- suffix_len
, suffix_len
);
2462 *(suffix
+ suffix_len
) = '\0';
2463 *(end
- suffix_len
) = '\0';
2465 dup
[lxc_char_right_gc(dup
, strlen(dup
))] = '\0';
2467 ret
= lxc_safe_long_long(dup
, &conv
);
2471 if (suffix_len
!= 2) {
2476 if (strcasecmp(suffix
, "KB") == 0)
2478 else if (strcasecmp(suffix
, "MB") == 0)
2479 mltpl
= 1024 * 1024;
2480 else if (strcasecmp(suffix
, "GB") == 0)
2481 mltpl
= 1024 * 1024 * 1024;
2485 overflow
= conv
* mltpl
;
2486 if (conv
!= 0 && (overflow
/ conv
) != mltpl
)
2489 *converted
= overflow
;
2493 uint64_t lxc_find_next_power2(uint64_t n
)
2495 /* 0 is not valid input. We return 0 to the caller since 0 is not a
2496 * valid power of two.
2511 int lxc_set_death_signal(int signal
)
2516 ret
= prctl(PR_SET_PDEATHSIG
, signal
, 0, 0, 0);
2518 /* Check whether we have been orphaned. */
2519 ppid
= (pid_t
)syscall(SYS_getppid
);
2523 self
= lxc_raw_getpid();
2524 ret
= kill(self
, SIGKILL
);
2530 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal
);