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>
35 #include <sys/param.h>
36 #include <sys/mount.h>
40 #include <sys/types.h>
43 #include <sys/prctl.h>
48 #include "namespace.h"
54 #ifndef PR_SET_MM_ARG_START
55 #define PR_SET_MM_ARG_START 8
58 #ifndef PR_SET_MM_ARG_END
59 #define PR_SET_MM_ARG_END 9
62 #ifndef PR_SET_MM_ENV_START
63 #define PR_SET_MM_ENV_START 10
66 #ifndef PR_SET_MM_ENV_END
67 #define PR_SET_MM_ENV_END 11
70 lxc_log_define(lxc_utils
, lxc
);
73 * if path is btrfs, tries to remove it and any subvolumes beneath it
75 extern bool btrfs_try_remove_subvol(const char *path
);
77 static int _recursive_rmdir(char *dirname
, dev_t pdev
,
78 const char *exclude
, int level
, bool onedev
)
80 struct dirent dirent
, *direntp
;
83 char pathname
[MAXPATHLEN
];
84 bool hadexclude
= false;
86 dir
= opendir(dirname
);
88 ERROR("%s: failed to open %s", __func__
, dirname
);
92 while (!readdir_r(dir
, &dirent
, &direntp
)) {
99 if (!strcmp(direntp
->d_name
, ".") ||
100 !strcmp(direntp
->d_name
, ".."))
103 rc
= snprintf(pathname
, MAXPATHLEN
, "%s/%s", dirname
, direntp
->d_name
);
104 if (rc
< 0 || rc
>= MAXPATHLEN
) {
105 ERROR("pathname too long");
110 if (!level
&& exclude
&& !strcmp(direntp
->d_name
, exclude
)) {
111 ret
= rmdir(pathname
);
115 INFO("Not deleting snapshot %s", pathname
);
119 ret
= unlink(pathname
);
121 INFO("%s: failed to remove %s", __func__
, pathname
);
124 SYSERROR("%s: failed to rmdir %s", __func__
, pathname
);
132 ret
= lstat(pathname
, &mystat
);
134 ERROR("%s: failed to stat %s", __func__
, pathname
);
138 if (onedev
&& mystat
.st_dev
!= pdev
) {
139 /* TODO should we be checking /proc/self/mountinfo for
140 * pathname and not doing this if found? */
141 if (btrfs_try_remove_subvol(pathname
))
142 INFO("Removed btrfs subvolume at %s\n", pathname
);
145 if (S_ISDIR(mystat
.st_mode
)) {
146 if (_recursive_rmdir(pathname
, pdev
, exclude
, level
+1, onedev
) < 0)
149 if (unlink(pathname
) < 0) {
150 SYSERROR("%s: failed to delete %s", __func__
, pathname
);
156 if (rmdir(dirname
) < 0 && !btrfs_try_remove_subvol(dirname
) && !hadexclude
) {
157 ERROR("%s: failed to delete %s", __func__
, dirname
);
163 ERROR("%s: failed to close directory %s", __func__
, dirname
);
167 return failed
? -1 : 0;
170 /* we have two different magic values for overlayfs, yay */
171 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
172 #define OVERLAY_SUPER_MAGIC 0x794c7630
174 * In overlayfs, st_dev is unreliable. so on overlayfs we don't do
175 * the lxc_rmdir_onedev()
177 static bool is_native_overlayfs(const char *path
)
181 if (statfs(path
, &sb
) < 0)
183 if (sb
.f_type
== OVERLAYFS_SUPER_MAGIC
||
184 sb
.f_type
== OVERLAY_SUPER_MAGIC
)
189 /* returns 0 on success, -1 if there were any failures */
190 extern int lxc_rmdir_onedev(char *path
, const char *exclude
)
195 if (is_native_overlayfs(path
)) {
199 if (lstat(path
, &mystat
) < 0) {
200 ERROR("%s: failed to stat %s", __func__
, path
);
204 return _recursive_rmdir(path
, mystat
.st_dev
, exclude
, 0, onedev
);
207 /* borrowed from iproute2 */
208 extern int get_u16(unsigned short *val
, const char *arg
, int base
)
217 res
= strtoul(arg
, &ptr
, base
);
218 if (!ptr
|| ptr
== arg
|| *ptr
|| res
> 0xFFFF || errno
!= 0)
226 extern int mkdir_p(const char *dir
, mode_t mode
)
228 const char *tmp
= dir
;
229 const char *orig
= dir
;
233 dir
= tmp
+ strspn(tmp
, "/");
234 tmp
= dir
+ strcspn(dir
, "/");
235 makeme
= strndup(orig
, dir
- orig
);
237 if (mkdir(makeme
, mode
) && errno
!= EEXIST
) {
238 SYSERROR("failed to create directory '%s'", makeme
);
254 if (geteuid() == 0) {
255 rundir
= strdup(RUNTIME_PATH
);
259 rundir
= getenv("XDG_RUNTIME_DIR");
261 rundir
= strdup(rundir
);
265 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
266 homedir
= getenv("HOME");
268 ERROR("HOME isn't set in the environment.");
272 rundir
= malloc(sizeof(char) * (17 + strlen(homedir
)));
273 sprintf(rundir
, "%s/.cache/lxc/run/", homedir
);
278 int wait_for_pid(pid_t pid
)
283 ret
= waitpid(pid
, &status
, 0);
291 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
296 int lxc_wait_for_pid_status(pid_t pid
)
301 ret
= waitpid(pid
, &status
, 0);
312 ssize_t
lxc_write_nointr(int fd
, const void* buf
, size_t count
)
316 ret
= write(fd
, buf
, count
);
317 if (ret
< 0 && errno
== EINTR
)
322 ssize_t
lxc_read_nointr(int fd
, void* buf
, size_t count
)
326 ret
= read(fd
, buf
, count
);
327 if (ret
< 0 && errno
== EINTR
)
332 ssize_t
lxc_read_nointr_expect(int fd
, void* buf
, size_t count
, const void* expected_buf
)
335 ret
= lxc_read_nointr(fd
, buf
, count
);
338 if ((size_t)ret
!= count
)
340 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
348 #include <gnutls/gnutls.h>
349 #include <gnutls/crypto.h>
351 __attribute__((constructor
))
352 static void gnutls_lxc_init(void)
354 gnutls_global_init();
357 int sha1sum_file(char *fnam
, unsigned char *digest
)
366 f
= fopen_cloexec(fnam
, "r");
368 SYSERROR("Error opening template");
371 if (fseek(f
, 0, SEEK_END
) < 0) {
372 SYSERROR("Error seeking to end of template");
376 if ((flen
= ftell(f
)) < 0) {
377 SYSERROR("Error telling size of template");
381 if (fseek(f
, 0, SEEK_SET
) < 0) {
382 SYSERROR("Error seeking to start of template");
386 if ((buf
= malloc(flen
+1)) == NULL
) {
387 SYSERROR("Out of memory");
391 if (fread(buf
, 1, flen
, f
) != flen
) {
392 SYSERROR("Failure reading template");
398 SYSERROR("Failre closing template");
403 ret
= gnutls_hash_fast(GNUTLS_DIG_SHA1
, buf
, flen
, (void *)digest
);
409 char** lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
412 size_t count
= 1 + skip
;
415 /* first determine size of argument list, we don't want to reallocate
420 char* arg
= va_arg(ap2
, char*);
427 result
= calloc(count
, sizeof(char*));
432 char* arg
= va_arg(ap
, char*);
435 arg
= do_strdup
? strdup(arg
) : arg
;
438 result
[count
++] = arg
;
441 /* calloc has already set last element to NULL*/
449 const char** lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
451 return (const char**)lxc_va_arg_list_to_argv(ap
, skip
, 0);
454 extern struct lxc_popen_FILE
*lxc_popen(const char *command
)
456 struct lxc_popen_FILE
*fp
= NULL
;
457 int parent_end
= -1, child_end
= -1;
461 int r
= pipe2(pipe_fds
, O_CLOEXEC
);
464 ERROR("pipe2 failure");
468 parent_end
= pipe_fds
[0];
469 child_end
= pipe_fds
[1];
473 if (child_pid
== 0) {
475 int child_std_end
= STDOUT_FILENO
;
477 if (child_end
!= child_std_end
) {
478 /* dup2() doesn't dup close-on-exec flag */
479 dup2(child_end
, child_std_end
);
481 /* it's safe not to close child_end here
482 * as it's marked close-on-exec anyway
486 * The descriptor is already the one we will use.
487 * But it must not be marked close-on-exec.
490 if (fcntl(child_end
, F_SETFD
, 0) != 0) {
491 SYSERROR("Failed to remove FD_CLOEXEC from fd.");
498 * This is the main/only reason
499 * why we do our lousy popen() emulation.
504 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
507 execl("/bin/sh", "sh", "-c", command
, (char *) NULL
);
517 ERROR("fork failure");
521 fp
= calloc(1, sizeof(*fp
));
523 ERROR("failed to allocate memory");
527 fp
->f
= fdopen(parent_end
, "r");
529 ERROR("fdopen failure");
533 fp
->child_pid
= child_pid
;
542 parent_end
= -1; /* so we do not close it second time */
548 if (parent_end
!= -1)
554 extern int lxc_pclose(struct lxc_popen_FILE
*fp
)
563 child_pid
= fp
->child_pid
;
564 /* free memory (we still need to close file stream) */
569 if (!f
|| fclose(f
)) {
570 ERROR("fclose failure");
575 wait_pid
= waitpid(child_pid
, &wstatus
, 0);
576 } while (wait_pid
== -1 && errno
== EINTR
);
578 if (wait_pid
== -1) {
579 ERROR("waitpid failure");
586 char *lxc_string_replace(const char *needle
, const char *replacement
, const char *haystack
)
588 ssize_t len
= -1, saved_len
= -1;
590 size_t replacement_len
= strlen(replacement
);
591 size_t needle_len
= strlen(needle
);
593 /* should be executed exactly twice */
594 while (len
== -1 || result
== NULL
) {
600 result
= calloc(1, len
+ 1);
608 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
; last_p
= p
, p
= strstr(last_p
, needle
)) {
609 part_len
= (ssize_t
)(p
- last_p
);
610 if (result
&& part_len
> 0)
611 memcpy(&result
[len
], last_p
, part_len
);
613 if (result
&& replacement_len
> 0)
614 memcpy(&result
[len
], replacement
, replacement_len
);
615 len
+= replacement_len
;
618 part_len
= strlen(last_p
);
619 if (result
&& part_len
> 0)
620 memcpy(&result
[len
], last_p
, part_len
);
624 /* make sure we did the same thing twice,
625 * once for calculating length, the other
626 * time for copying data */
627 assert(saved_len
== len
);
628 /* make sure we didn't overwrite any buffer,
629 * due to calloc the string should be 0-terminated */
630 assert(result
[len
] == '\0');
635 bool lxc_string_in_array(const char *needle
, const char **haystack
)
637 for (; haystack
&& *haystack
; haystack
++)
638 if (!strcmp(needle
, *haystack
))
643 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
647 size_t sep_len
= strlen(sep
);
648 size_t result_len
= use_as_prefix
* sep_len
;
650 /* calculate new string length */
651 for (p
= (char **)parts
; *p
; p
++)
652 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
654 result
= calloc(result_len
+ 1, 1);
660 for (p
= (char **)parts
; *p
; p
++) {
661 if (p
> (char **)parts
)
669 char **lxc_normalize_path(const char *path
)
673 size_t components_len
= 0;
676 components
= lxc_string_split(path
, '/');
679 for (p
= components
; *p
; p
++)
682 /* resolve '.' and '..' */
683 for (pos
= 0; pos
< components_len
; ) {
684 if (!strcmp(components
[pos
], ".") || (!strcmp(components
[pos
], "..") && pos
== 0)) {
685 /* eat this element */
686 free(components
[pos
]);
687 memmove(&components
[pos
], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
689 } else if (!strcmp(components
[pos
], "..")) {
690 /* eat this and the previous element */
691 free(components
[pos
- 1]);
692 free(components
[pos
]);
693 memmove(&components
[pos
-1], &components
[pos
+1], sizeof(char *) * (components_len
- pos
));
704 char *lxc_append_paths(const char *first
, const char *second
)
706 size_t len
= strlen(first
) + strlen(second
) + 1;
707 const char *pattern
= "%s%s";
710 if (second
[0] != '/') {
715 result
= calloc(1, len
);
719 snprintf(result
, len
, pattern
, first
, second
);
723 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
725 char *token
, *str
, *saveptr
= NULL
;
726 char sep
[2] = { _sep
, '\0' };
728 if (!haystack
|| !needle
)
731 str
= alloca(strlen(haystack
)+1);
732 strcpy(str
, haystack
);
733 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
734 if (strcmp(needle
, token
) == 0)
741 char **lxc_string_split(const char *string
, char _sep
)
743 char *token
, *str
, *saveptr
= NULL
;
744 char sep
[2] = { _sep
, '\0' };
745 char **result
= NULL
;
746 size_t result_capacity
= 0;
747 size_t result_count
= 0;
751 return calloc(1, sizeof(char *));
753 str
= alloca(strlen(string
)+1);
755 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
756 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
759 result
[result_count
] = strdup(token
);
760 if (!result
[result_count
])
765 /* if we allocated too much, reduce it */
766 return realloc(result
, (result_count
+ 1) * sizeof(char *));
769 lxc_free_array((void **)result
, free
);
774 char **lxc_string_split_and_trim(const char *string
, char _sep
)
776 char *token
, *str
, *saveptr
= NULL
;
777 char sep
[2] = { _sep
, '\0' };
778 char **result
= NULL
;
779 size_t result_capacity
= 0;
780 size_t result_count
= 0;
785 return calloc(1, sizeof(char *));
787 str
= alloca(strlen(string
)+1);
789 for (; (token
= strtok_r(str
, sep
, &saveptr
)); str
= NULL
) {
790 while (token
[0] == ' ' || token
[0] == '\t')
793 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
797 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
800 result
[result_count
] = strdup(token
);
801 if (!result
[result_count
])
806 /* if we allocated too much, reduce it */
807 return realloc(result
, (result_count
+ 1) * sizeof(char *));
810 lxc_free_array((void **)result
, free
);
815 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
818 for (p
= array
; p
&& *p
; p
++)
823 int lxc_grow_array(void ***array
, size_t* capacity
, size_t new_size
, size_t capacity_increment
)
828 /* first time around, catch some trivial mistakes of the user
829 * only initializing one of these */
830 if (!*array
|| !*capacity
) {
835 new_capacity
= *capacity
;
836 while (new_size
+ 1 > new_capacity
)
837 new_capacity
+= capacity_increment
;
838 if (new_capacity
!= *capacity
) {
839 /* we have to reallocate */
840 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
843 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
845 *capacity
= new_capacity
;
848 /* array has sufficient elements */
852 size_t lxc_array_len(void **array
)
857 for (p
= array
; p
&& *p
; p
++)
863 int lxc_write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
868 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
871 ret
= lxc_write_nointr(fd
, buf
, count
);
874 if ((size_t)ret
!= count
)
877 ret
= lxc_write_nointr(fd
, "\n", 1);
891 int lxc_read_from_file(const char *filename
, void* buf
, size_t count
)
893 int fd
= -1, saved_errno
;
896 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
900 if (!buf
|| !count
) {
903 while ((ret
= read(fd
, buf2
, 100)) > 0)
908 memset(buf
, 0, count
);
909 ret
= read(fd
, buf
, count
);
913 ERROR("read %s: %s", filename
, strerror(errno
));
921 void **lxc_append_null_to_array(void **array
, size_t count
)
925 /* Append NULL to the array */
927 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
930 for (i
= 0; i
< count
; i
++)
941 int randseed(bool srand_it
)
944 srand pre-seed function based on /dev/urandom
946 unsigned int seed
=time(NULL
)+getpid();
949 f
= fopen("/dev/urandom", "r");
951 int ret
= fread(&seed
, sizeof(seed
), 1, f
);
953 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno
));
963 uid_t
get_ns_uid(uid_t orig
)
967 uid_t nsid
, hostid
, range
;
968 FILE *f
= fopen("/proc/self/uid_map", "r");
972 while (getline(&line
, &sz
, f
) != -1) {
973 if (sscanf(line
, "%u %u %u", &nsid
, &hostid
, &range
) != 3)
975 if (hostid
<= orig
&& hostid
+ range
> orig
) {
976 nsid
+= orig
- hostid
;
988 bool dir_exists(const char *path
)
993 ret
= stat(path
, &sb
);
995 // could be something other than eexist, just say no
997 return S_ISDIR(sb
.st_mode
);
1000 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1001 * FNV has good anti collision properties and we're not worried
1002 * about pre-image resistance or one-way-ness, we're just trying to make
1003 * the name unique in the 108 bytes of space we have.
1005 uint64_t fnv_64a_buf(void *buf
, size_t len
, uint64_t hval
)
1009 for(bp
= buf
; bp
< (unsigned char *)buf
+ len
; bp
++)
1011 /* xor the bottom with the current octet */
1012 hval
^= (uint64_t)*bp
;
1015 * multiply by the 64 bit FNV magic prime mod 2^64
1017 hval
+= (hval
<< 1) + (hval
<< 4) + (hval
<< 5) +
1018 (hval
<< 7) + (hval
<< 8) + (hval
<< 40);
1025 * Detect whether / is mounted MS_SHARED. The only way I know of to
1026 * check that is through /proc/self/mountinfo.
1027 * I'm only checking for /. If the container rootfs or mount location
1028 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1029 * out would be too much work to be worth it.
1031 #define LINELEN 4096
1032 int detect_shared_rootfs(void)
1034 char buf
[LINELEN
], *p
;
1039 f
= fopen("/proc/self/mountinfo", "r");
1042 while (fgets(buf
, LINELEN
, f
)) {
1043 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1044 p
= strchr(p
+1, ' ');
1047 p2
= strchr(p
+1, ' ');
1051 if (strcmp(p
+1, "/") == 0) {
1052 // this is '/'. is it shared?
1053 p
= strchr(p2
+1, ' ');
1054 if (p
&& strstr(p
, "shared:")) {
1064 bool switch_to_ns(pid_t pid
, const char *ns
) {
1066 char nspath
[MAXPATHLEN
];
1068 /* Switch to new ns */
1069 ret
= snprintf(nspath
, MAXPATHLEN
, "/proc/%d/ns/%s", pid
, ns
);
1070 if (ret
< 0 || ret
>= MAXPATHLEN
)
1073 fd
= open(nspath
, O_RDONLY
);
1075 SYSERROR("failed to open %s", nspath
);
1081 SYSERROR("failed to set process %d to %s of %d.", pid
, ns
, fd
);
1090 * looking at fs/proc_namespace.c, it appears we can
1091 * actually expect the rootfs entry to very specifically contain
1092 * " - rootfs rootfs "
1093 * IIUC, so long as we've chrooted so that rootfs is not our root,
1094 * the rootfs entry should always be skipped in mountinfo contents.
1096 int detect_ramfs_rootfs(void)
1098 char buf
[LINELEN
], *p
;
1103 f
= fopen("/proc/self/mountinfo", "r");
1106 while (fgets(buf
, LINELEN
, f
)) {
1107 for (p
= buf
, i
=0; p
&& i
< 4; i
++)
1108 p
= strchr(p
+1, ' ');
1111 p2
= strchr(p
+1, ' ');
1115 if (strcmp(p
+1, "/") == 0) {
1116 // this is '/'. is it the ramfs?
1117 p
= strchr(p2
+1, '-');
1118 if (p
&& strncmp(p
, "- rootfs rootfs ", 16) == 0) {
1128 char *on_path(char *cmd
, const char *rootfs
) {
1131 char *saveptr
= NULL
;
1132 char cmdpath
[MAXPATHLEN
];
1135 path
= getenv("PATH");
1139 path
= strdup(path
);
1143 entry
= strtok_r(path
, ":", &saveptr
);
1146 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s/%s", rootfs
, entry
, cmd
);
1148 ret
= snprintf(cmdpath
, MAXPATHLEN
, "%s/%s", entry
, cmd
);
1150 if (ret
< 0 || ret
>= MAXPATHLEN
)
1153 if (access(cmdpath
, X_OK
) == 0) {
1155 return strdup(cmdpath
);
1159 entry
= strtok_r(NULL
, ":", &saveptr
);
1166 bool file_exists(const char *f
)
1168 struct stat statbuf
;
1170 return stat(f
, &statbuf
) == 0;
1173 /* historically lxc-init has been under /usr/lib/lxc and under
1174 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1176 char *choose_init(const char *rootfs
)
1179 const char *empty
= "",
1181 int ret
, env_set
= 0;
1184 if (!getenv("PATH")) {
1185 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1186 SYSERROR("Failed to setenv");
1190 retv
= on_path("init.lxc", rootfs
);
1193 if (unsetenv("PATH"))
1194 SYSERROR("Failed to unsetenv");
1200 retv
= malloc(PATH_MAX
);
1209 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, SBINDIR
, "/init.lxc");
1210 if (ret
< 0 || ret
>= PATH_MAX
) {
1211 ERROR("pathname too long");
1215 ret
= stat(retv
, &mystat
);
1219 ret
= snprintf(retv
, PATH_MAX
, "%s/%s/%s", tmp
, LXCINITDIR
, "/lxc/lxc-init");
1220 if (ret
< 0 || ret
>= PATH_MAX
) {
1221 ERROR("pathname too long");
1225 ret
= stat(retv
, &mystat
);
1229 ret
= snprintf(retv
, PATH_MAX
, "%s/usr/lib/lxc/lxc-init", tmp
);
1230 if (ret
< 0 || ret
>= PATH_MAX
) {
1231 ERROR("pathname too long");
1234 ret
= stat(retv
, &mystat
);
1238 ret
= snprintf(retv
, PATH_MAX
, "%s/sbin/lxc-init", tmp
);
1239 if (ret
< 0 || ret
>= PATH_MAX
) {
1240 ERROR("pathname too long");
1243 ret
= stat(retv
, &mystat
);
1248 * Last resort, look for the statically compiled init.lxc which we
1249 * hopefully bind-mounted in.
1250 * If we are called during container setup, and we get to this point,
1251 * then the init.lxc.static from the host will need to be bind-mounted
1252 * in. So we return NULL here to indicate that.
1257 ret
= snprintf(retv
, PATH_MAX
, "/init.lxc.static");
1258 if (ret
< 0 || ret
>= PATH_MAX
) {
1259 WARN("Nonsense - name /lxc.init.static too long");
1262 ret
= stat(retv
, &mystat
);
1271 int print_to_file(const char *file
, const char *content
)
1276 f
= fopen(file
, "w");
1279 if (fprintf(f
, "%s", content
) != strlen(content
))
1285 int is_dir(const char *path
)
1287 struct stat statbuf
;
1288 int ret
= stat(path
, &statbuf
);
1289 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
1295 * Given the '-t' template option to lxc-create, figure out what to
1296 * do. If the template is a full executable path, use that. If it
1297 * is something like 'sshd', then return $templatepath/lxc-sshd.
1298 * On success return the template, on error return NULL.
1300 char *get_template_path(const char *t
)
1305 if (t
[0] == '/' && access(t
, X_OK
) == 0) {
1310 len
= strlen(LXCTEMPLATEDIR
) + strlen(t
) + strlen("/lxc-") + 1;
1311 tpath
= malloc(len
);
1314 ret
= snprintf(tpath
, len
, "%s/lxc-%s", LXCTEMPLATEDIR
, t
);
1315 if (ret
< 0 || ret
>= len
) {
1319 if (access(tpath
, X_OK
) < 0) {
1320 SYSERROR("bad template: %s", t
);
1329 * Sets the process title to the specified title. Note:
1330 * 1. this function requires root to succeed
1331 * 2. it clears /proc/self/environ
1332 * 3. it may not succed (e.g. if title is longer than /proc/self/environ +
1333 * the original title)
1335 int setproctitle(char *title
)
1337 char buf
[2048], *tmp
;
1339 int i
, len
, ret
= 0;
1340 unsigned long arg_start
, arg_end
, env_start
, env_end
;
1342 f
= fopen_cloexec("/proc/self/stat", "r");
1347 tmp
= fgets(buf
, sizeof(buf
), f
);
1353 /* Skip the first 47 fields, column 48-51 are ARG_START and
1355 tmp
= strchr(buf
, ' ');
1356 for (i
= 0; i
< 46; i
++) {
1359 tmp
= strchr(tmp
+1, ' ');
1365 i
= sscanf(tmp
, "%lu %lu %lu %lu", &arg_start
, &arg_end
, &env_start
, &env_end
);
1370 /* Include the null byte here, because in the calculations below we
1371 * want to have room for it. */
1372 len
= strlen(title
) + 1;
1374 /* We're truncating the environment, so we should use at most the
1375 * length of the argument + environment for the title. */
1376 if (len
> env_end
- arg_start
) {
1378 len
= env_end
- arg_start
;
1380 /* Only truncate the environment if we're actually going to
1381 * overwrite part of it. */
1382 if (len
>= arg_end
- arg_start
) {
1383 env_start
= env_end
;
1386 arg_end
= arg_start
+ len
;
1388 /* check overflow */
1389 if (arg_end
< len
|| arg_end
< arg_start
) {
1395 strcpy((char*)arg_start
, title
);
1397 ret
|= prctl(PR_SET_MM
, PR_SET_MM_ARG_START
, arg_start
, 0, 0);
1398 ret
|= prctl(PR_SET_MM
, PR_SET_MM_ARG_END
, arg_end
, 0, 0);
1399 ret
|= prctl(PR_SET_MM
, PR_SET_MM_ENV_START
, env_start
, 0, 0);
1400 ret
|= prctl(PR_SET_MM
, PR_SET_MM_ENV_END
, env_end
, 0, 0);