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
379 const char *get_rundir()
383 rundir
= getenv("XDG_RUNTIME_DIR");
384 if (geteuid() == 0 || rundir
== NULL
)
389 int wait_for_pid(pid_t pid
)
394 ret
= waitpid(pid
, &status
, 0);
402 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
407 int lxc_wait_for_pid_status(pid_t pid
)
412 ret
= waitpid(pid
, &status
, 0);
423 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
427 ret
= write(fd
, buf
, count
);
428 if (ret
< 0 && errno
== EINTR
)
433 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
437 ret
= read(fd
, buf
, count
);
438 if (ret
< 0 && errno
== EINTR
)
443 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
446 ret
= lxc_read_nointr(fd
, buf
, count
);
449 if ((size_t)ret
!= count
)
451 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
459 #include <gnutls/gnutls.h>
460 #include <gnutls/crypto.h>
462 __attribute__((constructor
))
463 static void gnutls_lxc_init(void)
465 gnutls_global_init();
468 int sha1sum_file(char *fnam
, unsigned char *digest
)
477 f
= fopen_cloexec(fnam
, "r");
479 SYSERROR("Error opening template");
482 if (fseek(f
, 0, SEEK_END
) < 0) {
483 SYSERROR("Error seeking to end of template");
487 if ((flen
= ftell(f
)) < 0) {
488 SYSERROR("Error telling size of template");
492 if (fseek(f
, 0, SEEK_SET
) < 0) {
493 SYSERROR("Error seeking to start of template");
497 if ((buf
= malloc(flen
+1)) == NULL
) {
498 SYSERROR("Out of memory");
502 if (fread(buf
, 1, flen
, f
) != flen
) {
503 SYSERROR("Failure reading template");
509 SYSERROR("Failre closing template");
514 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
520 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
523 size_t count
= 1 + skip
;
526 /* first determine size of argument list, we don't want to reallocate
531 char* arg
= va_arg(ap2
, char*);
538 result
= calloc(count
, sizeof(char*));
543 char* arg
= va_arg(ap
, char*);
546 arg
= do_strdup
? strdup(arg
) : arg
;
549 result
[count
++] = arg
;
552 /* calloc has already set last element to NULL*/
560 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
562 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
565 FILE *fopen_cloexec(const char *path
, const char *mode
)
573 if (!strncmp(mode
, "r+", 2)) {
576 } else if (!strncmp(mode
, "r", 1)) {
577 open_mode
= O_RDONLY
;
579 } else if (!strncmp(mode
, "w+", 2)) {
580 open_mode
= O_RDWR
| O_TRUNC
| O_CREAT
;
582 } else if (!strncmp(mode
, "w", 1)) {
583 open_mode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
585 } else if (!strncmp(mode
, "a+", 2)) {
586 open_mode
= O_RDWR
| O_CREAT
| O_APPEND
;
588 } else if (!strncmp(mode
, "a", 1)) {
589 open_mode
= O_WRONLY
| O_CREAT
| O_APPEND
;
592 for (; mode
[step
]; step
++)
593 if (mode
[step
] == 'x')
595 open_mode
|= O_CLOEXEC
;
597 fd
= open(path
, open_mode
, 0666);
601 ret
= fdopen(fd
, mode
);
609 extern struct lxc_popen_FILE
*lxc_popen(const char *command
)
611 struct lxc_popen_FILE
*fp
= NULL
;
612 int parent_end
= -1, child_end
= -1;
616 int r
= pipe2(pipe_fds
, O_CLOEXEC
);
619 ERROR("pipe2 failure");
623 parent_end
= pipe_fds
[0];
624 child_end
= pipe_fds
[1];
628 if (child_pid
== 0) {
630 int child_std_end
= STDOUT_FILENO
;
632 if (child_end
!= child_std_end
) {
633 /* dup2() doesn't dup close-on-exec flag */
634 dup2(child_end
, child_std_end
);
636 /* it's safe not to close child_end here
637 * as it's marked close-on-exec anyway
641 * The descriptor is already the one we will use.
642 * But it must not be marked close-on-exec.
645 fcntl(child_end
, F_SETFD
, 0);
650 * This is the main/only reason
651 * why we do our lousy popen() emulation.
656 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
659 execl("/bin/sh", "sh", "-c", command
, (char *) NULL
);
669 ERROR("fork failure");
673 fp
= calloc(1, sizeof(*fp
));
675 ERROR("failed to allocate memory");
679 fp
->f
= fdopen(parent_end
, "r");
681 ERROR("fdopen failure");
685 fp
->child_pid
= child_pid
;
694 parent_end
= -1; /* so we do not close it second time */
700 if (parent_end
!= -1)
706 extern int lxc_pclose(struct lxc_popen_FILE
*fp
)
715 child_pid
= fp
->child_pid
;
716 /* free memory (we still need to close file stream) */
721 if (!f
|| fclose(f
)) {
722 ERROR("fclose failure");
727 wait_pid
= waitpid(child_pid
, &wstatus
, 0);
728 } while (wait_pid
== -1 && errno
== EINTR
);
730 if (wait_pid
== -1) {
731 ERROR("waitpid failure");
738 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
740 ssize_t len
= -1, saved_len
= -1;
742 size_t replacement_len
= strlen(replacement
);
743 size_t needle_len
= strlen(needle
);
745 /* should be executed exactly twice */
746 while (len
== -1 || result
== NULL
) {
752 result
= calloc(1, len
+ 1);
760 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
761 part_len
= (ssize_t
)(p
- last_p
);
762 if (result
&& part_len
> 0)
763 memcpy(&result
[len
], last_p
, part_len
);
765 if (result
&& replacement_len
> 0)
766 memcpy(&result
[len
], replacement
, replacement_len
);
767 len
+= replacement_len
;
770 part_len
= strlen(last_p
);
771 if (result
&& part_len
> 0)
772 memcpy(&result
[len
], last_p
, part_len
);
776 /* make sure we did the same thing twice,
777 * once for calculating length, the other
778 * time for copying data */
779 assert(saved_len
== len
);
780 /* make sure we didn't overwrite any buffer,
781 * due to calloc the string should be 0-terminated */
782 assert(result
[len
] == '\0');
787 bool lxc_string_in_array(const char *needle
, const char **haystack
)
789 for (; haystack
&& *haystack
; haystack
++)
790 if (!strcmp(needle
, *haystack
))
795 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
799 size_t sep_len
= strlen(sep
);
800 size_t result_len
= use_as_prefix
* sep_len
;
802 /* calculate new string length */
803 for (p
= (char **)parts
; *p
; p
++)
804 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
806 result
= calloc(result_len
+ 1, 1);
812 for (p
= (char **)parts
; *p
; p
++) {
813 if (p
> (char **)parts
)
821 char **lxc_normalize_path(const char *path
)
825 size_t components_len
= 0;
828 components
= lxc_string_split(path
, '/');
831 for (p
= components
; *p
; p
++)
834 /* resolve '.' and '..' */
835 for (pos
= 0; pos
< components_len
; ) {
836 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
837 /* eat this element */
838 free(components
[pos
]);
839 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
841 } else if (!strcmp(components
[pos
], "..")) {
842 /* eat this and the previous element */
843 free(components
[pos
- 1]);
844 free(components
[pos
]);
845 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
856 char *lxc_append_paths(const char *first
, const char *second
)
858 size_t len
= strlen(first
) + strlen(second
) + 1;
859 const char *pattern
= "%s%s";
862 if (second
[0] != '/') {
867 result
= calloc(1, len
);
871 snprintf(result
, len
, pattern
, first
, second
);
875 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
877 char *token
, *str
, *saveptr
= NULL
;
878 char sep
[2] = { _sep
, '\0' };
880 if (!haystack
|| !needle
)
883 str
= alloca(strlen(haystack
)+1);
884 strcpy(str
, haystack
);
885 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
886 if (strcmp(needle
, token
) == 0)
893 char **lxc_string_split(const char *string
, char _sep
)
895 char *token
, *str
, *saveptr
= NULL
;
896 char sep
[2] = { _sep
, '\0' };
897 char **result
= NULL
;
898 size_t result_capacity
= 0;
899 size_t result_count
= 0;
903 return calloc(1, sizeof(char *));
905 str
= alloca(strlen(string
)+1);
907 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
908 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
911 result
[result_count
] = strdup(token
);
912 if (!result
[result_count
])
917 /* if we allocated too much, reduce it */
918 return realloc(result
, (result_count
+ 1) * sizeof(char *));
921 lxc_free_array((void **)result
, free
);
926 char **lxc_string_split_and_trim(const char *string
, char _sep
)
928 char *token
, *str
, *saveptr
= NULL
;
929 char sep
[2] = { _sep
, '\0' };
930 char **result
= NULL
;
931 size_t result_capacity
= 0;
932 size_t result_count
= 0;
937 return calloc(1, sizeof(char *));
939 str
= alloca(strlen(string
)+1);
941 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
942 while (token
[0] == ' ' || token
[0] == '\t')
945 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
949 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
952 result
[result_count
] = strdup(token
);
953 if (!result
[result_count
])
958 /* if we allocated too much, reduce it */
959 return realloc(result
, (result_count
+ 1) * sizeof(char *));
962 lxc_free_array((void **)result
, free
);
967 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
970 for (p
= array
; p
&& *p
; p
++)
975 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
980 /* first time around, catch some trivial mistakes of the user
981 * only initializing one of these */
982 if (!*array
|| !*capacity
) {
987 new_capacity
= *capacity
;
988 while (new_size
+ 1 > new_capacity
)
989 new_capacity
+= capacity_increment
;
990 if (new_capacity
!= *capacity
) {
991 /* we have to reallocate */
992 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
995 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
997 *capacity
= new_capacity
;
1000 /* array has sufficient elements */
1004 size_t lxc_array_len(void **array
)
1009 for (p
= array
; p
&& *p
; p
++)
1015 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
1017 int fd
, saved_errno
;
1020 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
1023 ret
= lxc_write_nointr(fd
, buf
, count
);
1026 if ((size_t)ret
!= count
)
1029 ret
= lxc_write_nointr(fd
, "\n", 1);
1037 saved_errno
= errno
;
1039 errno
= saved_errno
;
1043 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
1045 int fd
= -1, saved_errno
;
1048 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1052 if (!buf
|| !count
) {
1055 while ((ret
= read(fd
, buf2
, 100)) > 0)
1060 memset(buf
, 0, count
);
1061 ret
= read(fd
, buf
, count
);
1065 ERROR("read %s: %s", filename
, strerror(errno
));
1067 saved_errno
= errno
;
1069 errno
= saved_errno
;
1073 void **lxc_append_null_to_array(void **array
, size_t count
)
1077 /* Append NULL to the array */
1079 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
1082 for (i
= 0; i
< count
; i
++)
1088 array
[count
] = NULL
;
1093 int randseed(bool srand_it
)
1096 srand pre-seed function based on /dev/urandom
1098 unsigned int seed
=time(NULL
)+getpid();
1101 f
= fopen("/dev/urandom", "r");
1103 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
1105 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
1115 uid_t
get_ns_uid(uid_t orig
)
1119 uid_t nsid
, hostid
, range
;
1120 FILE *f
= fopen("/proc/self/uid_map", "r");
1124 while (getline(&line
, &sz
, f
) != -1) {
1125 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
1127 if (hostid
<= orig
&& hostid
+ range
> orig
) {
1128 nsid
+= orig
- hostid
;
1140 bool dir_exists(const char *path
)
1145 ret
= stat(path
, &sb
);
1147 // could be something other than eexist, just say no
1149 return S_ISDIR(sb
.st_mode
);