lxccontainer: increase cleanup macro usage

parent b816bdde
...@@ -2803,10 +2803,11 @@ out: ...@@ -2803,10 +2803,11 @@ out:
void mod_all_rdeps(struct lxc_container *c, bool inc) void mod_all_rdeps(struct lxc_container *c, bool inc)
{ {
struct lxc_container *p; __do_free char *lxcpath = NULL, *lxcname = NULL;
char *lxcpath = NULL, *lxcname = NULL, path[PATH_MAX]; __do_fclose FILE *f = NULL;
size_t pathlen = 0, namelen = 0; size_t pathlen = 0, namelen = 0;
FILE *f; struct lxc_container *p;
char path[PATH_MAX];
int ret; int ret;
ret = snprintf(path, PATH_MAX, "%s/%s/lxc_rdepends", ret = snprintf(path, PATH_MAX, "%s/%s/lxc_rdepends",
...@@ -2817,13 +2818,13 @@ void mod_all_rdeps(struct lxc_container *c, bool inc) ...@@ -2817,13 +2818,13 @@ void mod_all_rdeps(struct lxc_container *c, bool inc)
} }
f = fopen(path, "re"); f = fopen(path, "re");
if (f == NULL) if (!f)
return; return;
while (getline(&lxcpath, &pathlen, f) != -1) { while (getline(&lxcpath, &pathlen, f) != -1) {
if (getline(&lxcname, &namelen, f) == -1) { if (getline(&lxcname, &namelen, f) == -1) {
ERROR("badly formatted file %s", path); ERROR("badly formatted file %s", path);
goto out; return;
} }
remove_trailing_newlines(lxcpath); remove_trailing_newlines(lxcpath);
...@@ -2841,47 +2842,36 @@ void mod_all_rdeps(struct lxc_container *c, bool inc) ...@@ -2841,47 +2842,36 @@ void mod_all_rdeps(struct lxc_container *c, bool inc)
lxc_container_put(p); lxc_container_put(p);
} }
out:
free(lxcpath);
free(lxcname);
fclose(f);
} }
static bool has_fs_snapshots(struct lxc_container *c) static bool has_fs_snapshots(struct lxc_container *c)
{ {
FILE *f; __do_fclose FILE *f = NULL;
char path[PATH_MAX]; char path[PATH_MAX];
int ret, v; int ret, v;
struct stat fbuf; struct stat fbuf;
bool bret = false;
ret = snprintf(path, PATH_MAX, "%s/%s/lxc_snapshots", c->config_path, ret = snprintf(path, PATH_MAX, "%s/%s/lxc_snapshots", c->config_path,
c->name); c->name);
if (ret < 0 || ret > PATH_MAX) if (ret < 0 || ret > PATH_MAX)
goto out; return false;
/* If the file doesn't exist there are no snapshots. */ /* If the file doesn't exist there are no snapshots. */
if (stat(path, &fbuf) < 0) if (stat(path, &fbuf) < 0)
goto out; return false;
v = fbuf.st_size; v = fbuf.st_size;
if (v != 0) { if (v != 0) {
f = fopen(path, "re"); f = fopen(path, "re");
if (!f) if (!f)
goto out; return false;
ret = fscanf(f, "%d", &v); ret = fscanf(f, "%d", &v);
fclose(f);
/* TODO: Figure out what to do with the return value of fscanf. */
if (ret != 1) if (ret != 1)
INFO("Container uses new lxc-snapshots format %s", path); INFO("Container uses new lxc-snapshots format %s", path);
} }
bret = v != 0; return v != 0;
out:
return bret;
} }
static bool has_snapshots(struct lxc_container *c) static bool has_snapshots(struct lxc_container *c)
...@@ -3528,30 +3518,20 @@ static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0) ...@@ -3528,30 +3518,20 @@ static void copy_rdepends(struct lxc_container *c, struct lxc_container *c0)
static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0) static bool add_rdepends(struct lxc_container *c, struct lxc_container *c0)
{ {
__do_fclose FILE *f = NULL;
int ret; int ret;
char path[PATH_MAX]; char path[PATH_MAX];
FILE *f;
bool bret;
ret = snprintf(path, PATH_MAX, "%s/%s/lxc_rdepends", c->config_path, ret = snprintf(path, sizeof(path), "%s/%s/lxc_rdepends", c->config_path, c->name);
c->name); if (ret < 0 || ret >= sizeof(path))
if (ret < 0 || ret >= PATH_MAX)
return false; return false;
f = fopen(path, "ae"); f = fopen(path, "ae");
if (!f) if (!f)
return false; return false;
bret = true;
/* If anything goes wrong, just return an error. */ /* If anything goes wrong, just return an error. */
if (fprintf(f, "%s\n%s\n", c0->config_path, c0->name) < 0) return fprintf(f, "%s\n%s\n", c0->config_path, c0->name) > 0;
bret = false;
if (fclose(f) != 0)
bret = false;
return bret;
} }
/* /*
...@@ -4274,9 +4254,10 @@ static char *get_snapcomment_path(char* snappath, char *name) ...@@ -4274,9 +4254,10 @@ static char *get_snapcomment_path(char* snappath, char *name)
static char *get_timestamp(char* snappath, char *name) static char *get_timestamp(char* snappath, char *name)
{ {
char path[PATH_MAX], *s = NULL; __do_free char *s = NULL;
__do_fclose FILE *fin = NULL;
char path[PATH_MAX];
int ret, len; int ret, len;
FILE *fin;
ret = snprintf(path, PATH_MAX, "%s/%s/ts", snappath, name); ret = snprintf(path, PATH_MAX, "%s/%s/ts", snappath, name);
if (ret < 0 || ret >= PATH_MAX) if (ret < 0 || ret >= PATH_MAX)
...@@ -4293,16 +4274,12 @@ static char *get_timestamp(char* snappath, char *name) ...@@ -4293,16 +4274,12 @@ static char *get_timestamp(char* snappath, char *name)
s = malloc(len+1); s = malloc(len+1);
if (s) { if (s) {
s[len] = '\0'; s[len] = '\0';
if (fread(s, 1, len, fin) != len) { if (fread(s, 1, len, fin) != len)
SYSERROR("reading timestamp"); return log_error_errno(NULL, errno, "reading timestamp");
free(s);
s = NULL;
}
} }
} }
fclose(fin); return move_ptr(s);
return s;
} }
static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps) static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot **ret_snaps)
...@@ -4366,9 +4343,6 @@ static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot ...@@ -4366,9 +4343,6 @@ static int do_lxcapi_snapshot_list(struct lxc_container *c, struct lxc_snapshot
count++; count++;
} }
if (closedir(dir))
WARN("Failed to close directory");
*ret_snaps = snaps; *ret_snaps = snaps;
return count; return count;
...@@ -5516,9 +5490,10 @@ free_bad: ...@@ -5516,9 +5490,10 @@ free_bad:
int list_active_containers(const char *lxcpath, char ***nret, int list_active_containers(const char *lxcpath, char ***nret,
struct lxc_container ***cret) struct lxc_container ***cret)
{ {
__do_free char *line = NULL;
__do_fclose FILE *f = NULL;
int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0; int i, ret = -1, cret_cnt = 0, ct_name_cnt = 0;
int lxcpath_len; int lxcpath_len;
char *line = NULL;
char **ct_name = NULL; char **ct_name = NULL;
size_t len = 0; size_t len = 0;
struct lxc_container *c = NULL; struct lxc_container *c = NULL;
...@@ -5534,7 +5509,7 @@ int list_active_containers(const char *lxcpath, char ***nret, ...@@ -5534,7 +5509,7 @@ int list_active_containers(const char *lxcpath, char ***nret,
if (nret) if (nret)
*nret = NULL; *nret = NULL;
FILE *f = fopen("/proc/net/unix", "re"); f = fopen("/proc/net/unix", "re");
if (!f) if (!f)
return -1; return -1;
...@@ -5662,8 +5637,6 @@ free_ct_name: ...@@ -5662,8 +5637,6 @@ free_ct_name:
} }
out: out:
free(line);
fclose(f);
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment