diff options
author | Sven Schnelle <svens@linux.ibm.com> | 2021-12-16 12:43:54 +0100 |
---|---|---|
committer | Simon Horman <horms@verge.net.au> | 2022-01-14 16:00:11 +0100 |
commit | 193e51deccc62544f6423eb5e5eefc8a23aad679 (patch) | |
tree | d410a3f779153b46f22eff3a20abcf1078013fb9 /kexec | |
parent | 91a3d0e00a5c18ee9bdd2c6c03ac64a6471e2559 (diff) |
add slurp_proc_file()
slurp_file() cannot be used to read proc files, as they are returning
a size of zero in stat(). Add a function slurp_proc_file() which is
similar to slurp_file(), but doesn't require the size of the file to
be known.
Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
Diffstat (limited to 'kexec')
-rw-r--r-- | kexec/kexec.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/kexec/kexec.c b/kexec/kexec.c index f63b36b..f3adac5 100644 --- a/kexec/kexec.c +++ b/kexec/kexec.c @@ -1106,6 +1106,57 @@ static void remove_parameter(char *line, const char *param_name) } } +static ssize_t _read(int fd, void *buf, size_t count) +{ + ssize_t ret, offset = 0; + + do { + ret = read(fd, buf + offset, count - offset); + if (ret < 0) { + if ((errno == EINTR) || (errno == EAGAIN)) + continue; + return ret; + } + offset += ret; + } while (ret && offset < count); + + return offset; +} + +static char *slurp_proc_file(const char *filename, size_t *len) +{ + ssize_t ret, startpos = 0; + unsigned int size = 64; + char *buf = NULL, *tmp; + int fd; + + fd = open(filename, O_RDONLY); + if (fd == -1) + return NULL; + + do { + size *= 2; + tmp = realloc(buf, size); + if (!tmp) { + free(buf); + return NULL; + } + buf = tmp; + + ret = _read(fd, buf + startpos, size - startpos); + if (ret < 0) { + free(buf); + return NULL; + } + + startpos += ret; + + } while(ret); + + *len = startpos; + return buf; +} + /* * Returns the contents of the current command line to be used with * --reuse-cmdline option. The function gets called from architecture specific |