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