[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <de58fd1514d5477aaf144234aa096ab1@AcuMS.aculab.com>
Date: Fri, 20 May 2022 13:55:35 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Adrian Hunter' <adrian.hunter@...el.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>
CC: Jiri Olsa <jolsa@...nel.org>, Namhyung Kim <namhyung@...nel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: RE: [PATCH 2/5] libperf: Add preadn()
From: Adrian Hunter
> Sent: 20 May 2022 14:24
>
> Add preadn() to provide pread() and readn() semantics.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@...el.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)
Powered by blists - more mailing lists