dovecot-2.0: process title hack: Free allocated memory at deinit.

dovecot at dovecot.org dovecot at dovecot.org
Tue Dec 8 22:19:37 EET 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/32aba99c2e4f
changeset: 10431:32aba99c2e4f
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Dec 08 15:19:31 2009 -0500
description:
process title hack: Free allocated memory at deinit.

diffstat:

3 files changed, 34 insertions(+), 9 deletions(-)
src/lib/lib.c           |    2 ++
src/lib/process-title.c |   38 +++++++++++++++++++++++++++++---------
src/lib/process-title.h |    3 +++

diffs (103 lines):

diff -r 328e42919f2f -r 32aba99c2e4f src/lib/lib.c
--- a/src/lib/lib.c	Tue Dec 08 15:08:41 2009 -0500
+++ b/src/lib/lib.c	Tue Dec 08 15:19:31 2009 -0500
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "env-util.h"
 #include "hostpid.h"
+#include "process-title.h"
 
 #include <stdlib.h>
 #include <time.h>
@@ -31,4 +32,5 @@ void lib_deinit(void)
 	data_stack_deinit();
 	env_deinit();
 	failures_deinit();
+	process_title_deinit();
 }
diff -r 328e42919f2f -r 32aba99c2e4f src/lib/process-title.c
--- a/src/lib/process-title.c	Tue Dec 08 15:08:41 2009 -0500
+++ b/src/lib/process-title.c	Tue Dec 08 15:19:31 2009 -0500
@@ -18,6 +18,7 @@ static char *process_name = NULL;
 
 static char *process_title;
 static size_t process_title_len, process_title_clean_pos;
+static void *argv_memblock, *environ_memblock;
 
 static void proctitle_hack_init(char *argv[], char *env[])
 {
@@ -52,21 +53,34 @@ static void proctitle_hack_init(char *ar
 	}
 }
 
-static char **argv_dup(char *old_argv[])
+static char **argv_dup(char *old_argv[], void **memblock_r)
 {
+	/* @UNSAFE */
+	void *memblock, *memblock_end;
 	char **new_argv;
 	unsigned int i, count;
+	size_t len, memblock_len = 0;
 
-	for (count = 0; old_argv[count] != NULL; count++) ;
+	for (count = 0; old_argv[count] != NULL; count++)
+		memblock_len += strlen(old_argv[count]) + 1;
+	memblock_len += sizeof(char *) * (count + 1);
 
-	new_argv = malloc(sizeof(char *) * (count + 1));
-	if (new_argv == NULL)
+	memblock = malloc(memblock_len);
+	if (memblock == NULL)
 		i_fatal_status(FATAL_OUTOFMEM, "malloc() failed: %m");
+	*memblock_r = memblock;
+	memblock_end = PTR_OFFSET(memblock, memblock_len);
+
+	new_argv = memblock;
+	memblock = PTR_OFFSET(memblock, sizeof(char *) * (count + 1));
+
 	for (i = 0; i < count; i++) {
-		new_argv[i] = strdup(old_argv[i]);
-		if (new_argv[i] == NULL)
-			i_fatal_status(FATAL_OUTOFMEM, "strdup() failed: %m");
+		new_argv[i] = memblock;
+		len = strlen(old_argv[i]) + 1;
+		memcpy(memblock, old_argv[i], len);
+		memblock = PTR_OFFSET(memblock, len);
 	}
+	i_assert(memblock == memblock_end);
 	new_argv[i] = NULL;
 	return new_argv;
 }
@@ -99,8 +113,8 @@ void process_title_init(char **argv[])
 	char **orig_argv = *argv;
 	char **orig_environ = environ;
 
-	*argv = argv_dup(orig_argv);
-	environ = argv_dup(orig_environ);
+	*argv = argv_dup(orig_argv, &argv_memblock);
+	environ = argv_dup(orig_environ, &environ_memblock);
 	proctitle_hack_init(orig_argv, orig_environ);
 #endif
 	process_name = (*argv)[0];
@@ -119,3 +133,9 @@ void process_title_set(const char *title
 	proctitle_hack_set(t_strconcat(process_name, " ", title, NULL));
 #endif
 }
+
+void process_title_deinit(void)
+{
+	free(argv_memblock);
+	free(environ_memblock);
+}
diff -r 328e42919f2f -r 32aba99c2e4f src/lib/process-title.h
--- a/src/lib/process-title.h	Tue Dec 08 15:08:41 2009 -0500
+++ b/src/lib/process-title.h	Tue Dec 08 15:19:31 2009 -0500
@@ -6,5 +6,8 @@ void process_title_init(char **argv[]);
 
 /* Change the process title if possible. */
 void process_title_set(const char *title);
+/* Free all memory used by process title hacks. This should be the last
+   function called by the process, since it frees argv and environment. */
+void process_title_deinit(void);
 
 #endif


More information about the dovecot-cvs mailing list