From: Sven Schnelle <svens@linux.ibm.com>
To: Philipp Rudo <prudo@redhat.com>
Cc: Simon Horman <horms@verge.net.au>, kexec@lists.infradead.org
Subject: Re: [PATCH v2 3/5] add slurp_proc_file()
Date: Thu, 16 Dec 2021 08:28:29 +0100 [thread overview]
Message-ID: <yt9dlf0lj9eq.fsf@linux.ibm.com> (raw)
In-Reply-To: <20211215183602.2797fdf5@rhtmp> (Philipp Rudo's message of "Wed, 15 Dec 2021 18:36:02 +0100")
Philipp Rudo <prudo@redhat.com> writes:
> On Wed, 15 Dec 2021 11:18:34 +0100
> Sven Schnelle <svens@linux.ibm.com> wrote:
>
>> 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>
>> ---
>> kexec/kexec.c | 32 ++++++++++++++++++++++++++++++++
>> 1 file changed, 32 insertions(+)
>>
>> diff --git a/kexec/kexec.c b/kexec/kexec.c
>> index f63b36b771eb..a1acba2adf2a 100644
>> --- a/kexec/kexec.c
>> +++ b/kexec/kexec.c
>> @@ -1106,6 +1106,38 @@ static void remove_parameter(char *line, const char *param_name)
>> }
>> }
>>
>> +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;
>> + *len = startpos;
>> + } while (ret == size);
>
> I don't think this will work as intended.
>
> 1) read returns the bytes read. So ret has a maximum value of
> size - startpos and thus can only be equal to size on the first pass
> when startpos = 0.
>
> 2) it's not an error when read reads less bytes than requested but can
> happen when, e.g. it get's interrupted.
>
> The simplest solution I see is to simply use 'while (ret)' Even when
> that means that there is an extra pass in the loop with an extra
> call to realloc.
>
> The cleaner solution probably would be to put the read into a second
> loop. I.e. the following should work (no tested)
>
> do {
> [...]
> do {
> ret = read(fd, buf + startpos, size - startpos);
> if (ret < 0) {
> free(buf);
> return NULL;
> }
> startpos += ret;
> while (ret && startpos != size);
>
> *len = startpos;
> } while (ret);
>
> Thanks
> Philipp
Sigh. True. I'll prepare a v3...
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
next prev parent reply other threads:[~2021-12-16 7:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-15 10:18 [PATCH v2 0/5] s390: add support for extended cmdline length Sven Schnelle
2021-12-15 10:18 ` [PATCH v2 1/5] s390: add variable command line size Sven Schnelle
2021-12-15 10:18 ` [PATCH v2 2/5] s390: use KEXEC_ALL_OPTIONS Sven Schnelle
2021-12-15 10:18 ` [PATCH v2 3/5] add slurp_proc_file() Sven Schnelle
2021-12-15 17:36 ` Philipp Rudo
2021-12-16 7:28 ` Sven Schnelle [this message]
2021-12-15 10:18 ` [PATCH v2 4/5] use slurp_proc_file() in get_command_line() Sven Schnelle
2021-12-15 10:18 ` [PATCH v2 5/5] s390: add support for --reuse-cmdline Sven Schnelle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=yt9dlf0lj9eq.fsf@linux.ibm.com \
--to=svens@linux.ibm.com \
--cc=horms@verge.net.au \
--cc=kexec@lists.infradead.org \
--cc=prudo@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).