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 #include "include/strlcpy.h"
59 #include "include/strlcat.h"
63 #define O_PATH 010000000
67 #define O_NOFOLLOW 00400000
70 lxc_log_define(utils
, lxc
);
73 * if path is btrfs, tries to remove it and any subvolumes beneath it
75 extern bool btrfs_try_remove_subvol(const char *path
);
77 static int _recursive_rmdir(const char *dirname
, dev_t pdev
,
78 const char *exclude
, int level
, bool onedev
)
80 struct dirent
*direntp
;
83 char pathname
[MAXPATHLEN
];
84 bool hadexclude
= false;
86 dir
= opendir(dirname
);
88 ERROR("failed to open %s", dirname
);
92 while ((direntp
= readdir(dir
))) {
96 if (!strcmp(direntp
->d_name
, ".") ||
97 !strcmp(direntp
->d_name
, ".."))
100 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
101 if (rc
< 0 || rc
>= MAXPATHLEN
) {
102 ERROR("pathname too long");
107 if (!level
&& exclude
&& !strcmp(direntp
->d_name
, exclude
)) {
108 ret
= rmdir(pathname
);
112 INFO("Not deleting snapshot %s", pathname
);
116 ret
= unlink(pathname
);
118 INFO("Failed to remove %s", pathname
);
121 SYSERROR("Failed to rmdir %s", pathname
);
129 ret
= lstat(pathname
, &mystat
);
131 ERROR("Failed to stat %s", pathname
);
136 if (onedev
&& mystat
.st_dev
!= pdev
) {
137 /* TODO should we be checking /proc/self/mountinfo for
138 * pathname and not doing this if found? */
139 if (btrfs_try_remove_subvol(pathname
))
140 INFO("Removed btrfs subvolume at %s\n", pathname
);
144 if (S_ISDIR(mystat
.st_mode
)) {
145 if (_recursive_rmdir(pathname
, pdev
, exclude
, level
+1, onedev
) < 0)
148 if (unlink(pathname
) < 0) {
149 SYSERROR("Failed to delete %s", pathname
);
155 if (rmdir(dirname
) < 0 && !btrfs_try_remove_subvol(dirname
) && !hadexclude
) {
156 ERROR("Failed to delete %s", dirname
);
162 ERROR("Failed to close directory %s", dirname
);
166 return failed
? -1 : 0;
169 /* We have two different magic values for overlayfs, yay. */
170 #ifndef OVERLAYFS_SUPER_MAGIC
171 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
174 #ifndef OVERLAY_SUPER_MAGIC
175 #define OVERLAY_SUPER_MAGIC 0x794c7630
178 /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
181 static bool is_native_overlayfs(const char *path
)
183 if (has_fs_type(path
, OVERLAY_SUPER_MAGIC
) ||
184 has_fs_type(path
, OVERLAYFS_SUPER_MAGIC
))
190 /* returns 0 on success, -1 if there were any failures */
191 extern int lxc_rmdir_onedev(const char *path
, const char *exclude
)
196 if (is_native_overlayfs(path
))
199 if (lstat(path
, &mystat
) < 0) {
203 ERROR("Failed to stat %s", path
);
207 return _recursive_rmdir(path
, mystat
.st_dev
, exclude
, 0, onedev
);
210 /* borrowed from iproute2 */
211 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
220 res
= strtoul(arg
, &ptr
, base
);
221 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
229 extern int mkdir_p(const char *dir
, mode_t mode
)
231 const char *tmp
= dir
;
232 const char *orig
= dir
;
236 dir
= tmp
+ strspn(tmp
, "/");
237 tmp
= dir
+ strcspn(dir
, "/");
239 makeme
= strndup(orig
, dir
- orig
);
241 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
242 SYSERROR("failed to create directory '%s'", makeme
);
259 if (stat(RUNTIME_PATH
, &sb
) < 0)
262 if (geteuid() == sb
.st_uid
|| getegid() == sb
.st_gid
) {
263 rundir
= strdup(RUNTIME_PATH
);
267 rundir
= getenv("XDG_RUNTIME_DIR");
269 rundir
= strdup(rundir
);
273 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
274 homedir
= getenv("HOME");
276 ERROR("HOME isn't set in the environment.");
280 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
284 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
289 int wait_for_pid(pid_t pid
)
294 ret
= waitpid(pid
, &status
, 0);
305 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
311 int lxc_wait_for_pid_status(pid_t pid
)
316 ret
= waitpid(pid
, &status
, 0);
330 ssize_t
lxc_write_nointr(int fd
, const void *buf
, size_t count
)
334 ret
= write(fd
, buf
, count
);
335 if (ret
< 0 && errno
== EINTR
)
341 ssize_t
lxc_read_nointr(int fd
, void *buf
, size_t count
)
345 ret
= read(fd
, buf
, count
);
346 if (ret
< 0 && errno
== EINTR
)
352 ssize_t
lxc_read_nointr_expect(int fd
, void *buf
, size_t count
, const void *expected_buf
)
356 ret
= lxc_read_nointr(fd
, buf
, count
);
360 if ((size_t)ret
!= count
)
363 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
372 #include <gnutls/gnutls.h>
373 #include <gnutls/crypto.h>
375 __attribute__((constructor
))
376 static void gnutls_lxc_init(void)
378 gnutls_global_init();
381 int sha1sum_file(char *fnam
, unsigned char *digest
)
391 f
= fopen_cloexec(fnam
, "r");
393 SYSERROR("Error opening template");
397 if (fseek(f
, 0, SEEK_END
) < 0) {
398 SYSERROR("Error seeking to end of template");
403 if ((flen
= ftell(f
)) < 0) {
404 SYSERROR("Error telling size of template");
409 if (fseek(f
, 0, SEEK_SET
) < 0) {
410 SYSERROR("Error seeking to start of template");
415 if ((buf
= malloc(flen
+1)) == NULL
) {
416 SYSERROR("Out of memory");
421 if (fread(buf
, 1, flen
, f
) != flen
) {
422 SYSERROR("Failure reading template");
429 SYSERROR("Failre closing template");
435 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
441 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
444 size_t count
= 1 + skip
;
447 /* first determine size of argument list, we don't want to reallocate
452 char* arg
= va_arg(ap2
, char*);
459 result
= calloc(count
, sizeof(char*));
465 char* arg
= va_arg(ap
, char*);
468 arg
= do_strdup
? strdup(arg
) : arg
;
471 result
[count
++] = arg
;
474 /* calloc has already set last element to NULL*/
482 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
484 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
487 struct lxc_popen_FILE
*lxc_popen(const char *command
)
492 struct lxc_popen_FILE
*fp
= NULL
;
494 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
507 /* duplicate stdout */
508 if (pipe_fds
[1] != STDOUT_FILENO
)
509 ret
= dup2(pipe_fds
[1], STDOUT_FILENO
);
511 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
517 /* duplicate stderr */
518 if (pipe_fds
[1] != STDERR_FILENO
)
519 ret
= dup2(pipe_fds
[1], STDERR_FILENO
);
521 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
526 /* unblock all signals */
527 ret
= sigfillset(&mask
);
531 ret
= pthread_sigmask(SIG_UNBLOCK
, &mask
, NULL
);
535 execl("/bin/sh", "sh", "-c", command
, (char *)NULL
);
542 fp
= malloc(sizeof(*fp
));
546 memset(fp
, 0, sizeof(*fp
));
548 fp
->child_pid
= child_pid
;
549 fp
->pipe
= pipe_fds
[0];
551 /* From now on, closing fp->f will also close fp->pipe. So only ever
552 * call fclose(fp->f).
554 fp
->f
= fdopen(pipe_fds
[0], "r");
561 /* We can only close pipe_fds[0] if fdopen() didn't succeed or wasn't
562 * called yet. Otherwise the fd belongs to the file opened by fdopen()
563 * since it isn't dup()ed.
565 if (fp
&& !fp
->f
&& pipe_fds
[0] >= 0)
568 if (pipe_fds
[1] >= 0)
580 int lxc_pclose(struct lxc_popen_FILE
*fp
)
589 wait_pid
= waitpid(fp
->child_pid
, &wstatus
, 0);
590 } while (wait_pid
< 0 && errno
== EINTR
);
601 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
603 ssize_t len
= -1, saved_len
= -1;
605 size_t replacement_len
= strlen(replacement
);
606 size_t needle_len
= strlen(needle
);
608 /* should be executed exactly twice */
609 while (len
== -1 || result
== NULL
) {
615 result
= calloc(1, len
+ 1);
624 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
625 part_len
= (ssize_t
)(p
- last_p
);
626 if (result
&& part_len
> 0)
627 memcpy(&result
[len
], last_p
, part_len
);
631 if (result
&& replacement_len
> 0)
632 memcpy(&result
[len
], replacement
, replacement_len
);
634 len
+= replacement_len
;
638 part_len
= strlen(last_p
);
639 if (result
&& part_len
> 0)
640 memcpy(&result
[len
], last_p
, part_len
);
645 /* make sure we did the same thing twice,
646 * once for calculating length, the other
647 * time for copying data */
648 if (saved_len
!= len
) {
653 /* make sure we didn't overwrite any buffer,
654 * due to calloc the string should be 0-terminated */
655 if (result
[len
] != '\0') {
663 bool lxc_string_in_array(const char *needle
, const char **haystack
)
665 for (; haystack
&& *haystack
; haystack
++)
666 if (!strcmp(needle
, *haystack
))
672 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
676 size_t sep_len
= strlen(sep
);
677 size_t result_len
= use_as_prefix
* sep_len
;
680 /* calculate new string length */
681 for (p
= (char **)parts
; *p
; p
++)
682 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
684 buf_len
= result_len
+ 1;
685 result
= calloc(buf_len
, 1);
690 (void)strlcpy(result
, sep
, buf_len
);
692 for (p
= (char **)parts
; *p
; p
++) {
693 if (p
> (char **)parts
)
694 (void)strlcat(result
, sep
, buf_len
);
696 (void)strlcat(result
, *p
, buf_len
);
702 char **lxc_normalize_path(const char *path
)
706 size_t components_len
= 0;
709 components
= lxc_string_split(path
, '/');
713 for (p
= components
; *p
; p
++)
716 /* resolve '.' and '..' */
717 for (pos
= 0; pos
< components_len
; ) {
718 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
719 /* eat this element */
720 free(components
[pos
]);
721 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
723 } else if (!strcmp(components
[pos
], "..")) {
724 /* eat this and the previous element */
725 free(components
[pos
- 1]);
726 free(components
[pos
]);
727 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
738 char *lxc_deslashify(const char *path
)
748 parts
= lxc_normalize_path(dup
);
754 /* We'll end up here if path == "///" or path == "". */
758 lxc_free_array((void **)parts
, free
);
762 n
= strcspn(dup
, "/");
765 lxc_free_array((void **)parts
, free
);
775 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
777 lxc_free_array((void **)parts
, free
);
781 char *lxc_append_paths(const char *first
, const char *second
)
786 const char *pattern
= "%s%s";
788 len
= strlen(first
) + strlen(second
) + 1;
789 if (second
[0] != '/') {
794 result
= calloc(1, len
);
798 ret
= snprintf(result
, len
, pattern
, first
, second
);
799 if (ret
< 0 || (size_t)ret
>= len
) {
807 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
809 char *token
, *str
, *saveptr
= NULL
;
810 char sep
[2] = { _sep
, '\0' };
813 if (!haystack
|| !needle
)
816 len
= strlen(haystack
);
817 str
= alloca(len
+ 1);
818 (void)strlcpy(str
, haystack
, len
+ 1);
820 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
821 if (strcmp(needle
, token
) == 0)
828 char **lxc_string_split(const char *string
, char _sep
)
830 char *token
, *str
, *saveptr
= NULL
;
831 char sep
[2] = {_sep
, '\0'};
832 char **tmp
= NULL
, **result
= NULL
;
833 size_t result_capacity
= 0;
834 size_t result_count
= 0;
839 return calloc(1, sizeof(char *));
841 len
= strlen(string
);
842 str
= alloca(len
+ 1);
843 (void)strlcpy(str
, string
, len
+ 1);
845 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
846 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
850 result
[result_count
] = strdup(token
);
851 if (!result
[result_count
])
857 /* if we allocated too much, reduce it */
858 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
864 /* Make sure we don't return uninitialized memory. */
865 if (result_count
== 0)
872 lxc_free_array((void **)result
, free
);
877 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
, size_t *cnt
)
881 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
885 (*result
)[*cnt
] = strndup(start
, end
- start
);
886 if (!(*result
)[*cnt
])
895 * Given a a string 'one two "three four"', split into three words,
896 * one, two, and "three four"
898 char **lxc_string_split_quoted(char *string
)
900 char *nextword
= string
, *p
, state
;
901 char **result
= NULL
;
902 size_t result_capacity
= 0;
903 size_t result_count
= 0;
905 if (!string
|| !*string
)
906 return calloc(1, sizeof(char *));
908 // TODO I'm *not* handling escaped quote
910 for (p
= string
; *p
; p
++) {
915 else if (*p
== '"' || *p
== '\'') {
925 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
933 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
942 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
944 return realloc(result
, (result_count
+ 1) * sizeof(char *));
947 char **lxc_string_split_and_trim(const char *string
, char _sep
)
949 char *token
, *str
, *saveptr
= NULL
;
950 char sep
[2] = { _sep
, '\0' };
951 char **result
= NULL
;
952 size_t result_capacity
= 0;
953 size_t result_count
= 0;
959 return calloc(1, sizeof(char *));
961 len
= strlen(string
);
962 str
= alloca(len
+ 1);
963 (void)strlcpy(str
, string
, len
+ 1);
965 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
966 while (token
[0] == ' ' || token
[0] == '\t')
970 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
975 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
979 result
[result_count
] = strdup(token
);
980 if (!result
[result_count
])
986 /* if we allocated too much, reduce it */
987 return realloc(result
, (result_count
+ 1) * sizeof(char *));
991 lxc_free_array((void **)result
, free
);
996 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
1000 for (p
= array
; p
&& *p
; p
++)
1001 element_free_fn(*p
);
1006 int lxc_grow_array(void ***array
, size_t *capacity
, size_t new_size
, size_t capacity_increment
)
1008 size_t new_capacity
;
1011 /* first time around, catch some trivial mistakes of the user
1012 * only initializing one of these */
1013 if (!*array
|| !*capacity
) {
1018 new_capacity
= *capacity
;
1019 while (new_size
+ 1 > new_capacity
)
1020 new_capacity
+= capacity_increment
;
1022 if (new_capacity
!= *capacity
) {
1023 /* we have to reallocate */
1024 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
1028 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
1030 *capacity
= new_capacity
;
1033 /* array has sufficient elements */
1037 size_t lxc_array_len(void **array
)
1042 for (p
= array
; p
&& *p
; p
++)
1048 int lxc_write_to_file(const char *filename
, const void *buf
, size_t count
,
1049 bool add_newline
, mode_t mode
)
1051 int fd
, saved_errno
;
1054 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, mode
);
1058 ret
= lxc_write_nointr(fd
, buf
, count
);
1062 if ((size_t)ret
!= count
)
1066 ret
= lxc_write_nointr(fd
, "\n", 1);
1075 saved_errno
= errno
;
1077 errno
= saved_errno
;
1081 int lxc_read_from_file(const char *filename
, void *buf
, size_t count
)
1083 int fd
= -1, saved_errno
;
1086 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1090 if (!buf
|| !count
) {
1094 while ((ret
= read(fd
, buf2
, 100)) > 0)
1100 memset(buf
, 0, count
);
1101 ret
= read(fd
, buf
, count
);
1105 SYSERROR("Read %s", filename
);
1107 saved_errno
= errno
;
1109 errno
= saved_errno
;
1113 void **lxc_append_null_to_array(void **array
, size_t count
)
1117 /* Append NULL to the array */
1119 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1122 for (i
= 0; i
< count
; i
++)
1129 array
[count
] = NULL
;
1135 int randseed(bool srand_it
)
1138 srand pre-seed function based on /dev/urandom
1140 unsigned int seed
= time(NULL
) + getpid();
1143 f
= fopen("/dev/urandom", "r");
1145 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1147 SYSDEBUG("unable to fread /dev/urandom, fallback to time+pid rand seed");
1158 uid_t
get_ns_uid(uid_t orig
)
1162 uid_t nsid
, hostid
, range
;
1163 FILE *f
= fopen("/proc/self/uid_map", "r");
1167 while (getline(&line
, &sz
, f
) != -1) {
1168 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1171 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1172 nsid
+= orig
- hostid
;
1185 bool dir_exists(const char *path
)
1190 ret
= stat(path
, &sb
);
1192 /* Could be something other than eexist, just say "no". */
1195 return S_ISDIR(sb
.st_mode
);
1198 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1199 * FNV has good anti collision properties and we're not worried
1200 * about pre-image resistance or one-way-ness, we're just trying to make
1201 * the name unique in the 108 bytes of space we have.
1203 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1207 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1209 /* xor the bottom with the current octet */
1210 hval
^= (uint64_t)*bp
;
1213 * multiply by the 64 bit FNV magic prime mod 2^64
1215 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1216 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1222 bool is_shared_mountpoint(const char *path
)
1224 char buf
[LXC_LINELEN
];
1229 f
= fopen("/proc/self/mountinfo", "r");
1233 while (fgets(buf
, LXC_LINELEN
, f
)) {
1234 for (p
= buf
, i
= 0; p
&& i
< 4; i
++)
1235 p
= strchr(p
+ 1, ' ');
1239 p2
= strchr(p
+ 1, ' ');
1244 if (strcmp(p
+ 1, path
) == 0) {
1245 /* This is the path. Is it shared? */
1246 p
= strchr(p2
+ 1, ' ');
1247 if (p
&& strstr(p
, "shared:")) {
1259 * Detect whether / is mounted MS_SHARED. The only way I know of to
1260 * check that is through /proc/self/mountinfo.
1261 * I'm only checking for /. If the container rootfs or mount location
1262 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1263 * out would be too much work to be worth it.
1265 int detect_shared_rootfs(void)
1267 if (is_shared_mountpoint("/"))
1272 bool switch_to_ns(pid_t pid
, const char *ns
) {
1274 char nspath
[MAXPATHLEN
];
1276 /* Switch to new ns */
1277 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1278 if (ret
< 0 || ret
>= MAXPATHLEN
)
1281 fd
= open(nspath
, O_RDONLY
);
1283 SYSERROR("Failed to open %s", nspath
);
1289 SYSERROR("Failed to set process %d to %s of %d.", pid
, ns
, fd
);
1299 * looking at fs/proc_namespace.c, it appears we can
1300 * actually expect the rootfs entry to very specifically contain
1301 * " - rootfs rootfs "
1302 * IIUC, so long as we've chrooted so that rootfs is not our root,
1303 * the rootfs entry should always be skipped in mountinfo contents.
1305 bool detect_ramfs_rootfs(void)
1313 f
= fopen("/proc/self/mountinfo", "r");
1317 while (getline(&line
, &len
, f
) != -1) {
1318 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
1319 p
= strchr(p
+ 1, ' ');
1323 p2
= strchr(p
+ 1, ' ');
1328 if (strcmp(p
+ 1, "/") == 0) {
1329 /* This is '/'. Is it the ramfs? */
1330 p
= strchr(p2
+ 1, '-');
1331 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1344 char *on_path(const char *cmd
, const char *rootfs
) {
1347 char *saveptr
= NULL
;
1348 char cmdpath
[MAXPATHLEN
];
1351 path
= getenv("PATH");
1355 path
= strdup(path
);
1359 entry
= strtok_r(path
, ":", &saveptr
);
1362 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1364 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1366 if (ret
< 0 || ret
>= MAXPATHLEN
)
1369 if (access(cmdpath
, X_OK
) == 0) {
1371 return strdup(cmdpath
);
1375 entry
= strtok_r(NULL
, ":", &saveptr
);
1382 bool file_exists(const char *f
)
1384 struct stat statbuf
;
1386 return stat(f
, &statbuf
) == 0;
1389 bool cgns_supported(void)
1391 return file_exists("/proc/self/ns/cgroup");
1394 /* historically lxc-init has been under /usr/lib/lxc and under
1395 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1397 char *choose_init(const char *rootfs
)
1400 const char *empty
= "",
1402 int ret
, env_set
= 0;
1404 if (!getenv("PATH")) {
1405 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1406 SYSERROR("Failed to setenv");
1411 retv
= on_path("init.lxc", rootfs
);
1414 if (unsetenv("PATH"))
1415 SYSERROR("Failed to unsetenv");
1421 retv
= malloc(PATH_MAX
);
1430 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1431 if (ret
< 0 || ret
>= PATH_MAX
) {
1432 ERROR("pathname too long");
1436 if (access(retv
, X_OK
) == 0)
1439 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1440 if (ret
< 0 || ret
>= PATH_MAX
) {
1441 ERROR("pathname too long");
1445 if (access(retv
, X_OK
) == 0)
1448 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1449 if (ret
< 0 || ret
>= PATH_MAX
) {
1450 ERROR("pathname too long");
1454 if (access(retv
, X_OK
) == 0)
1457 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1458 if (ret
< 0 || ret
>= PATH_MAX
) {
1459 ERROR("pathname too long");
1463 if (access(retv
, X_OK
) == 0)
1467 * Last resort, look for the statically compiled init.lxc which we
1468 * hopefully bind-mounted in.
1469 * If we are called during container setup, and we get to this point,
1470 * then the init.lxc.static from the host will need to be bind-mounted
1471 * in. So we return NULL here to indicate that.
1476 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1477 if (ret
< 0 || ret
>= PATH_MAX
) {
1478 WARN("Nonsense - name /lxc.init.static too long");
1482 if (access(retv
, X_OK
) == 0)
1490 int print_to_file(const char *file
, const char *content
)
1495 f
= fopen(file
, "w");
1499 if (fprintf(f
, "%s", content
) != strlen(content
))
1506 int is_dir(const char *path
)
1508 struct stat statbuf
;
1511 ret
= stat(path
, &statbuf
);
1512 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1519 * Given the '-t' template option to lxc-create, figure out what to
1520 * do. If the template is a full executable path, use that. If it
1521 * is something like 'sshd', then return $templatepath/lxc-sshd.
1522 * On success return the template, on error return NULL.
1524 char *get_template_path(const char *t
)
1529 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1534 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1536 tpath
= malloc(len
);
1540 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1541 if (ret
< 0 || ret
>= len
) {
1546 if (access(tpath
, X_OK
) < 0) {
1547 SYSERROR("bad template: %s", t
);
1556 * @path: a pathname where / replaced with '\0'.
1557 * @offsetp: pointer to int showing which path segment was last seen.
1558 * Updated on return to reflect the next segment.
1559 * @fulllen: full original path length.
1560 * Returns a pointer to the next path segment, or NULL if done.
1562 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1564 int offset
= *offsetp
;
1566 if (offset
>= fulllen
)
1569 while (path
[offset
] != '\0' && offset
< fulllen
)
1572 while (path
[offset
] == '\0' && offset
< fulllen
)
1576 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1580 * Check that @subdir is a subdir of @dir. @len is the length of
1581 * @dir (to avoid having to recalculate it).
1583 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1585 size_t subdirlen
= strlen(subdir
);
1587 if (subdirlen
< len
)
1590 if (strncmp(subdir
, dir
, len
) != 0)
1593 if (dir
[len
-1] == '/')
1596 if (subdir
[len
] == '/' || subdirlen
== len
)
1603 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1604 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1606 static int check_symlink(int fd
)
1611 ret
= fstat(fd
, &sb
);
1615 if (S_ISLNK(sb
.st_mode
))
1622 * Open a file or directory, provided that it contains no symlinks.
1624 * CAVEAT: This function must not be used for other purposes than container
1625 * setup before executing the container's init
1627 static int open_if_safe(int dirfd
, const char *nextpath
)
1629 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1630 if (newfd
>= 0) /* Was not a symlink, all good. */
1636 if (errno
== EPERM
|| errno
== EACCES
) {
1637 /* We're not root (cause we got EPERM) so try opening with
1640 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1642 /* O_PATH will return an fd for symlinks. We know
1643 * nextpath wasn't a symlink at last openat, so if fd is
1644 * now a link, then something * fishy is going on.
1646 int ret
= check_symlink(newfd
);
1658 * Open a path intending for mounting, ensuring that the final path
1659 * is inside the container's rootfs.
1661 * CAVEAT: This function must not be used for other purposes than container
1662 * setup before executing the container's init
1664 * @target: path to be opened
1665 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1666 * would be the container's rootfs.
1668 * Return an open fd for the path, or <0 on error.
1670 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1672 int curlen
= 0, dirfd
, fulllen
, i
;
1675 fulllen
= strlen(target
);
1677 /* make sure prefix-skip makes sense */
1678 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1679 curlen
= strlen(prefix_skip
);
1680 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1681 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1682 target
, prefix_skip
);
1687 * get_nextpath() expects the curlen argument to be
1688 * on a (turned into \0) / or before it, so decrement
1689 * curlen to make sure that happens
1698 /* Make a copy of target which we can hack up, and tokenize it */
1699 if ((dup
= strdup(target
)) == NULL
) {
1700 SYSERROR("Out of memory checking for symbolic link");
1704 for (i
= 0; i
< fulllen
; i
++) {
1709 dirfd
= open(prefix_skip
, O_RDONLY
);
1714 int newfd
, saved_errno
;
1717 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1720 newfd
= open_if_safe(dirfd
, nextpath
);
1721 saved_errno
= errno
;
1726 errno
= saved_errno
;
1728 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1740 * Safely mount a path into a container, ensuring that the mount target
1741 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1742 * uses the host's /)
1744 * CAVEAT: This function must not be used for other purposes than container
1745 * setup before executing the container's init
1747 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1748 unsigned long flags
, const void *data
, const char *rootfs
)
1750 int destfd
, ret
, saved_errno
;
1751 /* Only needs enough for /proc/self/fd/<fd>. */
1752 char srcbuf
[50], destbuf
[50];
1754 const char *mntsrc
= src
;
1759 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1760 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1761 INFO("this is a relative bind mount");
1763 srcfd
= open_without_symlink(src
, NULL
);
1767 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1768 if (ret
< 0 || ret
> 50) {
1770 ERROR("Out of memory");
1776 destfd
= open_without_symlink(dest
, rootfs
);
1779 saved_errno
= errno
;
1781 errno
= saved_errno
;
1787 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1788 if (ret
< 0 || ret
> 50) {
1793 ERROR("Out of memory");
1797 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1798 saved_errno
= errno
;
1804 errno
= saved_errno
;
1805 SYSERROR("Failed to mount %s onto %s", src
? src
: "(null)", dest
);
1813 * Mount a proc under @rootfs if proc self points to a pid other than
1814 * my own. This is needed to have a known-good proc mount for setting
1815 * up LSMs both at container startup and attach.
1817 * @rootfs : the rootfs where proc should be mounted
1819 * Returns < 0 on failure, 0 if the correct proc was already mounted
1820 * and 1 if a new proc was mounted.
1822 * NOTE: not to be called from inside the container namespace!
1824 int lxc_mount_proc_if_needed(const char *rootfs
)
1826 char path
[MAXPATHLEN
];
1827 int link_to_pid
, linklen
, mypid
, ret
;
1828 char link
[LXC_NUMSTRLEN64
] = {0};
1830 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1831 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1832 SYSERROR("proc path name too long");
1836 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1838 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1839 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1840 SYSERROR("proc path name too long");
1844 /* /proc not mounted */
1846 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1850 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1851 link
[linklen
- 1] = '\0';
1852 ERROR("readlink returned truncated content: \"%s\"", link
);
1856 mypid
= lxc_raw_getpid();
1857 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1859 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1862 /* correct procfs is already mounted */
1863 if (link_to_pid
== mypid
)
1866 ret
= umount2(path
, MNT_DETACH
);
1868 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1871 /* rootfs is NULL */
1872 if (!strcmp(rootfs
, ""))
1873 ret
= mount("proc", path
, "proc", 0, NULL
);
1875 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1879 INFO("mounted /proc in container for security transition");
1883 int open_devnull(void)
1885 int fd
= open("/dev/null", O_RDWR
);
1888 SYSERROR("Can't open /dev/null");
1893 int set_stdfds(int fd
)
1900 ret
= dup2(fd
, STDIN_FILENO
);
1904 ret
= dup2(fd
, STDOUT_FILENO
);
1908 ret
= dup2(fd
, STDERR_FILENO
);
1915 int null_stdfds(void)
1920 fd
= open_devnull();
1922 ret
= set_stdfds(fd
);
1930 * Return the number of lines in file @fn, or -1 on error
1932 int lxc_count_file_lines(const char *fn
)
1939 f
= fopen_cloexec(fn
, "r");
1943 while (getline(&line
, &sz
, f
) != -1) {
1952 /* Check whether a signal is blocked by a process. */
1953 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1954 #define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1)
1955 bool task_blocks_signal(pid_t pid
, int signal
)
1958 char status
[__PROC_STATUS_LEN
];
1960 uint64_t sigblk
= 0, one
= 1;
1965 ret
= snprintf(status
, __PROC_STATUS_LEN
, "/proc/%d/status", pid
);
1966 if (ret
< 0 || ret
>= __PROC_STATUS_LEN
)
1969 f
= fopen(status
, "r");
1973 while (getline(&line
, &n
, f
) != -1) {
1976 if (strncmp(line
, "SigBlk:", 7))
1979 numstr
= lxc_trim_whitespace_in_place(line
+ 7);
1980 ret
= lxc_safe_uint64(numstr
, &sigblk
, 16);
1987 if (sigblk
& (one
<< (signal
- 1)))
1996 static int lxc_append_null_to_list(void ***list
)
2002 for (; (*list
)[newentry
]; newentry
++) {
2006 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
2011 (*list
)[newentry
+ 1] = NULL
;
2016 int lxc_append_string(char ***list
, char *entry
)
2021 newentry
= lxc_append_null_to_list((void ***)list
);
2025 copy
= strdup(entry
);
2029 (*list
)[newentry
] = copy
;
2034 int lxc_preserve_ns(const int pid
, const char *ns
)
2037 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
2038 #define __NS_PATH_LEN 50
2039 char path
[__NS_PATH_LEN
];
2041 /* This way we can use this function to also check whether namespaces
2042 * are supported by the kernel by passing in the NULL or the empty
2045 ret
= snprintf(path
, __NS_PATH_LEN
, "/proc/%d/ns%s%s", pid
,
2046 !ns
|| strcmp(ns
, "") == 0 ? "" : "/",
2047 !ns
|| strcmp(ns
, "") == 0 ? "" : ns
);
2048 if (ret
< 0 || (size_t)ret
>= __NS_PATH_LEN
) {
2053 return open(path
, O_RDONLY
| O_CLOEXEC
);
2056 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
2059 unsigned long int uli
;
2061 while (isspace(*numstr
))
2068 uli
= strtoul(numstr
, &err
, 0);
2069 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2072 if (err
== numstr
|| *err
!= '\0')
2078 *converted
= (unsigned int)uli
;
2082 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
2085 unsigned long int uli
;
2087 while (isspace(*numstr
))
2094 uli
= strtoul(numstr
, &err
, 0);
2095 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2098 if (err
== numstr
|| *err
!= '\0')
2105 int lxc_safe_uint64(const char *numstr
, uint64_t *converted
, int base
)
2110 while (isspace(*numstr
))
2117 u
= strtoull(numstr
, &err
, base
);
2118 if (errno
== ERANGE
&& u
== ULLONG_MAX
)
2121 if (err
== numstr
|| *err
!= '\0')
2128 int lxc_safe_int(const char *numstr
, int *converted
)
2131 signed long int sli
;
2134 sli
= strtol(numstr
, &err
, 0);
2135 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2138 if (errno
!= 0 && sli
== 0)
2141 if (err
== numstr
|| *err
!= '\0')
2144 if (sli
> INT_MAX
|| sli
< INT_MIN
)
2147 *converted
= (int)sli
;
2151 int lxc_safe_long(const char *numstr
, long int *converted
)
2154 signed long int sli
;
2157 sli
= strtol(numstr
, &err
, 0);
2158 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2161 if (errno
!= 0 && sli
== 0)
2164 if (err
== numstr
|| *err
!= '\0')
2171 int lxc_safe_long_long(const char *numstr
, long long int *converted
)
2174 signed long long int sli
;
2177 sli
= strtoll(numstr
, &err
, 0);
2178 if (errno
== ERANGE
&& (sli
== LLONG_MAX
|| sli
== LLONG_MIN
))
2181 if (errno
!= 0 && sli
== 0)
2184 if (err
== numstr
|| *err
!= '\0')
2191 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2193 if (setgid(gid
) < 0) {
2194 SYSERROR("Failed to switch to gid %d.", gid
);
2197 NOTICE("Switched to gid %d.", gid
);
2199 if (setuid(uid
) < 0) {
2200 SYSERROR("Failed to switch to uid %d.", uid
);
2203 NOTICE("Switched to uid %d.", uid
);
2208 /* Simple covenience function which enables uniform logging. */
2209 int lxc_setgroups(int size
, gid_t list
[])
2211 if (setgroups(size
, list
) < 0) {
2212 SYSERROR("Failed to setgroups().");
2215 NOTICE("Dropped additional groups.");
2220 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2223 struct loop_info64 lo64
;
2225 int dfd
= -1, fd
= -1, ret
= -1;
2227 dir
= opendir("/dev");
2231 while ((dp
= readdir(dir
))) {
2232 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2239 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2243 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2245 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2253 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2254 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2271 static int lxc_get_unused_loop_dev(char *name_loop
)
2274 int fd_ctl
= -1, fd_tmp
= -1;
2276 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2280 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2284 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2285 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2288 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2297 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2300 struct loop_info64 lo64
;
2301 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2303 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2305 if (fd_loop
== -ENODEV
)
2306 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2311 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2315 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2319 memset(&lo64
, 0, sizeof(lo64
));
2320 lo64
.lo_flags
= flags
;
2322 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2332 if (fret
< 0 && fd_loop
>= 0) {
2340 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2346 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2348 /* We consider anything else than EINVAL deadly to prevent going
2349 * into an infinite loop. (The other alternative is constantly
2350 * parsing /proc/self/mountinfo which is yucky and probably
2353 if (errno
!= EINVAL
)
2356 /* Just stop counting when this happens. That'd just be so
2357 * stupid that we won't even bother trying to report back the
2358 * correct value anymore.
2360 if (umounts
!= INT_MAX
)
2363 /* We succeeded in umounting. Make sure that there's no other
2364 * mountpoint stacked underneath.
2372 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2375 int ret
, fret
, pipefd
[2];
2378 /* Make sure our callers do not receive uninitialized memory. */
2379 if (buf_size
> 0 && buf
)
2382 if (pipe(pipefd
) < 0) {
2383 SYSERROR("failed to create pipe");
2387 child
= lxc_raw_clone(0);
2391 SYSERROR("failed to create new process");
2396 /* Close the read-end of the pipe. */
2399 /* Redirect std{err,out} to write-end of the
2402 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2404 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2406 /* Close the write-end of the pipe. */
2410 SYSERROR("failed to duplicate std{err,out} file descriptor");
2411 _exit(EXIT_FAILURE
);
2414 /* Does not return. */
2416 ERROR("failed to exec command");
2417 _exit(EXIT_FAILURE
);
2420 /* close the write-end of the pipe */
2423 if (buf
&& buf_size
> 0) {
2424 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2426 buf
[bytes
- 1] = '\0';
2429 fret
= wait_for_pid(child
);
2430 /* close the read-end of the pipe */
2436 char *must_concat(const char *first
, ...)
2440 size_t cur_len
, it_len
;
2442 dest
= must_copy_string(first
);
2443 cur_len
= it_len
= strlen(first
);
2445 va_start(args
, first
);
2446 while ((cur
= va_arg(args
, char *)) != NULL
) {
2447 it_len
= strlen(cur
);
2449 dest
= must_realloc(dest
, cur_len
+ it_len
+ 1);
2451 (void)memcpy(dest
+ cur_len
, cur
, it_len
);
2460 char *must_make_path(const char *first
, ...)
2464 size_t full_len
= strlen(first
);
2467 dest
= must_copy_string(first
);
2469 va_start(args
, first
);
2470 while ((cur
= va_arg(args
, char *)) != NULL
) {
2471 full_len
+= strlen(cur
);
2475 buf_len
= full_len
+ 1;
2476 dest
= must_realloc(dest
, buf_len
);
2479 (void)strlcat(dest
, "/", buf_len
);
2480 (void)strlcat(dest
, cur
, buf_len
);
2487 char *must_append_path(char *first
, ...)
2495 full_len
= strlen(first
);
2496 va_start(args
, first
);
2497 while ((cur
= va_arg(args
, char *)) != NULL
) {
2498 full_len
+= strlen(cur
);
2502 buf_len
= full_len
+ 1;
2503 dest
= must_realloc(dest
, buf_len
);
2506 (void)strlcat(dest
, "/", buf_len
);
2507 (void)strlcat(dest
, cur
, buf_len
);
2514 char *must_copy_string(const char *entry
)
2522 ret
= strdup(entry
);
2528 void *must_realloc(void *orig
, size_t sz
)
2533 ret
= realloc(orig
, sz
);
2539 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2541 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2544 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2550 ret
= statfs(path
, &sb
);
2554 has_type
= is_fs_type(&sb
, magic_val
);
2555 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2556 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2561 bool lxc_nic_exists(char *nic
)
2563 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2564 char path
[__LXC_SYS_CLASS_NET_LEN
];
2568 if (!strcmp(nic
, "none"))
2571 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2572 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2575 ret
= stat(path
, &sb
);
2582 int lxc_make_tmpfile(char *template, bool rm
)
2588 fd
= mkstemp(template);
2596 ret
= unlink(template);
2605 int parse_byte_size_string(const char *s
, int64_t *converted
)
2607 int ret
, suffix_len
;
2609 int64_t mltpl
, overflow
;
2611 char dup
[LXC_NUMSTRLEN64
+ 2];
2612 char suffix
[3] = {0};
2614 if (!s
|| !strcmp(s
, ""))
2617 end
= stpncpy(dup
, s
, sizeof(dup
) - 1);
2621 if (isdigit(*(end
- 1)))
2623 else if (isalpha(*(end
- 1)))
2628 if (suffix_len
> 0 && (end
- 2) == dup
&& !isdigit(*(end
- 2)))
2631 if (suffix_len
> 0 && isalpha(*(end
- 2)))
2634 if (suffix_len
> 0) {
2635 memcpy(suffix
, end
- suffix_len
, suffix_len
);
2636 *(suffix
+ suffix_len
) = '\0';
2637 *(end
- suffix_len
) = '\0';
2639 dup
[lxc_char_right_gc(dup
, strlen(dup
))] = '\0';
2641 ret
= lxc_safe_long_long(dup
, &conv
);
2645 if (suffix_len
!= 2) {
2650 if (strcasecmp(suffix
, "KB") == 0)
2652 else if (strcasecmp(suffix
, "MB") == 0)
2653 mltpl
= 1024 * 1024;
2654 else if (strcasecmp(suffix
, "GB") == 0)
2655 mltpl
= 1024 * 1024 * 1024;
2659 overflow
= conv
* mltpl
;
2660 if (conv
!= 0 && (overflow
/ conv
) != mltpl
)
2663 *converted
= overflow
;
2667 uint64_t lxc_find_next_power2(uint64_t n
)
2669 /* 0 is not valid input. We return 0 to the caller since 0 is not a
2670 * valid power of two.
2685 int lxc_set_death_signal(int signal
)
2690 ret
= prctl(PR_SET_PDEATHSIG
, signal
, 0, 0, 0);
2692 /* Check whether we have been orphaned. */
2693 ppid
= (pid_t
)syscall(SYS_getppid
);
2697 self
= lxc_raw_getpid();
2698 ret
= kill(self
, SIGKILL
);
2704 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal
);
2711 void remove_trailing_newlines(char *l
)
2718 while (--p
>= l
&& *p
== '\n')
2722 int fd_cloexec(int fd
, bool cloexec
)
2726 oflags
= fcntl(fd
, F_GETFD
, 0);
2731 nflags
= oflags
| FD_CLOEXEC
;
2733 nflags
= oflags
& ~FD_CLOEXEC
;
2735 if (nflags
== oflags
)
2738 if (fcntl(fd
, F_SETFD
, nflags
) < 0)
2744 int recursive_destroy(char *dirname
)
2747 struct dirent
*direntp
;
2751 dir
= opendir(dirname
);
2755 while ((direntp
= readdir(dir
))) {
2759 if (!strcmp(direntp
->d_name
, ".") ||
2760 !strcmp(direntp
->d_name
, ".."))
2763 pathname
= must_make_path(dirname
, direntp
->d_name
, NULL
);
2765 ret
= lstat(pathname
, &mystat
);
2768 WARN("Failed to stat \"%s\"", pathname
);
2774 if (!S_ISDIR(mystat
.st_mode
))
2777 ret
= recursive_destroy(pathname
);
2785 ret
= rmdir(dirname
);
2788 SYSWARN("Failed to delete \"%s\"", dirname
);
2793 ret
= closedir(dir
);
2796 SYSWARN("Failed to delete \"%s\"", dirname
);