diff options
author | Andrea Adami <andrea.adami@gmail.com> | 2009-11-27 21:48:08 +1100 |
---|---|---|
committer | Simon Horman <horms@verge.net.au> | 2009-11-27 21:48:08 +1100 |
commit | d61381a70a57a01b87afee90c976675f047d447d (patch) | |
tree | 1064a3dd0f1cb014306760a24c434548e8dc91dd /kexec/kexec.c | |
parent | 5545216da4360d6494cc7ba574e04620a826a332 (diff) |
kexec.c: workaround getline and fscanf to make it *libc agnostic. Tested against klibc and dietlibc.
Based on a patch by Andrea Adami and Yuri Bushmelev
I have:
* Cleaned up indentation
* Rearranged the logic in get_command_line()
* Increased the buffer for reading the command line
from 1024 to 2048 bytes as this is now possible on x86
Only compile tested against glibc.
Cc: Andrea Adami <andrea.adami@gmail.com>
Cc: Yuri Bushmelev <jay4mail@gmail.com>
Cc: Bernhard Walle <bernhard@bwalle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
Diffstat (limited to 'kexec/kexec.c')
-rw-r--r-- | kexec/kexec.c | 51 |
1 files changed, 36 insertions, 15 deletions
diff --git a/kexec/kexec.c b/kexec/kexec.c index ce105cd..4480d9a 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); + + p = fgets(line, sizeof(line), fp); fclose(fp); - return ret; + + if (p == NULL) + return -1; + + ret = strtol(line, &p, 10); + + /* Too long */ + if (ret > INT_MAX) + return -1; + + /* No digits were found */ + if (p == line) + return -1; + + return (int)ret; } /* @@ -989,24 +1006,28 @@ static void remove_parameter(char *line, const char *param_name) char *get_command_line(void) { FILE *fp; - size_t len; - char *line = NULL; + char *line; + const int sizeof_line = 2048; + + line = malloc(sizeof_line); + if (line == NULL) + die("Could not allocate memory to read /proc/cmdline."); fp = fopen("/proc/cmdline", "r"); if (!fp) - die("Could not read /proc/cmdline."); - getline(&line, &len, fp); + die("Could not open /proc/cmdline."); + + if (fgets(line, sizeof_line, fp) == NULL) + die("Can't read /proc/cmdline."); + fclose(fp); - if (line) { - /* strip newline */ - *(line + strlen(line) - 1) = 0; + /* strip newline */ + line[strlen(line) - 1] = '\0'; - remove_parameter(line, "BOOT_IMAGE"); - if (kexec_flags & KEXEC_ON_CRASH) - remove_parameter(line, "crashkernel"); - } else - line = strdup(""); + remove_parameter(line, "BOOT_IMAGE"); + if (kexec_flags & KEXEC_ON_CRASH) + remove_parameter(line, "crashkernel"); return line; } |