From 272464fac03859636383f1d9eeafe8af56470ab1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 5 Aug 2016 14:13:22 +0200 Subject: Fix bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/coopgammad.c | 19 +++++++++++-------- src/servers/kernel.c | 28 ++++++++++++++++++++++++++-- src/state.c | 1 - 3 files changed, 37 insertions(+), 11 deletions(-) (limited to 'src') 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); } -- cgit v1.2.3-70-g09d2