From: Christian Brauner Date: Tue, 10 Oct 2017 14:53:25 +0000 (+0200) Subject: init: rework dumb init X-Git-Tag: lxc-2.0.10~508 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=228aeaca212e9088dc6dc0405bec338aa779637a;p=thirdparty%2Flxc.git init: rework dumb init Signed-off-by: Christian Brauner --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 7df56f54f..a3ff3c955 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -289,8 +289,7 @@ endif if HAVE_STATIC_LIBCAP sbin_PROGRAMS += init.lxc.static -init_lxc_static_SOURCES = lxc_init.c error.c log.c initutils.c caps.c \ - tools/arguments.c +init_lxc_static_SOURCES = lxc_init.c error.c log.c initutils.c caps.c if !HAVE_GETLINE if HAVE_FGETLN diff --git a/src/lxc/initutils.c b/src/lxc/initutils.c index c190d6d50..12c33630b 100644 --- a/src/lxc/initutils.c +++ b/src/lxc/initutils.c @@ -21,6 +21,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + #include "initutils.h" #include "log.h" @@ -296,3 +298,107 @@ FILE *fopen_cloexec(const char *path, const char *mode) errno = saved_errno; return ret; } + +/* + * Sets the process title to the specified title. Note that this may fail if + * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18). + */ +int setproctitle(char *title) +{ + static char *proctitle = NULL; + char buf[2048], *tmp; + FILE *f; + int i, len, ret = 0; + + /* We don't really need to know all of this stuff, but unfortunately + * PR_SET_MM_MAP requires us to set it all at once, so we have to + * figure it out anyway. + */ + unsigned long start_data, end_data, start_brk, start_code, end_code, + start_stack, arg_start, arg_end, env_start, env_end, + brk_val; + struct prctl_mm_map prctl_map; + + f = fopen_cloexec("/proc/self/stat", "r"); + if (!f) { + return -1; + } + + tmp = fgets(buf, sizeof(buf), f); + fclose(f); + if (!tmp) { + return -1; + } + + /* Skip the first 25 fields, column 26-28 are start_code, end_code, + * and start_stack */ + tmp = strchr(buf, ' '); + for (i = 0; i < 24; i++) { + if (!tmp) + return -1; + tmp = strchr(tmp+1, ' '); + } + if (!tmp) + return -1; + + i = sscanf(tmp, "%lu %lu %lu", &start_code, &end_code, &start_stack); + if (i != 3) + return -1; + + /* Skip the next 19 fields, column 45-51 are start_data to arg_end */ + for (i = 0; i < 19; i++) { + if (!tmp) + return -1; + tmp = strchr(tmp+1, ' '); + } + + if (!tmp) + return -1; + + i = sscanf(tmp, "%lu %lu %lu %*u %*u %lu %lu", + &start_data, + &end_data, + &start_brk, + &env_start, + &env_end); + if (i != 5) + return -1; + + /* Include the null byte here, because in the calculations below we + * want to have room for it. */ + len = strlen(title) + 1; + + proctitle = realloc(proctitle, len); + if (!proctitle) + return -1; + + arg_start = (unsigned long) proctitle; + arg_end = arg_start + len; + + brk_val = syscall(__NR_brk, 0); + + prctl_map = (struct prctl_mm_map) { + .start_code = start_code, + .end_code = end_code, + .start_stack = start_stack, + .start_data = start_data, + .end_data = end_data, + .start_brk = start_brk, + .brk = brk_val, + .arg_start = arg_start, + .arg_end = arg_end, + .env_start = env_start, + .env_end = env_end, + .auxv = NULL, + .auxv_size = 0, + .exe_fd = -1, + }; + + ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0); + if (ret == 0) + strcpy((char*)arg_start, title); + else + INFO("setting cmdline failed - %s", strerror(errno)); + + return ret; +} diff --git a/src/lxc/initutils.h b/src/lxc/initutils.h index c021fd617..ec44554e3 100644 --- a/src/lxc/initutils.h +++ b/src/lxc/initutils.h @@ -25,17 +25,16 @@ #define __LXC_INITUTILS_H #include -#include +#include +#include #include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include - #include "config.h" @@ -44,11 +43,37 @@ #define DEFAULT_ZFSROOT "lxc" #define DEFAULT_RBDPOOL "lxc" +#ifndef PR_SET_MM +#define PR_SET_MM 35 +#endif + +#ifndef PR_SET_MM_MAP +#define PR_SET_MM_MAP 14 + +struct prctl_mm_map { + uint64_t start_code; + uint64_t end_code; + uint64_t start_data; + uint64_t end_data; + uint64_t start_brk; + uint64_t brk; + uint64_t start_stack; + uint64_t arg_start; + uint64_t arg_end; + uint64_t env_start; + uint64_t env_end; + uint64_t *auxv; + uint32_t auxv_size; + uint32_t exe_fd; +}; +#endif + extern void lxc_setup_fs(void); extern const char *lxc_global_config_value(const char *option_name); /* open a file with O_CLOEXEC */ extern void remove_trailing_slashes(char *p); -FILE *fopen_cloexec(const char *path, const char *mode); +extern FILE *fopen_cloexec(const char *path, const char *mode); +extern int setproctitle(char *title); #endif /* __LXC_INITUTILS_H */ diff --git a/src/lxc/lxc_init.c b/src/lxc/lxc_init.c index 55d43f809..823dc1850 100644 --- a/src/lxc/lxc_init.c +++ b/src/lxc/lxc_init.c @@ -36,7 +36,6 @@ #include #include "log.h" -#include "arguments.h" #include "error.h" #include "initutils.h" diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 97d34fa9c..e63660055 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -48,6 +48,7 @@ #include "confile.h" #include "console.h" #include "criu.h" +#include "initutils.h" #include "log.h" #include "lxc.h" #include "lxccontainer.h" diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 148f23429..9ea467d3c 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -50,31 +50,6 @@ #include "parse.h" #include "utils.h" -#ifndef PR_SET_MM -#define PR_SET_MM 35 -#endif - -#ifndef PR_SET_MM_MAP -#define PR_SET_MM_MAP 14 - -struct prctl_mm_map { - uint64_t start_code; - uint64_t end_code; - uint64_t start_data; - uint64_t end_data; - uint64_t start_brk; - uint64_t brk; - uint64_t start_stack; - uint64_t arg_start; - uint64_t arg_end; - uint64_t env_start; - uint64_t env_end; - uint64_t *auxv; - uint32_t auxv_size; - uint32_t exe_fd; -}; -#endif - #ifndef O_PATH #define O_PATH 010000000 #endif @@ -1371,110 +1346,6 @@ char *get_template_path(const char *t) return tpath; } -/* - * Sets the process title to the specified title. Note that this may fail if - * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18). - */ -int setproctitle(char *title) -{ - static char *proctitle = NULL; - char buf[2048], *tmp; - FILE *f; - int i, len, ret = 0; - - /* We don't really need to know all of this stuff, but unfortunately - * PR_SET_MM_MAP requires us to set it all at once, so we have to - * figure it out anyway. - */ - unsigned long start_data, end_data, start_brk, start_code, end_code, - start_stack, arg_start, arg_end, env_start, env_end, - brk_val; - struct prctl_mm_map prctl_map; - - f = fopen_cloexec("/proc/self/stat", "r"); - if (!f) { - return -1; - } - - tmp = fgets(buf, sizeof(buf), f); - fclose(f); - if (!tmp) { - return -1; - } - - /* Skip the first 25 fields, column 26-28 are start_code, end_code, - * and start_stack */ - tmp = strchr(buf, ' '); - for (i = 0; i < 24; i++) { - if (!tmp) - return -1; - tmp = strchr(tmp+1, ' '); - } - if (!tmp) - return -1; - - i = sscanf(tmp, "%lu %lu %lu", &start_code, &end_code, &start_stack); - if (i != 3) - return -1; - - /* Skip the next 19 fields, column 45-51 are start_data to arg_end */ - for (i = 0; i < 19; i++) { - if (!tmp) - return -1; - tmp = strchr(tmp+1, ' '); - } - - if (!tmp) - return -1; - - i = sscanf(tmp, "%lu %lu %lu %*u %*u %lu %lu", - &start_data, - &end_data, - &start_brk, - &env_start, - &env_end); - if (i != 5) - return -1; - - /* Include the null byte here, because in the calculations below we - * want to have room for it. */ - len = strlen(title) + 1; - - proctitle = realloc(proctitle, len); - if (!proctitle) - return -1; - - arg_start = (unsigned long) proctitle; - arg_end = arg_start + len; - - brk_val = syscall(__NR_brk, 0); - - prctl_map = (struct prctl_mm_map) { - .start_code = start_code, - .end_code = end_code, - .start_stack = start_stack, - .start_data = start_data, - .end_data = end_data, - .start_brk = start_brk, - .brk = brk_val, - .arg_start = arg_start, - .arg_end = arg_end, - .env_start = env_start, - .env_end = env_end, - .auxv = NULL, - .auxv_size = 0, - .exe_fd = -1, - }; - - ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0); - if (ret == 0) - strcpy((char*)arg_start, title); - else - INFO("setting cmdline failed - %s", strerror(errno)); - - return ret; -} - /* * @path: a pathname where / replaced with '\0'. * @offsetp: pointer to int showing which path segment was last seen. diff --git a/src/lxc/utils.h b/src/lxc/utils.h index a92edafce..6999eed41 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -442,7 +442,6 @@ int print_to_file(const char *file, const char *content); bool switch_to_ns(pid_t pid, const char *ns); int is_dir(const char *path); char *get_template_path(const char *t); -int setproctitle(char *title); int safe_mount(const char *src, const char *dest, const char *fstype, unsigned long flags, const void *data, const char *rootfs); int lxc_mount_proc_if_needed(const char *rootfs);