[PATCH 1/4] slurpfile: use lseek() on character nodes instead of fstat() for file size

Simon Horman horms at verge.net.au
Mon Nov 3 18:13:19 EST 2008


On Mon, Nov 03, 2008 at 10:59:00PM +0100, Sebastian Andrzej Siewior wrote:
> From: Sebastian Andrzej Siewior <bigeasy at linutronix.de>
> 
> fstat() is used to determine the filesize before read() and
> it requires a filesystem. That it means it can not be used
> on character nodes. This makes it impossible to obtains the
> kernel from a char node like mtd or more likely ubi.
> We can't use this in every case because files in /proc don't
> support lseek(). This is required by the powerpc part to read
> some device-tree entries.
> 
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy at linutronix.de>
> ---
>  kexec/kexec.c |   22 ++++++++++++++++++++--
>  1 files changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index 0616091..3c3f73e 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -467,7 +467,7 @@ char *slurp_file(const char *filename, off_t *r_size)
>  {
>  	int fd;
>  	char *buf;
> -	off_t size, progress;
> +	off_t size, progress, err;
>  	ssize_t result;
>  	struct stat stats;
>  	
> @@ -486,7 +486,25 @@ char *slurp_file(const char *filename, off_t *r_size)
>  		die("Cannot stat: %s: %s\n",
>  			filename, strerror(errno));
>  	}
> -	size = stats.st_size;
> +	/*
> +	 * Seek in case the kernel is a character node like /dev/ubi0_0.
> +	 * This does not work on regular files which live in /proc and
> +	 * we need this for some /proc/device-tree entries
> +	 */
> +	if (S_ISCHR(stats.st_mode)) {
> +
> +		size = lseek(fd, 0, SEEK_END);
> +		if (size < 0)
> +			die("Can not seek file %s: %s\n", filename, strerror(errno));
> +
> +		err = lseek(fd, 0, SEEK_SET);
> +		if (err < 0)
> +			die("Can not seek to the begin of file %s: %s\n", filename,
> +					strerror(errno));

Please reformat these lines so they are <= 80 columns wide.

> +	} else {
> +		size = stats.st_size;
> +	}
> +
>  	*r_size = size;
>  	buf = xmalloc(size);
>  	progress = 0;
> -- 
> 1.6.0.2
> 
> 
> _______________________________________________
> kexec mailing list
> kexec at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/kexec

-- 
Simon Horman
  VA Linux Systems Japan K.K., Sydney, Australia Satellite Office
  H: www.vergenet.net/~horms/             W: www.valinux.co.jp/en




More information about the kexec mailing list