lkml.org 
[lkml]   [2022]   [May]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: [PATCH 2/5] libperf: Add preadn()
Date
From: Adrian Hunter
> Sent: 20 May 2022 14:24
>
> Add preadn() to provide pread() and readn() semantics.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
> ---
> tools/lib/perf/include/internal/lib.h | 2 ++
> tools/lib/perf/lib.c | 20 ++++++++++++++++++++
> 2 files changed, 22 insertions(+)
>
> diff --git a/tools/lib/perf/include/internal/lib.h b/tools/lib/perf/include/internal/lib.h
> index 5175d491b2d4..85471a4b900f 100644
> --- a/tools/lib/perf/include/internal/lib.h
> +++ b/tools/lib/perf/include/internal/lib.h
> @@ -9,4 +9,6 @@ extern unsigned int page_size;
> ssize_t readn(int fd, void *buf, size_t n);
> ssize_t writen(int fd, const void *buf, size_t n);
>
> +ssize_t preadn(int fd, void *buf, size_t n, off_t offs);
> +
> #endif /* __LIBPERF_INTERNAL_CPUMAP_H */
> diff --git a/tools/lib/perf/lib.c b/tools/lib/perf/lib.c
> index 18658931fc71..ecc8035a3ae3 100644
> --- a/tools/lib/perf/lib.c
> +++ b/tools/lib/perf/lib.c
> @@ -38,6 +38,26 @@ ssize_t readn(int fd, void *buf, size_t n)
> return ion(true, fd, buf, n);
> }
>
> +ssize_t preadn(int fd, void *buf, size_t n, off_t offs)
> +{
> + ssize_t ret;
> + off_t cur;
> +
> + cur = lseek(fd, 0, SEEK_CUR);
> + if (cur < 0)
> + return -1;
> +
> + if (lseek(fd, offs, SEEK_SET) < 0)
> + return -1;
> +
> + ret = readn(fd, buf, n);
> +
> + if (lseek(fd, cur, SEEK_CUR) < 0)
> + return -1;
> +
> + return ret;
> +}

Please don't ever write that code, not ever.
It isn't an implementation of pread().

Oh, and shoot whoever put in into (IIRC) uclibc.

pread() needs to use the syscall, you cannot implement
it in userspace.

It is better to have the function missing that that version.

There is a similar problem with clock_nanosleep() and TIMER_ABSTIME
in glibc - completely broken emulation.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

\
 
 \ /
  Last update: 2022-05-20 15:57    [W:0.100 / U:0.136 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site