]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
init: rework dumb init
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 10 Oct 2017 14:53:25 +0000 (16:53 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 17 Dec 2017 15:20:53 +0000 (16:20 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/Makefile.am
src/lxc/initutils.c
src/lxc/initutils.h
src/lxc/lxc_init.c
src/lxc/lxccontainer.c
src/lxc/utils.c
src/lxc/utils.h

index 7df56f54f2bdb32899bccd29b8a62d1a270a8c44..a3ff3c955cd2a1a4c3a17d8ff5db181ec1c31aeb 100644 (file)
@@ -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
index c190d6d50a6905619a8b5913929357c9cd1ab02b..12c33630b14a44e0ff7f318557770b0eed0f783b 100644 (file)
@@ -21,6 +21,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include <sys/prctl.h>
+
 #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;
+}
index c021fd6172d4d35e88b5d99de595dd6cf49553a1..ec44554e3cb6802a7e290b3527a732e2bca1991e 100644 (file)
 #define __LXC_INITUTILS_H
 
 #include <errno.h>
-#include <stdio.h>
+#include <fcntl.h>
+#include <inttypes.h>
 #include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mount.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mount.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <fcntl.h>
-
 
 #include "config.h"
 
 #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 */
index 55d43f809628f5a6b12b134f42496fccdc3e4de6..823dc18508a5ecb6eac8a571f788b178010897bf 100644 (file)
@@ -36,7 +36,6 @@
 #include <lxc/lxccontainer.h>
 
 #include "log.h"
-#include "arguments.h"
 #include "error.h"
 #include "initutils.h"
 
index 97d34fa9c00e2ee8ac31122d49e9e8bdbfb8dd87..e6366005526db32f9d8eb5c1345cabbe15222989 100644 (file)
@@ -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"
index 148f23429e7c7fcae6e494a3c96c13567cdf8ef5..9ea467d3cc2847c20a8496d4141cffccfe2438a5 100644 (file)
 #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.
index a92edafce644ff27887754faf34a03119b8cea86..6999eed41f908c801bb37a314e6a1d1bac1f8929 100644 (file)
@@ -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);