[lxc-devel] [lxc/master] tree-wide: cleanup macros

brauner on Github lxc-bot at linuxcontainers.org
Sat Feb 9 10:47:08 UTC 2019


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/20190209/4bba10af/attachment.bin>
-------------- next part --------------
From 319db95f93473357459f7cdae18f918baa52ccc1 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 11:32:45 +0100
Subject: [PATCH 1/3] start: move variable into tighter scope

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/start.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/lxc/start.c b/src/lxc/start.c
index 89262c736..928702559 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -452,7 +452,6 @@ int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
 	size_t retlen;
 	ssize_t ret;
 	struct lxc_list *cur, *next;
-	struct lxc_state_client *client;
 	struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
 
 	if (state == THAWED)
@@ -472,7 +471,7 @@ int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
 		return -E2BIG;
 
 	lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
-		client = cur->elem;
+		struct lxc_state_client *client = cur->elem;
 
 		if (client->states[state] == 0) {
 			TRACE("State %s not registered for state client %d",

From 08c1c9aebf3e6f9b5088c0ad6c7afbf43fb8e0dd Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 11:36:18 +0100
Subject: [PATCH 2/3] af_unix: use __do_free

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/af_unix.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/lxc/af_unix.c b/src/lxc/af_unix.c
index 02f32c454..3b00d64fb 100644
--- a/src/lxc/af_unix.c
+++ b/src/lxc/af_unix.c
@@ -37,6 +37,7 @@
 
 #include "config.h"
 #include "log.h"
+#include "memory_utils.h"
 #include "raw_syscalls.h"
 #include "utils.h"
 
@@ -155,12 +156,11 @@ int lxc_abstract_unix_connect(const char *path)
 int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds,
 			       void *data, size_t size)
 {
-	int ret;
+	__do_free char *cmsgbuf;
 	struct msghdr msg;
 	struct iovec iov;
 	struct cmsghdr *cmsg = NULL;
 	char buf[1] = {0};
-	char *cmsgbuf;
 	size_t cmsgbufsize = CMSG_SPACE(num_sendfds * sizeof(int));
 
 	memset(&msg, 0, sizeof(msg));
@@ -189,20 +189,18 @@ int lxc_abstract_unix_send_fds(int fd, int *sendfds, int num_sendfds,
 	msg.msg_iov = &iov;
 	msg.msg_iovlen = 1;
 
-	ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
-	free(cmsgbuf);
-	return ret;
+	return sendmsg(fd, &msg, MSG_NOSIGNAL);
 }
 
 int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds,
 			       void *data, size_t size)
 {
+	__do_free char *cmsgbuf;
 	int ret;
 	struct msghdr msg;
 	struct iovec iov;
 	struct cmsghdr *cmsg = NULL;
 	char buf[1] = {0};
-	char *cmsgbuf;
 	size_t cmsgbufsize = CMSG_SPACE(num_recvfds * sizeof(int));
 
 	memset(&msg, 0, sizeof(msg));
@@ -234,7 +232,6 @@ int lxc_abstract_unix_recv_fds(int fd, int *recvfds, int num_recvfds,
 		memcpy(recvfds, CMSG_DATA(cmsg), num_recvfds * sizeof(int));
 
 out:
-	free(cmsgbuf);
 	return ret;
 }
 

From a254779f21bc8e5ce85be183339262d5f63355f6 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 11:46:15 +0100
Subject: [PATCH 3/3] attach: use __do_free

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/attach.c | 67 ++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 45 deletions(-)

diff --git a/src/lxc/attach.c b/src/lxc/attach.c
index df9dda02e..a6e987526 100644
--- a/src/lxc/attach.c
+++ b/src/lxc/attach.c
@@ -59,6 +59,7 @@
 #include "lxcseccomp.h"
 #include "macro.h"
 #include "mainloop.h"
+#include "memory_utils.h"
 #include "namespace.h"
 #include "raw_syscalls.h"
 #include "syscall_wrappers.h"
@@ -76,31 +77,28 @@ static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_D
 
 static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
 {
+	__do_free char *line = NULL;
+	__do_fclose FILE *proc_file = NULL;
 	int ret;
 	bool found;
-	FILE *proc_file;
 	char proc_fn[LXC_PROC_STATUS_LEN];
+	struct lxc_proc_context_info *info;
 	size_t line_bufsz = 0;
-	char *line = NULL;
-	struct lxc_proc_context_info *info = NULL;
 
 	/* Read capabilities. */
 	ret = snprintf(proc_fn, LXC_PROC_STATUS_LEN, "/proc/%d/status", pid);
 	if (ret < 0 || ret >= LXC_PROC_STATUS_LEN)
-		goto on_error;
+		return NULL;
 
 	proc_file = fopen(proc_fn, "r");
 	if (!proc_file) {
-		SYSERROR("Could not open %s", proc_fn);
-		goto on_error;
+		SYSERROR("Failed to open %s", proc_fn);
+		return NULL;
 	}
 
 	info = calloc(1, sizeof(*info));
-	if (!info) {
-		SYSERROR("Could not allocate memory");
-		fclose(proc_file);
+	if (!info)
 		return NULL;
-	}
 
 	found = false;
 
@@ -112,13 +110,10 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
 		}
 	}
 
-	free(line);
-	fclose(proc_file);
-
 	if (!found) {
-		ERROR("Could not read capability bounding set from %s",
-		      proc_fn);
-		goto on_error;
+		ERROR("Could not read capability bounding set from %s", proc_fn);
+		free(info);
+		return NULL;
 	}
 
 	info->lsm_label = lsm_process_label_get(pid);
@@ -126,10 +121,6 @@ static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
 	memset(info->ns_fd, -1, sizeof(int) * LXC_NS_MAX);
 
 	return info;
-
-on_error:
-	free(info);
-	return NULL;
 }
 
 static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
@@ -440,13 +431,14 @@ static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx,
 
 static char *lxc_attach_getpwshell(uid_t uid)
 {
+	__do_free char *line = NULL;
+	__do_fclose FILE *pipe_f = NULL;
 	int fd, ret;
 	pid_t pid;
 	int pipes[2];
-	FILE *pipe_f;
 	bool found = false;
 	size_t line_bufsz = 0;
-	char *line = NULL, *result = NULL;
+	char *result = NULL;
 
 	/* We need to fork off a process that runs the getent program, and we
 	 * need to capture its output, so we use a pipe for that purpose.
@@ -567,9 +559,6 @@ static char *lxc_attach_getpwshell(uid_t uid)
 		found = true;
 	}
 
-	free(line);
-	fclose(pipe_f);
-
 	ret = wait_for_pid(pid);
 	if (ret < 0) {
 		free(result);
@@ -586,10 +575,10 @@ static char *lxc_attach_getpwshell(uid_t uid)
 
 static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
 {
-	FILE *proc_file;
+	__do_free char *line = NULL;
+	__do_fclose FILE *proc_file = NULL;
 	char proc_fn[LXC_PROC_STATUS_LEN];
 	int ret;
-	char *line = NULL;
 	size_t line_bufsz = 0;
 	long value = -1;
 	uid_t uid = (uid_t)-1;
@@ -620,9 +609,6 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
 			break;
 	}
 
-	fclose(proc_file);
-	free(line);
-
 	/* Only override arguments if we found something. */
 	if (uid != (uid_t)-1)
 		*init_uid = uid;
@@ -637,9 +623,9 @@ static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
 
 static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options)
 {
+	__do_free char *path = NULL;
 	int ret;
 	bool bret;
-	char *path;
 
 	if (!(options->namespaces & CLONE_NEWNS) ||
 	    !(options->attach_flags & LXC_ATTACH_LSM)) {
@@ -667,7 +653,6 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options
 
 	/* Copy the value into the new lxc_conf. */
 	bret = c->set_config_item(c, "lxc.seccomp.profile", path);
-	free(path);
 	if (!bret)
 		return false;
 
@@ -684,8 +669,7 @@ static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options
 
 static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
 {
-	bool bret;
-	char *val;
+	__do_free char *val = NULL;
 
 	/* Remove current setting. */
 	if (!c->set_config_item(c, "lxc.no_new_privs", "")) {
@@ -701,24 +685,18 @@ static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
 	}
 
 	/* Set currently active setting. */
-	bret = c->set_config_item(c, "lxc.no_new_privs", val);
-	free(val);
-	return bret;
+	return c->set_config_item(c, "lxc.no_new_privs", val);
 }
 
 static signed long get_personality(const char *name, const char *lxcpath)
 {
-	char *p;
-	signed long ret;
+	__do_free char *p;
 
 	p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
 	if (!p)
 		return -1;
 
-	ret = lxc_config_parse_arch(p);
-	free(p);
-
-	return ret;
+	return lxc_config_parse_arch(p);
 }
 
 struct attach_clone_payload {
@@ -1547,11 +1525,11 @@ int lxc_attach_run_command(void *payload)
 
 int lxc_attach_run_shell(void* payload)
 {
+	__do_free char *buf = NULL;
 	uid_t uid;
 	struct passwd pwent;
 	struct passwd *pwentp = NULL;
 	char *user_shell;
-	char *buf;
 	size_t bufsize;
 	int ret;
 
@@ -1598,6 +1576,5 @@ int lxc_attach_run_shell(void* payload)
 	if (!pwentp)
 		free(user_shell);
 
-	free(buf);
 	return -1;
 }


More information about the lxc-devel mailing list