rundir: Fix memory leaks

parent 0130df54
...@@ -98,7 +98,7 @@ static char *lxclock_name(const char *p, const char *n) ...@@ -98,7 +98,7 @@ static char *lxclock_name(const char *p, const char *n)
int ret; int ret;
int len; int len;
char *dest; char *dest;
const char *rundir; char *rundir;
/* lockfile will be: /* lockfile will be:
* "/run" + "/lock/lxc/$lxcpath/$lxcname + '\0' if root * "/run" + "/lock/lxc/$lxcpath/$lxcname + '\0' if root
...@@ -113,12 +113,15 @@ static char *lxclock_name(const char *p, const char *n) ...@@ -113,12 +113,15 @@ static char *lxclock_name(const char *p, const char *n)
return NULL; return NULL;
len += strlen(rundir); len += strlen(rundir);
if ((dest = malloc(len)) == NULL) if ((dest = malloc(len)) == NULL) {
free(rundir);
return NULL; return NULL;
}
ret = snprintf(dest, len, "%s/lock/lxc/%s", rundir, p); ret = snprintf(dest, len, "%s/lock/lxc/%s", rundir, p);
if (ret < 0 || ret >= len) { if (ret < 0 || ret >= len) {
free(dest); free(dest);
free(rundir);
return NULL; return NULL;
} }
ret = mkdir_p(dest, 0755); ret = mkdir_p(dest, 0755);
...@@ -130,6 +133,7 @@ static char *lxclock_name(const char *p, const char *n) ...@@ -130,6 +133,7 @@ static char *lxclock_name(const char *p, const char *n)
d = realloc(dest, l2); d = realloc(dest, l2);
if (!d) { if (!d) {
free(dest); free(dest);
free(rundir);
return NULL; return NULL;
} }
len = l2; len = l2;
...@@ -138,12 +142,15 @@ static char *lxclock_name(const char *p, const char *n) ...@@ -138,12 +142,15 @@ static char *lxclock_name(const char *p, const char *n)
ret = snprintf(dest, len, "/tmp/%d/lxc/%s", geteuid(), p); ret = snprintf(dest, len, "/tmp/%d/lxc/%s", geteuid(), p);
if (ret < 0 || ret >= len) { if (ret < 0 || ret >= len) {
free(dest); free(dest);
free(rundir);
return NULL; return NULL;
} }
ret = snprintf(dest, len, "/tmp/%d/lxc/%s/%s", geteuid(), p, n); ret = snprintf(dest, len, "/tmp/%d/lxc/%s/%s", geteuid(), p, n);
} else } else
ret = snprintf(dest, len, "%s/lock/lxc/%s/%s", rundir, p, n); ret = snprintf(dest, len, "%s/lock/lxc/%s/%s", rundir, p, n);
free(rundir);
if (ret < 0 || ret >= len) { if (ret < 0 || ret >= len) {
free(dest); free(dest);
return NULL; return NULL;
......
...@@ -54,7 +54,7 @@ int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path ...@@ -54,7 +54,7 @@ int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path
int do_mkdirp) int do_mkdirp)
{ {
int ret; int ret;
const char *rundir; char *rundir;
rundir = get_rundir(); rundir = get_rundir();
if (!rundir) if (!rundir)
...@@ -64,19 +64,23 @@ int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path ...@@ -64,19 +64,23 @@ int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path
ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath); ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
if (ret < 0 || ret >= fifo_path_sz) { if (ret < 0 || ret >= fifo_path_sz) {
ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath); ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
free(rundir);
return -1; return -1;
} }
ret = mkdir_p(fifo_path, 0755); ret = mkdir_p(fifo_path, 0755);
if (ret < 0) { if (ret < 0) {
ERROR("unable to create monitor fifo dir %s", fifo_path); ERROR("unable to create monitor fifo dir %s", fifo_path);
free(rundir);
return ret; return ret;
} }
} }
ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath); ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
if (ret < 0 || ret >= fifo_path_sz) { if (ret < 0 || ret >= fifo_path_sz) {
ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath); ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
free(rundir);
return -1; return -1;
} }
free(rundir);
return 0; return 0;
} }
......
...@@ -376,7 +376,7 @@ out: ...@@ -376,7 +376,7 @@ out:
return values[i]; return values[i];
} }
const char *get_rundir() char *get_rundir()
{ {
char *rundir; char *rundir;
const char *homedir; const char *homedir;
...@@ -387,18 +387,21 @@ const char *get_rundir() ...@@ -387,18 +387,21 @@ const char *get_rundir()
} }
rundir = getenv("XDG_RUNTIME_DIR"); rundir = getenv("XDG_RUNTIME_DIR");
if (!rundir) { if (rundir) {
INFO("XDG_RUNTIME_DIR isn't set in the environment."); rundir = strdup(rundir);
homedir = getenv("HOME"); return rundir;
if (!homedir) { }
ERROR("HOME isn't set in the environment.");
return NULL;
}
rundir = malloc(sizeof(char) * (17 + strlen(homedir))); INFO("XDG_RUNTIME_DIR isn't set in the environment.");
sprintf(rundir, "%s/.cache/lxc/run/", homedir); homedir = getenv("HOME");
if (!homedir) {
ERROR("HOME isn't set in the environment.");
return NULL;
} }
rundir = malloc(sizeof(char) * (17 + strlen(homedir)));
sprintf(rundir, "%s/.cache/lxc/run/", homedir);
return rundir; return rundir;
} }
......
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