rundir: Fix memory leaks

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