aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-08-05 14:13:22 +0200
committerMattias Andrée <maandree@kth.se>2016-08-05 14:13:22 +0200
commit272464fac03859636383f1d9eeafe8af56470ab1 (patch)
tree47c0b05ecc83866c03e3af5da5a5edb66ed9de88
parentFix reading of uninitialised value (diff)
downloadcoopgammad-272464fac03859636383f1d9eeafe8af56470ab1.tar.gz
coopgammad-272464fac03859636383f1d9eeafe8af56470ab1.tar.bz2
coopgammad-272464fac03859636383f1d9eeafe8af56470ab1.tar.xz
Fix bugs
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--Makefile3
-rw-r--r--src/coopgammad.c19
-rw-r--r--src/servers/kernel.c28
-rw-r--r--src/state.c1
4 files changed, 40 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 87e7ff9..21afbc7 100644
--- a/Makefile
+++ b/Makefile
@@ -39,6 +39,9 @@ CPP_linux-libre = $(CPP_linux)
CCFLAGS = -std=c99 $(WARN) $(FFLAGS) $(OPTIMISE)
LDFLAGS = $(OPTIMISE) -lgamma
CPPFLAGS = -D'PKGNAME="$(PKGNAME)"' -D'COMMAND="$(COMMAND)"' -D_XOPEN_SOURCE=700 $(CPP_$(KERNEL))
+ifdef USE_VALGRIND
+CPPFLAGS += -DUSE_VALGRIND
+endif
.PHONY: all
diff --git a/src/coopgammad.c b/src/coopgammad.c
index 83352e0..1276189 100644
--- a/src/coopgammad.c
+++ b/src/coopgammad.c
@@ -419,14 +419,13 @@ static enum init_status initialise(int foreground, int keep_stderr, int query)
static void destroy(int full)
{
if (full)
- disconnect_all();
-
- if (full)
- close_socket(socketpath);
-
- if (full && (outputs != NULL))
- restore_gamma();
-
+ {
+ disconnect_all();
+ close_socket(socketpath);
+ free(argv0_real);
+ if (outputs != NULL)
+ restore_gamma();
+ }
state_destroy();
free(socketpath);
if (full && (pidpath != NULL))
@@ -618,7 +617,11 @@ static char* reexecute(void)
destroy(0);
+#if !defined(USE_VALGRIND)
execlp(argv0_real ? argv0_real : argv0, argv0, "- ", statefile, NULL);
+#else
+ execlp("valgrind", "valgrind", "--leak-check=full", argv0_real ? argv0_real : argv0, "- ", statefile, NULL);
+#endif
saved_errno = errno;
free(argv0_real), argv0_real = NULL;
errno = saved_errno;
diff --git a/src/servers/kernel.c b/src/servers/kernel.c
index de64e04..19af912 100644
--- a/src/servers/kernel.c
+++ b/src/servers/kernel.c
@@ -197,6 +197,7 @@ static int is_pidfile_reusable(const char* restrict pidpath, const char* restric
sprintf(temp, "%llu\n", (unsigned long long)pid);
if (strcmp(content, temp))
goto bad;
+ free(content);
/* Validate PID */
#if defined(HAVE_LINUX_PROCFS)
@@ -211,7 +212,8 @@ static int is_pidfile_reusable(const char* restrict pidpath, const char* restric
for (end = (p = content) + n; p != end; p = strchr(p, '\0') + 1)
if (!strcmp(p, token))
- return 0;
+ return free(content), 0;
+ free(content);
#else
if ((kill(pid, 0) == 0) || (errno == EINVAL))
return 0;
@@ -250,8 +252,20 @@ int create_pidfile(char* pidpath)
if (token == NULL)
return -1;
sprintf(token, "COOPGAMMAD_PIDFILE_TOKEN=%s", pidpath);
+#if !defined(USE_VALGRIND)
if (putenv(token))
- goto fail;
+ goto putenv_fail;
+ /* `token` must not be free! */
+#else
+ {
+ static char static_token[sizeof("COOPGAMMAD_PIDFILE_TOKEN=") + PATH_MAX];
+ if (strlen(pidpath) > PATH_MAX)
+ abort();
+ strcpy(static_token, token);
+ if (putenv(static_token))
+ goto fail;
+ }
+#endif
/* Create PID file's directory. */
for (p = pidpath; *p == '/'; p++);
@@ -294,14 +308,24 @@ int create_pidfile(char* pidpath)
goto fail;
/* Done */
+#if defined(USE_VALGRIND)
free(token);
+#endif
if (close(fd) < 0)
if (errno != EINTR)
return -1;
return 0;
+#if !defined(USE_VALGRIND)
+ putenv_fail:
+ saved_errno = errno;
+ free(token);
+ errno = saved_errno;
+#endif
fail:
saved_errno = errno;
+#if defined(USE_VALGRIND)
free(token);
+#endif
if (fd >= 0)
{
close(fd);
diff --git a/src/state.c b/src/state.c
index c8cb1d9..d840ccb 100644
--- a/src/state.c
+++ b/src/state.c
@@ -172,7 +172,6 @@ void state_destroy(void)
free(partitions);
libgamma_site_destroy(&site);
- free(argv0_real);
free(sitename);
}