[PATCH v2 07/13] tools/nolibc: sys_lseek: add pure 64bit lseek

Arnd Bergmann arnd at arndb.de
Tue May 30 01:10:16 PDT 2023


On Mon, May 29, 2023, at 21:54, Zhangjin Wu wrote:
> use sys_llseek instead of sys_lseek to add 64bit seek even in 32bit
> platforms.
>
> This code is based on sysdeps/unix/sysv/linux/lseek.c of glibc and
> src/unistd/lseek.c of musl.
>
> Signed-off-by: Zhangjin Wu <falcon at tinylab.org>
> Signed-off-by: Willy Tarreau <w at 1wt.eu>
> ---
>  tools/include/nolibc/sys.h | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
> index 98cfa2f6d021..d0720af84b6d 100644
> --- a/tools/include/nolibc/sys.h
> +++ b/tools/include/nolibc/sys.h
> @@ -672,7 +672,17 @@ int link(const char *old, const char *new)
>  static __attribute__((unused))
>  off_t sys_lseek(int fd, off_t offset, int whence)
>  {
> +#if defined(__NR_llseek) || defined(__NR__llseek)
> +#ifndef __NR__llseek
> +#define __NR__llseek __NR_llseek
> +#endif
> +	off_t result;
> +	return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, 
> whence) ?: result;
> +#elif defined(__NR_lseek)
>  	return my_syscall3(__NR_lseek, fd, offset, whence);
> +#else
> +#error None of __NR_lseek, __NR_llseek nor __NR__llseek defined, 
> cannot implement sys_lseek()
> +#endif
>  }

This is not technically wrong, but I think a different approach
would be clearer: Instead of having a sys_lseek() that works
differently depending on the macros, why not define the low-level
helpers to match the kernel arguments like

static inline __attribute__((unused))
__kernel_loff_t sys_lseek(int fd, __kernel_loff_t offset, int whence)
{
#ifdef __NR__llseek
	__kernel_loff_t result;
	return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result,  whence) ?: result;
#else
        
#endif
}

static inline __attribute__((unused))
__kernel_off_t sys_lseek(int fd, __kernel_off_t offset, int whence)
{
#ifdef __NR_lseek
	return my_syscall3(__NR_lseek, fd, offset, whence);
#else
        return -ENOSYS;
#endif
}

And then do the selection inside of the actual lseek,
something like

static __attribute__((unused))
off_t lseek(int fd, off_t offset, int whence)
{
        off_t ret = -ENOSYS;

        if (BITS_PER_LONG == 32)
               ret = sys_llseek(fd, offset, whence);

        if (ret == -ENOSYS)
               ret = sys_lseek(fd, offset, whence);

        if (ret < 0) {
                SET_ERRNO(-ret);
                ret = -1;
        }
        return ret;
       
}

For the loff_t selection, there is no real need to handle the
fallback, so this could just be an if()/else to select 32-bit
or 64-bit, but for the time_t ones the fallback is required
for pre-5.6 kernels.

       Arnd



More information about the linux-riscv mailing list