498804b
From b608ddda22ac33195ba57311d42ac83facbd8695 Mon Sep 17 00:00:00 2001
498804b
From: Michal Schmidt <mschmidt@redhat.com>
498804b
Date: Thu, 16 Feb 2012 00:04:00 +0100
498804b
Subject: [PATCH 2/2] better error message
498804b
498804b
Only suggest CONFIG_LATENCYTOP on ENOENT.
498804b
Print the message after curses is cleaned up to make it visible.
498804b
---
498804b
 src/latencytop.c |   18 +++++++++++++++---
498804b
 1 files changed, 15 insertions(+), 3 deletions(-)
498804b
498804b
diff --git a/src/latencytop.c b/src/latencytop.c
498804b
index 0ffb7f3..3b62c5d 100644
498804b
--- a/src/latencytop.c
498804b
+++ b/src/latencytop.c
498804b
@@ -24,6 +24,7 @@
498804b
 
498804b
 #define _GNU_SOURCE
498804b
 
498804b
+#include <errno.h>
498804b
 #include <stdio.h>
498804b
 #include <stdlib.h>
498804b
 #include <unistd.h>
498804b
@@ -113,6 +114,17 @@ static void fixup_reason(struct latency_line *line, char *c)
498804b
 		strncpy(line->reason, c2, 1024);
498804b
 }
498804b
 
498804b
+static int die_errno;
498804b
+
498804b
+static void print_error_msg(void) {
498804b
+	if (!die_errno)
498804b
+		return;
498804b
+
498804b
+	fprintf(stderr, "Failed to open /proc/latency_stats: %s\n", strerror(die_errno));
498804b
+	if (die_errno == ENOENT)
498804b
+		fprintf(stderr, "Please enable the CONFIG_LATENCYTOP configuration in your kernel.\n");
498804b
+}
498804b
+
498804b
 void parse_global_list(void)
498804b
 {
498804b
 	FILE *file;
498804b
@@ -120,8 +132,7 @@ void parse_global_list(void)
498804b
 	size_t dummy;
498804b
 	file = fopen("/proc/latency_stats","r+");
498804b
 	if (!file) {
498804b
-		fprintf(stderr, "Please enable the CONFIG_LATENCYTOP configuration in your kernel.\n");
498804b
-		fprintf(stderr, "Exiting...\n");
498804b
+		die_errno = errno;
498804b
 		exit(EXIT_FAILURE);
498804b
 	}
498804b
 	/* wipe first line */
498804b
@@ -540,7 +551,8 @@ int main(int argc, char **argv)
498804b
 
498804b
 	enable_sysctl();
498804b
 	atexit(cleanup_sysctl);
498804b
-
498804b
+	/* needs to run after curses is gone */
498804b
+	atexit(print_error_msg);
498804b
 #ifdef HAS_GTK_GUI
498804b
 	if (preinitialize_gtk_ui(&argc, &argv))
498804b
 		use_gtk = 1;
498804b
-- 
498804b
1.7.7.6
498804b