From 30e9f21eac66bf2c4ee02e37cb54aa54d616fad4 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 29 Dec 2023 15:23:00 +0100 Subject: [PATCH] Get rid of workarounds for Linux systems without pread/pwrite Many things have changed since we introduced work-arounds for Linux systems with missing pread/pwrite in 1999 (if you are curious, it was in commit bc6346df8d89ece4814be7dff951ec1a7d259938). I believe that it is supported by all reasonably recent Linux systems now. After all, pread() was already defined by POSIX.1-2001. This should also fix problems with musl libc mentioned in GitHub issue #158. --- lib/Makefile | 4 ++-- lib/pci.h | 1 - lib/pread.h | 59 ---------------------------------------------------- lib/proc.c | 8 +++---- lib/sysfs.c | 21 +++---------------- 5 files changed, 8 insertions(+), 85 deletions(-) delete mode 100644 lib/pread.h diff --git a/lib/Makefile b/lib/Makefile index 64d742f..a89ac14 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -125,8 +125,8 @@ params.o: params.c $(INCL) i386-ports.o: i386-ports.c $(INCL) i386-io-hurd.h i386-io-linux.h i386-io-sunos.h i386-io-windows.h i386-io-cygwin.h mmio-ports.o: mmio-ports.c $(INCL) ecam.o: ecam.c $(INCL) -proc.o: proc.c $(INCL) pread.h -sysfs.o: sysfs.c $(INCL) pread.h +proc.o: proc.c $(INCL) +sysfs.o: sysfs.c $(INCL) generic.o: generic.c $(INCL) emulated.o: emulated.c $(INCL) syscalls.o: syscalls.c $(INCL) diff --git a/lib/pci.h b/lib/pci.h index 2322bf7..0b27dc3 100644 --- a/lib/pci.h +++ b/lib/pci.h @@ -87,7 +87,6 @@ struct pci_access { struct udev_hwdb *id_udev_hwdb; int fd; /* proc/sys: fd for config space */ int fd_rw; /* proc/sys: fd opened read-write */ - int fd_pos; /* proc/sys: current position */ int fd_vpd; /* sys: fd for VPD */ struct pci_dev *cached_dev; /* proc/sys: device the fds are for */ void *aux; /* Auxiliary data for use by the back-end */ diff --git a/lib/pread.h b/lib/pread.h deleted file mode 100644 index 99a91b2..0000000 --- a/lib/pread.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * The PCI Library -- Portable interface to pread() and pwrite() - * - * Copyright (c) 1997--2003 Martin Mares - * - * Can be freely distributed and used under the terms of the GNU GPL v2+ - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ - -/* - * We'd like to use pread/pwrite for configuration space accesses, but - * unfortunately it isn't simple at all since all libc's until glibc 2.1 - * don't define it. - */ - -#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0 -/* glibc 2.1 or newer -> pread/pwrite supported automatically */ - -#elif defined(i386) && defined(__GLIBC__) -/* glibc 2.0 on i386 -> call syscalls directly */ -#include -#include -#ifndef SYS_pread -#define SYS_pread 180 -#endif -static int pread(unsigned int fd, void *buf, size_t size, loff_t where) -{ return syscall(SYS_pread, fd, buf, size, where); } -#ifndef SYS_pwrite -#define SYS_pwrite 181 -#endif -static int pwrite(unsigned int fd, void *buf, size_t size, loff_t where) -{ return syscall(SYS_pwrite, fd, buf, size, where); } - -#else -/* In all other cases we use lseek/read/write instead to be safe */ -#define make_rw_glue(op) \ - static int do_##op(struct pci_dev *d, int fd, void *buf, size_t size, int where) \ - { \ - struct pci_access *a = d->access; \ - int r; \ - if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0) \ - return -1; \ - r = op(fd, buf, size); \ - if (r < 0) \ - a->fd_pos = -1; \ - else \ - a->fd_pos = where + r; \ - return r; \ - } -make_rw_glue(read) -make_rw_glue(write) -#define PCI_HAVE_DO_READ -#endif - -#ifndef PCI_HAVE_DO_READ -#define do_read(d,f,b,l,p) pread(f,b,l,p) -#define do_write(d,f,b,l,p) pwrite(f,b,l,p) -#endif diff --git a/lib/proc.c b/lib/proc.c index 2eb5dc5..429cea6 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -1,7 +1,7 @@ /* * The PCI Library -- Configuration Access via /proc/bus/pci * - * Copyright (c) 1997--2003 Martin Mares + * Copyright (c) 1997--2023 Martin Mares * * Can be freely distributed and used under the terms of the GNU GPL v2+. * @@ -19,7 +19,6 @@ #include #include "internal.h" -#include "pread.h" static void proc_config(struct pci_access *a) @@ -162,7 +161,6 @@ proc_setup(struct pci_dev *d, int rw) if (a->fd < 0) a->warning("Cannot open %s", buf); a->cached_dev = d; - a->fd_pos = 0; } return a->fd; } @@ -175,7 +173,7 @@ proc_read(struct pci_dev *d, int pos, byte *buf, int len) if (fd < 0) return 0; - res = do_read(d, fd, buf, len, pos); + res = pread(fd, buf, len, pos); if (res < 0) { d->access->warning("proc_read: read failed: %s", strerror(errno)); @@ -194,7 +192,7 @@ proc_write(struct pci_dev *d, int pos, byte *buf, int len) if (fd < 0) return 0; - res = do_write(d, fd, buf, len, pos); + res = pwrite(fd, buf, len, pos); if (res < 0) { d->access->warning("proc_write: write failed: %s", strerror(errno)); diff --git a/lib/sysfs.c b/lib/sysfs.c index 0a4604b..cd2379e 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -2,7 +2,7 @@ * The PCI Library -- Configuration Access via /sys/bus/pci * * Copyright (c) 2003 Matthew Wilcox - * Copyright (c) 1997--2008 Martin Mares + * Copyright (c) 1997--2023 Martin Mares * * Can be freely distributed and used under the terms of the GNU GPL v2+. * @@ -22,7 +22,6 @@ #include #include "internal.h" -#include "pread.h" static void sysfs_config(struct pci_access *a) @@ -524,7 +523,6 @@ sysfs_setup(struct pci_dev *d, int intent) a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY); if (a->fd < 0) a->warning("Cannot open %s", namebuf); - a->fd_pos = 0; } return a->fd; } @@ -536,7 +534,7 @@ static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len) if (fd < 0) return 0; - res = do_read(d, fd, buf, len, pos); + res = pread(fd, buf, len, pos); if (res < 0) { d->access->warning("sysfs_read: read failed: %s", strerror(errno)); @@ -554,7 +552,7 @@ static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len) if (fd < 0) return 0; - res = do_write(d, fd, buf, len, pos); + res = pwrite(fd, buf, len, pos); if (res < 0) { d->access->warning("sysfs_write: write failed: %s", strerror(errno)); @@ -568,17 +566,6 @@ static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len) return 1; } -#ifdef PCI_HAVE_DO_READ - -/* pread() is not available and do_read() only works for a single fd, so we - * cannot implement read_vpd properly. */ -static int sysfs_read_vpd(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED) -{ - return 0; -} - -#else /* !PCI_HAVE_DO_READ */ - static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len) { int fd = sysfs_setup(d, SETUP_READ_VPD); @@ -597,8 +584,6 @@ static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len) return 1; } -#endif /* PCI_HAVE_DO_READ */ - static void sysfs_cleanup_dev(struct pci_dev *d) { struct pci_access *a = d->access; -- 2.39.5