[lxc-devel] [lxc/master] tree-wide: cleanup macros
brauner on Github
lxc-bot at linuxcontainers.org
Fri Feb 15 23:51:24 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/20190215/0c826d7e/attachment-0001.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/7] 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/7] 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/7] 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;
}
From 31ac90591bbb5b39d1bfe636da575c6bdae2cd3e Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 11:51:04 +0100
Subject: [PATCH 4/7] cgroup_utils: use __do_free
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/cgroups/cgroup_utils.c | 34 +++++++++-------------------------
1 file changed, 9 insertions(+), 25 deletions(-)
diff --git a/src/lxc/cgroups/cgroup_utils.c b/src/lxc/cgroups/cgroup_utils.c
index 5a44f36db..1b780b2bc 100644
--- a/src/lxc/cgroups/cgroup_utils.c
+++ b/src/lxc/cgroups/cgroup_utils.c
@@ -33,6 +33,7 @@
#include "cgroup_utils.h"
#include "config.h"
#include "macro.h"
+#include "memory_utils.h"
#include "utils.h"
int get_cgroup_version(char *line)
@@ -65,12 +66,8 @@ bool is_cgroupfs_v2(char *line)
bool test_writeable_v1(char *mountpoint, char *path)
{
- char *fullpath = must_make_path(mountpoint, path, NULL);
- int ret;
-
- ret = access(fullpath, W_OK);
- free(fullpath);
- return ret == 0;
+ __do_free char *fullpath = must_make_path(mountpoint, path, NULL);
+ return (access(fullpath, W_OK) == 0);
}
bool test_writeable_v2(char *mountpoint, char *path)
@@ -81,39 +78,26 @@ bool test_writeable_v2(char *mountpoint, char *path)
* file.
*/
int ret;
- char *cgroup_path, *cgroup_procs_file, *cgroup_threads_file;
+ __do_free char *cgroup_path = NULL, *cgroup_procs_file = NULL,
+ *cgroup_threads_file = NULL;
cgroup_path = must_make_path(mountpoint, path, NULL);
cgroup_procs_file = must_make_path(cgroup_path, "cgroup.procs", NULL);
ret = access(cgroup_path, W_OK);
- if (ret < 0) {
- free(cgroup_path);
- free(cgroup_procs_file);
+ if (ret < 0)
return false;
- }
ret = access(cgroup_procs_file, W_OK);
- free(cgroup_procs_file);
- if (ret < 0) {
- free(cgroup_path);
+ if (ret < 0)
return false;
- }
/* Newer versions of cgroup2 now also require write access to the
* "cgroup.threads" file.
*/
cgroup_threads_file = must_make_path(cgroup_path, "cgroup.threads", NULL);
- free(cgroup_path);
- if (!file_exists(cgroup_threads_file)) {
- free(cgroup_threads_file);
+ if (!file_exists(cgroup_threads_file))
return true;
- }
-
- ret = access(cgroup_threads_file, W_OK);
- free(cgroup_threads_file);
- if (ret < 0)
- return false;
- return ret == 0;
+ return (access(cgroup_threads_file, W_OK) == 0);
}
From fc907a43b5503149ff9c2acaca37d6e5b0e273ee Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 11:57:07 +0100
Subject: [PATCH 5/7] lxc-init: use cleanup macros
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/Makefile.am | 2 ++
src/lxc/cmd/lxc_init.c | 22 ++++++++--------------
2 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 6ba9ecad2..2408eaa32 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -355,6 +355,7 @@ init_lxc_SOURCES = cmd/lxc_init.c \
compiler.h \
error.h \
initutils.c initutils.h \
+ memory_utils.h \
parse.c parse.h \
raw_syscalls.c raw_syscalls.h \
string_utils.c string_utils.h
@@ -403,6 +404,7 @@ init_lxc_static_SOURCES = cmd/lxc_init.c \
file_utils.c file_utils.h \
log.c log.h \
macro.h \
+ memory_utils.h \
namespace.c namespace.h \
string_utils.c string_utils.h
diff --git a/src/lxc/cmd/lxc_init.c b/src/lxc/cmd/lxc_init.c
index b3849545a..b3d927b94 100644
--- a/src/lxc/cmd/lxc_init.c
+++ b/src/lxc/cmd/lxc_init.c
@@ -47,6 +47,7 @@
#include "config.h"
#include "error.h"
#include "initutils.h"
+#include "memory_utils.h"
#include "parse.h"
#include "raw_syscalls.h"
#include "string_utils.h"
@@ -100,10 +101,10 @@ static struct arguments my_args = {
static void prevent_forking(void)
{
- FILE *f;
- size_t len = 0;
- char *line = NULL;
+ __do_free char *line = NULL;
+ __do_fclose FILE *f = NULL;
char path[PATH_MAX];
+ size_t len = 0;
f = fopen("/proc/self/cgroup", "r");
if (!f)
@@ -138,14 +139,14 @@ static void prevent_forking(void)
if (ret < 0 || (size_t)ret >= sizeof(path)) {
if (my_args.quiet)
fprintf(stderr, "Failed to create string\n");
- goto on_error;
+ return;
}
fd = open(path, O_WRONLY);
if (fd < 0) {
if (my_args.quiet)
fprintf(stderr, "Failed to open \"%s\"\n", path);
- goto on_error;
+ return;
}
ret = write(fd, "1", 1);
@@ -153,17 +154,13 @@ static void prevent_forking(void)
fprintf(stderr, "Failed to write to \"%s\"\n", path);
close(fd);
- break;
+ return;
}
-
-on_error:
- free(line);
- fclose(f);
}
static void kill_children(pid_t pid)
{
- FILE *f;
+ __do_fclose FILE *f = NULL;
char path[PATH_MAX];
int ret;
@@ -187,15 +184,12 @@ static void kill_children(pid_t pid)
if (fscanf(f, "%d ", &find_pid) != 1) {
if (my_args.quiet)
fprintf(stderr, "Failed to retrieve pid\n");
- fclose(f);
return;
}
(void)kill_children(find_pid);
(void)kill(find_pid, SIGKILL);
}
-
- fclose(f);
}
static void remove_self(void)
From 4a18ee8694f860b6253b0d108d56368f9302b3f8 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 12:02:22 +0100
Subject: [PATCH 6/7] lxc-user-nic: use cleanup macros
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/cmd/lxc_user_nic.c | 51 ++++++++------------------------------
1 file changed, 11 insertions(+), 40 deletions(-)
diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index be6b395be..b9683c296 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -111,10 +111,9 @@ static int open_and_lock(char *path)
static char *get_username(void)
{
+ __do_free char *buf = NULL;
struct passwd pwent;
struct passwd *pwentp = NULL;
- char *buf;
- char *username;
size_t bufsize;
int ret;
@@ -132,14 +131,10 @@ static char *get_username(void)
usernic_error("%s", "Could not find matched password record\n");
CMD_SYSERROR("Failed to get username: %u\n", getuid());
- free(buf);
return NULL;
}
- username = strdup(pwent.pw_name);
- free(buf);
-
- return username;
+ return strdup(pwent.pw_name);
}
static void free_groupnames(char **groupnames)
@@ -157,13 +152,13 @@ static void free_groupnames(char **groupnames)
static char **get_groupnames(void)
{
+ __do_free char *buf = NULL;
+ __do_free gid_t *group_ids = NULL;
int ngroups;
- gid_t *group_ids;
int ret, i;
char **groupnames;
struct group grent;
struct group *grentp = NULL;
- char *buf;
size_t bufsize;
ngroups = getgroups(0, NULL);
@@ -182,14 +177,12 @@ static char **get_groupnames(void)
ret = getgroups(ngroups, group_ids);
if (ret < 0) {
- free(group_ids);
CMD_SYSERROR("Failed to get process groups\n");
return NULL;
}
groupnames = malloc(sizeof(char *) * (ngroups + 1));
if (!groupnames) {
- free(group_ids);
CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL;
}
@@ -202,7 +195,6 @@ static char **get_groupnames(void)
buf = malloc(bufsize);
if (!buf) {
- free(group_ids);
free_groupnames(groupnames);
CMD_SYSERROR("Failed to allocate memory while getting group names\n");
return NULL;
@@ -215,8 +207,6 @@ static char **get_groupnames(void)
usernic_error("%s", "Could not find matched group record\n");
CMD_SYSERROR("Failed to get group name: %u\n", group_ids[i]);
- free(buf);
- free(group_ids);
free_groupnames(groupnames);
return NULL;
}
@@ -224,16 +214,11 @@ static char **get_groupnames(void)
groupnames[i] = strdup(grent.gr_name);
if (!groupnames[i]) {
usernic_error("Failed to copy group name \"%s\"", grent.gr_name);
- free(buf);
- free(group_ids);
free_groupnames(groupnames);
return NULL;
}
}
- free(buf);
- free(group_ids);
-
return groupnames;
}
@@ -321,13 +306,13 @@ static void free_alloted(struct alloted_s **head)
static int get_alloted(char *me, char *intype, char *link,
struct alloted_s **alloted)
{
+ __do_free char *line = NULL;
+ __do_fclose FILE *fin = NULL;
int n, ret;
char name[100], type[100], br[100];
char **groups;
- FILE *fin;
int count = 0;
size_t len = 0;
- char *line = NULL;
fin = fopen(LXC_USERNIC_CONF, "r");
if (!fin) {
@@ -378,8 +363,6 @@ static int get_alloted(char *me, char *intype, char *link,
}
free_groupnames(groups);
- fclose(fin);
- free(line);
/* Now return the total number of nics that this user can create. */
return count;
@@ -610,12 +593,12 @@ struct entry_line {
static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
char *net_dev, bool *found_nicname)
{
+ __do_free struct entry_line *entry_lines = NULL;
int i, ret;
char *buf, *buf_end, *buf_start;
struct stat sb;
int n = 0;
bool found, keep;
- struct entry_line *entry_lines = NULL;
ret = fstat(fd, &sb);
if (ret < 0) {
@@ -641,7 +624,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
newe = realloc(entry_lines, sizeof(*entry_lines) * (n + 1));
if (!newe) {
- free(entry_lines);
lxc_strmunmap(buf, sb.st_size);
return false;
}
@@ -672,8 +654,6 @@ static bool cull_entries(int fd, char *name, char *net_type, char *net_link,
buf_start++;
}
- free(entry_lines);
-
ret = ftruncate(fd, buf_start - buf);
lxc_strmunmap(buf, sb.st_size);
if (ret < 0)
@@ -706,9 +686,10 @@ static int count_entries(char *buf, off_t len, char *name, char *net_type, char
static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
char *intype, char *br, int allowed, char **cnic)
{
+ __do_free char *newline = NULL;
int ret;
size_t slen;
- char *newline, *owner;
+ char *owner;
char nicname[IFNAMSIZ];
struct stat sb;
struct alloted_s *n;
@@ -795,7 +776,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
slen = strlen(owner) + strlen(intype) + strlen(br) + strlen(nicname) + 4;
newline = malloc(slen + 1);
if (!newline) {
- free(newline);
CMD_SYSERROR("Failed allocate memory\n");
return NULL;
}
@@ -805,7 +785,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
- free(newline);
return NULL;
}
@@ -824,7 +803,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
if (lxc_netdev_delete_by_name(nicname) != 0)
usernic_error("Error unlinking %s\n", nicname);
- free(newline);
return NULL;
}
@@ -832,7 +810,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
* \0 byte! Files are not \0-terminated!
*/
memmove(buf + sb.st_size, newline, slen);
- free(newline);
lxc_strmunmap(buf, sb.st_size + slen);
return strdup(nicname);
@@ -1115,11 +1092,11 @@ static bool is_privileged_over_netns(int netns_fd)
int main(int argc, char *argv[])
{
+ __do_free char *me = NULL, *newname = NULL, *nicname = NULL;
int fd, n, pid, request, ret;
- char *me, *newname;
struct user_nic_args args;
int container_veth_ifidx = -1, host_veth_ifidx = -1, netns_fd = -1;
- char *cnic = NULL, *nicname = NULL;
+ char *cnic = NULL;
struct alloted_s *alloted = NULL;
if (argc < 7 || argc > 8) {
@@ -1248,7 +1225,6 @@ int main(int argc, char *argv[])
}
n = get_alloted(me, args.type, args.link, &alloted);
- free(me);
if (request == LXC_USERNIC_DELETE) {
struct alloted_s *it;
@@ -1313,14 +1289,11 @@ int main(int argc, char *argv[])
if (ret < 0)
usernic_error("Failed to delete \"%s\"\n", cnic);
- free(nicname);
_exit(EXIT_FAILURE);
}
host_veth_ifidx = if_nametoindex(nicname);
if (!host_veth_ifidx) {
- free(newname);
- free(nicname);
CMD_SYSERROR("Failed to get netdev index\n");
_exit(EXIT_FAILURE);
}
@@ -1330,8 +1303,6 @@ int main(int argc, char *argv[])
*/
fprintf(stdout, "%s:%d:%s:%d\n", newname, container_veth_ifidx, nicname,
host_veth_ifidx);
- free(newname);
- free(nicname);
fflush(stdout);
_exit(EXIT_SUCCESS);
From 5cfca67649881d28070bda3903d12085795af78a Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 9 Feb 2019 12:05:46 +0100
Subject: [PATCH 7/7] lxc-usernsexec: use cleanup macros
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/lxc/Makefile.am | 3 ++-
src/lxc/cmd/lxc_usernsexec.c | 24 ++++++++----------------
2 files changed, 10 insertions(+), 17 deletions(-)
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 2408eaa32..4909a838c 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -377,10 +377,11 @@ lxc_user_nic_SOURCES = cmd/lxc_user_nic.c \
syscall_wrappers.h
lxc_usernsexec_SOURCES = cmd/lxc_usernsexec.c \
conf.c conf.h \
+ file_utils.c file_utils.h \
list.h \
log.c log.h \
macro.h \
- file_utils.c file_utils.h \
+ memory_utils.h \
string_utils.c string_utils.h \
syscall_wrappers.h \
utils.c utils.h
diff --git a/src/lxc/cmd/lxc_usernsexec.c b/src/lxc/cmd/lxc_usernsexec.c
index ab0dffcff..3641d94df 100644
--- a/src/lxc/cmd/lxc_usernsexec.c
+++ b/src/lxc/cmd/lxc_usernsexec.c
@@ -47,6 +47,7 @@
#include "list.h"
#include "log.h"
#include "macro.h"
+#include "memory_utils.h"
#include "file_utils.h"
#include "string_utils.h"
#include "syscall_wrappers.h"
@@ -198,13 +199,13 @@ static int parse_map(char *map)
*/
static int read_default_map(char *fnam, int which, char *user)
{
+ __do_free char *line = NULL;
+ __do_fclose FILE *fin = NULL;
size_t len;
char *p1, *p2;
unsigned long ul1, ul2;
- FILE *fin;
int ret = -1;
size_t sz = 0;
- char *line = NULL;
struct lxc_list *tmp = NULL;
struct id_map *newmap = NULL;
@@ -259,16 +260,13 @@ static int read_default_map(char *fnam, int which, char *user)
break;
}
- fclose(fin);
- free(line);
-
return ret;
}
static int find_default_map(void)
{
+ __do_free char *buf = NULL;
size_t bufsize;
- char *buf;
struct passwd pwent;
int ret = -1;
struct passwd *pwentp = NULL;
@@ -287,24 +285,18 @@ static int find_default_map(void)
CMD_SYSERROR("Failed to find matched password record");
CMD_SYSERROR("Failed to get password record for uid %d", getuid());
- ret = -1;
- goto out;
+ return -1;
}
ret = read_default_map(subuidfile, ID_TYPE_UID, pwent.pw_name);
if (ret < 0)
- goto out;
+ return -1;
ret = read_default_map(subgidfile, ID_TYPE_GID, pwent.pw_name);
if (ret < 0)
- goto out;
-
- ret = 0;
-
-out:
- free(buf);
+ return -1;
- return ret;
+ return 0;
}
int main(int argc, char *argv[])
More information about the lxc-devel
mailing list