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"
57 #define PR_SET_MM_MAP 14
78 #define O_PATH 010000000
82 #define O_NOFOLLOW 00400000
85 lxc_log_define(lxc_utils
, lxc
);
88 * if path is btrfs, tries to remove it and any subvolumes beneath it
90 extern bool btrfs_try_remove_subvol(const char *path
);
92 static int _recursive_rmdir(char *dirname
, dev_t pdev
,
93 const char *exclude
, int level
, bool onedev
)
95 struct dirent
*direntp
;
98 char pathname
[MAXPATHLEN
];
99 bool hadexclude
= false;
101 dir
= opendir(dirname
);
103 ERROR("failed to open %s", dirname
);
107 while ((direntp
= readdir(dir
))) {
114 if (!strcmp(direntp
->d_name
, ".") ||
115 !strcmp(direntp
->d_name
, ".."))
118 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
119 if (rc
< 0 || rc
>= MAXPATHLEN
) {
120 ERROR("pathname too long");
125 if (!level
&& exclude
&& !strcmp(direntp
->d_name
, exclude
)) {
126 ret
= rmdir(pathname
);
130 INFO("Not deleting snapshot %s", pathname
);
134 ret
= unlink(pathname
);
136 INFO("Failed to remove %s", pathname
);
139 SYSERROR("Failed to rmdir %s", pathname
);
147 ret
= lstat(pathname
, &mystat
);
149 ERROR("Failed to stat %s", pathname
);
153 if (onedev
&& mystat
.st_dev
!= pdev
) {
154 /* TODO should we be checking /proc/self/mountinfo for
155 * pathname and not doing this if found? */
156 if (btrfs_try_remove_subvol(pathname
))
157 INFO("Removed btrfs subvolume at %s\n", pathname
);
160 if (S_ISDIR(mystat
.st_mode
)) {
161 if (_recursive_rmdir(pathname
, pdev
, exclude
, level
+1, onedev
) < 0)
164 if (unlink(pathname
) < 0) {
165 SYSERROR("Failed to delete %s", pathname
);
171 if (rmdir(dirname
) < 0 && !btrfs_try_remove_subvol(dirname
) && !hadexclude
) {
172 ERROR("Failed to delete %s", dirname
);
178 ERROR("Failed to close directory %s", dirname
);
182 return failed
? -1 : 0;
185 /* We have two different magic values for overlayfs, yay. */
186 #ifndef OVERLAYFS_SUPER_MAGIC
187 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
190 #ifndef OVERLAY_SUPER_MAGIC
191 #define OVERLAY_SUPER_MAGIC 0x794c7630
194 /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
197 static bool is_native_overlayfs(const char *path
)
199 if (has_fs_type(path
, OVERLAY_SUPER_MAGIC
) ||
200 has_fs_type(path
, OVERLAYFS_SUPER_MAGIC
))
206 /* returns 0 on success, -1 if there were any failures */
207 extern int lxc_rmdir_onedev(char *path
, const char *exclude
)
212 if (is_native_overlayfs(path
)) {
216 if (lstat(path
, &mystat
) < 0) {
219 ERROR("Failed to stat %s", path
);
223 return _recursive_rmdir(path
, mystat
.st_dev
, exclude
, 0, onedev
);
226 /* borrowed from iproute2 */
227 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
236 res
= strtoul(arg
, &ptr
, base
);
237 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
245 extern int mkdir_p(const char *dir
, mode_t mode
)
247 const char *tmp
= dir
;
248 const char *orig
= dir
;
252 dir
= tmp
+ strspn(tmp
, "/");
253 tmp
= dir
+ strcspn(dir
, "/");
254 makeme
= strndup(orig
, dir
- orig
);
256 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
257 SYSERROR("failed to create directory '%s'", makeme
);
273 if (geteuid() == 0) {
274 rundir
= strdup(RUNTIME_PATH
);
278 rundir
= getenv("XDG_RUNTIME_DIR");
280 rundir
= strdup(rundir
);
284 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
285 homedir
= getenv("HOME");
287 ERROR("HOME isn't set in the environment.");
291 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
292 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
297 int wait_for_pid(pid_t pid
)
302 ret
= waitpid(pid
, &status
, 0);
310 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
315 int lxc_wait_for_pid_status(pid_t pid
)
320 ret
= waitpid(pid
, &status
, 0);
331 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
335 ret
= write(fd
, buf
, count
);
336 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
)
351 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
354 ret
= lxc_read_nointr(fd
, buf
, count
);
357 if ((size_t)ret
!= count
)
359 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
367 #include <gnutls/gnutls.h>
368 #include <gnutls/crypto.h>
370 __attribute__((constructor
))
371 static void gnutls_lxc_init(void)
373 gnutls_global_init();
376 int sha1sum_file(char *fnam
, unsigned char *digest
)
385 f
= fopen_cloexec(fnam
, "r");
387 SYSERROR("Error opening template");
390 if (fseek(f
, 0, SEEK_END
) < 0) {
391 SYSERROR("Error seeking to end of template");
395 if ((flen
= ftell(f
)) < 0) {
396 SYSERROR("Error telling size of template");
400 if (fseek(f
, 0, SEEK_SET
) < 0) {
401 SYSERROR("Error seeking to start of template");
405 if ((buf
= malloc(flen
+1)) == NULL
) {
406 SYSERROR("Out of memory");
410 if (fread(buf
, 1, flen
, f
) != flen
) {
411 SYSERROR("Failure reading template");
417 SYSERROR("Failre closing template");
422 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
428 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
431 size_t count
= 1 + skip
;
434 /* first determine size of argument list, we don't want to reallocate
439 char* arg
= va_arg(ap2
, char*);
446 result
= calloc(count
, sizeof(char*));
451 char* arg
= va_arg(ap
, char*);
454 arg
= do_strdup
? strdup(arg
) : arg
;
457 result
[count
++] = arg
;
460 /* calloc has already set last element to NULL*/
468 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
470 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
473 struct lxc_popen_FILE
*lxc_popen(const char *command
)
478 struct lxc_popen_FILE
*fp
= NULL
;
480 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
493 /* duplicate stdout */
494 if (pipe_fds
[1] != STDOUT_FILENO
)
495 ret
= dup2(pipe_fds
[1], STDOUT_FILENO
);
497 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
503 /* duplicate stderr */
504 if (pipe_fds
[1] != STDERR_FILENO
)
505 ret
= dup2(pipe_fds
[1], STDERR_FILENO
);
507 ret
= fcntl(pipe_fds
[1], F_SETFD
, 0);
512 /* unblock all signals */
513 ret
= sigfillset(&mask
);
517 ret
= sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
521 execl("/bin/sh", "sh", "-c", command
, (char *)NULL
);
528 fp
= malloc(sizeof(*fp
));
532 fp
->child_pid
= child_pid
;
533 fp
->pipe
= pipe_fds
[0];
535 fp
->f
= fdopen(pipe_fds
[0], "r");
545 if (pipe_fds
[0] >= 0)
548 if (pipe_fds
[1] >= 0)
554 int lxc_pclose(struct lxc_popen_FILE
*fp
)
563 wait_pid
= waitpid(fp
->child_pid
, &wstatus
, 0);
564 } while (wait_pid
< 0 && errno
== EINTR
);
576 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
578 ssize_t len
= -1, saved_len
= -1;
580 size_t replacement_len
= strlen(replacement
);
581 size_t needle_len
= strlen(needle
);
583 /* should be executed exactly twice */
584 while (len
== -1 || result
== NULL
) {
590 result
= calloc(1, len
+ 1);
598 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
599 part_len
= (ssize_t
)(p
- last_p
);
600 if (result
&& part_len
> 0)
601 memcpy(&result
[len
], last_p
, part_len
);
603 if (result
&& replacement_len
> 0)
604 memcpy(&result
[len
], replacement
, replacement_len
);
605 len
+= replacement_len
;
608 part_len
= strlen(last_p
);
609 if (result
&& part_len
> 0)
610 memcpy(&result
[len
], last_p
, part_len
);
614 /* make sure we did the same thing twice,
615 * once for calculating length, the other
616 * time for copying data */
617 if (saved_len
!= len
) {
621 /* make sure we didn't overwrite any buffer,
622 * due to calloc the string should be 0-terminated */
623 if (result
[len
] != '\0') {
631 bool lxc_string_in_array(const char *needle
, const char **haystack
)
633 for (; haystack
&& *haystack
; haystack
++)
634 if (!strcmp(needle
, *haystack
))
639 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
643 size_t sep_len
= strlen(sep
);
644 size_t result_len
= use_as_prefix
* sep_len
;
646 /* calculate new string length */
647 for (p
= (char **)parts
; *p
; p
++)
648 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
650 result
= calloc(result_len
+ 1, 1);
656 for (p
= (char **)parts
; *p
; p
++) {
657 if (p
> (char **)parts
)
665 char **lxc_normalize_path(const char *path
)
669 size_t components_len
= 0;
672 components
= lxc_string_split(path
, '/');
675 for (p
= components
; *p
; p
++)
678 /* resolve '.' and '..' */
679 for (pos
= 0; pos
< components_len
; ) {
680 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
681 /* eat this element */
682 free(components
[pos
]);
683 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
685 } else if (!strcmp(components
[pos
], "..")) {
686 /* eat this and the previous element */
687 free(components
[pos
- 1]);
688 free(components
[pos
]);
689 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
700 char *lxc_deslashify(const char *path
)
710 parts
= lxc_normalize_path(dup
);
716 /* We'll end up here if path == "///" or path == "". */
720 lxc_free_array((void **)parts
, free
);
723 n
= strcspn(dup
, "/");
726 lxc_free_array((void **)parts
, free
);
736 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
738 lxc_free_array((void **)parts
, free
);
742 char *lxc_append_paths(const char *first
, const char *second
)
744 size_t len
= strlen(first
) + strlen(second
) + 1;
745 const char *pattern
= "%s%s";
748 if (second
[0] != '/') {
753 result
= calloc(1, len
);
757 snprintf(result
, len
, pattern
, first
, second
);
761 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
763 char *token
, *str
, *saveptr
= NULL
;
764 char sep
[2] = { _sep
, '\0' };
766 if (!haystack
|| !needle
)
769 str
= alloca(strlen(haystack
)+1);
770 strcpy(str
, haystack
);
771 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
772 if (strcmp(needle
, token
) == 0)
779 char **lxc_string_split(const char *string
, char _sep
)
781 char *token
, *str
, *saveptr
= NULL
;
782 char sep
[2] = {_sep
, '\0'};
783 char **tmp
= NULL
, **result
= NULL
;
784 size_t result_capacity
= 0;
785 size_t result_count
= 0;
789 return calloc(1, sizeof(char *));
791 str
= alloca(strlen(string
) + 1);
793 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
794 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
797 result
[result_count
] = strdup(token
);
798 if (!result
[result_count
])
803 /* if we allocated too much, reduce it */
804 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
808 /* Make sure we don't return uninitialized memory. */
809 if (result_count
== 0)
814 lxc_free_array((void **)result
, free
);
819 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
, size_t *cnt
)
823 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
826 (*result
)[*cnt
] = strndup(start
, end
- start
);
827 if (!(*result
)[*cnt
])
835 * Given a a string 'one two "three four"', split into three words,
836 * one, two, and "three four"
838 char **lxc_string_split_quoted(char *string
)
840 char *nextword
= string
, *p
, state
;
841 char **result
= NULL
;
842 size_t result_capacity
= 0;
843 size_t result_count
= 0;
845 if (!string
|| !*string
)
846 return calloc(1, sizeof(char *));
848 // TODO I'm *not* handling escaped quote
850 for (p
= string
; *p
; p
++) {
855 else if (*p
== '"' || *p
== '\'') {
865 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
873 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
882 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
884 return realloc(result
, (result_count
+ 1) * sizeof(char *));
887 char **lxc_string_split_and_trim(const char *string
, char _sep
)
889 char *token
, *str
, *saveptr
= NULL
;
890 char sep
[2] = { _sep
, '\0' };
891 char **result
= NULL
;
892 size_t result_capacity
= 0;
893 size_t result_count
= 0;
898 return calloc(1, sizeof(char *));
900 str
= alloca(strlen(string
)+1);
902 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
903 while (token
[0] == ' ' || token
[0] == '\t')
906 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
910 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
913 result
[result_count
] = strdup(token
);
914 if (!result
[result_count
])
919 /* if we allocated too much, reduce it */
920 return realloc(result
, (result_count
+ 1) * sizeof(char *));
923 lxc_free_array((void **)result
, free
);
928 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
931 for (p
= array
; p
&& *p
; p
++)
936 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
941 /* first time around, catch some trivial mistakes of the user
942 * only initializing one of these */
943 if (!*array
|| !*capacity
) {
948 new_capacity
= *capacity
;
949 while (new_size
+ 1 > new_capacity
)
950 new_capacity
+= capacity_increment
;
951 if (new_capacity
!= *capacity
) {
952 /* we have to reallocate */
953 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
956 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
958 *capacity
= new_capacity
;
961 /* array has sufficient elements */
965 size_t lxc_array_len(void **array
)
970 for (p
= array
; p
&& *p
; p
++)
976 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
981 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
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 * Sets the process title to the specified title. Note that this may fail if
1443 * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18).
1445 int setproctitle(char *title
)
1447 static char *proctitle
= NULL
;
1448 char buf
[2048], *tmp
;
1450 int i
, len
, ret
= 0;
1452 /* We don't really need to know all of this stuff, but unfortunately
1453 * PR_SET_MM_MAP requires us to set it all at once, so we have to
1454 * figure it out anyway.
1456 unsigned long start_data
, end_data
, start_brk
, start_code
, end_code
,
1457 start_stack
, arg_start
, arg_end
, env_start
, env_end
,
1459 struct prctl_mm_map prctl_map
;
1461 f
= fopen_cloexec("/proc/self/stat", "r");
1466 tmp
= fgets(buf
, sizeof(buf
), f
);
1472 /* Skip the first 25 fields, column 26-28 are start_code, end_code,
1473 * and start_stack */
1474 tmp
= strchr(buf
, ' ');
1475 for (i
= 0; i
< 24; i
++) {
1478 tmp
= strchr(tmp
+1, ' ');
1483 i
= sscanf(tmp
, "%lu %lu %lu", &start_code
, &end_code
, &start_stack
);
1487 /* Skip the next 19 fields, column 45-51 are start_data to arg_end */
1488 for (i
= 0; i
< 19; i
++) {
1491 tmp
= strchr(tmp
+1, ' ');
1497 i
= sscanf(tmp
, "%lu %lu %lu %*u %*u %lu %lu",
1506 /* Include the null byte here, because in the calculations below we
1507 * want to have room for it. */
1508 len
= strlen(title
) + 1;
1510 proctitle
= realloc(proctitle
, len
);
1514 arg_start
= (unsigned long) proctitle
;
1515 arg_end
= arg_start
+ len
;
1517 brk_val
= syscall(__NR_brk
, 0);
1519 prctl_map
= (struct prctl_mm_map
) {
1520 .start_code
= start_code
,
1521 .end_code
= end_code
,
1522 .start_stack
= start_stack
,
1523 .start_data
= start_data
,
1524 .end_data
= end_data
,
1525 .start_brk
= start_brk
,
1527 .arg_start
= arg_start
,
1529 .env_start
= env_start
,
1536 ret
= prctl(PR_SET_MM
, PR_SET_MM_MAP
, (long) &prctl_map
, sizeof(prctl_map
), 0);
1538 strcpy((char*)arg_start
, title
);
1540 INFO("setting cmdline failed - %s", strerror(errno
));
1546 * @path: a pathname where / replaced with '\0'.
1547 * @offsetp: pointer to int showing which path segment was last seen.
1548 * Updated on return to reflect the next segment.
1549 * @fulllen: full original path length.
1550 * Returns a pointer to the next path segment, or NULL if done.
1552 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1554 int offset
= *offsetp
;
1556 if (offset
>= fulllen
)
1559 while (path
[offset
] != '\0' && offset
< fulllen
)
1561 while (path
[offset
] == '\0' && offset
< fulllen
)
1565 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1569 * Check that @subdir is a subdir of @dir. @len is the length of
1570 * @dir (to avoid having to recalculate it).
1572 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1574 size_t subdirlen
= strlen(subdir
);
1576 if (subdirlen
< len
)
1578 if (strncmp(subdir
, dir
, len
) != 0)
1580 if (dir
[len
-1] == '/')
1582 if (subdir
[len
] == '/' || subdirlen
== len
)
1588 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1589 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1591 static int check_symlink(int fd
)
1594 int ret
= fstat(fd
, &sb
);
1597 if (S_ISLNK(sb
.st_mode
))
1603 * Open a file or directory, provided that it contains no symlinks.
1605 * CAVEAT: This function must not be used for other purposes than container
1606 * setup before executing the container's init
1608 static int open_if_safe(int dirfd
, const char *nextpath
)
1610 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1611 if (newfd
>= 0) /* Was not a symlink, all good. */
1617 if (errno
== EPERM
|| errno
== EACCES
) {
1618 /* We're not root (cause we got EPERM) so try opening with
1621 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1623 /* O_PATH will return an fd for symlinks. We know
1624 * nextpath wasn't a symlink at last openat, so if fd is
1625 * now a link, then something * fishy is going on.
1627 int ret
= check_symlink(newfd
);
1639 * Open a path intending for mounting, ensuring that the final path
1640 * is inside the container's rootfs.
1642 * CAVEAT: This function must not be used for other purposes than container
1643 * setup before executing the container's init
1645 * @target: path to be opened
1646 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1647 * would be the container's rootfs.
1649 * Return an open fd for the path, or <0 on error.
1651 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1653 int curlen
= 0, dirfd
, fulllen
, i
;
1656 fulllen
= strlen(target
);
1658 /* make sure prefix-skip makes sense */
1659 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1660 curlen
= strlen(prefix_skip
);
1661 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1662 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1663 target
, prefix_skip
);
1667 * get_nextpath() expects the curlen argument to be
1668 * on a (turned into \0) / or before it, so decrement
1669 * curlen to make sure that happens
1678 /* Make a copy of target which we can hack up, and tokenize it */
1679 if ((dup
= strdup(target
)) == NULL
) {
1680 SYSERROR("Out of memory checking for symbolic link");
1683 for (i
= 0; i
< fulllen
; i
++) {
1688 dirfd
= open(prefix_skip
, O_RDONLY
);
1692 int newfd
, saved_errno
;
1695 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1697 newfd
= open_if_safe(dirfd
, nextpath
);
1698 saved_errno
= errno
;
1702 errno
= saved_errno
;
1704 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1715 * Safely mount a path into a container, ensuring that the mount target
1716 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1717 * uses the host's /)
1719 * CAVEAT: This function must not be used for other purposes than container
1720 * setup before executing the container's init
1722 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1723 unsigned long flags
, const void *data
, const char *rootfs
)
1725 int destfd
, ret
, saved_errno
;
1726 /* Only needs enough for /proc/self/fd/<fd>. */
1727 char srcbuf
[50], destbuf
[50];
1729 const char *mntsrc
= src
;
1734 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1735 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1736 INFO("this is a relative bind mount");
1737 srcfd
= open_without_symlink(src
, NULL
);
1740 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1741 if (ret
< 0 || ret
> 50) {
1743 ERROR("Out of memory");
1749 destfd
= open_without_symlink(dest
, rootfs
);
1752 saved_errno
= errno
;
1754 errno
= saved_errno
;
1759 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1760 if (ret
< 0 || ret
> 50) {
1764 ERROR("Out of memory");
1768 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1769 saved_errno
= errno
;
1774 errno
= saved_errno
;
1775 SYSERROR("Failed to mount %s onto %s", src
, dest
);
1783 * Mount a proc under @rootfs if proc self points to a pid other than
1784 * my own. This is needed to have a known-good proc mount for setting
1785 * up LSMs both at container startup and attach.
1787 * @rootfs : the rootfs where proc should be mounted
1789 * Returns < 0 on failure, 0 if the correct proc was already mounted
1790 * and 1 if a new proc was mounted.
1792 * NOTE: not to be called from inside the container namespace!
1794 int lxc_mount_proc_if_needed(const char *rootfs
)
1796 char path
[MAXPATHLEN
];
1797 int link_to_pid
, linklen
, mypid
, ret
;
1798 char link
[LXC_NUMSTRLEN64
] = {0};
1800 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1801 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1802 SYSERROR("proc path name too long");
1806 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1808 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1809 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1810 SYSERROR("proc path name too long");
1814 /* /proc not mounted */
1816 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1819 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1820 link
[linklen
- 1] = '\0';
1821 ERROR("readlink returned truncated content: \"%s\"", link
);
1826 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1828 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1831 /* correct procfs is already mounted */
1832 if (link_to_pid
== mypid
)
1835 ret
= umount2(path
, MNT_DETACH
);
1837 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1840 /* rootfs is NULL */
1841 if (!strcmp(rootfs
, ""))
1842 ret
= mount("proc", path
, "proc", 0, NULL
);
1844 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1848 INFO("mounted /proc in container for security transition");
1852 int open_devnull(void)
1854 int fd
= open("/dev/null", O_RDWR
);
1857 SYSERROR("Can't open /dev/null");
1862 int set_stdfds(int fd
)
1869 ret
= dup2(fd
, STDIN_FILENO
);
1873 ret
= dup2(fd
, STDOUT_FILENO
);
1877 ret
= dup2(fd
, STDERR_FILENO
);
1884 int null_stdfds(void)
1887 int fd
= open_devnull();
1890 ret
= set_stdfds(fd
);
1898 * Return the number of lines in file @fn, or -1 on error
1900 int lxc_count_file_lines(const char *fn
)
1907 f
= fopen_cloexec(fn
, "r");
1911 while (getline(&line
, &sz
, f
) != -1) {
1919 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
1922 void *tmp
= NULL
, *overlap
= NULL
;
1924 /* We establish an anonymous mapping that is one byte larger than the
1925 * underlying file. The pages handed to us are zero filled. */
1926 tmp
= mmap(addr
, length
+ 1, PROT_READ
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1927 if (tmp
== MAP_FAILED
)
1930 /* Now we establish a fixed-address mapping starting at the address we
1931 * received from our anonymous mapping and replace all bytes excluding
1932 * the additional \0-byte with the file. This allows us to use normal
1933 * string-handling functions. */
1934 overlap
= mmap(tmp
, length
, prot
, MAP_FIXED
| flags
, fd
, offset
);
1935 if (overlap
== MAP_FAILED
)
1936 munmap(tmp
, length
+ 1);
1941 int lxc_strmunmap(void *addr
, size_t length
)
1943 return munmap(addr
, length
+ 1);
1946 /* Check whether a signal is blocked by a process. */
1947 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1948 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
1949 bool task_blocking_signal(pid_t pid
, int signal
)
1953 long unsigned int sigblk
= 0;
1958 char status
[__PROC_STATUS_LEN
];
1960 ret
= snprintf(status
, __PROC_STATUS_LEN
, "/proc/%d/status", pid
);
1961 if (ret
< 0 || ret
>= __PROC_STATUS_LEN
)
1964 f
= fopen(status
, "r");
1968 while (getline(&line
, &n
, f
) != -1) {
1969 if (strncmp(line
, "SigBlk:\t", 8))
1972 if (sscanf(line
+ 8, "%lx", &sigblk
) != 1)
1976 if (sigblk
& (1LU << (signal
- 1)))
1985 static int lxc_append_null_to_list(void ***list
)
1991 for (; (*list
)[newentry
]; newentry
++) {
1995 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
2000 (*list
)[newentry
+ 1] = NULL
;
2005 int lxc_append_string(char ***list
, char *entry
)
2010 newentry
= lxc_append_null_to_list((void ***)list
);
2014 copy
= strdup(entry
);
2018 (*list
)[newentry
] = copy
;
2023 int lxc_preserve_ns(const int pid
, const char *ns
)
2026 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
2027 #define __NS_PATH_LEN 50
2028 char path
[__NS_PATH_LEN
];
2030 /* This way we can use this function to also check whether namespaces
2031 * are supported by the kernel by passing in the NULL or the empty
2034 ret
= snprintf(path
, __NS_PATH_LEN
, "/proc/%d/ns%s%s", pid
,
2035 !ns
|| strcmp(ns
, "") == 0 ? "" : "/",
2036 !ns
|| strcmp(ns
, "") == 0 ? "" : ns
);
2037 if (ret
< 0 || (size_t)ret
>= __NS_PATH_LEN
)
2040 return open(path
, O_RDONLY
| O_CLOEXEC
);
2043 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
2046 unsigned long int uli
;
2048 while (isspace(*numstr
))
2055 uli
= strtoul(numstr
, &err
, 0);
2056 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2059 if (err
== numstr
|| *err
!= '\0')
2065 *converted
= (unsigned int)uli
;
2069 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
2072 unsigned long int uli
;
2074 while (isspace(*numstr
))
2081 uli
= strtoul(numstr
, &err
, 0);
2082 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2085 if (err
== numstr
|| *err
!= '\0')
2092 int lxc_safe_int(const char *numstr
, int *converted
)
2095 signed long int sli
;
2098 sli
= strtol(numstr
, &err
, 0);
2099 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2102 if (errno
!= 0 && sli
== 0)
2105 if (err
== numstr
|| *err
!= '\0')
2108 if (sli
> INT_MAX
|| sli
< INT_MIN
)
2111 *converted
= (int)sli
;
2115 int lxc_safe_long(const char *numstr
, long int *converted
)
2118 signed long int sli
;
2121 sli
= strtol(numstr
, &err
, 0);
2122 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2125 if (errno
!= 0 && sli
== 0)
2128 if (err
== numstr
|| *err
!= '\0')
2135 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2137 if (setgid(gid
) < 0) {
2138 SYSERROR("Failed to switch to gid %d.", gid
);
2141 NOTICE("Switched to gid %d.", gid
);
2143 if (setuid(uid
) < 0) {
2144 SYSERROR("Failed to switch to uid %d.", uid
);
2147 NOTICE("Switched to uid %d.", uid
);
2152 /* Simple covenience function which enables uniform logging. */
2153 int lxc_setgroups(int size
, gid_t list
[])
2155 if (setgroups(size
, list
) < 0) {
2156 SYSERROR("Failed to setgroups().");
2159 NOTICE("Dropped additional groups.");
2164 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2167 struct loop_info64 lo64
;
2169 int dfd
= -1, fd
= -1, ret
= -1;
2171 dir
= opendir("/dev");
2175 while ((dp
= readdir(dir
))) {
2179 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2186 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2190 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2192 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2200 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2201 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2218 static int lxc_get_unused_loop_dev(char *name_loop
)
2221 int fd_ctl
= -1, fd_tmp
= -1;
2223 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2227 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2231 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2232 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2235 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2244 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2247 struct loop_info64 lo64
;
2248 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2250 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2252 if (fd_loop
== -ENODEV
)
2253 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2258 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2262 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2266 memset(&lo64
, 0, sizeof(lo64
));
2267 lo64
.lo_flags
= flags
;
2269 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2279 if (fret
< 0 && fd_loop
>= 0) {
2287 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2293 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2295 /* We consider anything else than EINVAL deadly to prevent going
2296 * into an infinite loop. (The other alternative is constantly
2297 * parsing /proc/self/mountinfo which is yucky and probably
2300 if (errno
!= EINVAL
)
2303 /* Just stop counting when this happens. That'd just be so
2304 * stupid that we won't even bother trying to report back the
2305 * correct value anymore.
2307 if (umounts
!= INT_MAX
)
2309 /* We succeeded in umounting. Make sure that there's no other
2310 * mountpoint stacked underneath.
2318 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2321 int ret
, fret
, pipefd
[2];
2324 /* Make sure our callers do not receive unitialized memory. */
2325 if (buf_size
> 0 && buf
)
2328 if (pipe(pipefd
) < 0) {
2329 SYSERROR("failed to create pipe");
2337 SYSERROR("failed to create new process");
2342 /* Close the read-end of the pipe. */
2345 /* Redirect std{err,out} to write-end of the
2348 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2350 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2352 /* Close the write-end of the pipe. */
2356 SYSERROR("failed to duplicate std{err,out} file descriptor");
2360 /* Does not return. */
2362 ERROR("failed to exec command");
2366 /* close the write-end of the pipe */
2369 if (buf
&& buf_size
> 0) {
2370 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2372 buf
[bytes
- 1] = '\0';
2375 fret
= wait_for_pid(child
);
2376 /* close the read-end of the pipe */
2382 char *must_make_path(const char *first
, ...)
2386 size_t full_len
= strlen(first
);
2388 dest
= must_copy_string(first
);
2390 va_start(args
, first
);
2391 while ((cur
= va_arg(args
, char *)) != NULL
) {
2392 full_len
+= strlen(cur
);
2395 dest
= must_realloc(dest
, full_len
+ 1);
2405 char *must_copy_string(const char *entry
)
2412 ret
= strdup(entry
);
2418 void *must_realloc(void *orig
, size_t sz
)
2423 ret
= realloc(orig
, sz
);
2429 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2431 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2434 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2440 ret
= statfs(path
, &sb
);
2444 has_type
= is_fs_type(&sb
, magic_val
);
2445 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2446 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2451 bool lxc_nic_exists(char *nic
)
2453 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2454 char path
[__LXC_SYS_CLASS_NET_LEN
];
2458 if (!strcmp(nic
, "none"))
2461 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2462 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2465 ret
= stat(path
, &sb
);