Skip to content

Commit

Permalink
attach: use __do_free
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Feb 17, 2019
1 parent 83c11f1 commit cd8f566
Showing 1 changed file with 22 additions and 45 deletions.
67 changes: 22 additions & 45 deletions src/lxc/attach.c
Expand Up @@ -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"
Expand All @@ -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;

Expand All @@ -112,24 +110,17 @@ 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);
info->ns_inherited = 0;
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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;

Expand All @@ -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", "")) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -1598,6 +1576,5 @@ int lxc_attach_run_shell(void* payload)
if (!pwentp)
free(user_shell);

free(buf);
return -1;
}

0 comments on commit cd8f566

Please sign in to comment.