[PATCH] kexec.c: workaround getline and fscanf to make it *libc agnostic. Tested against klibc and dietlibc.

Andrea Adami andrea.adami at gmail.com
Sat Nov 21 18:12:49 EST 2009


Signed-off-by: Andrea Adami <andrea.adami at gmail.com>
Signed-off-by: Yuri Bushmelev <jay4mail at gmail.com>
---
 kexec/kexec.c |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index ce105cd..a3ca79c 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -933,15 +933,32 @@ void usage(void)
 
 static int kexec_loaded(void)
 {
-	int ret;
+	long ret = -1;
 	FILE *fp;
+	char *p;
+	char line[3];
 
 	fp = fopen("/sys/kernel/kexec_loaded", "r");
 	if (fp == NULL)
 		return -1;
-	fscanf(fp, "%d", &ret);
+/*	fscanf(fp, "%d", &ret); */
+	p = fgets(line, sizeof(line), fp);
 	fclose(fp);
-	return ret;
+
+	if ( NULL == p)
+		return -1;
+
+	ret = strtol(line, &p, 10);
+
+	if (ret > INT_MAX)
+	/* Too long */
+		return -1;
+
+	if (p == line)
+	/* No digits were found */
+		return -1;
+
+	return (int)ret;
 }
 
 /*
@@ -989,18 +1006,23 @@ static void remove_parameter(char *line, const char *param_name)
 char *get_command_line(void)
 {
 	FILE *fp;
-	size_t len;
-	char *line = NULL;
+	const int sizeof_line = 1024;
+	char *line = malloc(sizeof_line); /* according to strdup() later */
 
 	fp = fopen("/proc/cmdline", "r");
 	if (!fp)
-		die("Could not read /proc/cmdline.");
-	getline(&line, &len, fp);
+		die("Could not open /proc/cmdline.");
+
+	if ( NULL == fgets(line, sizeof(line), fp) ) {
+		die("Can't read /proc/cmdline.");
+
+/* 	getline(&line, &len, fp); */
 	fclose(fp);
+	}
 
 	if (line) {
 		/* strip newline */
-		*(line + strlen(line) - 1) = 0;
+		line[strlen(line) - 1] = '\0';
 
 		remove_parameter(line, "BOOT_IMAGE");
 		if (kexec_flags & KEXEC_ON_CRASH)
-- 
1.6.4.4




More information about the kexec mailing list