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 extern struct lxc_popen_FILE
*lxc_popen(const char *command
)
475 struct lxc_popen_FILE
*fp
= NULL
;
476 int parent_end
= -1, child_end
= -1;
480 int r
= pipe2(pipe_fds
, O_CLOEXEC
);
483 ERROR("pipe2 failure");
487 parent_end
= pipe_fds
[0];
488 child_end
= pipe_fds
[1];
492 if (child_pid
== 0) {
494 int child_std_end
= STDOUT_FILENO
;
499 if (child_end
!= child_std_end
) {
500 /* dup2() doesn't dup close-on-exec flag */
501 dup2(child_end
, child_std_end
);
503 /* it's safe not to close child_end here
504 * as it's marked close-on-exec anyway
508 * The descriptor is already the one we will use.
509 * But it must not be marked close-on-exec.
512 if (fcntl(child_end
, F_SETFD
, 0) != 0) {
513 SYSERROR("Failed to remove FD_CLOEXEC from fd.");
520 * This is the main/only reason
521 * why we do our lousy popen() emulation.
526 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
529 execl("/bin/sh", "sh", "-c", command
, (char *) NULL
);
539 ERROR("fork failure");
543 fp
= calloc(1, sizeof(*fp
));
545 ERROR("failed to allocate memory");
549 fp
->f
= fdopen(parent_end
, "r");
551 ERROR("fdopen failure");
555 fp
->child_pid
= child_pid
;
564 parent_end
= -1; /* so we do not close it second time */
570 if (parent_end
!= -1)
576 extern int lxc_pclose(struct lxc_popen_FILE
*fp
)
585 child_pid
= fp
->child_pid
;
586 /* free memory (we still need to close file stream) */
591 if (!f
|| fclose(f
)) {
592 ERROR("fclose failure");
597 wait_pid
= waitpid(child_pid
, &wstatus
, 0);
598 } while (wait_pid
== -1 && errno
== EINTR
);
600 if (wait_pid
== -1) {
601 ERROR("waitpid failure");
608 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
610 ssize_t len
= -1, saved_len
= -1;
612 size_t replacement_len
= strlen(replacement
);
613 size_t needle_len
= strlen(needle
);
615 /* should be executed exactly twice */
616 while (len
== -1 || result
== NULL
) {
622 result
= calloc(1, len
+ 1);
630 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
631 part_len
= (ssize_t
)(p
- last_p
);
632 if (result
&& part_len
> 0)
633 memcpy(&result
[len
], last_p
, part_len
);
635 if (result
&& replacement_len
> 0)
636 memcpy(&result
[len
], replacement
, replacement_len
);
637 len
+= replacement_len
;
640 part_len
= strlen(last_p
);
641 if (result
&& part_len
> 0)
642 memcpy(&result
[len
], last_p
, part_len
);
646 /* make sure we did the same thing twice,
647 * once for calculating length, the other
648 * time for copying data */
649 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
))
671 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
675 size_t sep_len
= strlen(sep
);
676 size_t result_len
= use_as_prefix
* sep_len
;
678 /* calculate new string length */
679 for (p
= (char **)parts
; *p
; p
++)
680 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
682 result
= calloc(result_len
+ 1, 1);
688 for (p
= (char **)parts
; *p
; p
++) {
689 if (p
> (char **)parts
)
697 char **lxc_normalize_path(const char *path
)
701 size_t components_len
= 0;
704 components
= lxc_string_split(path
, '/');
707 for (p
= components
; *p
; p
++)
710 /* resolve '.' and '..' */
711 for (pos
= 0; pos
< components_len
; ) {
712 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
713 /* eat this element */
714 free(components
[pos
]);
715 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
717 } else if (!strcmp(components
[pos
], "..")) {
718 /* eat this and the previous element */
719 free(components
[pos
- 1]);
720 free(components
[pos
]);
721 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
732 char *lxc_deslashify(const char *path
)
742 parts
= lxc_normalize_path(dup
);
748 /* We'll end up here if path == "///" or path == "". */
752 lxc_free_array((void **)parts
, free
);
755 n
= strcspn(dup
, "/");
758 lxc_free_array((void **)parts
, free
);
768 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
770 lxc_free_array((void **)parts
, free
);
774 char *lxc_append_paths(const char *first
, const char *second
)
776 size_t len
= strlen(first
) + strlen(second
) + 1;
777 const char *pattern
= "%s%s";
780 if (second
[0] != '/') {
785 result
= calloc(1, len
);
789 snprintf(result
, len
, pattern
, first
, second
);
793 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
795 char *token
, *str
, *saveptr
= NULL
;
796 char sep
[2] = { _sep
, '\0' };
798 if (!haystack
|| !needle
)
801 str
= alloca(strlen(haystack
)+1);
802 strcpy(str
, haystack
);
803 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
804 if (strcmp(needle
, token
) == 0)
811 char **lxc_string_split(const char *string
, char _sep
)
813 char *token
, *str
, *saveptr
= NULL
;
814 char sep
[2] = {_sep
, '\0'};
815 char **tmp
= NULL
, **result
= NULL
;
816 size_t result_capacity
= 0;
817 size_t result_count
= 0;
821 return calloc(1, sizeof(char *));
823 str
= alloca(strlen(string
) + 1);
825 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
826 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
829 result
[result_count
] = strdup(token
);
830 if (!result
[result_count
])
835 /* if we allocated too much, reduce it */
836 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
840 /* Make sure we don't return uninitialized memory. */
841 if (result_count
== 0)
846 lxc_free_array((void **)result
, free
);
851 char **lxc_string_split_and_trim(const char *string
, char _sep
)
853 char *token
, *str
, *saveptr
= NULL
;
854 char sep
[2] = { _sep
, '\0' };
855 char **result
= NULL
;
856 size_t result_capacity
= 0;
857 size_t result_count
= 0;
862 return calloc(1, sizeof(char *));
864 str
= alloca(strlen(string
)+1);
866 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
867 while (token
[0] == ' ' || token
[0] == '\t')
870 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
874 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
877 result
[result_count
] = strdup(token
);
878 if (!result
[result_count
])
883 /* if we allocated too much, reduce it */
884 return realloc(result
, (result_count
+ 1) * sizeof(char *));
887 lxc_free_array((void **)result
, free
);
892 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
895 for (p
= array
; p
&& *p
; p
++)
900 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
905 /* first time around, catch some trivial mistakes of the user
906 * only initializing one of these */
907 if (!*array
|| !*capacity
) {
912 new_capacity
= *capacity
;
913 while (new_size
+ 1 > new_capacity
)
914 new_capacity
+= capacity_increment
;
915 if (new_capacity
!= *capacity
) {
916 /* we have to reallocate */
917 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
920 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
922 *capacity
= new_capacity
;
925 /* array has sufficient elements */
929 size_t lxc_array_len(void **array
)
934 for (p
= array
; p
&& *p
; p
++)
940 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
945 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
948 ret
= lxc_write_nointr(fd
, buf
, count
);
951 if ((size_t)ret
!= count
)
954 ret
= lxc_write_nointr(fd
, "\n", 1);
968 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
970 int fd
= -1, saved_errno
;
973 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
977 if (!buf
|| !count
) {
980 while ((ret
= read(fd
, buf2
, 100)) > 0)
985 memset(buf
, 0, count
);
986 ret
= read(fd
, buf
, count
);
990 ERROR("read %s: %s", filename
, strerror(errno
));
998 void **lxc_append_null_to_array(void **array
, size_t count
)
1002 /* Append NULL to the array */
1004 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1007 for (i
= 0; i
< count
; i
++)
1013 array
[count
] = NULL
;
1018 int randseed(bool srand_it
)
1021 srand pre-seed function based on /dev/urandom
1023 unsigned int seed
= time(NULL
) + getpid();
1026 f
= fopen("/dev/urandom", "r");
1028 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1030 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1040 uid_t
get_ns_uid(uid_t orig
)
1044 uid_t nsid
, hostid
, range
;
1045 FILE *f
= fopen("/proc/self/uid_map", "r");
1049 while (getline(&line
, &sz
, f
) != -1) {
1050 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1052 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1053 nsid
+= orig
- hostid
;
1065 bool dir_exists(const char *path
)
1070 ret
= stat(path
, &sb
);
1072 /* Could be something other than eexist, just say "no". */
1074 return S_ISDIR(sb
.st_mode
);
1077 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1078 * FNV has good anti collision properties and we're not worried
1079 * about pre-image resistance or one-way-ness, we're just trying to make
1080 * the name unique in the 108 bytes of space we have.
1082 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1086 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1088 /* xor the bottom with the current octet */
1089 hval
^= (uint64_t)*bp
;
1092 * multiply by the 64 bit FNV magic prime mod 2^64
1094 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1095 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1102 * Detect whether / is mounted MS_SHARED. The only way I know of to
1103 * check that is through /proc/self/mountinfo.
1104 * I'm only checking for /. If the container rootfs or mount location
1105 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1106 * out would be too much work to be worth it.
1108 int detect_shared_rootfs(void)
1110 char buf
[LXC_LINELEN
], *p
;
1115 f
= fopen("/proc/self/mountinfo", "r");
1118 while (fgets(buf
, LXC_LINELEN
, f
)) {
1119 for (p
= buf
, i
= 0; p
&& i
< 4; i
++)
1120 p
= strchr(p
+ 1, ' ');
1123 p2
= strchr(p
+ 1, ' ');
1127 if (strcmp(p
+ 1, "/") == 0) {
1128 /* This is '/'. Is it shared? */
1129 p
= strchr(p2
+ 1, ' ');
1130 if (p
&& strstr(p
, "shared:")) {
1140 bool switch_to_ns(pid_t pid
, const char *ns
) {
1142 char nspath
[MAXPATHLEN
];
1144 /* Switch to new ns */
1145 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1146 if (ret
< 0 || ret
>= MAXPATHLEN
)
1149 fd
= open(nspath
, O_RDONLY
);
1151 SYSERROR("failed to open %s", nspath
);
1157 SYSERROR("failed to set process %d to %s of %d.", pid
, ns
, fd
);
1166 * looking at fs/proc_namespace.c, it appears we can
1167 * actually expect the rootfs entry to very specifically contain
1168 * " - rootfs rootfs "
1169 * IIUC, so long as we've chrooted so that rootfs is not our root,
1170 * the rootfs entry should always be skipped in mountinfo contents.
1172 bool detect_ramfs_rootfs(void)
1180 f
= fopen("/proc/self/mountinfo", "r");
1184 while (getline(&line
, &len
, f
) != -1) {
1185 for (p
= line
, i
= 0; p
&& i
< 4; i
++)
1186 p
= strchr(p
+ 1, ' ');
1189 p2
= strchr(p
+ 1, ' ');
1193 if (strcmp(p
+ 1, "/") == 0) {
1194 /* This is '/'. Is it the ramfs? */
1195 p
= strchr(p2
+ 1, '-');
1196 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1208 char *on_path(const char *cmd
, const char *rootfs
) {
1211 char *saveptr
= NULL
;
1212 char cmdpath
[MAXPATHLEN
];
1215 path
= getenv("PATH");
1219 path
= strdup(path
);
1223 entry
= strtok_r(path
, ":", &saveptr
);
1226 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1228 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1230 if (ret
< 0 || ret
>= MAXPATHLEN
)
1233 if (access(cmdpath
, X_OK
) == 0) {
1235 return strdup(cmdpath
);
1239 entry
= strtok_r(NULL
, ":", &saveptr
);
1246 bool file_exists(const char *f
)
1248 struct stat statbuf
;
1250 return stat(f
, &statbuf
) == 0;
1253 bool cgns_supported(void)
1255 return file_exists("/proc/self/ns/cgroup");
1258 /* historically lxc-init has been under /usr/lib/lxc and under
1259 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1261 char *choose_init(const char *rootfs
)
1264 const char *empty
= "",
1266 int ret
, env_set
= 0;
1268 if (!getenv("PATH")) {
1269 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1270 SYSERROR("Failed to setenv");
1274 retv
= on_path("init.lxc", rootfs
);
1277 if (unsetenv("PATH"))
1278 SYSERROR("Failed to unsetenv");
1284 retv
= malloc(PATH_MAX
);
1293 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1294 if (ret
< 0 || ret
>= PATH_MAX
) {
1295 ERROR("pathname too long");
1298 if (access(retv
, X_OK
) == 0)
1301 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1302 if (ret
< 0 || ret
>= PATH_MAX
) {
1303 ERROR("pathname too long");
1306 if (access(retv
, X_OK
) == 0)
1309 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1310 if (ret
< 0 || ret
>= PATH_MAX
) {
1311 ERROR("pathname too long");
1314 if (access(retv
, X_OK
) == 0)
1317 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1318 if (ret
< 0 || ret
>= PATH_MAX
) {
1319 ERROR("pathname too long");
1322 if (access(retv
, X_OK
) == 0)
1326 * Last resort, look for the statically compiled init.lxc which we
1327 * hopefully bind-mounted in.
1328 * If we are called during container setup, and we get to this point,
1329 * then the init.lxc.static from the host will need to be bind-mounted
1330 * in. So we return NULL here to indicate that.
1335 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1336 if (ret
< 0 || ret
>= PATH_MAX
) {
1337 WARN("Nonsense - name /lxc.init.static too long");
1340 if (access(retv
, X_OK
) == 0)
1348 int print_to_file(const char *file
, const char *content
)
1353 f
= fopen(file
, "w");
1356 if (fprintf(f
, "%s", content
) != strlen(content
))
1362 int is_dir(const char *path
)
1364 struct stat statbuf
;
1365 int ret
= stat(path
, &statbuf
);
1366 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1372 * Given the '-t' template option to lxc-create, figure out what to
1373 * do. If the template is a full executable path, use that. If it
1374 * is something like 'sshd', then return $templatepath/lxc-sshd.
1375 * On success return the template, on error return NULL.
1377 char *get_template_path(const char *t
)
1382 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1387 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1388 tpath
= malloc(len
);
1391 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1392 if (ret
< 0 || ret
>= len
) {
1396 if (access(tpath
, X_OK
) < 0) {
1397 SYSERROR("bad template: %s", t
);
1406 * Sets the process title to the specified title. Note that this may fail if
1407 * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18).
1409 int setproctitle(char *title
)
1411 static char *proctitle
= NULL
;
1412 char buf
[2048], *tmp
;
1414 int i
, len
, ret
= 0;
1416 /* We don't really need to know all of this stuff, but unfortunately
1417 * PR_SET_MM_MAP requires us to set it all at once, so we have to
1418 * figure it out anyway.
1420 unsigned long start_data
, end_data
, start_brk
, start_code
, end_code
,
1421 start_stack
, arg_start
, arg_end
, env_start
, env_end
,
1423 struct prctl_mm_map prctl_map
;
1425 f
= fopen_cloexec("/proc/self/stat", "r");
1430 tmp
= fgets(buf
, sizeof(buf
), f
);
1436 /* Skip the first 25 fields, column 26-28 are start_code, end_code,
1437 * and start_stack */
1438 tmp
= strchr(buf
, ' ');
1439 for (i
= 0; i
< 24; i
++) {
1442 tmp
= strchr(tmp
+1, ' ');
1447 i
= sscanf(tmp
, "%lu %lu %lu", &start_code
, &end_code
, &start_stack
);
1451 /* Skip the next 19 fields, column 45-51 are start_data to arg_end */
1452 for (i
= 0; i
< 19; i
++) {
1455 tmp
= strchr(tmp
+1, ' ');
1461 i
= sscanf(tmp
, "%lu %lu %lu %*u %*u %lu %lu",
1470 /* Include the null byte here, because in the calculations below we
1471 * want to have room for it. */
1472 len
= strlen(title
) + 1;
1474 proctitle
= realloc(proctitle
, len
);
1478 arg_start
= (unsigned long) proctitle
;
1479 arg_end
= arg_start
+ len
;
1481 brk_val
= syscall(__NR_brk
, 0);
1483 prctl_map
= (struct prctl_mm_map
) {
1484 .start_code
= start_code
,
1485 .end_code
= end_code
,
1486 .start_stack
= start_stack
,
1487 .start_data
= start_data
,
1488 .end_data
= end_data
,
1489 .start_brk
= start_brk
,
1491 .arg_start
= arg_start
,
1493 .env_start
= env_start
,
1500 ret
= prctl(PR_SET_MM
, PR_SET_MM_MAP
, (long) &prctl_map
, sizeof(prctl_map
), 0);
1502 strcpy((char*)arg_start
, title
);
1504 INFO("setting cmdline failed - %s", strerror(errno
));
1510 * @path: a pathname where / replaced with '\0'.
1511 * @offsetp: pointer to int showing which path segment was last seen.
1512 * Updated on return to reflect the next segment.
1513 * @fulllen: full original path length.
1514 * Returns a pointer to the next path segment, or NULL if done.
1516 static char *get_nextpath(char *path
, int *offsetp
, int fulllen
)
1518 int offset
= *offsetp
;
1520 if (offset
>= fulllen
)
1523 while (path
[offset
] != '\0' && offset
< fulllen
)
1525 while (path
[offset
] == '\0' && offset
< fulllen
)
1529 return (offset
< fulllen
) ? &path
[offset
] : NULL
;
1533 * Check that @subdir is a subdir of @dir. @len is the length of
1534 * @dir (to avoid having to recalculate it).
1536 static bool is_subdir(const char *subdir
, const char *dir
, size_t len
)
1538 size_t subdirlen
= strlen(subdir
);
1540 if (subdirlen
< len
)
1542 if (strncmp(subdir
, dir
, len
) != 0)
1544 if (dir
[len
-1] == '/')
1546 if (subdir
[len
] == '/' || subdirlen
== len
)
1552 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1553 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1555 static int check_symlink(int fd
)
1558 int ret
= fstat(fd
, &sb
);
1561 if (S_ISLNK(sb
.st_mode
))
1567 * Open a file or directory, provided that it contains no symlinks.
1569 * CAVEAT: This function must not be used for other purposes than container
1570 * setup before executing the container's init
1572 static int open_if_safe(int dirfd
, const char *nextpath
)
1574 int newfd
= openat(dirfd
, nextpath
, O_RDONLY
| O_NOFOLLOW
);
1575 if (newfd
>= 0) /* Was not a symlink, all good. */
1581 if (errno
== EPERM
|| errno
== EACCES
) {
1582 /* We're not root (cause we got EPERM) so try opening with
1585 newfd
= openat(dirfd
, nextpath
, O_PATH
| O_NOFOLLOW
);
1587 /* O_PATH will return an fd for symlinks. We know
1588 * nextpath wasn't a symlink at last openat, so if fd is
1589 * now a link, then something * fishy is going on.
1591 int ret
= check_symlink(newfd
);
1603 * Open a path intending for mounting, ensuring that the final path
1604 * is inside the container's rootfs.
1606 * CAVEAT: This function must not be used for other purposes than container
1607 * setup before executing the container's init
1609 * @target: path to be opened
1610 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1611 * would be the container's rootfs.
1613 * Return an open fd for the path, or <0 on error.
1615 static int open_without_symlink(const char *target
, const char *prefix_skip
)
1617 int curlen
= 0, dirfd
, fulllen
, i
;
1620 fulllen
= strlen(target
);
1622 /* make sure prefix-skip makes sense */
1623 if (prefix_skip
&& strlen(prefix_skip
) > 0) {
1624 curlen
= strlen(prefix_skip
);
1625 if (!is_subdir(target
, prefix_skip
, curlen
)) {
1626 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1627 target
, prefix_skip
);
1631 * get_nextpath() expects the curlen argument to be
1632 * on a (turned into \0) / or before it, so decrement
1633 * curlen to make sure that happens
1642 /* Make a copy of target which we can hack up, and tokenize it */
1643 if ((dup
= strdup(target
)) == NULL
) {
1644 SYSERROR("Out of memory checking for symbolic link");
1647 for (i
= 0; i
< fulllen
; i
++) {
1652 dirfd
= open(prefix_skip
, O_RDONLY
);
1656 int newfd
, saved_errno
;
1659 if ((nextpath
= get_nextpath(dup
, &curlen
, fulllen
)) == NULL
)
1661 newfd
= open_if_safe(dirfd
, nextpath
);
1662 saved_errno
= errno
;
1666 errno
= saved_errno
;
1668 SYSERROR("%s in %s was a symbolic link!", nextpath
, target
);
1679 * Safely mount a path into a container, ensuring that the mount target
1680 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1681 * uses the host's /)
1683 * CAVEAT: This function must not be used for other purposes than container
1684 * setup before executing the container's init
1686 int safe_mount(const char *src
, const char *dest
, const char *fstype
,
1687 unsigned long flags
, const void *data
, const char *rootfs
)
1689 int destfd
, ret
, saved_errno
;
1690 /* Only needs enough for /proc/self/fd/<fd>. */
1691 char srcbuf
[50], destbuf
[50];
1693 const char *mntsrc
= src
;
1698 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1699 if (flags
& MS_BIND
&& src
&& src
[0] != '/') {
1700 INFO("this is a relative bind mount");
1701 srcfd
= open_without_symlink(src
, NULL
);
1704 ret
= snprintf(srcbuf
, 50, "/proc/self/fd/%d", srcfd
);
1705 if (ret
< 0 || ret
> 50) {
1707 ERROR("Out of memory");
1713 destfd
= open_without_symlink(dest
, rootfs
);
1716 saved_errno
= errno
;
1718 errno
= saved_errno
;
1723 ret
= snprintf(destbuf
, 50, "/proc/self/fd/%d", destfd
);
1724 if (ret
< 0 || ret
> 50) {
1728 ERROR("Out of memory");
1732 ret
= mount(mntsrc
, destbuf
, fstype
, flags
, data
);
1733 saved_errno
= errno
;
1738 errno
= saved_errno
;
1739 SYSERROR("Failed to mount %s onto %s", src
, dest
);
1747 * Mount a proc under @rootfs if proc self points to a pid other than
1748 * my own. This is needed to have a known-good proc mount for setting
1749 * up LSMs both at container startup and attach.
1751 * @rootfs : the rootfs where proc should be mounted
1753 * Returns < 0 on failure, 0 if the correct proc was already mounted
1754 * and 1 if a new proc was mounted.
1756 * NOTE: not to be called from inside the container namespace!
1758 int lxc_mount_proc_if_needed(const char *rootfs
)
1760 char path
[MAXPATHLEN
];
1761 int link_to_pid
, linklen
, mypid
, ret
;
1762 char link
[LXC_NUMSTRLEN64
] = {0};
1764 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc/self", rootfs
);
1765 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1766 SYSERROR("proc path name too long");
1770 linklen
= readlink(path
, link
, LXC_NUMSTRLEN64
);
1772 ret
= snprintf(path
, MAXPATHLEN
, "%s/proc", rootfs
);
1773 if (ret
< 0 || ret
>= MAXPATHLEN
) {
1774 SYSERROR("proc path name too long");
1778 /* /proc not mounted */
1780 if (mkdir(path
, 0755) && errno
!= EEXIST
)
1783 } else if (linklen
>= LXC_NUMSTRLEN64
) {
1784 link
[linklen
- 1] = '\0';
1785 ERROR("readlink returned truncated content: \"%s\"", link
);
1790 INFO("I am %d, /proc/self points to \"%s\"", mypid
, link
);
1792 if (lxc_safe_int(link
, &link_to_pid
) < 0)
1795 /* correct procfs is already mounted */
1796 if (link_to_pid
== mypid
)
1799 ret
= umount2(path
, MNT_DETACH
);
1801 WARN("failed to umount \"%s\" with MNT_DETACH", path
);
1804 /* rootfs is NULL */
1805 if (!strcmp(rootfs
, ""))
1806 ret
= mount("proc", path
, "proc", 0, NULL
);
1808 ret
= safe_mount("proc", path
, "proc", 0, NULL
, rootfs
);
1812 INFO("mounted /proc in container for security transition");
1816 int open_devnull(void)
1818 int fd
= open("/dev/null", O_RDWR
);
1821 SYSERROR("Can't open /dev/null");
1826 int set_stdfds(int fd
)
1833 ret
= dup2(fd
, STDIN_FILENO
);
1837 ret
= dup2(fd
, STDOUT_FILENO
);
1841 ret
= dup2(fd
, STDERR_FILENO
);
1848 int null_stdfds(void)
1851 int fd
= open_devnull();
1854 ret
= set_stdfds(fd
);
1862 * Return the number of lines in file @fn, or -1 on error
1864 int lxc_count_file_lines(const char *fn
)
1871 f
= fopen_cloexec(fn
, "r");
1875 while (getline(&line
, &sz
, f
) != -1) {
1883 void *lxc_strmmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
1886 void *tmp
= NULL
, *overlap
= NULL
;
1888 /* We establish an anonymous mapping that is one byte larger than the
1889 * underlying file. The pages handed to us are zero filled. */
1890 tmp
= mmap(addr
, length
+ 1, PROT_READ
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
1891 if (tmp
== MAP_FAILED
)
1894 /* Now we establish a fixed-address mapping starting at the address we
1895 * received from our anonymous mapping and replace all bytes excluding
1896 * the additional \0-byte with the file. This allows us to use normal
1897 * string-handling functions. */
1898 overlap
= mmap(tmp
, length
, prot
, MAP_FIXED
| flags
, fd
, offset
);
1899 if (overlap
== MAP_FAILED
)
1900 munmap(tmp
, length
+ 1);
1905 int lxc_strmunmap(void *addr
, size_t length
)
1907 return munmap(addr
, length
+ 1);
1910 /* Check whether a signal is blocked by a process. */
1911 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1912 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
1913 bool task_blocking_signal(pid_t pid
, int signal
)
1917 long unsigned int sigblk
= 0;
1922 char status
[__PROC_STATUS_LEN
];
1924 ret
= snprintf(status
, __PROC_STATUS_LEN
, "/proc/%d/status", pid
);
1925 if (ret
< 0 || ret
>= __PROC_STATUS_LEN
)
1928 f
= fopen(status
, "r");
1932 while (getline(&line
, &n
, f
) != -1) {
1933 if (strncmp(line
, "SigBlk:\t", 8))
1936 if (sscanf(line
+ 8, "%lx", &sigblk
) != 1)
1940 if (sigblk
& (1LU << (signal
- 1)))
1949 static int lxc_append_null_to_list(void ***list
)
1955 for (; (*list
)[newentry
]; newentry
++) {
1959 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
1964 (*list
)[newentry
+ 1] = NULL
;
1969 int lxc_append_string(char ***list
, char *entry
)
1974 newentry
= lxc_append_null_to_list((void ***)list
);
1978 copy
= strdup(entry
);
1982 (*list
)[newentry
] = copy
;
1987 int lxc_preserve_ns(const int pid
, const char *ns
)
1990 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
1991 #define __NS_PATH_LEN 50
1992 char path
[__NS_PATH_LEN
];
1994 /* This way we can use this function to also check whether namespaces
1995 * are supported by the kernel by passing in the NULL or the empty
1998 ret
= snprintf(path
, __NS_PATH_LEN
, "/proc/%d/ns%s%s", pid
,
1999 !ns
|| strcmp(ns
, "") == 0 ? "" : "/",
2000 !ns
|| strcmp(ns
, "") == 0 ? "" : ns
);
2001 if (ret
< 0 || (size_t)ret
>= __NS_PATH_LEN
)
2004 return open(path
, O_RDONLY
| O_CLOEXEC
);
2007 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
2010 unsigned long int uli
;
2012 while (isspace(*numstr
))
2019 uli
= strtoul(numstr
, &err
, 0);
2020 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2023 if (err
== numstr
|| *err
!= '\0')
2029 *converted
= (unsigned int)uli
;
2033 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
2036 unsigned long int uli
;
2038 while (isspace(*numstr
))
2045 uli
= strtoul(numstr
, &err
, 0);
2046 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
2049 if (err
== numstr
|| *err
!= '\0')
2056 int lxc_safe_int(const char *numstr
, int *converted
)
2059 signed long int sli
;
2062 sli
= strtol(numstr
, &err
, 0);
2063 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2066 if (errno
!= 0 && sli
== 0)
2069 if (err
== numstr
|| *err
!= '\0')
2072 if (sli
> INT_MAX
|| sli
< INT_MIN
)
2075 *converted
= (int)sli
;
2079 int lxc_safe_long(const char *numstr
, long int *converted
)
2082 signed long int sli
;
2085 sli
= strtol(numstr
, &err
, 0);
2086 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
2089 if (errno
!= 0 && sli
== 0)
2092 if (err
== numstr
|| *err
!= '\0')
2099 int lxc_switch_uid_gid(uid_t uid
, gid_t gid
)
2101 if (setgid(gid
) < 0) {
2102 SYSERROR("Failed to switch to gid %d.", gid
);
2105 NOTICE("Switched to gid %d.", gid
);
2107 if (setuid(uid
) < 0) {
2108 SYSERROR("Failed to switch to uid %d.", uid
);
2111 NOTICE("Switched to uid %d.", uid
);
2116 /* Simple covenience function which enables uniform logging. */
2117 int lxc_setgroups(int size
, gid_t list
[])
2119 if (setgroups(size
, list
) < 0) {
2120 SYSERROR("Failed to setgroups().");
2123 NOTICE("Dropped additional groups.");
2128 static int lxc_get_unused_loop_dev_legacy(char *loop_name
)
2131 struct loop_info64 lo64
;
2133 int dfd
= -1, fd
= -1, ret
= -1;
2135 dir
= opendir("/dev");
2139 while ((dp
= readdir(dir
))) {
2143 if (strncmp(dp
->d_name
, "loop", 4) != 0)
2150 fd
= openat(dfd
, dp
->d_name
, O_RDWR
);
2154 ret
= ioctl(fd
, LOOP_GET_STATUS64
, &lo64
);
2156 if (ioctl(fd
, LOOP_GET_STATUS64
, &lo64
) == 0 ||
2164 ret
= snprintf(loop_name
, LO_NAME_SIZE
, "/dev/%s", dp
->d_name
);
2165 if (ret
< 0 || ret
>= LO_NAME_SIZE
) {
2182 static int lxc_get_unused_loop_dev(char *name_loop
)
2185 int fd_ctl
= -1, fd_tmp
= -1;
2187 fd_ctl
= open("/dev/loop-control", O_RDWR
| O_CLOEXEC
);
2191 loop_nr
= ioctl(fd_ctl
, LOOP_CTL_GET_FREE
);
2195 ret
= snprintf(name_loop
, LO_NAME_SIZE
, "/dev/loop%d", loop_nr
);
2196 if (ret
< 0 || ret
>= LO_NAME_SIZE
)
2199 fd_tmp
= open(name_loop
, O_RDWR
| O_CLOEXEC
);
2208 int lxc_prepare_loop_dev(const char *source
, char *loop_dev
, int flags
)
2211 struct loop_info64 lo64
;
2212 int fd_img
= -1, fret
= -1, fd_loop
= -1;
2214 fd_loop
= lxc_get_unused_loop_dev(loop_dev
);
2216 if (fd_loop
== -ENODEV
)
2217 fd_loop
= lxc_get_unused_loop_dev_legacy(loop_dev
);
2222 fd_img
= open(source
, O_RDWR
| O_CLOEXEC
);
2226 ret
= ioctl(fd_loop
, LOOP_SET_FD
, fd_img
);
2230 memset(&lo64
, 0, sizeof(lo64
));
2231 lo64
.lo_flags
= flags
;
2233 ret
= ioctl(fd_loop
, LOOP_SET_STATUS64
, &lo64
);
2243 if (fret
< 0 && fd_loop
>= 0) {
2251 int lxc_unstack_mountpoint(const char *path
, bool lazy
)
2257 ret
= umount2(path
, lazy
? MNT_DETACH
: 0);
2259 /* We consider anything else than EINVAL deadly to prevent going
2260 * into an infinite loop. (The other alternative is constantly
2261 * parsing /proc/self/mountinfo which is yucky and probably
2264 if (errno
!= EINVAL
)
2267 /* Just stop counting when this happens. That'd just be so
2268 * stupid that we won't even bother trying to report back the
2269 * correct value anymore.
2271 if (umounts
!= INT_MAX
)
2273 /* We succeeded in umounting. Make sure that there's no other
2274 * mountpoint stacked underneath.
2282 int run_command(char *buf
, size_t buf_size
, int (*child_fn
)(void *), void *args
)
2285 int ret
, fret
, pipefd
[2];
2288 /* Make sure our callers do not receive unitialized memory. */
2289 if (buf_size
> 0 && buf
)
2292 if (pipe(pipefd
) < 0) {
2293 SYSERROR("failed to create pipe");
2301 SYSERROR("failed to create new process");
2306 /* Close the read-end of the pipe. */
2309 /* Redirect std{err,out} to write-end of the
2312 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2314 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2316 /* Close the write-end of the pipe. */
2320 SYSERROR("failed to duplicate std{err,out} file descriptor");
2324 /* Does not return. */
2326 ERROR("failed to exec command");
2330 /* close the write-end of the pipe */
2333 if (buf
&& buf_size
> 0) {
2334 bytes
= read(pipefd
[0], buf
, buf_size
- 1);
2336 buf
[bytes
- 1] = '\0';
2339 fret
= wait_for_pid(child
);
2340 /* close the read-end of the pipe */
2346 char *must_make_path(const char *first
, ...)
2350 size_t full_len
= strlen(first
);
2352 dest
= must_copy_string(first
);
2354 va_start(args
, first
);
2355 while ((cur
= va_arg(args
, char *)) != NULL
) {
2356 full_len
+= strlen(cur
);
2359 dest
= must_realloc(dest
, full_len
+ 1);
2369 char *must_copy_string(const char *entry
)
2376 ret
= strdup(entry
);
2382 void *must_realloc(void *orig
, size_t sz
)
2387 ret
= realloc(orig
, sz
);
2393 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
2395 return (fs
->f_type
== (fs_type_magic
)magic_val
);
2398 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
2404 ret
= statfs(path
, &sb
);
2408 has_type
= is_fs_type(&sb
, magic_val
);
2409 if (!has_type
&& magic_val
== RAMFS_MAGIC
)
2410 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2415 bool lxc_nic_exists(char *nic
)
2417 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2418 char path
[__LXC_SYS_CLASS_NET_LEN
];
2422 if (!strcmp(nic
, "none"))
2425 ret
= snprintf(path
, __LXC_SYS_CLASS_NET_LEN
, "/sys/class/net/%s", nic
);
2426 if (ret
< 0 || (size_t)ret
>= __LXC_SYS_CLASS_NET_LEN
)
2429 ret
= stat(path
, &sb
);