X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fsysfs.c;h=0e763dcbb9124e532c7863cdf60580583f3c1b64;hb=9f3d614e4578bdec2b60d97caec400b28d4af9d3;hp=1bb4ae91595b9a97cce656cd63c68db3145a5382;hpb=8a5d293fb78ca42a969df9e2b617456391636596;p=pciutils.git diff --git a/lib/sysfs.c b/lib/sysfs.c index 1bb4ae9..0e763dc 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) @@ -106,12 +105,13 @@ sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory) return 0; } n = read(fd, buf, OBJBUFSIZE); + int read_errno = errno; close(fd); if (n < 0) { - warn("Error reading %s: %s", namebuf, strerror(errno)); + warn("Error reading %s: %s", namebuf, strerror(read_errno)); return 0; - } + } if (n >= OBJBUFSIZE) { warn("Value in %s too long", namebuf); @@ -409,12 +409,18 @@ sysfs_fill_info(struct pci_dev *d, unsigned int flags) path_canon = realpath(path_rel, NULL); if (!path_canon || strcmp(path_canon, path_abs) != 0) parent = NULL; + + if (path_canon) + free(path_canon); } if (parent) d->parent = parent; else clear_fill(d, PCI_FILL_PARENT); + + if (path_abs) + free(path_abs); } } @@ -518,7 +524,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; } @@ -530,7 +535,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)); @@ -548,7 +553,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)); @@ -562,17 +567,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); @@ -591,8 +585,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; @@ -602,17 +594,16 @@ static void sysfs_cleanup_dev(struct pci_dev *d) } struct pci_methods pm_linux_sysfs = { - "linux-sysfs", - "The sys filesystem on Linux", - sysfs_config, - sysfs_detect, - sysfs_init, - sysfs_cleanup, - sysfs_scan, - sysfs_fill_info, - sysfs_read, - sysfs_write, - sysfs_read_vpd, - NULL, /* init_dev */ - sysfs_cleanup_dev + .name = "linux-sysfs", + .help = "The sys filesystem on Linux", + .config = sysfs_config, + .detect = sysfs_detect, + .init = sysfs_init, + .cleanup = sysfs_cleanup, + .scan = sysfs_scan, + .fill_info = sysfs_fill_info, + .read = sysfs_read, + .write = sysfs_write, + .read_vpd = sysfs_read_vpd, + .cleanup_dev = sysfs_cleanup_dev, };