[lxc-devel] [lxd/master] forkexec: tweak
brauner on Github
lxc-bot at linuxcontainers.org
Sun Mar 29 11:33:22 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200329/a1a55e0c/attachment.bin>
-------------- next part --------------
From ea97c203053b51e3a1989e958a89c4df046b38dc Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sun, 29 Mar 2020 13:27:26 +0200
Subject: [PATCH] forkexec: tweak
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
lxd/include/macro.h | 13 ++++++++++
lxd/main_forkexec.go | 57 +++++++++++++++++++-------------------------
2 files changed, 38 insertions(+), 32 deletions(-)
diff --git a/lxd/include/macro.h b/lxd/include/macro.h
index 7ff60c385d..ece46bc2c7 100644
--- a/lxd/include/macro.h
+++ b/lxd/include/macro.h
@@ -255,4 +255,17 @@ enum {
__internal_fd__; \
})
+#define ret_errno(__errno__) \
+ ({ \
+ errno = (__errno__); \
+ -(__errno__); \
+ })
+
+#define log_error(__ret__, format, ...) \
+ ({ \
+ typeof(__ret__) __internal_ret__ = (__ret__); \
+ fprintf(stderr, format "\n", ##__VA_ARGS__); \
+ __internal_ret__; \
+ })
+
#endif /* __LXC_MACRO_H */
diff --git a/lxd/main_forkexec.go b/lxd/main_forkexec.go
index 6bc60daf3e..f63fce0eb6 100644
--- a/lxd/main_forkexec.go
+++ b/lxd/main_forkexec.go
@@ -22,6 +22,7 @@ import (
#include <unistd.h>
#include <limits.h>
+#include "include/macro.h"
#include "include/memory_utils.h"
#include <lxc/attach_options.h>
#include <lxc/lxccontainer.h>
@@ -63,52 +64,40 @@ again:
return status;
}
-static char *must_copy_string(const char *entry)
-{
- char *ret;
-
- if (!entry)
- return NULL;
-
- do {
- ret = strdup(entry);
- } while (!ret);
-
- return ret;
-}
-
-static void *must_realloc(void *orig, size_t sz)
-{
- void *ret;
-
- do {
- ret = realloc(orig, sz);
- } while (!ret);
-
- return ret;
-}
-
static int append_null_to_list(void ***list)
{
int newentry = 0;
+ void **new_list;
if (*list)
for (; (*list)[newentry]; newentry++)
;
- *list = must_realloc(*list, (newentry + 2) * sizeof(void **));
+ new_list = realloc(*list, (newentry + 2) * sizeof(void **));
+ if (!new_list)
+ return ret_errno(ENOMEM);
+
+ *list = new_list;
(*list)[newentry + 1] = NULL;
return newentry;
}
-static void must_append_string(char ***list, char *entry)
+static int push_vargs(char ***list, char *entry)
{
+ __do_free char *copy = NULL;
int newentry;
- char *copy;
+
+ copy = strdup(entry);
+ if (!copy)
+ return ret_errno(ENOMEM);
newentry = append_null_to_list((void ***)list);
- copy = must_copy_string(entry);
- (*list)[newentry] = copy;
+ if (newentry < 0)
+ return newentry;
+
+ (*list)[newentry] = move_ptr(copy);
+
+ return 0;
}
// We use a separate function because cleanup macros are called during stack
@@ -167,9 +156,13 @@ __attribute__ ((noinline)) static int __forkexec(void)
if (!strcmp(section, "env")) {
if (!strncmp(arg, "HOME=", STRLITERALLEN("HOME=")))
attach_options.initial_cwd = arg + STRLITERALLEN("HOME=");
- must_append_string(&envvp, arg);
+ ret = push_vargs(&envvp, arg);
+ if (ret < 0)
+ return log_error(ret, "Failed to add %s to env array", arg);
} else if (!strcmp(section, "cmd")) {
- must_append_string(&argvp, arg);
+ ret = push_vargs(&argvp, arg);
+ if (ret < 0)
+ return log_error(ret, "Failed to add %s to arg array", arg);
} else {
fprintf(stderr, "Invalid exec section %s\n", section);
return EXIT_FAILURE;
More information about the lxc-devel
mailing list