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
31 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/mount.h>
39 #include <sys/types.h>
47 #define MAX_STACKDEPTH 25
49 lxc_log_define(lxc_utils
, lxc
);
51 static int _recursive_rmdir_onedev(char *dirname
, dev_t pdev
)
53 struct dirent dirent
, *direntp
;
56 char pathname
[MAXPATHLEN
];
58 dir
= opendir(dirname
);
60 ERROR("%s: failed to open %s", __func__
, dirname
);
64 while (!readdir_r(dir
, &dirent
, &direntp
)) {
71 if (!strcmp(direntp
->d_name
, ".") ||
72 !strcmp(direntp
->d_name
, ".."))
75 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
76 if (rc
< 0 || rc
>= MAXPATHLEN
) {
77 ERROR("pathname too long");
81 ret
= lstat(pathname
, &mystat
);
83 ERROR("%s: failed to stat %s", __func__
, pathname
);
87 if (mystat
.st_dev
!= pdev
)
89 if (S_ISDIR(mystat
.st_mode
)) {
90 if (_recursive_rmdir_onedev(pathname
, pdev
) < 0)
93 if (unlink(pathname
) < 0) {
94 ERROR("%s: failed to delete %s", __func__
, pathname
);
100 if (rmdir(dirname
) < 0) {
101 ERROR("%s: failed to delete %s", __func__
, dirname
);
107 ERROR("%s: failed to close directory %s", __func__
, dirname
);
111 return failed
? -1 : 0;
114 /* returns 0 on success, -1 if there were any failures */
115 extern int lxc_rmdir_onedev(char *path
)
119 if (lstat(path
, &mystat
) < 0) {
120 ERROR("%s: failed to stat %s", __func__
, path
);
124 return _recursive_rmdir_onedev(path
, mystat
.st_dev
);
127 static int mount_fs(const char *source
, const char *target
, const char *type
)
129 /* the umount may fail */
131 WARN("failed to unmount %s : %s", target
, strerror(errno
));
133 if (mount(source
, target
, type
, 0, NULL
)) {
134 ERROR("failed to mount %s : %s", target
, strerror(errno
));
138 DEBUG("'%s' mounted on '%s'", source
, target
);
143 extern void lxc_setup_fs(void)
145 if (mount_fs("proc", "/proc", "proc"))
146 INFO("failed to remount proc");
148 /* if we can't mount /dev/shm, continue anyway */
149 if (mount_fs("shmfs", "/dev/shm", "tmpfs"))
150 INFO("failed to mount /dev/shm");
152 /* If we were able to mount /dev/shm, then /dev exists */
153 /* Sure, but it's read-only per config :) */
154 if (access("/dev/mqueue", F_OK
) && mkdir("/dev/mqueue", 0666)) {
155 DEBUG("failed to create '/dev/mqueue'");
159 /* continue even without posix message queue support */
160 if (mount_fs("mqueue", "/dev/mqueue", "mqueue"))
161 INFO("failed to mount /dev/mqueue");
164 /* borrowed from iproute2 */
165 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
174 res
= strtoul(arg
, &ptr
, base
);
175 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
183 extern int mkdir_p(const char *dir
, mode_t mode
)
185 const char *tmp
= dir
;
186 const char *orig
= dir
;
190 dir
= tmp
+ strspn(tmp
, "/");
191 tmp
= dir
+ strcspn(dir
, "/");
192 makeme
= strndup(orig
, dir
- orig
);
194 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
195 SYSERROR("failed to create directory '%s'", makeme
);
206 extern void remove_trailing_slashes(char *p
)
209 while (--l
>= 0 && (p
[l
] == '/' || p
[l
] == '\n'))
213 static char *copy_global_config_value(char *p
)
220 if (p
[len
-1] == '\n') {
224 retbuf
= malloc(len
+1);
231 #define DEFAULT_VG "lxc"
232 #define DEFAULT_THIN_POOL "lxc"
233 #define DEFAULT_ZFSROOT "lxc"
235 const char *lxc_global_config_value(const char *option_name
)
237 static const char * const options
[][2] = {
238 { "lxc.bdev.lvm.vg", DEFAULT_VG
},
239 { "lxc.bdev.lvm.thin_pool", DEFAULT_THIN_POOL
},
240 { "lxc.bdev.zfs.root", DEFAULT_ZFSROOT
},
241 { "lxc.lxcpath", NULL
},
242 { "lxc.default_config", NULL
},
243 { "lxc.cgroup.pattern", DEFAULT_CGROUP_PATTERN
},
244 { "lxc.cgroup.use", NULL
},
248 /* placed in the thread local storage pool for non-bionic targets */
250 static __thread
const char *values
[sizeof(options
) / sizeof(options
[0])] = { 0 };
252 static const char *values
[sizeof(options
) / sizeof(options
[0])] = { 0 };
254 char *user_config_path
= NULL
;
255 char *user_default_config_path
= NULL
;
256 char *user_lxc_path
= NULL
;
259 const char *user_home
= getenv("HOME");
263 user_config_path
= malloc(sizeof(char) * (22 + strlen(user_home
)));
264 user_default_config_path
= malloc(sizeof(char) * (26 + strlen(user_home
)));
265 user_lxc_path
= malloc(sizeof(char) * (19 + strlen(user_home
)));
267 sprintf(user_config_path
, "%s/.config/lxc/lxc.conf", user_home
);
268 sprintf(user_default_config_path
, "%s/.config/lxc/default.conf", user_home
);
269 sprintf(user_lxc_path
, "%s/.local/share/lxc/", user_home
);
272 user_config_path
= strdup(LXC_GLOBAL_CONF
);
273 user_default_config_path
= strdup(LXC_DEFAULT_CONFIG
);
274 user_lxc_path
= strdup(LXCPATH
);
277 const char * const (*ptr
)[2];
279 char buf
[1024], *p
, *p2
;
282 for (i
= 0, ptr
= options
; (*ptr
)[0]; ptr
++, i
++) {
283 if (!strcmp(option_name
, (*ptr
)[0]))
287 free(user_config_path
);
288 free(user_default_config_path
);
295 free(user_config_path
);
296 free(user_default_config_path
);
301 fin
= fopen_cloexec(user_config_path
, "r");
302 free(user_config_path
);
304 while (fgets(buf
, 1024, fin
)) {
307 p
= strstr(buf
, option_name
);
310 /* see if there was just white space in front
313 for (p2
= buf
; p2
< p
; p2
++) {
314 if (*p2
!= ' ' && *p2
!= '\t')
322 /* see if there was just white space after
325 for (p2
+= strlen(option_name
); p2
< p
; p2
++) {
326 if (*p2
!= ' ' && *p2
!= '\t')
332 while (*p
&& (*p
== ' ' || *p
== '\t')) p
++;
336 free(user_default_config_path
);
338 if (strcmp(option_name
, "lxc.lxcpath") == 0) {
340 user_lxc_path
= copy_global_config_value(p
);
341 remove_trailing_slashes(user_lxc_path
);
342 values
[i
] = user_lxc_path
;
346 values
[i
] = copy_global_config_value(p
);
351 /* could not find value, use default */
352 if (strcmp(option_name
, "lxc.lxcpath") == 0) {
353 remove_trailing_slashes(user_lxc_path
);
354 values
[i
] = user_lxc_path
;
355 free(user_default_config_path
);
357 else if (strcmp(option_name
, "lxc.default_config") == 0) {
358 values
[i
] = user_default_config_path
;
362 free(user_default_config_path
);
364 values
[i
] = (*ptr
)[1];
366 /* special case: if default value is NULL,
367 * and there is no config, don't view that
384 if (geteuid() == 0) {
385 rundir
= strdup(RUNTIME_PATH
);
389 rundir
= getenv("XDG_RUNTIME_DIR");
391 rundir
= strdup(rundir
);
395 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
396 homedir
= getenv("HOME");
398 ERROR("HOME isn't set in the environment.");
402 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
403 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
408 int wait_for_pid(pid_t pid
)
413 ret
= waitpid(pid
, &status
, 0);
421 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
426 int lxc_wait_for_pid_status(pid_t pid
)
431 ret
= waitpid(pid
, &status
, 0);
442 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
446 ret
= write(fd
, buf
, count
);
447 if (ret
< 0 && errno
== EINTR
)
452 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
456 ret
= read(fd
, buf
, count
);
457 if (ret
< 0 && errno
== EINTR
)
462 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
465 ret
= lxc_read_nointr(fd
, buf
, count
);
468 if ((size_t)ret
!= count
)
470 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
478 #include <gnutls/gnutls.h>
479 #include <gnutls/crypto.h>
481 __attribute__((constructor
))
482 static void gnutls_lxc_init(void)
484 gnutls_global_init();
487 int sha1sum_file(char *fnam
, unsigned char *digest
)
496 f
= fopen_cloexec(fnam
, "r");
498 SYSERROR("Error opening template");
501 if (fseek(f
, 0, SEEK_END
) < 0) {
502 SYSERROR("Error seeking to end of template");
506 if ((flen
= ftell(f
)) < 0) {
507 SYSERROR("Error telling size of template");
511 if (fseek(f
, 0, SEEK_SET
) < 0) {
512 SYSERROR("Error seeking to start of template");
516 if ((buf
= malloc(flen
+1)) == NULL
) {
517 SYSERROR("Out of memory");
521 if (fread(buf
, 1, flen
, f
) != flen
) {
522 SYSERROR("Failure reading template");
528 SYSERROR("Failre closing template");
533 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
539 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
542 size_t count
= 1 + skip
;
545 /* first determine size of argument list, we don't want to reallocate
550 char* arg
= va_arg(ap2
, char*);
557 result
= calloc(count
, sizeof(char*));
562 char* arg
= va_arg(ap
, char*);
565 arg
= do_strdup
? strdup(arg
) : arg
;
568 result
[count
++] = arg
;
571 /* calloc has already set last element to NULL*/
579 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
581 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
584 FILE *fopen_cloexec(const char *path
, const char *mode
)
592 if (!strncmp(mode
, "r+", 2)) {
595 } else if (!strncmp(mode
, "r", 1)) {
596 open_mode
= O_RDONLY
;
598 } else if (!strncmp(mode
, "w+", 2)) {
599 open_mode
= O_RDWR
| O_TRUNC
| O_CREAT
;
601 } else if (!strncmp(mode
, "w", 1)) {
602 open_mode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
604 } else if (!strncmp(mode
, "a+", 2)) {
605 open_mode
= O_RDWR
| O_CREAT
| O_APPEND
;
607 } else if (!strncmp(mode
, "a", 1)) {
608 open_mode
= O_WRONLY
| O_CREAT
| O_APPEND
;
611 for (; mode
[step
]; step
++)
612 if (mode
[step
] == 'x')
614 open_mode
|= O_CLOEXEC
;
616 fd
= open(path
, open_mode
, 0666);
620 ret
= fdopen(fd
, mode
);
628 extern struct lxc_popen_FILE
*lxc_popen(const char *command
)
630 struct lxc_popen_FILE
*fp
= NULL
;
631 int parent_end
= -1, child_end
= -1;
635 int r
= pipe2(pipe_fds
, O_CLOEXEC
);
638 ERROR("pipe2 failure");
642 parent_end
= pipe_fds
[0];
643 child_end
= pipe_fds
[1];
647 if (child_pid
== 0) {
649 int child_std_end
= STDOUT_FILENO
;
651 if (child_end
!= child_std_end
) {
652 /* dup2() doesn't dup close-on-exec flag */
653 dup2(child_end
, child_std_end
);
655 /* it's safe not to close child_end here
656 * as it's marked close-on-exec anyway
660 * The descriptor is already the one we will use.
661 * But it must not be marked close-on-exec.
664 if (fcntl(child_end
, F_SETFD
, 0) != 0) {
665 SYSERROR("Failed to remove FD_CLOEXEC from fd.");
672 * This is the main/only reason
673 * why we do our lousy popen() emulation.
678 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
681 execl("/bin/sh", "sh", "-c", command
, (char *) NULL
);
691 ERROR("fork failure");
695 fp
= calloc(1, sizeof(*fp
));
697 ERROR("failed to allocate memory");
701 fp
->f
= fdopen(parent_end
, "r");
703 ERROR("fdopen failure");
707 fp
->child_pid
= child_pid
;
716 parent_end
= -1; /* so we do not close it second time */
722 if (parent_end
!= -1)
728 extern int lxc_pclose(struct lxc_popen_FILE
*fp
)
737 child_pid
= fp
->child_pid
;
738 /* free memory (we still need to close file stream) */
743 if (!f
|| fclose(f
)) {
744 ERROR("fclose failure");
749 wait_pid
= waitpid(child_pid
, &wstatus
, 0);
750 } while (wait_pid
== -1 && errno
== EINTR
);
752 if (wait_pid
== -1) {
753 ERROR("waitpid failure");
760 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
762 ssize_t len
= -1, saved_len
= -1;
764 size_t replacement_len
= strlen(replacement
);
765 size_t needle_len
= strlen(needle
);
767 /* should be executed exactly twice */
768 while (len
== -1 || result
== NULL
) {
774 result
= calloc(1, len
+ 1);
782 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
783 part_len
= (ssize_t
)(p
- last_p
);
784 if (result
&& part_len
> 0)
785 memcpy(&result
[len
], last_p
, part_len
);
787 if (result
&& replacement_len
> 0)
788 memcpy(&result
[len
], replacement
, replacement_len
);
789 len
+= replacement_len
;
792 part_len
= strlen(last_p
);
793 if (result
&& part_len
> 0)
794 memcpy(&result
[len
], last_p
, part_len
);
798 /* make sure we did the same thing twice,
799 * once for calculating length, the other
800 * time for copying data */
801 assert(saved_len
== len
);
802 /* make sure we didn't overwrite any buffer,
803 * due to calloc the string should be 0-terminated */
804 assert(result
[len
] == '\0');
809 bool lxc_string_in_array(const char *needle
, const char **haystack
)
811 for (; haystack
&& *haystack
; haystack
++)
812 if (!strcmp(needle
, *haystack
))
817 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
821 size_t sep_len
= strlen(sep
);
822 size_t result_len
= use_as_prefix
* sep_len
;
824 /* calculate new string length */
825 for (p
= (char **)parts
; *p
; p
++)
826 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
828 result
= calloc(result_len
+ 1, 1);
834 for (p
= (char **)parts
; *p
; p
++) {
835 if (p
> (char **)parts
)
843 char **lxc_normalize_path(const char *path
)
847 size_t components_len
= 0;
850 components
= lxc_string_split(path
, '/');
853 for (p
= components
; *p
; p
++)
856 /* resolve '.' and '..' */
857 for (pos
= 0; pos
< components_len
; ) {
858 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
859 /* eat this element */
860 free(components
[pos
]);
861 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
863 } else if (!strcmp(components
[pos
], "..")) {
864 /* eat this and the previous element */
865 free(components
[pos
- 1]);
866 free(components
[pos
]);
867 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
878 char *lxc_append_paths(const char *first
, const char *second
)
880 size_t len
= strlen(first
) + strlen(second
) + 1;
881 const char *pattern
= "%s%s";
884 if (second
[0] != '/') {
889 result
= calloc(1, len
);
893 snprintf(result
, len
, pattern
, first
, second
);
897 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
899 char *token
, *str
, *saveptr
= NULL
;
900 char sep
[2] = { _sep
, '\0' };
902 if (!haystack
|| !needle
)
905 str
= alloca(strlen(haystack
)+1);
906 strcpy(str
, haystack
);
907 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
908 if (strcmp(needle
, token
) == 0)
915 char **lxc_string_split(const char *string
, char _sep
)
917 char *token
, *str
, *saveptr
= NULL
;
918 char sep
[2] = { _sep
, '\0' };
919 char **result
= NULL
;
920 size_t result_capacity
= 0;
921 size_t result_count
= 0;
925 return calloc(1, sizeof(char *));
927 str
= alloca(strlen(string
)+1);
929 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
930 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
933 result
[result_count
] = strdup(token
);
934 if (!result
[result_count
])
939 /* if we allocated too much, reduce it */
940 return realloc(result
, (result_count
+ 1) * sizeof(char *));
943 lxc_free_array((void **)result
, free
);
948 char **lxc_string_split_and_trim(const char *string
, char _sep
)
950 char *token
, *str
, *saveptr
= NULL
;
951 char sep
[2] = { _sep
, '\0' };
952 char **result
= NULL
;
953 size_t result_capacity
= 0;
954 size_t result_count
= 0;
959 return calloc(1, sizeof(char *));
961 str
= alloca(strlen(string
)+1);
963 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
964 while (token
[0] == ' ' || token
[0] == '\t')
967 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
971 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
974 result
[result_count
] = strdup(token
);
975 if (!result
[result_count
])
980 /* if we allocated too much, reduce it */
981 return realloc(result
, (result_count
+ 1) * sizeof(char *));
984 lxc_free_array((void **)result
, free
);
989 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
992 for (p
= array
; p
&& *p
; p
++)
997 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
1002 /* first time around, catch some trivial mistakes of the user
1003 * only initializing one of these */
1004 if (!*array
|| !*capacity
) {
1009 new_capacity
= *capacity
;
1010 while (new_size
+ 1 > new_capacity
)
1011 new_capacity
+= capacity_increment
;
1012 if (new_capacity
!= *capacity
) {
1013 /* we have to reallocate */
1014 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
1017 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
1019 *capacity
= new_capacity
;
1022 /* array has sufficient elements */
1026 size_t lxc_array_len(void **array
)
1031 for (p
= array
; p
&& *p
; p
++)
1037 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
1039 int fd
, saved_errno
;
1042 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
1045 ret
= lxc_write_nointr(fd
, buf
, count
);
1048 if ((size_t)ret
!= count
)
1051 ret
= lxc_write_nointr(fd
, "\n", 1);
1059 saved_errno
= errno
;
1061 errno
= saved_errno
;
1065 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
1067 int fd
= -1, saved_errno
;
1070 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1074 if (!buf
|| !count
) {
1077 while ((ret
= read(fd
, buf2
, 100)) > 0)
1082 memset(buf
, 0, count
);
1083 ret
= read(fd
, buf
, count
);
1087 ERROR("read %s: %s", filename
, strerror(errno
));
1089 saved_errno
= errno
;
1091 errno
= saved_errno
;
1095 void **lxc_append_null_to_array(void **array
, size_t count
)
1099 /* Append NULL to the array */
1101 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1104 for (i
= 0; i
< count
; i
++)
1110 array
[count
] = NULL
;
1115 int randseed(bool srand_it
)
1118 srand pre-seed function based on /dev/urandom
1120 unsigned int seed
=time(NULL
)+getpid();
1123 f
= fopen("/dev/urandom", "r");
1125 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1127 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1137 uid_t
get_ns_uid(uid_t orig
)
1141 uid_t nsid
, hostid
, range
;
1142 FILE *f
= fopen("/proc/self/uid_map", "r");
1146 while (getline(&line
, &sz
, f
) != -1) {
1147 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1149 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1150 nsid
+= orig
- hostid
;
1162 bool dir_exists(const char *path
)
1167 ret
= stat(path
, &sb
);
1169 // could be something other than eexist, just say no
1171 return S_ISDIR(sb
.st_mode
);
1174 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1175 * FNV has good anti collision properties and we're not worried
1176 * about pre-image resistance or one-way-ness, we're just trying to make
1177 * the name unique in the 108 bytes of space we have.
1179 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1183 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1185 /* xor the bottom with the current octet */
1186 hval
^= (uint64_t)*bp
;
1189 * multiply by the 64 bit FNV magic prime mod 2^64
1191 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1192 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);