Skip to contentSkip to conversation

tools/console/daemon: fix log_dir memory leak in xenconsoled

PATCH In review Andrew Mbugua 3 participants 5 messages Aug 13 – Oct 12

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/console/daemon: fix log_dir memory leak in xenconsoled

Valgrind reports that 21 bytes are "still reachable" from the (XEN_LOG_DIR "/console") allocation:

HEAP SUMMARY:
in use at exit: 21 bytes in 1 blocks
total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated

Since the dynamic memory allocation for the default log directory path happens before the process forks into the background, the parent and intermediate processes exit during daemonize() with the memory still reachable.

Move the strdup() allocation down below the daemonize() block. This ensures only the final background daemon allocates the default path, matching the lifetime of the free(log_dir) cleanup loop at the exit of main().

With this change, Valgrind reports a clean heap summary

HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated

---
 tools/console/daemon/main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/console/daemon/main.c +4 -4
index aac7233a48..9f81d73164 100644
@@ -181,10 +181,6 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (!log_dir) {
-		log_dir = strdup(XEN_LOG_DIR "/console");
-	}
-
 	if (geteuid() != 0) {
 		fprintf(stderr, "%s requires root to run.\n", argv[0]);
 		exit(EPERM);
@@ -201,6 +197,10 @@ int main(int argc, char **argv)
 		daemonize(pidfile ? pidfile : XEN_RUN_DIR "/xenconsoled.pid");
 	}
 
+	if (!log_dir) {
+                log_dir = strdup(XEN_LOG_DIR "/console");
+        }
+
 	if (!xen_setup())
 		exit(1);
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 14 lines · Andrew Mbugua

Valgrind reports that 21 bytes are "still reachable" from the (XEN_LOG_DIR "/console") allocation:

HEAP SUMMARY:
in use at exit: 21 bytes in 1 blocks
total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated

Since the dynamic memory allocation for the default log directory path happens before the process forks into the background, the parent and intermediate processes exit during daemonize() with the memory still reachable.

Move the strdup() allocation down below the daemonize() block. This ensures only the final background daemon allocates the default path, matching the lifetime of the free(log_dir) cleanup loop at the exit of main().

Quoted 7 lines

With this change, Valgrind reports a clean heap summary

HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated

Looks all plausible (albeit a little unnecessary, as memory is freed at program exit anyway), except that ...

Quoted 14 lines
tools/console/daemon/main.c +0 -4
@@ -181,10 +181,6 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (!log_dir) {
-		log_dir = strdup(XEN_LOG_DIR "/console");
-	}
-
 	if (geteuid() != 0) {
 		fprintf(stderr, "%s requires root to run.\n", argv[0]);
 		exit(EPERM);

tools/console/daemon/main.c int main(int argc, char **argv) · line 202

Quoted 6 lines
tools/console/daemon/main.c +3 -0
@@ -201,6 +197,10 @@ int main(int argc, char **argv)
 		daemonize(pidfile ? pidfile : XEN_RUN_DIR "/xenconsoled.pid");
 	}
 
+	if (!log_dir) {
+                log_dir = strdup(XEN_LOG_DIR "/console");
+        }

... can you please not screw up indentation? All you want is to move the code, without converting tabs to blanks.

Jan

#2 lore raw

Noted,it's my 1st first time contributing.

Should I generate a version 2 patch for this?

Quoted 57 lines · Jan Beulich
Quoted 1 line · Andrew Mbugua

Valgrind reports that 21 bytes are "still reachable" from the

(XEN_LOG_DIR "/console") allocation:

Quoted 6 lines

HEAP SUMMARY:
in use at exit: 21 bytes in 1 blocks
total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated

Since the dynamic memory allocation for the default log directory path

happens before the process

Quoted 1 line

forks into the background, the parent and intermediate processes exit

during daemonize()

Quoted 3 lines

with the memory still reachable.

Move the strdup() allocation down below the daemonize() block. This

ensures only the final

Quoted 1 line

background daemon allocates the default path, matching the lifetime of

the free(log_dir)

Quoted 9 lines

cleanup loop at the exit of main().

With this change, Valgrind reports a clean heap summary

HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated

Looks all plausible (albeit a little unnecessary, as memory is freed at program exit anyway), except that ...

Quoted 14 lines
tools/console/daemon/main.c +0 -4
@@ -181,10 +181,6 @@ int main(int argc, char **argv)
              }
      }
 
-     if (!log_dir) {
-             log_dir = strdup(XEN_LOG_DIR "/console");
-     }
-
      if (geteuid() != 0) {
              fprintf(stderr, "%s requires root to run.\n", argv[0]);
              exit(EPERM);

tools/console/daemon/main.c int main(int argc, char **argv) · line 197

Quoted 1 line
tools/console/daemon/main.c +0 -0
@@ -201,6 +197,10 @@ int main(int argc, char **argv)
              daemonize(pidfile ? pidfile : XEN_RUN_DIR

"/xenconsoled.pid");

tools/console/daemon/main.c int main(int argc, char **argv) · line 202

Quoted 5 lines
tools/console/daemon/main.c +3 -0
      }
 
+     if (!log_dir) {
+                log_dir = strdup(XEN_LOG_DIR "/console");
+        }

... can you please not screw up indentation? All you want is to move the code, without converting tabs to blanks.

Jan

#3 lore raw
Quoted 3 lines · Andrew Precious

Noted,it's my 1st first time contributing.

Should I generate a version 2 patch for this?

May not be necessary, the edit may be possible to do by the committer. Best wait for the maintainer to provide feedback.

Jan

Reviewed-by on v1 1/1
#4 lore raw
In reply to Andrew Mbugua
Quoted 14 lines · Andrew Mbugua

Valgrind reports that 21 bytes are "still reachable" from the (XEN_LOG_DIR "/console") allocation:

HEAP SUMMARY:
in use at exit: 21 bytes in 1 blocks
total heap usage: 9 allocs, 8 frees, 4,799 bytes allocated

Since the dynamic memory allocation for the default log directory path happens before the process forks into the background, the parent and intermediate processes exit during daemonize() with the memory still reachable.

Move the strdup() allocation down below the daemonize() block. This ensures only the final background daemon allocates the default path, matching the lifetime of the free(log_dir) cleanup loop at the exit of main().

Quoted 7 lines

With this change, Valgrind reports a clean heap summary

HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 8 allocs, 8 frees, 4,778 bytes allocated

I'll try to remember to fix the indentation on commit.

Thanks,

--
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech