summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstekloff <stekloff>2005-04-06 23:18:07 +0000
committerstekloff <stekloff>2005-04-06 23:18:07 +0000
commit5b95d4b7d64a96f5ef773c3ced3128bd3e743548 (patch)
tree60a28af77631785f2adaf18dc2a1d1c9abd97ad3
parentdd8c511d725f816bc64592028ff178616fcde607 (diff)
downloadsysfsutils-5b95d4b7d64a96f5ef773c3ced3128bd3e743548.tar.gz
Ananth's 2.0 initial changes.
-rw-r--r--ChangeLog12
-rw-r--r--Makefile.am3
-rw-r--r--Makefile.in70
-rw-r--r--README2
-rw-r--r--cmd/names.c2
-rw-r--r--cmd/names.h2
-rw-r--r--cmd/systool.c207
-rwxr-xr-xconfigure24
-rw-r--r--configure.ac4
-rw-r--r--docs/libsysfs.txt10
-rw-r--r--include/libsysfs.h188
-rw-r--r--lib/Makefile.am8
-rw-r--r--lib/Makefile.in78
-rw-r--r--lib/dlist.c3
-rw-r--r--lib/sysfs.h24
-rw-r--r--lib/sysfs_bus.c339
-rw-r--r--lib/sysfs_class.c647
-rw-r--r--lib/sysfs_device.c543
-rw-r--r--lib/sysfs_dir.c1002
-rw-r--r--lib/sysfs_driver.c393
-rw-r--r--lib/sysfs_utils.c264
-rw-r--r--test/Makefile.am16
-rw-r--r--test/Makefile.in115
-rw-r--r--test/get_bus_devices_list.c2
-rw-r--r--test/get_classdev_parent.c2
-rw-r--r--test/get_device.c22
-rw-r--r--test/get_driver.c27
-rw-r--r--test/test-defs.h2
-rw-r--r--test/test.c2
-rw-r--r--test/test_bus.c2
-rw-r--r--test/test_class.c2
-rw-r--r--test/test_dir.c2
-rw-r--r--test/test_driver.c2
-rw-r--r--test/test_root.c2
-rw-r--r--test/test_utils.c2
-rw-r--r--test/testout.c2
-rw-r--r--test/write_attr.c2
37 files changed, 1196 insertions, 2833 deletions
diff --git a/ChangeLog b/ChangeLog
index a4ce2d5..e12d8d1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,16 @@
+03/06/2005 - Ananth Mavinakayanahalli <ananth@in.ibm.com>
+ * sysfs_directory, sysfs_link baggage is gone
+ * When an attribute is requested, _only_ that attribute
+ is "stat"ed and read
+ * APIs have been audited and only the relevent ones
+ have been retained.
+ * Fixed sysfs mount point to /sys
+
+04/06/2005 - Daniel Stekloff <dsteklof@us.ibm.com>
+ * Merged tag sysfsutils-1_3_0 with main trunk
+ * Created new sysfsutils-2_0_0 branch
+
01/17/2005 - Kay Sievers <kay.sievers@vrfy.org>
* Fix dlist.c so it compiles with older gcc versions
diff --git a/Makefile.am b/Makefile.am
index 7689330..79493a1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,4 @@
-man_MANS = systool.1
-EXTRA_DIST = docs include $(man_MANS) CREDITS lib/LGPL cmd/GPL test/GPL
+EXTRA_DIST = docs include CREDITS lib/LGPL test/GPL
SUBDIRS = lib cmd test
include_HEADERS = include/libsysfs.h include/dlist.h
diff --git a/Makefile.in b/Makefile.in
index 4ce169a..513dbff 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -31,7 +31,7 @@ localstatedir = @localstatedir@
libdir = @libdir@
infodir = @infodir@
mandir = @mandir@
-includedir = /usr/include/sysfs
+includedir = @includedir@
oldincludedir = /usr/include
pkgdatadir = $(datadir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
@@ -83,8 +83,7 @@ VERSION = @VERSION@
am__include = @am__include@
am__quote = @am__quote@
install_sh = @install_sh@
-man_MANS = systool.1
-EXTRA_DIST = docs include $(man_MANS) CREDITS lib/LGPL cmd/GPL test/GPL
+EXTRA_DIST = docs include CREDITS lib/LGPL test/GPL
SUBDIRS = lib cmd test
include_HEADERS = include/libsysfs.h include/dlist.h
subdir = .
@@ -93,9 +92,6 @@ mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_HEADER = config.h
CONFIG_CLEAN_FILES =
DIST_SOURCES =
-
-NROFF = nroff
-MANS = $(man_MANS)
HEADERS = $(include_HEADERS)
@@ -155,49 +151,6 @@ clean-libtool:
distclean-libtool:
-rm -f libtool
uninstall-info-am:
-
-man1dir = $(mandir)/man1
-install-man1: $(man1_MANS) $(man_MANS)
- @$(NORMAL_INSTALL)
- $(mkinstalldirs) $(DESTDIR)$(man1dir)
- @list='$(man1_MANS) $(dist_man1_MANS) $(nodist_man1_MANS)'; \
- l2='$(man_MANS) $(dist_man_MANS) $(nodist_man_MANS)'; \
- for i in $$l2; do \
- case "$$i" in \
- *.1*) list="$$list $$i" ;; \
- esac; \
- done; \
- for i in $$list; do \
- if test -f $(srcdir)/$$i; then file=$(srcdir)/$$i; \
- else file=$$i; fi; \
- ext=`echo $$i | sed -e 's/^.*\\.//'`; \
- case "$$ext" in \
- 1*) ;; \
- *) ext='1' ;; \
- esac; \
- inst=`echo $$i | sed -e 's/\\.[0-9a-z]*$$//'`; \
- inst=`echo $$inst | sed -e 's/^.*\///'`; \
- inst=`echo $$inst | sed '$(transform)'`.$$ext; \
- echo " $(INSTALL_DATA) $$file $(DESTDIR)$(man1dir)/$$inst"; \
- $(INSTALL_DATA) $$file $(DESTDIR)$(man1dir)/$$inst; \
- done
-uninstall-man1:
- @$(NORMAL_UNINSTALL)
- @list='$(man1_MANS) $(dist_man1_MANS) $(nodist_man1_MANS)'; \
- l2='$(man_MANS) $(dist_man_MANS) $(nodist_man_MANS)'; \
- for i in $$l2; do \
- case "$$i" in \
- *.1*) list="$$list $$i" ;; \
- esac; \
- done; \
- for i in $$list; do \
- ext=`echo $$i | sed -e 's/^.*\\.//'`; \
- inst=`echo $$i | sed -e 's/\\.[0-9a-z]*$$//'`; \
- inst=`echo $$inst | sed -e 's/^.*\///'`; \
- inst=`echo $$inst | sed '$(transform)'`.$$ext; \
- echo " rm -f $(DESTDIR)$(man1dir)/$$inst"; \
- rm -f $(DESTDIR)$(man1dir)/$$inst; \
- done
includeHEADERS_INSTALL = $(INSTALL_HEADER)
install-includeHEADERS: $(include_HEADERS)
@$(NORMAL_INSTALL)
@@ -328,7 +281,7 @@ distcleancheck_listfiles = find . -type f -print
distdir: $(DISTFILES)
$(am__remove_distdir)
mkdir $(distdir)
- $(mkinstalldirs) $(distdir)/cmd $(distdir)/include $(distdir)/lib $(distdir)/test
+ $(mkinstalldirs) $(distdir)/include $(distdir)/lib $(distdir)/test
@list='$(DISTFILES)'; for file in $$list; do \
if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \
@@ -419,10 +372,10 @@ distcleancheck: distclean
exit 1; } >&2
check-am: all-am
check: check-recursive
-all-am: Makefile $(MANS) $(HEADERS) config.h
+all-am: Makefile $(HEADERS) config.h
installdirs: installdirs-recursive
installdirs-am:
- $(mkinstalldirs) $(DESTDIR)$(man1dir) $(DESTDIR)$(includedir)
+ $(mkinstalldirs) $(DESTDIR)$(includedir)
install: install-recursive
install-exec: install-exec-recursive
@@ -465,13 +418,13 @@ info: info-recursive
info-am:
-install-data-am: install-includeHEADERS install-man
+install-data-am: install-includeHEADERS
install-exec-am:
install-info: install-info-recursive
-install-man: install-man1
+install-man:
installcheck-am:
@@ -484,12 +437,10 @@ mostlyclean: mostlyclean-recursive
mostlyclean-am: mostlyclean-generic mostlyclean-libtool
-uninstall-am: uninstall-includeHEADERS uninstall-info-am uninstall-man
+uninstall-am: uninstall-includeHEADERS uninstall-info-am
uninstall-info: uninstall-info-recursive
-uninstall-man: uninstall-man1
-
.PHONY: $(RECURSIVE_TARGETS) GTAGS all all-am check check-am clean \
clean-generic clean-libtool clean-recursive dist dist-all \
dist-gzip distcheck distclean distclean-generic distclean-hdr \
@@ -498,15 +449,14 @@ uninstall-man: uninstall-man1
info-recursive install install-am install-data install-data-am \
install-data-recursive install-exec install-exec-am \
install-exec-recursive install-includeHEADERS install-info \
- install-info-am install-info-recursive install-man install-man1 \
+ install-info-am install-info-recursive install-man \
install-recursive install-strip installcheck installcheck-am \
installdirs installdirs-am installdirs-recursive \
maintainer-clean maintainer-clean-generic \
maintainer-clean-recursive mostlyclean mostlyclean-generic \
mostlyclean-libtool mostlyclean-recursive tags tags-recursive \
uninstall uninstall-am uninstall-includeHEADERS \
- uninstall-info-am uninstall-info-recursive uninstall-man \
- uninstall-man1 uninstall-recursive
+ uninstall-info-am uninstall-info-recursive uninstall-recursive
dist-hook:
diff --git a/README b/README
index fa71a87..ba39813 100644
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
- System Utilities Package - Includes Libsysfs(v. 1.2.0)
+ Sysfs Utilities Package - Includes Libsysfs(v. 2.0.0)
======================================================
Contents
diff --git a/cmd/names.c b/cmd/names.c
index c63116f..1e80d1b 100644
--- a/cmd/names.c
+++ b/cmd/names.c
@@ -1,5 +1,5 @@
/*
- * $Id: names.c,v 1.2 2005/04/06 20:57:12 stekloff Exp $
+ * $Id: names.c,v 1.2.2.1 2005/04/06 23:18:11 stekloff Exp $
*
* The PCI Library -- ID to Name Translation
*
diff --git a/cmd/names.h b/cmd/names.h
index da20938..dc32e9f 100644
--- a/cmd/names.h
+++ b/cmd/names.h
@@ -1,5 +1,5 @@
/*
- * $Id: names.h,v 1.2 2005/04/06 20:57:12 stekloff Exp $
+ * $Id: names.h,v 1.2.2.1 2005/04/06 23:18:11 stekloff Exp $
*
* The PCI Library
*
diff --git a/cmd/systool.c b/cmd/systool.c
index d3f938a..adef26d 100644
--- a/cmd/systool.c
+++ b/cmd/systool.c
@@ -3,7 +3,7 @@
*
* Sysfs utility to list buses, classes, and devices
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -33,10 +33,26 @@
#include "libsysfs.h"
#include "names.h"
+#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
+#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
+
+#define safestrcpymax(to, from, max) \
+do { \
+ to[max-1] = '\0'; \
+ strncpy(to, from, max-1); \
+} while (0)
+
+#define safestrcatmax(to, from, max) \
+do { \
+ to[max-1] = '\0'; \
+ strncat(to, from, max - strlen(to)-1); \
+} while (0)
+
/* Command Options */
static int show_options = 0; /* bitmask of show options */
static char *attribute_to_show = NULL; /* show value for this attribute */
static char *device_to_show = NULL; /* show only this bus device */
+static char sysfs_mnt_path[SYSFS_PATH_MAX]; /* sysfs mount point */
struct pci_access *pacc = NULL;
char *show_bus = NULL;
@@ -54,7 +70,8 @@ static void show_class_device(struct sysfs_class_device *dev, int level);
#define SHOW_ALL 0xff
-static char cmd_options[] = "aA:b:c:CdDhpPr:v";
+static char cmd_options[] = "aA:b:c:dDhpP:v";
+//static char cmd_options[] = "aA:b:c:CdDhpPr:v";
/*
* binary_files - defines existing sysfs binary files. These files will be
@@ -86,11 +103,15 @@ static void usage(void)
fprintf(stdout, "\t-d\t\t\tShow only devices\n");
fprintf(stdout, "\t-h\t\t\tShow usage\n");
fprintf(stdout, "\t-p\t\t\tShow path to device/driver\n");
+#if 0
fprintf(stdout,
"\t-r <root_device>\tShow a specific root device tree\n");
+#endif
fprintf(stdout, "\t-v\t\t\tShow all attributes with values\n");
fprintf(stdout, "\t-A <attribute_name>\tShow attribute value\n");
+#if 0
fprintf(stdout, "\t-C\t\t\tShow device's children\n");
+#endif
fprintf(stdout, "\t-D\t\t\tShow only drivers\n");
fprintf(stdout, "\t-P\t\t\tShow device's parent\n");
}
@@ -116,10 +137,11 @@ static void remove_end_newline(char *value)
{
char *p = value + (strlen(value) - 1);
- if (p != NULL && *p == '\n')
+ if (p && *p == '\n')
*p = '\0';
}
+#if 0
/**
* show_device_children: prints out device subdirs.
* @children: dlist of child devices.
@@ -149,6 +171,7 @@ static void show_device_children(struct sysfs_device *device, int level)
sysfs_close_device_tree(temp_device);
}
}
+#endif
/**
* isbinaryvalue: checks to see if attribute is binary or not.
@@ -159,7 +182,7 @@ static int isbinaryvalue(struct sysfs_attribute *attr)
{
int i;
- if (attr == NULL || attr->value == NULL)
+ if (!attr || !attr->value)
return 0;
for (i = 0; i < binfiles; i++)
@@ -175,11 +198,11 @@ static int isbinaryvalue(struct sysfs_attribute *attr)
*/
static void show_attribute_value(struct sysfs_attribute *attr, int level)
{
- if (attr == NULL)
+ if (!attr)
return;
if (attr->method & SYSFS_METHOD_SHOW) {
- if (isbinaryvalue(attr) != 0) {
+ if (isbinaryvalue(attr)) {
int i;
for (i = 0; i < attr->len; i++) {
if (!(i % 16) && (i != 0)) {
@@ -192,7 +215,7 @@ static void show_attribute_value(struct sysfs_attribute *attr, int level)
}
fprintf(stdout, "\n");
- } else if (attr->value != NULL && strlen(attr->value) > 0) {
+ } else if (attr->value && strlen(attr->value) > 0) {
remove_end_newline(attr->value);
fprintf(stdout, "\"%s\"\n", attr->value);
} else
@@ -208,7 +231,7 @@ static void show_attribute_value(struct sysfs_attribute *attr, int level)
*/
static void show_attribute(struct sysfs_attribute *attr, int level)
{
- if (attr == NULL)
+ if (!attr)
return;
if (show_options & SHOW_ALL_ATTRIB_VALUES) {
@@ -235,12 +258,12 @@ static void show_attribute(struct sysfs_attribute *attr, int level)
*/
static void show_attributes(struct dlist *attributes, int level)
{
- if (attributes != NULL) {
- struct sysfs_attribute *cur = NULL;
+ if (attributes) {
+ struct sysfs_attribute *cur;
dlist_for_each_data(attributes, cur,
struct sysfs_attribute) {
- show_attribute(cur, (level));
+ show_attribute(cur, level);
}
}
}
@@ -251,7 +274,7 @@ static void show_attributes(struct dlist *attributes, int level)
*/
static void show_device_parent(struct sysfs_device *device, int level)
{
- struct sysfs_device *parent = NULL;
+ struct sysfs_device *parent;
parent = sysfs_get_device_parent(device);
if (parent) {
@@ -268,28 +291,33 @@ static void show_device_parent(struct sysfs_device *device, int level)
*/
static void show_device(struct sysfs_device *device, int level)
{
- struct dlist *attributes = NULL;
+ struct dlist *attributes;
unsigned int vendor_id, device_id;
char buf[128], value[256], path[SYSFS_PATH_MAX];
- if (device != NULL) {
+ if (device) {
indent(level);
- if (show_bus != NULL && (!(strcmp(show_bus, "pci")))) {
+ if (show_bus && (!(strcmp(show_bus, "pci")))) {
fprintf(stdout, "%s ", device->bus_id);
memset(path, 0, SYSFS_PATH_MAX);
memset(value, 0, SYSFS_PATH_MAX);
safestrcpy(path, device->path);
safestrcat(path, "/config");
- if ((sysfs_read_attribute_value(path,
- value, 256)) == 0) {
- vendor_id = get_pciconfig_word
- (PCI_VENDOR_ID, value);
- device_id = get_pciconfig_word
- (PCI_DEVICE_ID, value);
- fprintf(stdout, "%s\n",
- pci_lookup_name(pacc, buf, 128,
- PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
+ struct sysfs_attribute *attr;
+ attr = sysfs_open_attribute(path);
+ if (attr) {
+ if (!sysfs_read_attribute(attr)) {
+ vendor_id = get_pciconfig_word
+ (PCI_VENDOR_ID, attr->value);
+ device_id = get_pciconfig_word
+ (PCI_DEVICE_ID, attr->value);
+ fprintf(stdout, "%s\n",
+ pci_lookup_name(pacc, buf, 128,
+ PCI_LOOKUP_VENDOR |
+ PCI_LOOKUP_DEVICE,
vendor_id, device_id, 0, 0));
+ }
+ sysfs_close_attribute(attr);
} else
fprintf(stdout, "\n");
} else
@@ -304,15 +332,16 @@ static void show_device(struct sysfs_device *device, int level)
if (show_options & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE |
SHOW_ALL_ATTRIB_VALUES)) {
attributes = sysfs_get_device_attributes(device);
- if (attributes != NULL)
+ if (attributes)
show_attributes(attributes, (level+2));
}
- if ((device_to_show != NULL) &&
- (show_options & SHOW_CHILDREN)) {
+#if 0
+ if ((device_to_show) && (show_options & SHOW_CHILDREN)) {
show_options &= ~SHOW_CHILDREN;
show_device_children(device, (level+2));
}
- if ((device_to_show != NULL) && (show_options & SHOW_PARENT)) {
+#endif
+ if ((device_to_show) && (show_options & SHOW_PARENT)) {
show_options &= ~SHOW_PARENT;
show_device_parent(device, (level+2));
}
@@ -328,12 +357,12 @@ static void show_device(struct sysfs_device *device, int level)
*/
static void show_driver_attributes(struct sysfs_driver *driver, int level)
{
- if (driver != NULL) {
- struct dlist *attributes = NULL;
+ if (driver) {
+ struct dlist *attributes;
attributes = sysfs_get_driver_attributes(driver);
- if (attributes != NULL) {
- struct sysfs_attribute *cur = NULL;
+ if (attributes) {
+ struct sysfs_attribute *cur;
dlist_for_each_data(attributes, cur,
struct sysfs_attribute) {
@@ -350,11 +379,11 @@ static void show_driver_attributes(struct sysfs_driver *driver, int level)
*/
static void show_driver(struct sysfs_driver *driver, int level)
{
- struct dlist *devlist = NULL;
+ struct dlist *devlist;
- if (driver != NULL) {
+ if (driver) {
indent(level);
- fprintf (stdout, "Driver = \"%s\"\n", driver->name);
+ fprintf(stdout, "Driver = \"%s\"\n", driver->name);
if (show_options & (SHOW_PATH | SHOW_ALL_ATTRIB_VALUES)) {
indent(level);
fprintf(stdout, "Driver path = \"%s\"\n",
@@ -364,8 +393,8 @@ static void show_driver(struct sysfs_driver *driver, int level)
| SHOW_ALL_ATTRIB_VALUES))
show_driver_attributes(driver, (level+2));
devlist = sysfs_get_driver_devices(driver);
- if (devlist != NULL) {
- struct sysfs_device *cur = NULL;
+ if (devlist) {
+ struct sysfs_device *cur;
indent(level+2);
fprintf(stdout, "Devices using \"%s\" are:\n",
@@ -385,6 +414,7 @@ static void show_driver(struct sysfs_driver *driver, int level)
}
}
+#if 0
/**
* show_device_tree: prints out device tree.
* @root: root device
@@ -408,6 +438,7 @@ static void show_device_tree(struct sysfs_device *root, int level)
}
}
}
+#endif
/**
* show_sysfs_bus: prints out everything on a bus.
@@ -416,13 +447,13 @@ static void show_device_tree(struct sysfs_device *root, int level)
*/
static int show_sysfs_bus(char *busname)
{
- struct sysfs_bus *bus = NULL;
- struct sysfs_device *curdev = NULL;
- struct sysfs_driver *curdrv = NULL;
- struct dlist *devlist = NULL;
- struct dlist *drvlist = NULL;
+ struct sysfs_bus *bus;
+ struct sysfs_device *curdev;
+ struct sysfs_driver *curdrv;
+ struct dlist *devlist;
+ struct dlist *drvlist;
- if (busname == NULL) {
+ if (!busname) {
errno = EINVAL;
return 1;
}
@@ -437,11 +468,10 @@ static int show_sysfs_bus(char *busname)
fprintf(stdout, "\n");
if (show_options & SHOW_DEVICES) {
devlist = sysfs_get_bus_devices(bus);
- if (devlist != NULL) {
+ if (devlist) {
dlist_for_each_data(devlist, curdev,
struct sysfs_device) {
- if (device_to_show == NULL ||
- (strcmp(device_to_show,
+ if (!device_to_show || (strcmp(device_to_show,
curdev->bus_id) == 0))
show_device(curdev, 2);
}
@@ -449,7 +479,7 @@ static int show_sysfs_bus(char *busname)
}
if (show_options & SHOW_DRIVERS) {
drvlist = sysfs_get_bus_drivers(bus);
- if (drvlist != NULL) {
+ if (drvlist) {
dlist_for_each_data(drvlist, curdrv,
struct sysfs_driver) {
show_driver(curdrv, 2);
@@ -466,7 +496,7 @@ static int show_sysfs_bus(char *busname)
*/
static void show_classdev_parent(struct sysfs_class_device *dev, int level)
{
- struct sysfs_class_device *parent = NULL;
+ struct sysfs_class_device *parent;
parent = sysfs_get_classdev_parent(dev);
if (parent) {
@@ -484,11 +514,10 @@ static void show_classdev_parent(struct sysfs_class_device *dev, int level)
*/
static void show_class_device(struct sysfs_class_device *dev, int level)
{
- struct dlist *attributes = NULL;
- struct sysfs_device *device = NULL;
- struct sysfs_driver *driver = NULL;
+ struct dlist *attributes;
+ struct sysfs_device *device;
- if (dev != NULL) {
+ if (dev) {
indent(level);
fprintf(stdout, "Class Device = \"%s\"\n", dev->name);
if (show_options & (SHOW_PATH | SHOW_ALL_ATTRIB_VALUES)) {
@@ -499,23 +528,17 @@ static void show_class_device(struct sysfs_class_device *dev, int level)
if (show_options & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE
| SHOW_ALL_ATTRIB_VALUES)) {
attributes = sysfs_get_classdev_attributes(dev);
- if (attributes != NULL)
+ if (attributes)
show_attributes(attributes, (level+2));
fprintf(stdout, "\n");
}
if (show_options & (SHOW_DEVICES | SHOW_ALL_ATTRIB_VALUES)) {
device = sysfs_get_classdev_device(dev);
- if (device != NULL) {
+ if (device) {
show_device(device, (level+2));
}
}
- if (show_options & (SHOW_DRIVERS | SHOW_ALL_ATTRIB_VALUES)) {
- driver = sysfs_get_classdev_driver(dev);
- if (driver != NULL) {
- show_driver(driver, (level+2));
- }
- }
- if ((device_to_show != NULL) && (show_options & SHOW_PARENT)) {
+ if ((device_to_show) && (show_options & SHOW_PARENT)) {
show_options &= ~SHOW_PARENT;
show_classdev_parent(dev, level+2);
}
@@ -532,11 +555,11 @@ static void show_class_device(struct sysfs_class_device *dev, int level)
*/
static int show_sysfs_class(char *classname)
{
- struct sysfs_class *cls = NULL;
- struct sysfs_class_device *cur = NULL;
- struct dlist *clsdevlist = NULL;
+ struct sysfs_class *cls;
+ struct sysfs_class_device *cur;
+ struct dlist *clsdevlist;
- if (classname == NULL) {
+ if (!classname) {
errno = EINVAL;
return 1;
}
@@ -547,7 +570,7 @@ static int show_sysfs_class(char *classname)
}
fprintf(stdout, "Class = \"%s\"\n\n", classname);
clsdevlist = sysfs_get_class_devices(cls);
- if (clsdevlist != NULL) {
+ if (clsdevlist) {
dlist_for_each_data(clsdevlist, cur,
struct sysfs_class_device) {
if (device_to_show == NULL || (strcmp(device_to_show,
@@ -560,6 +583,7 @@ static int show_sysfs_class(char *classname)
return 0;
}
+#if 0
/**
* show_sysfs_root: prints out sysfs root device tree
* @rootname: device root to print.
@@ -598,6 +622,7 @@ static int show_sysfs_root(char *rootname)
return 0;
}
+#endif
/**
* show_default_info: prints current buses, classes, and root devices
@@ -607,31 +632,37 @@ static int show_sysfs_root(char *rootname)
static int show_default_info(void)
{
char subsys[SYSFS_NAME_LEN];
- struct dlist *list = NULL;
- char *cur = NULL;
+ struct dlist *list;
+ char *cur;
int retval = 0;
- safestrcpy(subsys, SYSFS_BUS_NAME);
- list = sysfs_open_subsystem_list(subsys);
- if (list != NULL) {
+ safestrcpy(subsys, sysfs_mnt_path);
+ safestrcat(subsys, "/");
+ safestrcat(subsys, SYSFS_BUS_NAME);
+ list = sysfs_open_directory_list(subsys);
+ if (list) {
fprintf(stdout, "Supported sysfs buses:\n");
dlist_for_each_data(list, cur, char)
fprintf(stdout, "\t%s\n", cur);
sysfs_close_list(list);
}
- safestrcpy(subsys, SYSFS_CLASS_NAME);
- list = sysfs_open_subsystem_list(subsys);
- if (list != NULL) {
+ safestrcpy(subsys, sysfs_mnt_path);
+ safestrcat(subsys, "/");
+ safestrcat(subsys, SYSFS_CLASS_NAME);
+ list = sysfs_open_directory_list(subsys);
+ if (list) {
fprintf(stdout, "Supported sysfs classes:\n");
dlist_for_each_data(list, cur, char)
fprintf(stdout, "\t%s\n", cur);
sysfs_close_list(list);
}
- safestrcpy(subsys, SYSFS_DEVICES_NAME);
- list = sysfs_open_subsystem_list(subsys);
- if (list != NULL) {
+ safestrcpy(subsys, sysfs_mnt_path);
+ safestrcat(subsys, "/");
+ safestrcat(subsys, SYSFS_DEVICES_NAME);
+ list = sysfs_open_directory_list(subsys);
+ if (list) {
fprintf(stdout, "Supported sysfs devices:\n");
dlist_for_each_data(list, cur, char)
fprintf(stdout, "\t%s\n", cur);
@@ -647,9 +678,7 @@ static int show_default_info(void)
*/
static int check_sysfs_is_mounted(void)
{
- char path[SYSFS_PATH_MAX];
-
- if (sysfs_get_mnt_path(path, SYSFS_PATH_MAX) != 0)
+ if (sysfs_get_mnt_path(sysfs_mnt_path, SYSFS_PATH_MAX) != 0)
return 0;
return 1;
}
@@ -685,9 +714,11 @@ int main(int argc, char *argv[])
case 'c':
show_class = optarg;
break;
+#if 0
case 'C':
show_options |= SHOW_CHILDREN;
break;
+#endif
case 'd':
show_options |= SHOW_DEVICES;
break;
@@ -704,9 +735,11 @@ int main(int argc, char *argv[])
case 'P':
show_options |= SHOW_PARENT;
break;
+#if 0
case 'r':
show_root = optarg;
break;
+#endif
case 'v':
show_options |= SHOW_ALL_ATTRIB_VALUES;
break;
@@ -742,7 +775,7 @@ int main(int argc, char *argv[])
exit(1);
}
- if ((show_bus == NULL && show_class == NULL && show_root == NULL) &&
+ if ((!show_bus && !show_class && !show_root) &&
(show_options & (SHOW_ATTRIBUTES |
SHOW_ATTRIBUTE_VALUE | SHOW_DEVICES |
SHOW_DRIVERS | SHOW_ALL_ATTRIB_VALUES))) {
@@ -755,7 +788,7 @@ int main(int argc, char *argv[])
if (!(show_options & (SHOW_DEVICES | SHOW_DRIVERS)))
show_options |= SHOW_DEVICES;
- if (show_bus != NULL) {
+ if (show_bus) {
/* if ((!(strcmp(show_bus, "pci"))) &&
(show_options & SHOW_DEVICES)) { */
if ((!(strcmp(show_bus, "pci")))) {
@@ -766,15 +799,17 @@ int main(int argc, char *argv[])
}
retval = show_sysfs_bus(show_bus);
}
- if (show_class != NULL)
+ if (show_class)
retval = show_sysfs_class(show_class);
- if (show_root != NULL)
+#if 0
+ if (show_root)
retval = show_sysfs_root(show_root);
+#endif
- if (show_bus == NULL && show_class == NULL && show_root == NULL)
+ if (!show_bus && !show_class && !show_root)
retval = show_default_info();
- if (show_bus != NULL) {
+ if (show_bus) {
/*if ((!(strcmp(show_bus, "pci"))) &&
(show_options & SHOW_DEVICES)) { */
if ((!(strcmp(show_bus, "pci")))) {
diff --git a/configure b/configure
index 99cdf30..24296af 100755
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
#! /bin/sh
# Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.57 for sysfsutils 1.2.0.
+# Generated by GNU Autoconf 2.57 for sysfsutils 2.0.0.
#
# Report bugs to <linux-diag-devel@lists.sourceforge.net>.
#
@@ -427,8 +427,8 @@ SHELL=${CONFIG_SHELL-/bin/sh}
# Identity of this package.
PACKAGE_NAME='sysfsutils'
PACKAGE_TARNAME='sysfsutils'
-PACKAGE_VERSION='1.2.0'
-PACKAGE_STRING='sysfsutils 1.2.0'
+PACKAGE_VERSION='2.0.0'
+PACKAGE_STRING='sysfsutils 2.0.0'
PACKAGE_BUGREPORT='linux-diag-devel@lists.sourceforge.net'
ac_unique_file="config.h.in"
@@ -938,7 +938,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF
-\`configure' configures sysfsutils 1.2.0 to adapt to many kinds of systems.
+\`configure' configures sysfsutils 2.0.0 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]...
@@ -1004,7 +1004,7 @@ fi
if test -n "$ac_init_help"; then
case $ac_init_help in
- short | recursive ) echo "Configuration of sysfsutils 1.2.0:";;
+ short | recursive ) echo "Configuration of sysfsutils 2.0.0:";;
esac
cat <<\_ACEOF
@@ -1099,7 +1099,7 @@ fi
test -n "$ac_init_help" && exit 0
if $ac_init_version; then
cat <<\_ACEOF
-sysfsutils configure 1.2.0
+sysfsutils configure 2.0.0
generated by GNU Autoconf 2.57
Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
@@ -1114,7 +1114,7 @@ cat >&5 <<_ACEOF
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
-It was created by sysfsutils $as_me 1.2.0, which was
+It was created by sysfsutils $as_me 2.0.0, which was
generated by GNU Autoconf 2.57. Invocation command line was
$ $0 $@
@@ -1704,7 +1704,7 @@ fi
# Define the identity of the package.
PACKAGE=sysfsutils
- VERSION=1.2.0
+ VERSION=2.0.0
cat >>confdefs.h <<_ACEOF
@@ -10044,7 +10044,7 @@ fi
done
- ac_config_files="$ac_config_files Makefile cmd/Makefile lib/Makefile test/Makefile"
+ ac_config_files="$ac_config_files Makefile lib/Makefile cmd/Makefile test/Makefile"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@@ -10413,7 +10413,7 @@ _ASBOX
} >&5
cat >&5 <<_CSEOF
-This file was extended by sysfsutils $as_me 1.2.0, which was
+This file was extended by sysfsutils $as_me 2.0.0, which was
generated by GNU Autoconf 2.57. Invocation command line was
CONFIG_FILES = $CONFIG_FILES
@@ -10476,7 +10476,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF
ac_cs_version="\\
-sysfsutils config.status 1.2.0
+sysfsutils config.status 2.0.0
configured by $0, generated by GNU Autoconf 2.57,
with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\"
@@ -10588,8 +10588,8 @@ do
case "$ac_config_target" in
# Handling of arguments.
"Makefile" ) CONFIG_FILES="$CONFIG_FILES Makefile" ;;
- "cmd/Makefile" ) CONFIG_FILES="$CONFIG_FILES cmd/Makefile" ;;
"lib/Makefile" ) CONFIG_FILES="$CONFIG_FILES lib/Makefile" ;;
+ "cmd/Makefile" ) CONFIG_FILES="$CONFIG_FILES cmd/Makefile" ;;
"test/Makefile" ) CONFIG_FILES="$CONFIG_FILES test/Makefile" ;;
"depfiles" ) CONFIG_COMMANDS="$CONFIG_COMMANDS depfiles" ;;
"config.h" ) CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;;
diff --git a/configure.ac b/configure.ac
index d17a20a..a39d7ed 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
# Process this file with autoconf to produce a configure script.
-AC_INIT(sysfsutils, 1.2.0, linux-diag-devel@lists.sourceforge.net)
+AC_INIT(sysfsutils, 2.0.0, linux-diag-devel@lists.sourceforge.net)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([config.h.in])
AM_CONFIG_HEADER([config.h])
@@ -30,7 +30,7 @@ AC_FUNC_STAT
AC_CHECK_FUNCS([bzero getmntent isascii memset strchr strerror strrchr strstr strtol])
AC_CONFIG_FILES([Makefile
- cmd/Makefile
lib/Makefile
+ cmd/Makefile
test/Makefile])
AC_OUTPUT
diff --git a/docs/libsysfs.txt b/docs/libsysfs.txt
index c3e3b1f..b877cb8 100644
--- a/docs/libsysfs.txt
+++ b/docs/libsysfs.txt
@@ -894,11 +894,11 @@ Description: Reads the given sysfs_directory to create a list of subdirs.
Arguments: struct sysfs_directory *sysdir sysfs_directory whose
subdirs have to be read
-Returns: 0 with success.
- -1 with error. Errno will be set with error, returning
- - EINVAL for invalid arguments
-
-Prototype: int sysfs_read_dir_subdirs
+Returns: struct dlist * of links with success
+ NULL with error. Errno will be set on error, returning EINVAL
+ for invalid arguments
+
+Prototype: struct dlist *sysfs_read_dir_subdirs
(struct sysfs_directory *sysdir)
-------------------------------------------------------------------------------
diff --git a/include/libsysfs.h b/include/libsysfs.h
index 29194f6..057dcef 100644
--- a/include/libsysfs.h
+++ b/include/libsysfs.h
@@ -3,7 +3,7 @@
*
* Header Definitions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2004-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -27,27 +27,6 @@
#include <string.h>
#include "dlist.h"
-/*
- * Defines to prevent buffer overruns
- */
-#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
-#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
-
-#define safestrcpymax(to, from, max) \
-do { \
- to[max-1] = '\0'; \
- strncpy(to, from, max-1); \
-} while (0)
-
-#define safestrcatmax(to, from, max) \
-do { \
- to[max-1] = '\0'; \
- strncat(to, from, max - strlen(to)-1); \
-} while (0)
-
-/*
- * Generic #defines go here..
- */
#define SYSFS_FSTYPE_NAME "sysfs"
#define SYSFS_PROC_MNTS "/proc/mounts"
#define SYSFS_BUS_NAME "bus"
@@ -55,106 +34,92 @@ do { \
#define SYSFS_BLOCK_NAME "block"
#define SYSFS_DEVICES_NAME "devices"
#define SYSFS_DRIVERS_NAME "drivers"
+#define SYSFS_MODULE_NAME "module"
#define SYSFS_NAME_ATTRIBUTE "name"
#define SYSFS_UNKNOWN "unknown"
#define SYSFS_PATH_ENV "SYSFS_PATH"
-#define SYSFS_PATH_MAX 255
-#define SYSFS_NAME_LEN 50
-#define SYSFS_BUS_ID_SIZE 20
+#define SYSFS_PATH_MAX 256
+#define SYSFS_NAME_LEN 64
+#define SYSFS_BUS_ID_SIZE 32
+
+enum sysfs_attribute_method {
+ SYSFS_METHOD_SHOW = 0x01, /* attr can be read by user */
+ SYSFS_METHOD_STORE = 0x02, /* attr can be changed by user */
+};
-#define SYSFS_METHOD_SHOW 0x01 /* attr can be read by user */
-#define SYSFS_METHOD_STORE 0x02 /* attr can be changed by user */
+/* NOTE: statically define mnt path for sysfs */
+#define SYSFS_MNT_PATH "/sys"
/*
- * NOTE: We have the statically allocated "name" as the first element of all
+ * NOTE:
+ * 1. We have the statically allocated "name" as the first element of all
* the structures. This feature is used in the "sorter" function for dlists
+ * 2. As is the case with attrlist
+ * 3. As is the case with path
*/
-
struct sysfs_attribute {
char name[SYSFS_NAME_LEN];
char path[SYSFS_PATH_MAX];
char *value;
- unsigned short len; /* value length */
- unsigned short method; /* show and store */
-};
-
-struct sysfs_link {
- char name[SYSFS_NAME_LEN];
- char path[SYSFS_PATH_MAX];
- char target[SYSFS_PATH_MAX];
-};
-
-struct sysfs_directory {
- char name[SYSFS_NAME_LEN];
- char path[SYSFS_PATH_MAX];
-
- /* Private: for internal use only */
- struct dlist *subdirs;
- struct dlist *links;
- struct dlist *attributes;
+ unsigned short len; /* value length */
+ enum sysfs_attribute_method method; /* show and store */
};
struct sysfs_driver {
char name[SYSFS_NAME_LEN];
char path[SYSFS_PATH_MAX];
+ struct dlist *attrlist;
+ char bus[SYSFS_NAME_LEN];
/* Private: for internal use only */
struct dlist *devices;
- struct sysfs_directory *directory;
};
struct sysfs_device {
char name[SYSFS_NAME_LEN];
+ char path[SYSFS_PATH_MAX];
+ struct dlist *attrlist;
char bus_id[SYSFS_NAME_LEN];
char bus[SYSFS_NAME_LEN];
char driver_name[SYSFS_NAME_LEN];
- char path[SYSFS_PATH_MAX];
/* Private: for internal use only */
struct sysfs_device *parent;
+ /* NOTE - we still don't populate this */
struct dlist *children;
- struct sysfs_directory *directory;
-};
-
-struct sysfs_root_device {
- char name[SYSFS_NAME_LEN];
- char path[SYSFS_PATH_MAX];
-
- /* Private: for internal use only */
- struct dlist *devices;
- struct sysfs_directory *directory;
};
+/* NOTE: not used as of now */
struct sysfs_bus {
char name[SYSFS_NAME_LEN];
char path[SYSFS_PATH_MAX];
+ struct dlist *attrlist;
/* Private: for internal use only */
struct dlist *drivers;
struct dlist *devices;
- struct sysfs_directory *directory;
};
struct sysfs_class_device {
char name[SYSFS_NAME_LEN];
- char classname[SYSFS_NAME_LEN];
char path[SYSFS_PATH_MAX];
+ struct dlist *attrlist;
+ char classname[SYSFS_NAME_LEN];
/* Private: for internal use only */
struct sysfs_class_device *parent;
struct sysfs_device *sysdevice; /* NULL if virtual */
- struct sysfs_driver *driver; /* NULL if not implemented */
- struct sysfs_directory *directory;
};
+/* NOTE: not used as of now */
struct sysfs_class {
char name[SYSFS_NAME_LEN];
char path[SYSFS_PATH_MAX];
+ struct dlist *attrlist;
/* Private: for internal use only */
struct dlist *devices;
- struct sysfs_directory *directory;
};
#ifdef __cplusplus
@@ -171,43 +136,15 @@ extern int sysfs_path_is_dir(const char *path);
extern int sysfs_path_is_link(const char *path);
extern int sysfs_path_is_file(const char *path);
extern int sysfs_get_link(const char *path, char *target, size_t len);
-extern struct dlist *sysfs_open_subsystem_list(char *name);
-extern struct dlist *sysfs_open_bus_devices_list(char *name);
+extern struct dlist *sysfs_open_directory_list(const char *name);
extern void sysfs_close_list(struct dlist *list);
/* sysfs directory and file access */
extern void sysfs_close_attribute(struct sysfs_attribute *sysattr);
extern struct sysfs_attribute *sysfs_open_attribute(const char *path);
extern int sysfs_read_attribute(struct sysfs_attribute *sysattr);
-extern int sysfs_read_attribute_value(const char *attrpath,
- char *value, size_t vsize);
extern int sysfs_write_attribute(struct sysfs_attribute *sysattr,
const char *new_value, size_t len);
-extern char *sysfs_get_value_from_attributes(struct dlist *attr,
- const char *name);
-extern int sysfs_refresh_dir_attributes(struct sysfs_directory *sysdir);
-extern int sysfs_refresh_dir_links(struct sysfs_directory *sysdir);
-extern int sysfs_refresh_dir_subdirs(struct sysfs_directory *sysdir);
-extern void sysfs_close_directory(struct sysfs_directory *sysdir);
-extern struct sysfs_directory *sysfs_open_directory(const char *path);
-extern int sysfs_read_dir_attributes(struct sysfs_directory *sysdir);
-extern int sysfs_read_dir_links(struct sysfs_directory *sysdir);
-extern int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir);
-extern int sysfs_read_directory(struct sysfs_directory *sysdir);
-extern int sysfs_read_all_subdirs(struct sysfs_directory *sysdir);
-extern struct sysfs_directory *sysfs_get_subdirectory
- (struct sysfs_directory *dir, char *subname);
-extern void sysfs_close_link(struct sysfs_link *ln);
-extern struct sysfs_link *sysfs_open_link(const char *lnpath);
-extern struct sysfs_link *sysfs_get_directory_link
- (struct sysfs_directory *dir, char *linkname);
-extern struct sysfs_link *sysfs_get_subdirectory_link
- (struct sysfs_directory *dir, char *linkname);
-extern struct sysfs_attribute *sysfs_get_directory_attribute
- (struct sysfs_directory *dir, char *attrname);
-extern struct dlist *sysfs_get_dir_attributes(struct sysfs_directory *dir);
-extern struct dlist *sysfs_get_dir_links(struct sysfs_directory *dir);
-extern struct dlist *sysfs_get_dir_subdirs(struct sysfs_directory *dir);
/* sysfs driver access */
extern void sysfs_close_driver(struct sysfs_driver *driver);
@@ -218,19 +155,8 @@ extern struct sysfs_attribute *sysfs_get_driver_attr
(struct sysfs_driver *drv, const char *name);
extern struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver);
extern struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver);
-extern struct dlist *sysfs_refresh_driver_devices(struct sysfs_driver *driver);
-extern struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver);
-extern struct sysfs_device *sysfs_get_driver_device
- (struct sysfs_driver *driver, const char *name);
-extern struct dlist *sysfs_refresh_driver_attributes
- (struct sysfs_driver *driver);
-extern struct sysfs_attribute *sysfs_open_driver_attr
- (const char *bus, const char *drv, const char *attrib);
/* generic sysfs device access */
-extern void sysfs_close_root_device(struct sysfs_root_device *root);
-extern struct sysfs_root_device *sysfs_open_root_device(const char *name);
-extern struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root);
extern void sysfs_close_device_tree(struct sysfs_device *device);
extern struct sysfs_device *sysfs_open_device_tree(const char *path);
extern void sysfs_close_device(struct sysfs_device *dev);
@@ -241,27 +167,8 @@ extern struct sysfs_device *sysfs_open_device_path(const char *path);
extern int sysfs_get_device_bus(struct sysfs_device *dev);
extern struct sysfs_attribute *sysfs_get_device_attr
(struct sysfs_device *dev, const char *name);
-extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device);
-extern struct dlist *sysfs_refresh_device_attributes
- (struct sysfs_device *device);
-extern struct sysfs_attribute *sysfs_open_device_attr(const char *bus,
- const char *bus_id, const char *attrib);
-
-/* generic sysfs bus access */
-extern void sysfs_close_bus(struct sysfs_bus *bus);
-extern struct sysfs_bus *sysfs_open_bus(const char *name);
-extern struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
- char *id);
-extern struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
- char *drvname);
-extern struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus);
-extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
-extern struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus);
-extern struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus);
-extern struct sysfs_attribute *sysfs_get_bus_attribute
- (struct sysfs_bus *bus, char *attrname);
-extern int sysfs_find_driver_bus(const char *driver, char *busname,
- size_t bsize);
+extern struct dlist *sysfs_get_device_attributes
+ (struct sysfs_device *dev);
/* generic sysfs class access */
extern void sysfs_close_class_device(struct sysfs_class_device *dev);
@@ -269,26 +176,29 @@ extern struct sysfs_class_device *sysfs_open_class_device_path
(const char *path);
extern struct sysfs_class_device *sysfs_open_class_device
(const char *classname, const char *name);
-extern struct sysfs_device *sysfs_get_classdev_device
+extern struct sysfs_class_device *sysfs_get_classdev_parent
(struct sysfs_class_device *clsdev);
-extern struct sysfs_driver *sysfs_get_classdev_driver
+extern struct sysfs_attribute *sysfs_get_classdev_attr
+ (struct sysfs_class_device *clsdev, const char *name);
+extern struct dlist *sysfs_get_classdev_attributes
(struct sysfs_class_device *clsdev);
-extern struct sysfs_class_device *sysfs_get_classdev_parent
+extern struct sysfs_device *sysfs_get_classdev_device
(struct sysfs_class_device *clsdev);
extern void sysfs_close_class(struct sysfs_class *cls);
extern struct sysfs_class *sysfs_open_class(const char *name);
-extern struct dlist *sysfs_get_class_devices(struct sysfs_class *cls);
extern struct sysfs_class_device *sysfs_get_class_device
- (struct sysfs_class *cls, char *name);
-extern struct dlist *sysfs_get_classdev_attributes
- (struct sysfs_class_device *cdev);
-extern struct dlist *sysfs_refresh_classdev_attributes
- (struct sysfs_class_device *cdev);
-extern struct sysfs_attribute *sysfs_get_classdev_attr
- (struct sysfs_class_device *clsdev, const char *name);
-extern struct sysfs_attribute *sysfs_open_classdev_attr
- (const char *classname, const char *dev,
- const char *attrib);
+ (struct sysfs_class *cls, const char *name);
+extern struct dlist *sysfs_get_class_devices(struct sysfs_class *cls);
+
+/* generic sysfs bus access */
+extern void sysfs_close_bus(struct sysfs_bus *bus);
+extern struct sysfs_bus *sysfs_open_bus(const char *name);
+extern struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus);
+extern struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus);
+extern struct sysfs_device *sysfs_get_bus_device
+ (struct sysfs_bus *bus, const char *id);
+extern struct sysfs_driver *sysfs_get_bus_driver
+ (struct sysfs_bus *bus, const char *drvname);
/**
* sort_list: sorter function to keep list elements sorted in alphabetical
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 2a11ba9..1c58f1e 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,6 +1,8 @@
lib_LTLIBRARIES = libsysfs.la
-libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_bus.c sysfs_class.c \
- sysfs_device.c sysfs_driver.c sysfs.h dlist.c
+#libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_bus.c sysfs_class.c \
+# sysfs_device.c sysfs_driver.c sysfs.h dlist.c
+libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_class.c dlist.c \
+ sysfs_device.c sysfs_driver.c sysfs_bus.c sysfs.h
INCLUDES = -I../include
-libsysfs_la_LDFLAGS = -version-info 1:2:0
+libsysfs_la_LDFLAGS = -version-info 2:0:0
libsysfs_la_CFLAGS = -Wall -W -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls
diff --git a/lib/Makefile.in b/lib/Makefile.in
index 72a6e49..04a94f8 100644
--- a/lib/Makefile.in
+++ b/lib/Makefile.in
@@ -84,11 +84,13 @@ am__include = @am__include@
am__quote = @am__quote@
install_sh = @install_sh@
lib_LTLIBRARIES = libsysfs.la
-libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_bus.c sysfs_class.c \
- sysfs_device.c sysfs_driver.c sysfs.h dlist.c
+#libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_bus.c sysfs_class.c \
+# sysfs_device.c sysfs_driver.c sysfs.h dlist.c
+libsysfs_la_SOURCES = sysfs_utils.c sysfs_dir.c sysfs_class.c dlist.c \
+ sysfs_device.c sysfs_driver.c sysfs_bus.c sysfs.h
INCLUDES = -I../include
-libsysfs_la_LDFLAGS = -version-info 1:2:0
+libsysfs_la_LDFLAGS = -version-info 2:0:0
libsysfs_la_CFLAGS = -Wall -W -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls
subdir = lib
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
@@ -98,9 +100,9 @@ LTLIBRARIES = $(lib_LTLIBRARIES)
libsysfs_la_LIBADD =
am_libsysfs_la_OBJECTS = libsysfs_la-sysfs_utils.lo \
- libsysfs_la-sysfs_dir.lo libsysfs_la-sysfs_bus.lo \
- libsysfs_la-sysfs_class.lo libsysfs_la-sysfs_device.lo \
- libsysfs_la-sysfs_driver.lo libsysfs_la-dlist.lo
+ libsysfs_la-sysfs_dir.lo libsysfs_la-sysfs_class.lo \
+ libsysfs_la-dlist.lo libsysfs_la-sysfs_device.lo \
+ libsysfs_la-sysfs_driver.lo libsysfs_la-sysfs_bus.lo
libsysfs_la_OBJECTS = $(am_libsysfs_la_OBJECTS)
DEFS = @DEFS@
@@ -168,11 +170,11 @@ clean-libLTLIBRARIES:
done
libsysfs_la-sysfs_utils.lo: sysfs_utils.c
libsysfs_la-sysfs_dir.lo: sysfs_dir.c
-libsysfs_la-sysfs_bus.lo: sysfs_bus.c
libsysfs_la-sysfs_class.lo: sysfs_class.c
+libsysfs_la-dlist.lo: dlist.c
libsysfs_la-sysfs_device.lo: sysfs_device.c
libsysfs_la-sysfs_driver.lo: sysfs_driver.c
-libsysfs_la-dlist.lo: dlist.c
+libsysfs_la-sysfs_bus.lo: sysfs_bus.c
libsysfs.la: $(libsysfs_la_OBJECTS) $(libsysfs_la_DEPENDENCIES)
$(LINK) -rpath $(libdir) $(libsysfs_la_LDFLAGS) $(libsysfs_la_OBJECTS) $(libsysfs_la_LIBADD) $(LIBS)
@@ -247,24 +249,6 @@ libsysfs_la-sysfs_dir.lo: sysfs_dir.c
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_dir.lo `test -f 'sysfs_dir.c' || echo '$(srcdir)/'`sysfs_dir.c
-libsysfs_la-sysfs_bus.o: sysfs_bus.c
-@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.o' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPo' @AMDEPBACKSLASH@
-@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.o `test -f 'sysfs_bus.c' || echo '$(srcdir)/'`sysfs_bus.c
-
-libsysfs_la-sysfs_bus.obj: sysfs_bus.c
-@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.obj' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPo' @AMDEPBACKSLASH@
-@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.obj `cygpath -w sysfs_bus.c`
-
-libsysfs_la-sysfs_bus.lo: sysfs_bus.c
-@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.lo' libtool=yes @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Plo' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPlo' @AMDEPBACKSLASH@
-@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.lo `test -f 'sysfs_bus.c' || echo '$(srcdir)/'`sysfs_bus.c
-
libsysfs_la-sysfs_class.o: sysfs_class.c
@AMDEP_TRUE@ source='sysfs_class.c' object='libsysfs_la-sysfs_class.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_class.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_class.TPo' @AMDEPBACKSLASH@
@@ -283,6 +267,24 @@ libsysfs_la-sysfs_class.lo: sysfs_class.c
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_class.lo `test -f 'sysfs_class.c' || echo '$(srcdir)/'`sysfs_class.c
+libsysfs_la-dlist.o: dlist.c
+@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.o `test -f 'dlist.c' || echo '$(srcdir)/'`dlist.c
+
+libsysfs_la-dlist.obj: dlist.c
+@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.obj `cygpath -w dlist.c`
+
+libsysfs_la-dlist.lo: dlist.c
+@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Plo' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPlo' @AMDEPBACKSLASH@
+@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.lo `test -f 'dlist.c' || echo '$(srcdir)/'`dlist.c
+
libsysfs_la-sysfs_device.o: sysfs_device.c
@AMDEP_TRUE@ source='sysfs_device.c' object='libsysfs_la-sysfs_device.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_device.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_device.TPo' @AMDEPBACKSLASH@
@@ -319,23 +321,23 @@ libsysfs_la-sysfs_driver.lo: sysfs_driver.c
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_driver.lo `test -f 'sysfs_driver.c' || echo '$(srcdir)/'`sysfs_driver.c
-libsysfs_la-dlist.o: dlist.c
-@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.o' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPo' @AMDEPBACKSLASH@
+libsysfs_la-sysfs_bus.o: sysfs_bus.c
+@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPo' @AMDEPBACKSLASH@
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.o `test -f 'dlist.c' || echo '$(srcdir)/'`dlist.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.o `test -f 'sysfs_bus.c' || echo '$(srcdir)/'`sysfs_bus.c
-libsysfs_la-dlist.obj: dlist.c
-@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.obj' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPo' @AMDEPBACKSLASH@
+libsysfs_la-sysfs_bus.obj: sysfs_bus.c
+@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Po' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPo' @AMDEPBACKSLASH@
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.obj `cygpath -w dlist.c`
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.obj `cygpath -w sysfs_bus.c`
-libsysfs_la-dlist.lo: dlist.c
-@AMDEP_TRUE@ source='dlist.c' object='libsysfs_la-dlist.lo' libtool=yes @AMDEPBACKSLASH@
-@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-dlist.Plo' tmpdepfile='$(DEPDIR)/libsysfs_la-dlist.TPlo' @AMDEPBACKSLASH@
+libsysfs_la-sysfs_bus.lo: sysfs_bus.c
+@AMDEP_TRUE@ source='sysfs_bus.c' object='libsysfs_la-sysfs_bus.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@ depfile='$(DEPDIR)/libsysfs_la-sysfs_bus.Plo' tmpdepfile='$(DEPDIR)/libsysfs_la-sysfs_bus.TPlo' @AMDEPBACKSLASH@
@AMDEP_TRUE@ $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
- $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-dlist.lo `test -f 'dlist.c' || echo '$(srcdir)/'`dlist.c
+ $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libsysfs_la_CFLAGS) $(CFLAGS) -c -o libsysfs_la-sysfs_bus.lo `test -f 'sysfs_bus.c' || echo '$(srcdir)/'`sysfs_bus.c
CCDEPMODE = @CCDEPMODE@
mostlyclean-libtool:
diff --git a/lib/dlist.c b/lib/dlist.c
index c4ee324..7260d2b 100644
--- a/lib/dlist.c
+++ b/lib/dlist.c
@@ -550,10 +550,9 @@ void dlist_sort_custom(struct dlist *list, int (*compare)(void *, void *))
struct dlist *templist;
unsigned int passcount = 1;
unsigned int mergecount = 1;
-
+
dlist_start(list);
templist = dlist_new(list->data_size);
-
// do nothing if there isn't anything to sort
listsource = list;
listdest = templist;
diff --git a/lib/sysfs.h b/lib/sysfs.h
index 4ef8c28..a6789fe 100644
--- a/lib/sysfs.h
+++ b/lib/sysfs.h
@@ -3,7 +3,7 @@
*
* Internal Header Definitions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -27,14 +27,32 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <ctype.h>
#include <mntent.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
-/* external library functions */
-extern int isascii(int c);
+#define safestrcpy(to, from) strncpy(to, from, sizeof(to)-1)
+#define safestrcat(to, from) strncat(to, from, sizeof(to) - strlen(to)-1)
+
+#define safestrcpymax(to, from, max) \
+do { \
+ to[max-1] = '\0'; \
+ strncpy(to, from, max-1); \
+} while (0)
+
+#define safestrcatmax(to, from, max) \
+do { \
+ to[max-1] = '\0'; \
+ strncat(to, from, max - strlen(to)-1); \
+} while (0)
+
+extern struct sysfs_attribute *get_attribute(void *dev, const char *name);
+extern struct dlist *read_dir_subdirs(const char *path);
+extern struct dlist *read_dir_links(const char *path);
+extern struct dlist *get_attributes_list(void *dev);
/* Debugging */
#ifdef DEBUG
diff --git a/lib/sysfs_bus.c b/lib/sysfs_bus.c
index d47b94c..921ef32 100644
--- a/lib/sysfs_bus.c
+++ b/lib/sysfs_bus.c
@@ -3,7 +3,7 @@
*
* Generic bus utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -34,35 +34,19 @@ static void sysfs_close_drv(void *drv)
}
/*
- * compares devices' bus ids.
- * @a: device id looking for
+ * compares names.
+ * @a: name looked for
* @b: sysfs_device comparing being compared
- * returns 1 if a==b->bus_id or 0 not equal
- */
-static int bus_device_id_equal(void *a, void *b)
-{
- if (a == NULL || b == NULL)
- return 0;
-
- if (strcmp(((char *)a), ((struct sysfs_device *)b)->bus_id)
- == 0)
- return 1;
- return 0;
-}
-
-/*
- * compares drivers' names.
- * @a: driver name looking for
- * @b: sysfs_driver comparing being compared
* returns 1 if a==b->name or 0 not equal
*/
-static int bus_driver_name_equal(void *a, void *b)
+static int name_equal(void *a, void *b)
{
- if (a == NULL || b == NULL)
+ if (!a || !b)
return 0;
- if (strcmp(((char *)a), ((struct sysfs_driver *)b)->name) == 0)
+ if (strcmp(((char *)a), ((struct sysfs_device *)b)->name) == 0)
return 1;
+
return 0;
}
@@ -72,9 +56,9 @@ static int bus_driver_name_equal(void *a, void *b)
*/
void sysfs_close_bus(struct sysfs_bus *bus)
{
- if (bus != NULL) {
- if (bus->directory != NULL)
- sysfs_close_directory(bus->directory);
+ if (bus) {
+ if (bus->attrlist)
+ dlist_destroy(bus->attrlist);
if (bus->devices)
dlist_destroy(bus->devices);
if (bus->drivers)
@@ -99,12 +83,13 @@ static struct sysfs_bus *alloc_bus(void)
*/
struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
{
- struct sysfs_device *bdev = NULL;
- struct sysfs_directory *devdir = NULL;
- struct sysfs_link *curl = NULL;
- char path[SYSFS_PATH_MAX];
+ struct sysfs_device *dev;
+ struct dlist *linklist;
+ char path[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
+ char target[SYSFS_PATH_MAX];
+ char *curlink;
- if (bus == NULL) {
+ if (!bus) {
errno = EINVAL;
return NULL;
}
@@ -112,48 +97,54 @@ struct dlist *sysfs_get_bus_devices(struct sysfs_bus *bus)
safestrcpy(path, bus->path);
safestrcat(path, "/");
safestrcat(path, SYSFS_DEVICES_NAME);
- devdir = sysfs_open_directory(path);
- if (devdir == NULL)
- return NULL;
- if (sysfs_read_dir_links(devdir) != 0) {
- sysfs_close_directory(devdir);
- return NULL;
- }
-
- if (devdir->links != NULL) {
- dlist_for_each_data(devdir->links, curl, struct sysfs_link) {
- bdev = sysfs_open_device_path(curl->target);
- if (bdev == NULL) {
+ linklist = read_dir_links(path);
+ if (linklist) {
+ dlist_for_each_data(linklist, curlink, char) {
+ if (bus->devices) {
+ dev = (struct sysfs_device *)
+ dlist_find_custom(bus->devices,
+ (void *)curlink, name_equal);
+ if (dev)
+ continue;
+ }
+ safestrcpy(devpath, path);
+ safestrcat(devpath, "/");
+ safestrcat(devpath, curlink);
+ if (sysfs_get_link(devpath, target, SYSFS_PATH_MAX)) {
+ dprintf("Error getting link - %s\n", devpath);
+ continue;
+ }
+ dev = sysfs_open_device_path(target);
+ if (!dev) {
dprintf("Error opening device at %s\n",
- curl->target);
+ target);
continue;
}
- if (bus->devices == NULL)
+ if (!bus->devices)
bus->devices = dlist_new_with_delete
(sizeof(struct sysfs_device),
sysfs_close_dev);
- dlist_unshift_sorted(bus->devices, bdev, sort_list);
+ dlist_unshift_sorted(bus->devices, dev, sort_list);
}
+ sysfs_close_list(linklist);
}
- sysfs_close_directory(devdir);
-
return (bus->devices);
}
/**
- * sysfs_get_bus_drivers: get all pci drivers
- * @bus: pci bus to add drivers to
- * returns dlist of drivers with success and NULL with error
+ * sysfs_get_bus_drivers: gets all drivers for bus
+ * @bus: bus to get devices for
+ * returns dlist of devices with success and NULL with failure
*/
struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
{
- struct sysfs_driver *driver = NULL;
- struct sysfs_directory *drvdir = NULL;
- struct sysfs_directory *cursub = NULL;
- char path[SYSFS_PATH_MAX];
+ struct sysfs_driver *drv;
+ struct dlist *dirlist;
+ char path[SYSFS_PATH_MAX], drvpath[SYSFS_PATH_MAX];
+ char *curdir;
- if (bus == NULL) {
+ if (!bus) {
errno = EINVAL;
return NULL;
}
@@ -161,31 +152,34 @@ struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
safestrcpy(path, bus->path);
safestrcat(path, "/");
safestrcat(path, SYSFS_DRIVERS_NAME);
- drvdir = sysfs_open_directory(path);
- if (drvdir == NULL)
- return NULL;
- if (sysfs_read_dir_subdirs(drvdir) != 0) {
- sysfs_close_directory(drvdir);
- return NULL;
- }
- if (drvdir->subdirs != NULL) {
- dlist_for_each_data(drvdir->subdirs, cursub,
- struct sysfs_directory) {
- driver = sysfs_open_driver_path(cursub->path);
- if (driver == NULL) {
+ dirlist = read_dir_subdirs(path);
+ if (dirlist) {
+ dlist_for_each_data(dirlist, curdir, char) {
+ if (bus->drivers) {
+ drv = (struct sysfs_driver *)
+ dlist_find_custom(bus->drivers,
+ (void *)curdir, name_equal);
+ if (drv)
+ continue;
+ }
+ safestrcpy(drvpath, path);
+ safestrcat(drvpath, "/");
+ safestrcat(drvpath, curdir);
+ drv = sysfs_open_driver_path(drvpath);
+ if (!drv) {
dprintf("Error opening driver at %s\n",
- cursub->path);
+ drvpath);
continue;
}
- if (bus->drivers == NULL)
+ if (!bus->drivers)
bus->drivers = dlist_new_with_delete
(sizeof(struct sysfs_driver),
sysfs_close_drv);
- dlist_unshift_sorted(bus->drivers, driver, sort_list);
+ dlist_unshift_sorted(bus->drivers, drv, sort_list);
}
+ sysfs_close_list(dirlist);
}
- sysfs_close_directory(drvdir);
return (bus->drivers);
}
@@ -195,16 +189,16 @@ struct dlist *sysfs_get_bus_drivers(struct sysfs_bus *bus)
*/
struct sysfs_bus *sysfs_open_bus(const char *name)
{
- struct sysfs_bus *bus = NULL;
+ struct sysfs_bus *bus;
char buspath[SYSFS_PATH_MAX];
- if (name == NULL) {
+ if (!name) {
errno = EINVAL;
return NULL;
}
memset(buspath, 0, SYSFS_PATH_MAX);
- if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) != 0) {
+ if (sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX)) {
dprintf("Sysfs not supported on this system\n");
return NULL;
}
@@ -213,18 +207,18 @@ struct sysfs_bus *sysfs_open_bus(const char *name)
safestrcat(buspath, SYSFS_BUS_NAME);
safestrcat(buspath, "/");
safestrcat(buspath, name);
- if ((sysfs_path_is_dir(buspath)) != 0) {
+ if (sysfs_path_is_dir(buspath)) {
dprintf("Invalid path to bus: %s\n", buspath);
return NULL;
}
bus = alloc_bus();
- if (bus == NULL) {
+ if (!bus) {
dprintf("calloc failed\n");
return NULL;
}
safestrcpy(bus->name, name);
safestrcpy(bus->path, buspath);
- if ((sysfs_remove_trailing_slash(bus->path)) != 0) {
+ if (sysfs_remove_trailing_slash(bus->path)) {
dprintf("Incorrect path to bus %s\n", bus->path);
sysfs_close_bus(bus);
return NULL;
@@ -239,21 +233,45 @@ struct sysfs_bus *sysfs_open_bus(const char *name)
* @id: bus_id for device
* returns struct sysfs_device reference or NULL if not found.
*/
-struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, char *id)
+struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus,
+ const char *id)
{
- if (bus == NULL || id == NULL) {
+ struct sysfs_device *dev = NULL;
+ char devpath[SYSFS_PATH_MAX], target[SYSFS_PATH_MAX];
+
+ if (!bus || !id) {
errno = EINVAL;
return NULL;
}
- if (bus->devices == NULL) {
- bus->devices = sysfs_get_bus_devices(bus);
- if (bus->devices == NULL)
+ if (bus->devices) {
+ dev = (struct sysfs_device *)dlist_find_custom
+ (bus->devices, (void *)id, name_equal);
+ if (dev)
+ return dev;
+ }
+ safestrcpy(devpath, bus->path);
+ safestrcat(devpath, "/");
+ safestrcat(devpath, SYSFS_DEVICES_NAME);
+ safestrcat(devpath, "/");
+ safestrcat(devpath, id);
+ if (sysfs_path_is_link(devpath)) {
+ dprintf("No such device %s on bus %s?\n", id, bus->name);
+ return NULL;
+ }
+ if (!sysfs_get_link(devpath, target, SYSFS_PATH_MAX)) {
+ dev = sysfs_open_device_path(target);
+ if (!dev) {
+ dprintf("Error opening device at %s\n", target);
return NULL;
+ }
+ if (!bus->devices)
+ bus->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_device),
+ sysfs_close_dev);
+ dlist_unshift_sorted(bus->devices, dev, sort_list);
}
-
- return (struct sysfs_device *)dlist_find_custom(bus->devices, id,
- bus_device_id_equal);
+ return dev;
}
/**
@@ -262,139 +280,38 @@ struct sysfs_device *sysfs_get_bus_device(struct sysfs_bus *bus, char *id)
* @drvname: name of driver
* returns struct sysfs_driver reference or NULL if not found.
*/
-struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
- char *drvname)
+struct sysfs_driver *sysfs_get_bus_driver(struct sysfs_bus *bus,
+ const char *drvname)
{
- if (bus == NULL || drvname == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (bus->drivers == NULL) {
- bus->drivers = sysfs_get_bus_drivers(bus);
- if (bus->drivers == NULL)
- return NULL;
- }
+ struct sysfs_driver *drv;
+ char drvpath[SYSFS_PATH_MAX];
- return (struct sysfs_driver *)dlist_find_custom(bus->drivers, drvname,
- bus_driver_name_equal);
-}
-
-/**
- * sysfs_get_bus_attributes: returns bus' dlist of attributes
- * @bus: bus to get attributes for.
- * returns dlist of attributes or NULL if there aren't any.
- */
-struct dlist *sysfs_get_bus_attributes(struct sysfs_bus *bus)
-{
- if (bus == NULL)
- return NULL;
-
- if (bus->directory == NULL) {
- bus->directory = sysfs_open_directory(bus->path);
- if (bus->directory == NULL)
- return NULL;
- }
- if (bus->directory->attributes == NULL) {
- if ((sysfs_read_dir_attributes(bus->directory)) != 0)
- return NULL;
- }
- return bus->directory->attributes;
-}
-
-/**
- * sysfs_refresh_bus_attributes: refreshes the bus's list of attributes
- * @bus: sysfs_bus whose attributes to refresh
- *
- * NOTE: Upon return, prior references to sysfs_attributes for this bus
- * _may_ not be valid
- *
- * Returns list of attributes on success and NULL on failure
- */
-struct dlist *sysfs_refresh_bus_attributes(struct sysfs_bus *bus)
-{
- if (bus == NULL) {
+ if (!bus || !drvname) {
errno = EINVAL;
return NULL;
}
- if (bus->directory == NULL)
- return (sysfs_get_bus_attributes(bus));
-
- if ((sysfs_refresh_dir_attributes(bus->directory)) != 0) {
- dprintf("Error refreshing bus attributes\n");
- return NULL;
+ if (bus->drivers) {
+ drv = (struct sysfs_driver *)dlist_find_custom
+ (bus->drivers, (void *)drvname, name_equal);
+ if (drv)
+ return drv;
}
-
- return (bus->directory->attributes);
-}
-
-/**
- * sysfs_get_bus_attribute: gets a specific bus attribute, if buses had
- * attributes.
- * @bus: bus to retrieve attribute from
- * @attrname: attribute name to retrieve
- * returns reference to sysfs_attribute if found or NULL if not found
- */
-struct sysfs_attribute *sysfs_get_bus_attribute(struct sysfs_bus *bus,
- char *attrname)
-{
- struct dlist *attrlist = NULL;
-
- if (bus == NULL) {
- errno = EINVAL;
+ safestrcpy(drvpath, bus->path);
+ safestrcat(drvpath, "/");
+ safestrcat(drvpath, SYSFS_DRIVERS_NAME);
+ safestrcat(drvpath, "/");
+ safestrcat(drvpath, drvname);
+ drv = sysfs_open_driver_path(drvpath);
+ if (!drv) {
+ dprintf("Error opening driver at %s\n", drvpath);
return NULL;
}
- attrlist = sysfs_get_bus_attributes(bus);
- if (attrlist == NULL)
- return NULL;
-
- return sysfs_get_directory_attribute(bus->directory, attrname);
+ if (!bus->drivers)
+ bus->drivers = dlist_new_with_delete
+ (sizeof(struct sysfs_driver),
+ sysfs_close_drv);
+ dlist_unshift_sorted(bus->drivers, drv, sort_list);
+ return drv;
}
-/**
- * sysfs_find_driver_bus: locates the bus the driver is on.
- * @driver: name of the driver to locate
- * @busname: buffer to copy name to
- * @bsize: buffer size
- * returns 0 with success, -1 with error
- */
-int sysfs_find_driver_bus(const char *driver, char *busname, size_t bsize)
-{
- char subsys[SYSFS_PATH_MAX], *bus = NULL, *curdrv = NULL;
- struct dlist *buslist = NULL, *drivers = NULL;
-
- if (driver == NULL || busname == NULL) {
- errno = EINVAL;
- return -1;
- }
-
- memset(subsys, 0, SYSFS_PATH_MAX);
- safestrcpy(subsys, SYSFS_BUS_NAME);
- buslist = sysfs_open_subsystem_list(subsys);
- if (buslist != NULL) {
- dlist_for_each_data(buslist, bus, char) {
- memset(subsys, 0, SYSFS_PATH_MAX);
- safestrcpy(subsys, SYSFS_BUS_NAME);
- safestrcat(subsys, "/");
- safestrcat(subsys, bus);
- safestrcat(subsys, "/");
- safestrcat(subsys, SYSFS_DRIVERS_NAME);
- drivers = sysfs_open_subsystem_list(subsys);
- if (drivers != NULL) {
- dlist_for_each_data(drivers, curdrv, char) {
- if (strcmp(driver, curdrv) == 0) {
- safestrcpymax(busname,
- bus, bsize);
- sysfs_close_list(drivers);
- sysfs_close_list(buslist);
- return 0;
- }
- }
- sysfs_close_list(drivers);
- }
- }
- sysfs_close_list(buslist);
- }
- return -1;
-}
diff --git a/lib/sysfs_class.c b/lib/sysfs_class.c
index a132bb6..edf751b 100644
--- a/lib/sysfs_class.c
+++ b/lib/sysfs_class.c
@@ -3,7 +3,7 @@
*
* Generic class utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -23,88 +23,80 @@
#include "libsysfs.h"
#include "sysfs.h"
-static void sysfs_close_cls_dev(void *dev)
-{
- sysfs_close_class_device((struct sysfs_class_device *)dev);
-}
-
-/**
- * class_name_equal: compares class_devices' name
- * @a: class_name looking for
- * @b: sysfs_class_device being compared
- */
-static int class_name_equal(void *a, void *b)
-{
- if (a == NULL || b == NULL)
- return 0;
-
- if (strcmp(((char *)a), ((struct sysfs_class_device *)b)->name) == 0)
- return 1;
-
- return 0;
-}
-
/**
* sysfs_close_class_device: closes a single class device.
* @dev: class device to close.
*/
void sysfs_close_class_device(struct sysfs_class_device *dev)
{
- if (dev != NULL) {
- if (dev->directory != NULL)
- sysfs_close_directory(dev->directory);
- if (dev->sysdevice != NULL)
- sysfs_close_device(dev->sysdevice);
- if (dev->driver != NULL)
- sysfs_close_driver(dev->driver);
- if (dev->parent != NULL)
+ if (dev) {
+ if (dev->parent)
sysfs_close_class_device(dev->parent);
+ if (dev->sysdevice)
+ sysfs_close_device(dev->sysdevice);
+ if (dev->attrlist)
+ dlist_destroy(dev->attrlist);
free(dev);
}
}
+static void sysfs_close_cls_dev(void *dev)
+{
+ sysfs_close_class_device((struct sysfs_class_device *)dev);
+}
+
/**
- * sysfs_close_class: close single class
- * @cls: class structure
- */
+ * sysfs_close_class: close the given class
+ * @cls: sysfs_class to close
+ */
void sysfs_close_class(struct sysfs_class *cls)
{
- if (cls != NULL) {
- if (cls->directory != NULL)
- sysfs_close_directory(cls->directory);
- if (cls->devices != NULL)
+ if (cls) {
+ if (cls->devices)
dlist_destroy(cls->devices);
+ if (cls->attrlist)
+ dlist_destroy(cls->attrlist);
free(cls);
}
}
+static int cdev_name_equal(void *a, void *b)
+{
+ if (!a || !b)
+ return 0;
+
+ if (strncmp((char *)a, ((struct sysfs_class_device *)b)->name,
+ strlen((char *)a)) == 0)
+ return 1;
+
+ return 0;
+}
+
+static struct sysfs_class *alloc_class(void)
+{
+ return (struct sysfs_class *) calloc(1, sizeof(struct sysfs_class));
+}
+
/**
* alloc_class_device: mallocs and initializes new class device struct.
* returns sysfs_class_device or NULL.
*/
static struct sysfs_class_device *alloc_class_device(void)
{
- return (struct sysfs_class_device *)
- calloc(1, sizeof(struct sysfs_class_device));
-}
+ struct sysfs_class_device *dev;
-/**
- * alloc_class: mallocs new class structure
- * returns sysfs_class struct or NULL
- */
-static struct sysfs_class *alloc_class(void)
-{
- return (struct sysfs_class *)calloc(1, sizeof(struct sysfs_class));
+ dev = calloc(1, sizeof(struct sysfs_class_device));
+ return dev;
}
-/**
+/**
* set_classdev_classname: Grabs classname from path
* @cdev: class device to set
* Returns nothing
*/
static void set_classdev_classname(struct sysfs_class_device *cdev)
{
- char *c = NULL, *e = NULL;
+ char *c, *e;
int count = 0;
c = strstr(cdev->path, SYSFS_CLASS_NAME);
@@ -135,22 +127,22 @@ static void set_classdev_classname(struct sysfs_class_device *cdev)
*/
struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
{
- struct sysfs_class_device *cdev = NULL;
+ struct sysfs_class_device *cdev;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return NULL;
}
- if ((sysfs_path_is_dir(path)) != 0) {
+ if (sysfs_path_is_dir(path)) {
dprintf("%s is not a valid path to a class device\n", path);
return NULL;
}
cdev = alloc_class_device();
- if (cdev == NULL) {
+ if (!cdev) {
dprintf("calloc failed\n");
return NULL;
}
- if ((sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) != 0) {
+ if (sysfs_get_name_from_path(path, cdev->name, SYSFS_NAME_LEN)) {
errno = EINVAL;
dprintf("Error getting class device name\n");
sysfs_close_class_device(cdev);
@@ -158,7 +150,7 @@ struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
}
safestrcpy(cdev->path, path);
- if ((sysfs_remove_trailing_slash(cdev->path)) != 0) {
+ if (sysfs_remove_trailing_slash(cdev->path)) {
dprintf("Invalid path to class device %s\n", cdev->path);
sysfs_close_class_device(cdev);
return NULL;
@@ -168,245 +160,6 @@ struct sysfs_class_device *sysfs_open_class_device_path(const char *path)
return cdev;
}
-/**
- * sysfs_get_class_devices: gets all devices for class
- * @cls: class to get devices for
- * returns dlist of class_devices with success and NULL with error
- */
-struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
-{
- struct sysfs_class_device *dev = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (cls == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (cls->devices != NULL)
- return cls->devices;
-
- if (cls->directory == NULL) {
- cls->directory = sysfs_open_directory(cls->path);
- if (cls->directory == NULL)
- return NULL;
- }
-
- if ((sysfs_read_dir_subdirs(cls->directory)) != 0)
- return NULL;
-
- if (cls->directory->subdirs != NULL) {
- dlist_for_each_data(cls->directory->subdirs, cur,
- struct sysfs_directory) {
- dev = sysfs_open_class_device_path(cur->path);
- if (dev == NULL) {
- dprintf("Error opening device at %s\n",
- cur->path);
- continue;
- }
- if (cls->devices == NULL)
- cls->devices = dlist_new_with_delete
- (sizeof(struct sysfs_class_device),
- sysfs_close_cls_dev);
- dlist_unshift_sorted(cls->devices, dev, sort_list);
- }
- }
- return cls->devices;
-}
-
-/**
- * sysfs_open_class: opens specific class and all its devices on system
- * returns sysfs_class structure with success or NULL with error.
- */
-struct sysfs_class *sysfs_open_class(const char *name)
-{
- struct sysfs_class *cls = NULL;
- char classpath[SYSFS_PATH_MAX];
-
- if (name == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- memset(classpath, 0, SYSFS_PATH_MAX);
- if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
- dprintf("Sysfs not supported on this system\n");
- return NULL;
- }
-
- /*
- * We shall now treat "block" also as a class. Hence, check here
- * if "name" is "block" and proceed accordingly
- */
- if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
- safestrcat(classpath, "/");
- safestrcat(classpath, SYSFS_BLOCK_NAME);
- } else {
- safestrcat(classpath, "/");
- safestrcat(classpath, SYSFS_CLASS_NAME);
- safestrcat(classpath, "/");
- safestrcat(classpath, name);
- }
- if ((sysfs_path_is_dir(classpath)) != 0) {
- dprintf("Class %s not found on the system\n", name);
- return NULL;
- }
-
- cls = alloc_class();
- if (cls == NULL) {
- dprintf("calloc failed\n");
- return NULL;
- }
- safestrcpy(cls->name, name);
- safestrcpy(cls->path, classpath);
- if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
- dprintf("Invalid path to class device %s\n", cls->path);
- sysfs_close_class(cls);
- return NULL;
- }
-
- return cls;
-}
-
-/**
- * sysfs_get_class_device: Get specific class device using the device's id
- * @class: class to find device on
- * @name: class name of the device
- */
-struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
- char *name)
-{
- if (cls == NULL || name == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (cls->devices == NULL) {
- cls->devices = sysfs_get_class_devices(cls);
- if (cls->devices == NULL)
- return NULL;
- }
- return (struct sysfs_class_device *)dlist_find_custom(cls->devices,
- name, class_name_equal);
-}
-
-/**
- * sysfs_get_classdev_device: returns the sysfs_device corresponding to
- * sysfs_class_device, if present
- * @clsdev: class device whose sysfs_device is required
- * Returns sysfs_device on success, NULL on error or if device is not
- * implemented
- */
-struct sysfs_device *sysfs_get_classdev_device
- (struct sysfs_class_device *clsdev)
-{
- struct sysfs_link *devlink = NULL;
- char devpath[SYSFS_PATH_MAX];
-
- if (clsdev == NULL) {
- errno = EINVAL;
- return NULL;
- }
- safestrcpy(devpath, clsdev->path);
- safestrcat(devpath, "/device");
- if ((sysfs_path_is_link(devpath)) != 0) {
- if (clsdev->sysdevice != NULL) {
- sysfs_close_device(clsdev->sysdevice);
- clsdev->sysdevice = NULL;
- }
- return NULL;
- }
-
- if (clsdev->directory == NULL) {
- clsdev->directory = sysfs_open_directory(clsdev->path);
- if (clsdev->directory == NULL)
- return NULL;
- }
- devlink = sysfs_get_directory_link(clsdev->directory, "device");
- if (devlink == NULL) {
- if (clsdev->sysdevice != NULL) {
- dprintf("Device link no longer exists\n");
- sysfs_close_device(clsdev->sysdevice);
- clsdev->sysdevice = NULL;
- }
- return NULL;
- }
-
- if (clsdev->sysdevice != NULL) {
- if (!strncmp(devlink->target, clsdev->sysdevice->path,
- SYSFS_PATH_MAX))
- /* sysdevice hasn't changed */
- return (clsdev->sysdevice);
- else
- /* come here only if the device link for has changed */
- sysfs_close_device(clsdev->sysdevice);
- }
-
- clsdev->sysdevice = sysfs_open_device_path(devlink->target);
- if (clsdev->sysdevice == NULL)
- return NULL;
-
- return (clsdev->sysdevice);
-}
-
-/**
- * sysfs_get_classdev_driver: returns the sysfs_driver corresponding to
- * sysfs_class_device, if present
- * @clsdev: class device whose sysfs_device is required
- * Returns sysfs_driver on success, NULL on error or if driver is not
- * implemented
- */
-struct sysfs_driver *sysfs_get_classdev_driver
- (struct sysfs_class_device *clsdev)
-{
- struct sysfs_link *drvlink = NULL;
- char drvpath[SYSFS_PATH_MAX];
-
- if (clsdev == NULL) {
- errno = EINVAL;
- return NULL;
- }
- safestrcpy(drvpath, clsdev->path);
- safestrcat(drvpath, "/driver");
- if ((sysfs_path_is_link(drvpath)) != 0) {
- if (clsdev->driver != NULL) {
- sysfs_close_driver(clsdev->driver);
- clsdev->driver = NULL;
- }
- return NULL;
- }
-
- if (clsdev->directory == NULL) {
- clsdev->directory = sysfs_open_directory(clsdev->path);
- if (clsdev->directory == NULL)
- return NULL;
- }
- drvlink = sysfs_get_directory_link(clsdev->directory, "driver");
- if (drvlink == NULL) {
- if (clsdev->driver != NULL) {
- dprintf("Driver link no longer exists\n");
- sysfs_close_driver(clsdev->driver);
- clsdev->driver = NULL;
- }
- return NULL;
- }
- if (clsdev->driver != NULL) {
- if (!strncmp(drvlink->target, clsdev->driver->path,
- SYSFS_PATH_MAX))
- /* driver hasn't changed */
- return (clsdev->driver);
- else
- /* come here only if the device link for has changed */
- sysfs_close_driver(clsdev->driver);
- }
-
- clsdev->driver = sysfs_open_driver_path(drvlink->target);
- if (clsdev->driver == NULL)
- return NULL;
-
- return (clsdev->driver);
-}
-
/**
* get_blockdev_parent: Get the parent class device for a "block" subsystem
* device if present
@@ -415,7 +168,8 @@ struct sysfs_driver *sysfs_get_classdev_driver
*/
static int get_blockdev_parent(struct sysfs_class_device *clsdev)
{
- char parent_path[SYSFS_PATH_MAX], *c = NULL;
+ char parent_path[SYSFS_PATH_MAX];
+ char *c;
safestrcpy(parent_path, clsdev->path);
c = strstr(parent_path, SYSFS_BLOCK_NAME);
@@ -430,20 +184,20 @@ static int get_blockdev_parent(struct sysfs_class_device *clsdev)
else
goto errout;
- /* validate whether the given class device is a partition or not */
- if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
- dprintf("%s not a partition\n", clsdev->name);
- return 1;
- }
-
- c = strchr(c, '/');
+ /* validate whether the given class device is a partition or not */
+ if ((strncmp(c, clsdev->name, strlen(clsdev->name))) == 0) {
+ dprintf("%s not a partition\n", clsdev->name);
+ return 1;
+ }
+
+ c = strchr(c, '/');
if (c == NULL)
goto errout;
*c = '\0';
-
+
clsdev->parent = sysfs_open_class_device_path(parent_path);
- if (clsdev->parent == NULL) {
+ if (!clsdev->parent) {
dprintf("Error opening the parent class device at %s\n",
parent_path);
return 1;
@@ -466,19 +220,19 @@ errout:
struct sysfs_class_device *sysfs_get_classdev_parent
(struct sysfs_class_device *clsdev)
{
- if (clsdev == NULL) {
+ if (!clsdev) {
errno = EINVAL;
return NULL;
}
- if (clsdev->parent != NULL)
+ if (clsdev->parent)
return (clsdev->parent);
-
- /*
+
+ /*
* As of now, only block devices have a parent child heirarchy in sysfs
* We do not know, if, in the future, more classes will have a similar
* structure. Hence, we now call a specialized function for block and
* later we can add support functions for other subsystems as required.
- */
+ */
if (!(strncmp(clsdev->classname, SYSFS_BLOCK_NAME,
sizeof(SYSFS_BLOCK_NAME)))) {
if ((get_blockdev_parent(clsdev)) == 0)
@@ -499,13 +253,13 @@ struct sysfs_class_device *sysfs_get_classdev_parent
static int get_classdev_path(const char *classname, const char *clsdev,
char *path, size_t len)
{
- if (classname == NULL || clsdev == NULL || path == NULL) {
+ if (!classname || !clsdev || !path) {
errno = EINVAL;
return -1;
}
- if (sysfs_get_mnt_path(path, len) != 0) {
- dprintf("Error getting sysfs mount path\n");
- return -1;
+ if (sysfs_get_mnt_path(path, len) != 0) {
+ dprintf("Error getting sysfs mount path\n");
+ return -1;
}
if (strncmp(classname, SYSFS_BLOCK_NAME,
sizeof(SYSFS_BLOCK_NAME)) == 0) {
@@ -535,9 +289,9 @@ struct sysfs_class_device *sysfs_open_class_device
(const char *classname, const char *name)
{
char devpath[SYSFS_PATH_MAX];
- struct sysfs_class_device *cdev = NULL;
+ struct sysfs_class_device *cdev;
- if (classname == NULL || name == NULL) {
+ if (!classname || !name) {
errno = EINVAL;
return NULL;
}
@@ -549,9 +303,9 @@ struct sysfs_class_device *sysfs_open_class_device
name, classname);
return NULL;
}
-
+
cdev = sysfs_open_class_device_path(devpath);
- if (cdev == NULL) {
+ if (!cdev) {
dprintf("Error getting class device %s from class %s\n",
name, classname);
return NULL;
@@ -560,149 +314,208 @@ struct sysfs_class_device *sysfs_open_class_device
}
/**
- * sysfs_get_classdev_attributes: returns a dlist of attributes for
- * the requested class_device
- * @cdev: sysfs_class_dev for which attributes are needed
- * returns a dlist of attributes if exists, NULL otherwise
+ * sysfs_get_classdev_attr: searches class device's attributes by name
+ * @clsdev: class device to look through
+ * @name: attribute name to get
+ * returns sysfs_attribute reference with success or NULL with error
*/
-struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *cdev)
+struct sysfs_attribute *sysfs_get_classdev_attr
+ (struct sysfs_class_device *clsdev, const char *name)
{
- if (cdev == NULL)
+ if (!clsdev || !name) {
+ errno = EINVAL;
return NULL;
-
- if (cdev->directory == NULL) {
- cdev->directory = sysfs_open_directory(cdev->path);
- if (cdev->directory == NULL)
- return NULL;
}
- if (cdev->directory->attributes == NULL) {
- if ((sysfs_read_dir_attributes(cdev->directory)) != 0)
- return NULL;
- }
- return (cdev->directory->attributes);
+ return get_attribute(clsdev, (char *)name);
}
/**
- * sysfs_refresh_clsassdev_attributes: refreshes the driver's list of attributes
- * @clsdev: sysfs_class_device whose attributes to refresh
- *
- * NOTE: Upon return, prior references to sysfs_attributes for this classdev
- * _may_ not be valid
- *
- * Returns list of attributes on success and NULL on failure
+ * sysfs_get_classdev_attributes: gets list of classdev attributes
+ * @clsdev: class device whose attributes list is needed
+ * returns dlist of attributes on success or NULL on error
*/
-struct dlist *sysfs_refresh_classdev_attributes
- (struct sysfs_class_device *clsdev)
+struct dlist *sysfs_get_classdev_attributes(struct sysfs_class_device *clsdev)
{
- if (clsdev == NULL) {
+ if (!clsdev) {
errno = EINVAL;
return NULL;
}
+ return get_attributes_list(clsdev);
+}
- if (clsdev->directory == NULL)
- return (sysfs_get_classdev_attributes(clsdev));
+/**
+ * sysfs_get_classdev_device: gets the sysfs_device associated with the
+ * given sysfs_class_device
+ * @clsdev: class device whose associated sysfs_device is needed
+ * returns struct sysfs_device * on success or NULL on error
+ */
+struct sysfs_device *sysfs_get_classdev_device
+ (struct sysfs_class_device *clsdev)
+{
+ char linkpath[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
- if ((sysfs_refresh_dir_attributes(clsdev->directory)) != 0) {
- dprintf("Error refreshing class_device attributes\n");
+ if (!clsdev) {
+ errno = EINVAL;
return NULL;
}
- return (clsdev->directory->attributes);
+ if (clsdev->sysdevice)
+ return clsdev->sysdevice;
+
+ memset(linkpath, 0, SYSFS_PATH_MAX);
+ safestrcpy(linkpath, clsdev->path);
+ safestrcat(linkpath, "/device");
+ if (!sysfs_path_is_link(linkpath)) {
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ if (!sysfs_get_link(linkpath, devpath, SYSFS_PATH_MAX))
+ clsdev->sysdevice = sysfs_open_device_path(devpath);
+ }
+ return clsdev->sysdevice;
}
/**
- * sysfs_get_classdev_attr: searches class device's attributes by name
- * @clsdev: class device to look through
- * @name: attribute name to get
- * returns sysfs_attribute reference with success or NULL with error
+ * sysfs_open_class: opens specific class and all its devices on system
+ * returns sysfs_class structure with success or NULL with error.
*/
-struct sysfs_attribute *sysfs_get_classdev_attr
- (struct sysfs_class_device *clsdev, const char *name)
+struct sysfs_class *sysfs_open_class(const char *name)
{
- struct sysfs_attribute *cur = NULL;
- struct sysfs_directory *sdir = NULL;
- struct dlist *attrlist = NULL;
-
- if (clsdev == NULL || name == NULL) {
+ struct sysfs_class *cls = NULL;
+ char classpath[SYSFS_PATH_MAX];
+
+ if (!name) {
errno = EINVAL;
return NULL;
}
-
+
+ memset(classpath, 0, SYSFS_PATH_MAX);
+ if ((sysfs_get_mnt_path(classpath, SYSFS_PATH_MAX)) != 0) {
+ dprintf("Sysfs not supported on this system\n");
+ return NULL;
+ }
+
/*
- * First, see if it's in the current directory. Then look at
- * subdirs since class devices can have subdirs of attributes.
- */
- attrlist = sysfs_get_classdev_attributes(clsdev);
- if (attrlist != NULL) {
- cur = sysfs_get_directory_attribute(clsdev->directory,
- (char *)name);
- if (cur != NULL)
- return cur;
- }
-
- if (clsdev->directory == NULL)
+ * We shall now treat "block" also as a class. Hence, check here
+ * if "name" is "block" and proceed accordingly
+ */
+ if (strcmp(name, SYSFS_BLOCK_NAME) == 0) {
+ safestrcat(classpath, "/");
+ safestrcat(classpath, SYSFS_BLOCK_NAME);
+ } else {
+ safestrcat(classpath, "/");
+ safestrcat(classpath, SYSFS_CLASS_NAME);
+ safestrcat(classpath, "/");
+ safestrcat(classpath, name);
+ }
+ if (sysfs_path_is_dir(classpath)) {
+ dprintf("Class %s not found on the system\n", name);
return NULL;
+ }
- if (clsdev->directory->subdirs == NULL)
- if ((sysfs_read_dir_subdirs(clsdev->directory)) != 0 ||
- clsdev->directory->subdirs == NULL)
- return NULL;
-
- if (clsdev->directory->subdirs != NULL) {
- dlist_for_each_data(clsdev->directory->subdirs, sdir,
- struct sysfs_directory) {
- if ((sysfs_path_is_dir(sdir->path)) != 0)
- continue;
- cur = sysfs_get_directory_attribute(sdir,
- (char *)name);
- if (cur == NULL)
- continue;
- }
+ cls = alloc_class();
+ if (cls == NULL) {
+ dprintf("calloc failed\n");
+ return NULL;
}
- return cur;
+ safestrcpy(cls->name, name);
+ safestrcpy(cls->path, classpath);
+ if ((sysfs_remove_trailing_slash(cls->path)) != 0) {
+ dprintf("Invalid path to class device %s\n", cls->path);
+ sysfs_close_class(cls);
+ return NULL;
+ }
+
+ return cls;
}
/**
- * sysfs_open_classdev_attr: read an attribute for a given class device
- * @classname: name of the class on which to look
- * @dev: class device name for which the attribute has to be read
- * @attrib: attribute to read
- * Returns sysfs_attribute * on SUCCESS and NULL on error
+ * sysfs_get_class_device: get specific class device using the device's id
+ * @cls: sysfs_class to find the device on
+ * @name: name of the class device to look for
*
- * NOTE:
- * A call to sysfs_close_attribute() is required to close the
- * attribute returned and to free memory
- */
-struct sysfs_attribute *sysfs_open_classdev_attr(const char *classname,
- const char *dev, const char *attrib)
+ * Returns sysfs_class_device * on success and NULL on failure
+ */
+struct sysfs_class_device *sysfs_get_class_device(struct sysfs_class *cls,
+ const char *name)
{
- struct sysfs_attribute *attribute = NULL;
char path[SYSFS_PATH_MAX];
+ struct sysfs_class_device *cdev = NULL;
- if (classname == NULL || dev == NULL || attrib == NULL) {
+ if (!cls || !name) {
errno = EINVAL;
return NULL;
}
- memset(path, 0, SYSFS_PATH_MAX);
- if ((get_classdev_path(classname, dev, path, SYSFS_PATH_MAX)) != 0) {
- dprintf("Error getting to device %s on class %s\n",
- dev, classname);
- return NULL;
+
+ if (cls->devices) {
+ cdev = (struct sysfs_class_device *)dlist_find_custom
+ (cls->devices, (void *)name, cdev_name_equal);
+ if (cdev)
+ return cdev;
}
+
+ safestrcpy(path, cls->path);
safestrcat(path, "/");
- safestrcat(path, attrib);
- attribute = sysfs_open_attribute(path);
- if (attribute == NULL) {
- dprintf("Error opening attribute %s on class device %s\n",
- attrib, dev);
- return NULL;
+ safestrcat(path, name);
+ if (!sysfs_path_is_dir(path)) {
+ cdev = sysfs_open_class_device_path(path);
+ if (!cdev) {
+ dprintf("Error opening class device at %s\n", path);
+ return NULL;
+ }
}
- if ((sysfs_read_attribute(attribute)) != 0) {
- dprintf("Error reading attribute %s for class device %s\n",
- attrib, dev);
- sysfs_close_attribute(attribute);
+ if (!cls->devices)
+ cls->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_class_device),
+ sysfs_close_cls_dev);
+
+ dlist_unshift_sorted(cls->devices, cdev, sort_list);
+ return cdev;
+}
+
+/**
+ * sysfs_get_class_devices: get all class devices in the given class
+ * @cls: sysfs_class whose devices list is needed
+ *
+ * Returns a dlist of sysfs_class_device * on success and NULL on failure
+ */
+struct dlist *sysfs_get_class_devices(struct sysfs_class *cls)
+{
+ char path[SYSFS_PATH_MAX];
+ char *cdev_name;
+ struct sysfs_class_device *cdev = NULL;
+ struct dlist *dirlist;
+
+ if (!cls) {
+ errno = EINVAL;
return NULL;
}
- return attribute;
+
+ safestrcpy(path, cls->path);
+ dirlist = read_dir_subdirs(path);
+ if (dirlist) {
+ dlist_for_each_data(dirlist, cdev_name, char) {
+ if (cls->devices) {
+ cdev = (struct sysfs_class_device *)
+ dlist_find_custom(cls->devices,
+ (void *)cdev_name, cdev_name_equal);
+ if (cdev)
+ continue;
+ }
+ safestrcpy(path, cls->path);
+ safestrcat(path, "/");
+ safestrcat(path, cdev_name);
+ cdev = sysfs_open_class_device_path(path);
+ if (cdev) {
+ if (!cls->devices)
+ cls->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_class_device),
+ sysfs_close_cls_dev);
+ dlist_unshift_sorted(cls->devices, cdev,
+ sort_list);
+ }
+ }
+ sysfs_close_list(dirlist);
+ }
+
+ return cls->devices;
}
diff --git a/lib/sysfs_device.c b/lib/sysfs_device.c
index 83c3adc..d1c5b32 100644
--- a/lib/sysfs_device.c
+++ b/lib/sysfs_device.c
@@ -3,7 +3,7 @@
*
* Generic device utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -24,48 +24,63 @@
#include "sysfs.h"
/**
- * get_dev_driver: fills in the dev->driver_name field, but searches by
- * opening subsystem. Only to be used if no driver link exists in
- * device directory.
- *
- * Returns 0 on SUCCESS and 1 on error
+ * get_dev_driver: fills in the dev->driver_name field
+ * Returns 0 on SUCCESS and -1 on error
*/
static int get_dev_driver(struct sysfs_device *dev)
{
- struct dlist *drvlist = NULL;
- char path[SYSFS_PATH_MAX], devpath[SYSFS_PATH_MAX];
- char *drv = NULL, *c = NULL;
-
- if (dev == NULL) {
+ struct dlist *drvlist;
+ char path[SYSFS_PATH_MAX];
+ char devpath[SYSFS_PATH_MAX];
+ char *drv = NULL, *c;
+
+ if (!dev) {
errno = EINVAL;
- return 1;
+ return -1;
+ }
+ memset(path, 0, SYSFS_PATH_MAX);
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ safestrcpymax(path, dev->path, SYSFS_PATH_MAX);
+ safestrcatmax(path, "/driver", SYSFS_PATH_MAX);
+ if (!sysfs_path_is_link(path)) {
+ if (!sysfs_get_link(path, devpath, SYSFS_PATH_MAX)) {
+ if (sysfs_get_name_from_path(devpath,
+ dev->driver_name, SYSFS_NAME_LEN))
+ return -1;
+ }
+ return 0;
}
+
+ /*
+ * Devices on earlier kernels do not have the "driver" link.
+ * Look it up in the bus directory.
+ */
if (dev->bus[0] == '\0')
- return 1;
+ return -1;
memset(path, 0, SYSFS_PATH_MAX);
memset(devpath, 0, SYSFS_PATH_MAX);
- safestrcpy(path, SYSFS_BUS_NAME);
+ safestrcpy(path, dev->path);
+ c = strstr(path, SYSFS_DEVICES_NAME);
+ if (c == NULL) {
+ dprintf("Invalid path to device - %s\n", dev->path);
+ return -1;
+ } else
+ *c = '\0';
+ safestrcat(path, SYSFS_BUS_NAME);
safestrcat(path, "/");
safestrcat(path, dev->bus);
safestrcat(path, "/");
safestrcat(path, SYSFS_DRIVERS_NAME);
- safestrcpy(devpath, dev->path);
- c = strstr(devpath, SYSFS_DEVICES_NAME);
- if (c == NULL)
- return 1;
- *c = '\0';
- safestrcatmax(c, path, (sizeof(devpath) - strlen(devpath)));
-
- drvlist = sysfs_open_subsystem_list(path);
- if (drvlist != NULL) {
+ drvlist = sysfs_open_directory_list(path);
+ if (drvlist) {
dlist_for_each_data(drvlist, drv, char) {
- safestrcpy(path, devpath);
- safestrcat(path, "/");
- safestrcat(path, drv);
- safestrcat(path, "/");
- safestrcat(path, dev->bus_id);
- if (sysfs_path_is_link(path) == 0) {
+ safestrcpy(devpath, path);
+ safestrcat(devpath, "/");
+ safestrcat(devpath, drv);
+ safestrcat(devpath, "/");
+ safestrcat(devpath, dev->bus_id);
+ if (!sysfs_path_is_link(devpath)) {
safestrcpy(dev->driver_name, drv);
sysfs_close_list(drvlist);
return 0;
@@ -73,35 +88,9 @@ static int get_dev_driver(struct sysfs_device *dev)
}
sysfs_close_list(drvlist);
}
- return 1;
+ return -1;
}
-/*
- * get_device_driver_name: gets device's driver name, searches for driver
- * link first before going the brute force route.
- * @dev: device to retrieve driver
- * returns 0 with success and 1 with error
- */
-static int get_device_driver_name(struct sysfs_device *dev)
-{
- char devpath[SYSFS_PATH_MAX], drvpath[SYSFS_PATH_MAX];
-
- if (dev == NULL) {
- errno = EINVAL;
- return 1;
- }
- memset(devpath, 0, SYSFS_PATH_MAX);
- memset(drvpath, 0, SYSFS_PATH_MAX);
- safestrcpy(devpath, dev->path);
- safestrcat(devpath, "/driver");
-
- if ((sysfs_get_link(devpath, drvpath, SYSFS_PATH_MAX)) != 0)
- return(get_dev_driver(dev));
-
- return (sysfs_get_name_from_path(drvpath, dev->driver_name,
- SYSFS_NAME_LEN));
-}
-
/**
* sysfs_get_device_bus: retrieves the bus name the device is on, checks path
* to bus' link to make sure it has correct device.
@@ -110,41 +99,59 @@ static int get_device_driver_name(struct sysfs_device *dev)
*/
int sysfs_get_device_bus(struct sysfs_device *dev)
{
- char subsys[SYSFS_NAME_LEN], path[SYSFS_PATH_MAX];
- char target[SYSFS_PATH_MAX], *bus = NULL, *c = NULL;
- struct dlist *buslist = NULL;
+ char devpath[SYSFS_PATH_MAX];
+ char subsys[SYSFS_NAME_LEN];
+ char path[SYSFS_PATH_MAX];
+ char target[SYSFS_PATH_MAX];
+ char *bus = NULL, *c;
+ struct dlist *buslist;
- if (dev == NULL) {
+ if (!dev) {
errno = EINVAL;
return -1;
}
+ memset(path, 0, SYSFS_PATH_MAX);
+ memset(devpath, 0, SYSFS_PATH_MAX);
+ safestrcpymax(path, dev->path, SYSFS_PATH_MAX);
+ safestrcatmax(path, "/bus", SYSFS_PATH_MAX);
+ if (!sysfs_path_is_link(path)) {
+ if (!sysfs_get_link(path, devpath, SYSFS_PATH_MAX)) {
+ if (sysfs_get_name_from_path(devpath,
+ dev->bus, SYSFS_NAME_LEN))
+ return -1;
+ }
+ return 0;
+ }
+
+ /*
+ * Devices on on earlier kernels do not have the "bus" link.
+ * Look it up in the bus directory.
+ */
memset(subsys, 0, SYSFS_NAME_LEN);
- safestrcpy(subsys, SYSFS_BUS_NAME); /* subsys = bus */
- buslist = sysfs_open_subsystem_list(subsys);
- if (buslist != NULL) {
+ safestrcpy(subsys, dev->path);
+ c = strstr(subsys, SYSFS_DEVICES_NAME);
+ if (c == NULL) {
+ dprintf("Invalid path to device - %s\n", dev->path);
+ return -1;
+ } else
+ *c = '\0';
+ safestrcat(subsys, SYSFS_BUS_NAME);
+ buslist = sysfs_open_directory_list(subsys);
+ if (buslist) {
dlist_for_each_data(buslist, bus, char) {
memset(path, 0, SYSFS_PATH_MAX);
- safestrcpy(path, dev->path);
- c = strstr(path, "/devices");
- if (c == NULL) {
- dprintf("Invalid path to device %s\n", path);
- sysfs_close_list(buslist);
- return -1;
- }
- *c = '\0';
- safestrcat(path, "/");
- safestrcat(path, SYSFS_BUS_NAME);
+ safestrcpy(path, subsys);
safestrcat(path, "/");
safestrcat(path, bus);
safestrcat(path, "/");
safestrcat(path, SYSFS_DEVICES_NAME);
safestrcat(path, "/");
safestrcat(path, dev->bus_id);
- if ((sysfs_path_is_link(path)) == 0) {
+ if (!sysfs_path_is_link(path)) {
memset(target, 0, SYSFS_PATH_MAX);
- if ((sysfs_get_link(path, target,
- SYSFS_PATH_MAX)) != 0) {
+ if (sysfs_get_link(path, target,
+ SYSFS_PATH_MAX)) {
dprintf("Error getting link target\n");
sysfs_close_list(buslist);
return -1;
@@ -156,10 +163,10 @@ int sysfs_get_device_bus(struct sysfs_device *dev)
return 0;
}
}
- }
- sysfs_close_list(buslist);
- }
- return -1;
+ }
+ sysfs_close_list(buslist);
+ }
+ return -1;
}
/**
@@ -169,8 +176,8 @@ int sysfs_get_device_bus(struct sysfs_device *dev)
*/
void sysfs_close_device_tree(struct sysfs_device *devroot)
{
- if (devroot != NULL) {
- if (devroot->children != NULL) {
+ if (devroot) {
+ if (devroot->children) {
struct sysfs_device *child = NULL;
dlist_for_each_data(devroot->children, child,
@@ -183,26 +190,18 @@ void sysfs_close_device_tree(struct sysfs_device *devroot)
}
/**
- * sysfs_close_dev_tree: routine for dlist integration
- */
-static void sysfs_close_dev_tree(void *dev)
-{
- sysfs_close_device_tree((struct sysfs_device *)dev);
-}
-
-/**
* sysfs_close_device: closes and cleans up a device
* @dev = device to clean up
*/
void sysfs_close_device(struct sysfs_device *dev)
{
- if (dev != NULL) {
- if (dev->parent != NULL)
+ if (dev) {
+ if (dev->parent)
sysfs_close_device(dev->parent);
- if (dev->directory != NULL)
- sysfs_close_directory(dev->directory);
- if (dev->children != NULL && dev->children->count == 0)
+ if (dev->children && dev->children->count)
dlist_destroy(dev->children);
+ if (dev->attrlist)
+ dlist_destroy(dev->attrlist);
free(dev);
}
}
@@ -213,36 +212,7 @@ void sysfs_close_device(struct sysfs_device *dev)
*/
static struct sysfs_device *alloc_device(void)
{
- return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
-}
-
-/**
- * open_device_dir: opens up sysfs_directory for specific root dev
- * @name: name of root
- * returns struct sysfs_directory with success and NULL with error
- */
-static struct sysfs_directory *open_device_dir(const char *path)
-{
- struct sysfs_directory *rdir = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- rdir = sysfs_open_directory(path);
- if (rdir == NULL) {
- errno = EINVAL;
- dprintf ("Device %s not supported on this system\n", path);
- return NULL;
- }
- if ((sysfs_read_dir_subdirs(rdir)) != 0) {
- dprintf ("Error reading device at dir %s\n", path);
- sysfs_close_directory(rdir);
- return NULL;
- }
-
- return rdir;
+ return (struct sysfs_device *) calloc(1, sizeof(struct sysfs_device));
}
/**
@@ -252,45 +222,44 @@ static struct sysfs_directory *open_device_dir(const char *path)
*/
struct sysfs_device *sysfs_open_device_path(const char *path)
{
- struct sysfs_device *dev = NULL;
+ struct sysfs_device *dev;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return NULL;
}
- if ((sysfs_path_is_dir(path)) != 0) {
+ if (sysfs_path_is_dir(path)) {
dprintf("Incorrect path to device: %s\n", path);
return NULL;
}
- dev = alloc_device();
- if (dev == NULL) {
+ dev = alloc_device();
+ if (!dev) {
dprintf("Error allocating device at %s\n", path);
return NULL;
}
- if ((sysfs_get_name_from_path(path, dev->bus_id,
- SYSFS_NAME_LEN)) != 0) {
+ if (sysfs_get_name_from_path(path, dev->bus_id, SYSFS_NAME_LEN)) {
errno = EINVAL;
dprintf("Error getting device bus_id\n");
sysfs_close_device(dev);
return NULL;
}
safestrcpy(dev->path, path);
- if ((sysfs_remove_trailing_slash(dev->path)) != 0) {
+ if (sysfs_remove_trailing_slash(dev->path)) {
dprintf("Invalid path to device %s\n", dev->path);
sysfs_close_device(dev);
return NULL;
}
- /*
+ /*
* The "name" attribute no longer exists... return the device's
* sysfs representation instead, in the "dev->name" field, which
* implies that the dev->name and dev->bus_id contain same data.
*/
safestrcpy(dev->name, dev->bus_id);
-
- if (sysfs_get_device_bus(dev) != 0)
+
+ if (sysfs_get_device_bus(dev))
dprintf("Could not get device bus\n");
-
- if (get_device_driver_name(dev) != 0) {
+
+ if (get_dev_driver(dev)) {
dprintf("Could not get device %s's driver\n", dev->bus_id);
safestrcpy(dev->driver_name, SYSFS_UNKNOWN);
}
@@ -299,227 +268,33 @@ struct sysfs_device *sysfs_open_device_path(const char *path)
}
/**
- * sysfs_open_device_tree: opens root device and all of its children,
- * creating a tree of devices. Only opens children.
- * @path: sysfs path to devices
- * returns struct sysfs_device and its children with success or NULL with
- * error.
- */
-struct sysfs_device *sysfs_open_device_tree(const char *path)
-{
- struct sysfs_device *rootdev = NULL, *new = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
- rootdev = sysfs_open_device_path(path);
- if (rootdev == NULL) {
- dprintf("Error opening root device at %s\n", path);
- return NULL;
- }
- if (rootdev->directory == NULL) {
- rootdev->directory = open_device_dir(rootdev->path);
- if (rootdev->directory == NULL)
- return NULL;
- }
- if (rootdev->directory->subdirs != NULL) {
- dlist_for_each_data(rootdev->directory->subdirs, cur,
- struct sysfs_directory) {
- new = sysfs_open_device_tree(cur->path);
- if (new == NULL) {
- dprintf("Error opening device tree at %s\n",
- cur->path);
- sysfs_close_device_tree(rootdev);
- return NULL;
- }
- if (rootdev->children == NULL)
- rootdev->children = dlist_new_with_delete
- (sizeof(struct sysfs_device),
- sysfs_close_dev_tree);
- dlist_unshift_sorted(rootdev->children,
- new, sort_list);
- }
- }
-
- return rootdev;
-}
-
-/**
- * sysfs_close_root_device: closes root and all devices
- * @root: root device to close
- */
-void sysfs_close_root_device(struct sysfs_root_device *root)
-{
- if (root != NULL) {
- if (root->devices != NULL)
- dlist_destroy(root->devices);
- if (root->directory != NULL)
- sysfs_close_directory(root->directory);
- free(root);
- }
-}
-
-/**
- * sysfs_get_root_devices: opens up all the devices under this root device
- * @root: root device to open devices for
- * returns dlist of devices with success and NULL with error
- */
-struct dlist *sysfs_get_root_devices(struct sysfs_root_device *root)
-{
- struct sysfs_device *dev = NULL;
- struct sysfs_directory *cur = NULL;
-
- if (root == NULL) {
- errno = EINVAL;
- return NULL;
- }
- if (root->directory == NULL) {
- root->directory = open_device_dir(root->path);
- if (root->directory == NULL)
- return NULL;
- }
-
- if (root->directory->subdirs == NULL)
- return 0;
-
- dlist_for_each_data(root->directory->subdirs, cur,
- struct sysfs_directory) {
- dev = sysfs_open_device_tree(cur->path);
- if (dev == NULL) {
- dprintf ("Error opening device at %s\n", cur->path);
- continue;
- }
- if (root->devices == NULL)
- root->devices = dlist_new_with_delete
- (sizeof(struct sysfs_device),
- sysfs_close_dev_tree);
- dlist_unshift_sorted(root->devices, dev, sort_list);
- }
-
- return root->devices;
-}
-
-/**
- * sysfs_open_root_device: opens sysfs devices root and all of its
- * devices.
- * @name: name of /sys/devices/root to open
- * returns struct sysfs_root_device if success and NULL with error
- */
-struct sysfs_root_device *sysfs_open_root_device(const char *name)
-{
- struct sysfs_root_device *root = NULL;
- char rootpath[SYSFS_PATH_MAX];
-
- if (name == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- memset(rootpath, 0, SYSFS_PATH_MAX);
- if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
- dprintf ("Sysfs not supported on this system\n");
- return NULL;
- }
-
- safestrcat(rootpath, "/");
- safestrcat(rootpath, SYSFS_DEVICES_NAME);
- safestrcat(rootpath, "/");
- safestrcat(rootpath, name);
- if ((sysfs_path_is_dir(rootpath)) != 0) {
- errno = EINVAL;
- dprintf("Invalid root device: %s\n", name);
- return NULL;
- }
- root = (struct sysfs_root_device *)calloc
- (1, sizeof(struct sysfs_root_device));
- if (root == NULL) {
- dprintf("calloc failure\n");
- return NULL;
- }
- safestrcpy(root->name, name);
- safestrcpy(root->path, rootpath);
- if ((sysfs_remove_trailing_slash(root->path)) != 0) {
- dprintf("Invalid path to root device %s\n", root->path);
- sysfs_close_root_device(root);
- return NULL;
- }
- return root;
-}
-
-/**
- * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
- * the specific device
- * @device: struct sysfs_device * for which attributes are to be returned
- */
-struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
-{
- if (device == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (device->directory == NULL) {
- device->directory = sysfs_open_directory(device->path);
- if (device->directory == NULL)
- return NULL;
- }
- if (device->directory->attributes == NULL) {
- if ((sysfs_read_dir_attributes(device->directory)) != 0)
- return NULL;
- }
- return (device->directory->attributes);
-}
-
-/**
- * sysfs_refresh_device_attributes: refreshes the device's list of attributes
- * @device: sysfs_device whose attributes to refresh
- *
- * NOTE: Upon return, prior references to sysfs_attributes for this device
- * _may_ not be valid
- *
- * Returns list of attributes on success and NULL on failure
+ * sysfs_get_device_attr: searches dev's attributes by name
+ * @dev: device to look through
+ * @name: attribute name to get
+ * returns sysfs_attribute reference with success or NULL with error.
*/
-struct dlist *sysfs_refresh_device_attributes(struct sysfs_device *device)
+struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
+ const char *name)
{
- if (device == NULL) {
+ if (!dev || !name) {
errno = EINVAL;
return NULL;
}
-
- if (device->directory == NULL)
- return (sysfs_get_device_attributes(device));
-
- if ((sysfs_refresh_dir_attributes(device->directory)) != 0) {
- dprintf("Error refreshing device attributes\n");
- return NULL;
- }
-
- return (device->directory->attributes);
+ return get_attribute(dev, (char *)name);
}
/**
- * sysfs_get_device_attr: searches dev's attributes by name
- * @dev: device to look through
- * @name: attribute name to get
- * returns sysfs_attribute reference with success or NULL with error.
+ * sysfs_get_device_attributes: gets list of device attributes
+ * @dev: device whose attributes list is needed
+ * returns dlist of attributes on success or NULL on error
*/
-struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
- const char *name)
+struct dlist *sysfs_get_device_attributes(struct sysfs_device *dev)
{
- struct dlist *attrlist = NULL;
-
- if (dev == NULL || name == NULL) {
+ if (!dev) {
errno = EINVAL;
return NULL;
}
-
- attrlist = sysfs_get_device_attributes(dev);
- if (attrlist == NULL)
- return NULL;
-
- return sysfs_get_directory_attribute(dev->directory, (char *)name);
+ return get_attributes_list(dev);
}
/**
@@ -535,13 +310,13 @@ static int get_device_absolute_path(const char *device, const char *bus,
{
char bus_path[SYSFS_PATH_MAX];
- if (device == NULL || path == NULL) {
+ if (!device || !path) {
errno = EINVAL;
return -1;
}
memset(bus_path, 0, SYSFS_PATH_MAX);
- if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX) != 0) {
+ if (sysfs_get_mnt_path(bus_path, SYSFS_PATH_MAX)) {
dprintf ("Sysfs not supported on this system\n");
return -1;
}
@@ -557,7 +332,7 @@ static int get_device_absolute_path(const char *device, const char *bus,
* We now are at /sys/bus/"bus_name"/devices/"device" which is a link.
* Now read this link to reach to the device.
*/
- if ((sysfs_get_link(bus_path, path, psize)) != 0) {
+ if (sysfs_get_link(bus_path, path, psize)) {
dprintf("Error getting to device %s\n", device);
return -1;
}
@@ -578,21 +353,21 @@ static int get_device_absolute_path(const char *device, const char *bus,
struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
{
char sysfs_path[SYSFS_PATH_MAX];
- struct sysfs_device *device = NULL;
+ struct sysfs_device *device;
- if (bus_id == NULL || bus == NULL) {
+ if (!bus_id || !bus) {
errno = EINVAL;
return NULL;
}
memset(sysfs_path, 0, SYSFS_PATH_MAX);
- if ((get_device_absolute_path(bus_id, bus, sysfs_path,
- SYSFS_PATH_MAX)) != 0) {
+ if (get_device_absolute_path(bus_id, bus, sysfs_path,
+ SYSFS_PATH_MAX)) {
dprintf("Error getting to device %s\n", bus_id);
return NULL;
}
device = sysfs_open_device_path(sysfs_path);
- if (device == NULL) {
+ if (!device) {
dprintf("Error opening device %s\n", bus_id);
return NULL;
}
@@ -608,20 +383,20 @@ struct sysfs_device *sysfs_open_device(const char *bus, const char *bus_id)
*/
struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
{
- char ppath[SYSFS_PATH_MAX], *tmp = NULL;
+ char ppath[SYSFS_PATH_MAX], *tmp;
- if (dev == NULL) {
+ if (!dev) {
errno = EINVAL;
return NULL;
}
- if (dev->parent != NULL)
+ if (dev->parent)
return (dev->parent);
memset(ppath, 0, SYSFS_PATH_MAX);
safestrcpy(ppath, dev->path);
tmp = strrchr(ppath, '/');
- if (tmp == NULL) {
+ if (!tmp) {
dprintf("Invalid path to device %s\n", ppath);
return NULL;
}
@@ -634,68 +409,22 @@ struct sysfs_device *sysfs_get_device_parent(struct sysfs_device *dev)
}
}
*tmp = '\0';
-
+
/*
* All "devices" have the "detach_state" attribute - validate here
*/
safestrcat(ppath, "/detach_state");
- if ((sysfs_path_is_file(ppath)) != 0) {
+ if (sysfs_path_is_file(ppath)) {
dprintf("Device at %s does not have a parent\n", dev->path);
return NULL;
}
tmp = strrchr(ppath, '/');
*tmp = '\0';
dev->parent = sysfs_open_device_path(ppath);
- if (dev->parent == NULL) {
+ if (!dev->parent) {
dprintf("Error opening device %s's parent at %s\n",
dev->bus_id, ppath);
return NULL;
}
return (dev->parent);
}
-
-/*
- * sysfs_open_device_attr: open the given device's attribute
- * @bus: Bus on which to look
- * @dev_id: device for which attribute is required
- * @attrname: name of the attribute to look for
- * Returns struct sysfs_attribute on success and NULL on failure
- *
- * NOTE:
- * A call to sysfs_close_attribute() is required to close
- * the attribute returned and free memory.
- */
-struct sysfs_attribute *sysfs_open_device_attr(const char *bus,
- const char *bus_id, const char *attrib)
-{
- struct sysfs_attribute *attribute = NULL;
- char devpath[SYSFS_PATH_MAX];
-
- if (bus == NULL || bus_id == NULL || attrib == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- memset(devpath, 0, SYSFS_PATH_MAX);
- if ((get_device_absolute_path(bus_id, bus, devpath,
- SYSFS_PATH_MAX)) != 0) {
- dprintf("Error getting to device %s\n", bus_id);
- return NULL;
- }
- safestrcat(devpath, "/");
- safestrcat(devpath, attrib);
- attribute = sysfs_open_attribute(devpath);
- if (attribute == NULL) {
- dprintf("Error opening attribute %s for device %s\n",
- attrib, bus_id);
- return NULL;
- }
- if ((sysfs_read_attribute(attribute)) != 0) {
- dprintf("Error reading attribute %s for device %s\n",
- attrib, bus_id);
- sysfs_close_attribute(attribute);
- return NULL;
- }
- return attribute;
-}
-
diff --git a/lib/sysfs_dir.c b/lib/sysfs_dir.c
index 5276991..009da7a 100644
--- a/lib/sysfs_dir.c
+++ b/lib/sysfs_dir.c
@@ -3,7 +3,7 @@
*
* Directory utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -23,39 +23,38 @@
#include "libsysfs.h"
#include "sysfs.h"
-/**
- * sysfs_del_attribute: routine for dlist integration
- */
-static void sysfs_del_attribute(void *attr)
+static int sort_char(void *new, void *old)
{
- sysfs_close_attribute((struct sysfs_attribute *)attr);
+ return ((strncmp((char *)new, (char *)old,
+ strlen((char *)new))) < 0 ? 1 : 0);
}
/**
- * sysfs_del_link: routine for dlist integration
+ * sysfs_del_name: free function for sysfs_open_subsystem_list
+ * @name: memory area to be freed
*/
-static void sysfs_del_link(void *ln)
+static void sysfs_del_name(void *name)
{
- sysfs_close_link((struct sysfs_link *)ln);
+ free(name);
}
/**
- * sysfs_del_dir: routine for dlist integration
+ * sysfs_del_attribute: routine for dlist integration
*/
-static void sysfs_del_directory(void *dir)
+static void sysfs_del_attribute(void *attr)
{
- sysfs_close_directory((struct sysfs_directory *)dir);
+ sysfs_close_attribute((struct sysfs_attribute *)attr);
}
/**
- * dir_attribute_name_equal: compares dir attributes by name
+ * attr_name_equal: compares attributes by name
* @a: attribute name for comparison
* @b: sysfs_attribute to be compared.
* returns 1 if a==b->name or 0 if not equal
*/
-static int dir_attribute_name_equal(void *a, void *b)
+static int attr_name_equal(void *a, void *b)
{
- if (a == NULL || b == NULL)
+ if (!a || !b)
return 0;
if (strcmp(((char *)a), ((struct sysfs_attribute *)b)->name) == 0)
@@ -65,47 +64,13 @@ static int dir_attribute_name_equal(void *a, void *b)
}
/**
- * dir_link_name_equal: compares dir links by name
- * @a: link name for comparison
- * @b: sysfs_link to be compared.
- * returns 1 if a==b->name or 0 if not equal
- */
-static int dir_link_name_equal(void *a, void *b)
-{
- if (a == NULL || b == NULL)
- return 0;
-
- if (strcmp(((char *)a), ((struct sysfs_link *)b)->name) == 0)
- return 1;
-
- return 0;
-}
-
-/**
- * dir_subdir_name_equal: compares subdirs by name
- * @a: name of subdirectory to compare
- * @b: sysfs_directory subdirectory to be compared
- * returns 1 if a==b->name or 0 if not equal
- */
-static int dir_subdir_name_equal(void *a, void *b)
-{
- if (a == NULL || b == NULL)
- return 0;
-
- if (strcmp(((char *)a), ((struct sysfs_directory *)b)->name) == 0)
- return 1;
-
- return 0;
-}
-
-/**
* sysfs_close_attribute: closes and cleans up attribute
* @sysattr: attribute to close.
*/
void sysfs_close_attribute(struct sysfs_attribute *sysattr)
{
- if (sysattr != NULL) {
- if (sysattr->value != NULL)
+ if (sysattr) {
+ if (sysattr->value)
free(sysattr->value);
free(sysattr);
}
@@ -130,13 +95,13 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path)
{
struct sysfs_attribute *sysattr = NULL;
struct stat fileinfo;
-
- if (path == NULL) {
+
+ if (!path) {
errno = EINVAL;
return NULL;
}
sysattr = alloc_attribute();
- if (sysattr == NULL) {
+ if (!sysattr) {
dprintf("Error allocating attribute at %s\n", path);
return NULL;
}
@@ -163,94 +128,6 @@ struct sysfs_attribute *sysfs_open_attribute(const char *path)
}
/**
- * sysfs_write_attribute: write value to the attribute
- * @sysattr: attribute to write
- * @new_value: value to write
- * @len: length of "new_value"
- * returns 0 with success and -1 with error.
- */
-int sysfs_write_attribute(struct sysfs_attribute *sysattr,
- const char *new_value, size_t len)
-{
- int fd;
- int length;
-
- if (sysattr == NULL || new_value == NULL || len == 0) {
- errno = EINVAL;
- return -1;
- }
-
- if (!(sysattr->method & SYSFS_METHOD_STORE)) {
- dprintf ("Store method not supported for attribute %s\n",
- sysattr->path);
- errno = EACCES;
- return -1;
- }
- if (sysattr->method & SYSFS_METHOD_SHOW) {
- /*
- * read attribute again to see if we can get an updated value
- */
- if ((sysfs_read_attribute(sysattr)) != 0) {
- dprintf("Error reading attribute\n");
- return -1;
- }
- if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) {
- dprintf("Attr %s already has the requested value %s\n",
- sysattr->name, new_value);
- return 0;
- }
- }
- /*
- * open O_WRONLY since some attributes have no "read" but only
- * "write" permission
- */
- if ((fd = open(sysattr->path, O_WRONLY)) < 0) {
- dprintf("Error reading attribute %s\n", sysattr->path);
- return -1;
- }
-
- length = write(fd, new_value, len);
- if (length < 0) {
- dprintf("Error writing to the attribute %s - invalid value?\n",
- sysattr->name);
- close(fd);
- return -1;
- } else if ((unsigned int)length != len) {
- dprintf("Could not write %d bytes to attribute %s\n",
- len, sysattr->name);
- /*
- * since we could not write user supplied number of bytes,
- * restore the old value if one available
- */
- if (sysattr->method & SYSFS_METHOD_SHOW) {
- length = write(fd, sysattr->value, sysattr->len);
- close(fd);
- return -1;
- }
- }
-
- /*
- * Validate length that has been copied. Alloc appropriate area
- * in sysfs_attribute. Verify first if the attribute supports reading
- * (show method). If it does not, do not bother
- */
- if (sysattr->method & SYSFS_METHOD_SHOW) {
- if (length != sysattr->len) {
- sysattr->value = (char *)realloc
- (sysattr->value, length);
- sysattr->len = length;
- safestrcpymax(sysattr->value, new_value, length);
- } else {
- /*"length" of the new value is same as old one */
- safestrcpymax(sysattr->value, new_value, length);
- }
- }
-
- close(fd);
- return 0;
-}
-
-/**
* sysfs_read_attribute: reads value from attribute
* @sysattr: attribute to read
* returns 0 with success and -1 with error.
@@ -263,7 +140,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
long pgsize = 0;
int fd;
- if (sysattr == NULL) {
+ if (!sysattr) {
errno = EINVAL;
return -1;
}
@@ -275,7 +152,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
}
pgsize = sysconf(_SC_PAGESIZE);
fbuf = (char *)calloc(1, pgsize+1);
- if (fbuf == NULL) {
+ if (!fbuf) {
dprintf("calloc failed\n");
return -1;
}
@@ -303,7 +180,7 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
sysattr->len = length;
close(fd);
vbuf = (char *)realloc(fbuf, length+1);
- if (vbuf == NULL) {
+ if (!vbuf) {
dprintf("realloc failed\n");
free(fbuf);
return -1;
@@ -314,758 +191,301 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr)
}
/**
- * sysfs_read_attribute_value: given path to attribute, return its value.
- * values can be up to a pagesize, if buffer is smaller the value will
- * be truncated.
- * @attrpath: sysfs path to attribute
- * @value: buffer to put value
- * @vsize: size of value buffer
+ * sysfs_write_attribute: write value to the attribute
+ * @sysattr: attribute to write
+ * @new_value: value to write
+ * @len: length of "new_value"
* returns 0 with success and -1 with error.
*/
-int sysfs_read_attribute_value(const char *attrpath,
- char *value, size_t vsize)
+int sysfs_write_attribute(struct sysfs_attribute *sysattr,
+ const char *new_value, size_t len)
{
- struct sysfs_attribute *attr = NULL;
- size_t length = 0;
+ int fd;
+ int length;
- if (attrpath == NULL || value == NULL || vsize == 0) {
+ if (!sysattr || !new_value || len == 0) {
errno = EINVAL;
return -1;
}
- attr = sysfs_open_attribute(attrpath);
- if (attr == NULL) {
- dprintf("Invalid attribute path %s\n", attrpath);
- errno = EINVAL;
- return -1;
- }
- if((sysfs_read_attribute(attr)) != 0 || attr->value == NULL) {
- dprintf("Error reading from attribute %s\n", attrpath);
- sysfs_close_attribute(attr);
+ if (!(sysattr->method & SYSFS_METHOD_STORE)) {
+ dprintf ("Store method not supported for attribute %s\n",
+ sysattr->path);
+ errno = EACCES;
return -1;
}
- length = strlen(attr->value);
- if (length > vsize)
- dprintf("Value length %d is larger than supplied buffer %d\n",
- length, vsize);
- safestrcpymax(value, attr->value, vsize);
- sysfs_close_attribute(attr);
-
- return 0;
-}
-
-/**
- * sysfs_get_value_from_attrbutes: given a linked list of attributes and an
- * attribute name, return its value
- * @attr: attribute to search
- * @name: name to look for
- * returns char * value - could be NULL
- */
-char *sysfs_get_value_from_attributes(struct dlist *attr, const char *name)
-{
- struct sysfs_attribute *cur = NULL;
-
- if (attr == NULL || name == NULL) {
- errno = EINVAL;
- return NULL;
- }
- dlist_for_each_data(attr, cur, struct sysfs_attribute) {
- if (strcmp(cur->name, name) == 0)
- return cur->value;
+ if (sysattr->method & SYSFS_METHOD_SHOW) {
+ /*
+ * read attribute again to see if we can get an updated value
+ */
+ if ((sysfs_read_attribute(sysattr))) {
+ dprintf("Error reading attribute\n");
+ return -1;
+ }
+ if ((strncmp(sysattr->value, new_value, sysattr->len)) == 0) {
+ dprintf("Attr %s already has the requested value %s\n",
+ sysattr->name, new_value);
+ return 0;
+ }
}
- return NULL;
-}
-
-/**
- * sysfs_close_link: closes and cleans up link.
- * @ln: link to close.
- */
-void sysfs_close_link(struct sysfs_link *ln)
-{
- if (ln != NULL)
- free(ln);
-}
-
-/**
- * sysfs_close_directory: closes directory, cleans up attributes and links
- * @sysdir: sysfs_directory to close
- */
-void sysfs_close_directory(struct sysfs_directory *sysdir)
-{
- if (sysdir != NULL) {
- if (sysdir->subdirs != NULL)
- dlist_destroy(sysdir->subdirs);
- if (sysdir->links != NULL)
- dlist_destroy(sysdir->links);
- if (sysdir->attributes != NULL)
- dlist_destroy(sysdir->attributes);
- free(sysdir);
- sysdir = NULL;
+ /*
+ * open O_WRONLY since some attributes have no "read" but only
+ * "write" permission
+ */
+ if ((fd = open(sysattr->path, O_WRONLY)) < 0) {
+ dprintf("Error reading attribute %s\n", sysattr->path);
+ return -1;
}
-}
-
-/**
- * alloc_directory: allocates and initializes directory structure
- * returns struct sysfs_directory with success or NULL with error.
- */
-static struct sysfs_directory *alloc_directory(void)
-{
- return (struct sysfs_directory *)
- calloc(1, sizeof(struct sysfs_directory));
-}
-
-/**
- * alloc_link: allocates and initializes link structure
- * returns struct sysfs_link with success or NULL with error.
- */
-static struct sysfs_link *alloc_link(void)
-{
- return (struct sysfs_link *)calloc(1, sizeof(struct sysfs_link));
-}
-
-/**
- * sysfs_read_all_subdirs: calls sysfs_read_directory for all subdirs
- * @sysdir: directory whose subdirs need reading.
- * returns 0 with success and -1 with error.
- */
-int sysfs_read_all_subdirs(struct sysfs_directory *sysdir)
-{
- struct sysfs_directory *cursub = NULL;
- int retval = 0;
- if (sysdir == NULL) {
- errno = EINVAL;
+ length = write(fd, new_value, len);
+ if (length < 0) {
+ dprintf("Error writing to the attribute %s - invalid value?\n",
+ sysattr->name);
+ close(fd);
return -1;
- }
- if (sysdir->subdirs == NULL)
- if ((sysfs_read_dir_subdirs(sysdir)) != 0)
- return 0;
- if (sysdir->subdirs != NULL) {
- dlist_for_each_data(sysdir->subdirs, cursub,
- struct sysfs_directory) {
- if ((sysfs_read_dir_subdirs(cursub)) != 0) {
- dprintf ("Error reading subdirectory %s\n",
- cursub->name);
- retval = -1;
- }
+ } else if ((unsigned int)length != len) {
+ dprintf("Could not write %d bytes to attribute %s\n",
+ len, sysattr->name);
+ /*
+ * since we could not write user supplied number of bytes,
+ * restore the old value if one available
+ */
+ if (sysattr->method & SYSFS_METHOD_SHOW) {
+ length = write(fd, sysattr->value, sysattr->len);
+ close(fd);
+ return -1;
}
}
- if (!retval)
- errno = 0;
- return retval;
-}
-/**
- * sysfs_open_directory: opens a sysfs directory, creates dir struct, and
- * returns.
- * @path: path of directory to open.
- * returns: struct sysfs_directory * with success and NULL on error.
- */
-struct sysfs_directory *sysfs_open_directory(const char *path)
-{
- struct sysfs_directory *sdir = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (sysfs_path_is_dir(path) != 0) {
- dprintf("Invalid path to directory %s\n", path);
- errno = EINVAL;
- return NULL;
- }
-
- sdir = alloc_directory();
- if (sdir == NULL) {
- dprintf("Error allocating directory %s\n", path);
- return NULL;
- }
- if (sysfs_get_name_from_path(path, sdir->name, SYSFS_NAME_LEN) != 0) {
- dprintf("Error getting directory name from path: %s\n", path);
- sysfs_close_directory(sdir);
- return NULL;
- }
- safestrcpy(sdir->path, path);
-
- return sdir;
-}
-
-/**
- * sysfs_open_link: opens a sysfs link, creates struct, and returns
- * @path: path of link to open.
- * returns: struct sysfs_link * with success and NULL on error.
- */
-struct sysfs_link *sysfs_open_link(const char *linkpath)
-{
- struct sysfs_link *ln = NULL;
-
- if (linkpath == NULL || strlen(linkpath) > SYSFS_PATH_MAX) {
- errno = EINVAL;
- return NULL;
- }
-
- ln = alloc_link();
- if (ln == NULL) {
- dprintf("Error allocating link %s\n", linkpath);
- return NULL;
- }
- safestrcpy(ln->path, linkpath);
- if ((sysfs_get_name_from_path(linkpath, ln->name, SYSFS_NAME_LEN)) != 0
- || (sysfs_get_link(linkpath, ln->target, SYSFS_PATH_MAX)) != 0) {
- sysfs_close_link(ln);
- errno = EINVAL;
- dprintf("Invalid link path %s\n", linkpath);
- return NULL;
+ /*
+ * Validate length that has been copied. Alloc appropriate area
+ * in sysfs_attribute. Verify first if the attribute supports reading
+ * (show method). If it does not, do not bother
+ */
+ if (sysattr->method & SYSFS_METHOD_SHOW) {
+ if (length != sysattr->len) {
+ sysattr->value = (char *)realloc
+ (sysattr->value, length);
+ sysattr->len = length;
+ safestrcpymax(sysattr->value, new_value, length);
+ } else {
+ /*"length" of the new value is same as old one */
+ safestrcpymax(sysattr->value, new_value, length);
+ }
}
-
- return ln;
+
+ close(fd);
+ return 0;
}
/**
* add_attribute: open and add attribute at path to given directory
- * @sysdir: directory to add attribute to
+ * @dev: device whose attribute is to be added
* @path: path to attribute
- * returns 0 with success and -1 with error.
+ * returns pointer to attr added with success and NULL with error.
*/
-static int add_attribute(struct sysfs_directory *sysdir, const char *path)
+static struct sysfs_attribute *add_attribute(void *dev, const char *path)
{
- struct sysfs_attribute *attr = NULL;
+ struct sysfs_attribute *attr;
attr = sysfs_open_attribute(path);
- if (attr == NULL) {
+ if (!attr) {
dprintf("Error opening attribute %s\n", path);
- return -1;
+ return NULL;
}
if (attr->method & SYSFS_METHOD_SHOW) {
- if ((sysfs_read_attribute(attr)) != 0) {
+ if (sysfs_read_attribute(attr)) {
dprintf("Error reading attribute %s\n", path);
sysfs_close_attribute(attr);
- return 0;
+ return NULL;
}
}
-
- if (sysdir->attributes == NULL) {
- sysdir->attributes = dlist_new_with_delete
- (sizeof(struct sysfs_attribute), sysfs_del_attribute);
- }
- dlist_unshift_sorted(sysdir->attributes, attr, sort_list);
-
- return 0;
-}
-
-/**
- * add_subdirectory: open and add subdirectory at path to given directory
- * @sysdir: directory to add subdir to
- * @path: path to subdirectory
- * returns 0 with success and -1 with error.
- */
-static int add_subdirectory(struct sysfs_directory *sysdir, const char *path)
-{
- struct sysfs_directory *subdir = NULL;
- subdir = sysfs_open_directory(path);
- if (subdir == NULL) {
- dprintf("Error opening directory %s\n", path);
- return -1;
+ if (!((struct sysfs_device *)dev)->attrlist) {
+ ((struct sysfs_device *)dev)->attrlist = dlist_new_with_delete
+ (sizeof(struct sysfs_attribute), sysfs_del_attribute);
}
- if (sysdir->subdirs == NULL)
- sysdir->subdirs = dlist_new_with_delete
- (sizeof(struct sysfs_directory), sysfs_del_directory);
- dlist_unshift_sorted(sysdir->subdirs, subdir, sort_list);
- return 0;
-}
+ dlist_unshift_sorted(((struct sysfs_device *)dev)->attrlist,
+ attr, sort_list);
-/**
- * add_link: open and add link at path to given directory
- * @sysdir: directory to add link to
- * @path: path to link
- * returns 0 with success and -1 with error.
- */
-static int add_link(struct sysfs_directory *sysdir, const char *path)
-{
- struct sysfs_link *ln = NULL;
-
- ln = sysfs_open_link(path);
- if (ln == NULL) {
- dprintf("Error opening link %s\n", path);
- return -1;
- }
- if (sysdir->links == NULL)
- sysdir->links = dlist_new_with_delete
- (sizeof(struct sysfs_link), sysfs_del_link);
- dlist_unshift_sorted(sysdir->links, ln, sort_list);
- return 0;
+ return attr;
}
-/**
- * sysfs_read_dir_attributes: grabs attributes for the given directory
- * @sysdir: sysfs directory to open
- * returns 0 with success and -1 with error.
+/*
+ * get_attribute - given a sysfs_* struct and a name, return the
+ * sysfs_attribute corresponding to "name"
+ * returns sysfs_attribute on success and NULL on error
*/
-int sysfs_read_dir_attributes(struct sysfs_directory *sysdir)
+struct sysfs_attribute *get_attribute(void *dev, const char *name)
{
- DIR *dir = NULL;
- struct dirent *dirent = NULL;
- char file_path[SYSFS_PATH_MAX];
- int retval = 0;
+ struct sysfs_attribute *cur = NULL;
+ char path[SYSFS_PATH_MAX];
- if (sysdir == NULL) {
+ if (!dev || !name) {
errno = EINVAL;
- return -1;
- }
- dir = opendir(sysdir->path);
- if (dir == NULL) {
- dprintf("Error opening directory %s\n", sysdir->path);
- return -1;
+ return NULL;
}
- while(((dirent = readdir(dir)) != NULL) && retval == 0) {
- if (0 == strcmp(dirent->d_name, "."))
- continue;
- if (0 == strcmp(dirent->d_name, ".."))
- continue;
- memset(file_path, 0, SYSFS_PATH_MAX);
- safestrcpy(file_path, sysdir->path);
- safestrcat(file_path, "/");
- safestrcat(file_path, dirent->d_name);
- if ((sysfs_path_is_file(file_path)) == 0)
- retval = add_attribute(sysdir, file_path);
+
+ if (((struct sysfs_device *)dev)->attrlist) {
+ /* check if attr is already in the list */
+ cur = (struct sysfs_attribute *)dlist_find_custom
+ ((((struct sysfs_device *)dev)->attrlist),
+ (void *)name, attr_name_equal);
+ if (cur)
+ return cur;
}
- closedir(dir);
- if (!retval)
- errno = 0;
- return(retval);
+ safestrcpymax(path, ((struct sysfs_device *)dev)->path,
+ SYSFS_PATH_MAX);
+ safestrcatmax(path, "/", SYSFS_PATH_MAX);
+ safestrcatmax(path, name, SYSFS_PATH_MAX);
+ if (!sysfs_path_is_file(path))
+ cur = add_attribute((void *)dev, path);
+ return cur;
}
/**
- * sysfs_read_dir_links: grabs links in a specific directory
- * @sysdir: sysfs directory to read links
- * returns 0 with success and -1 with error.
+ * read_dir_links: grabs links in a specific directory
+ * @sysdir: sysfs directory to read
+ * returns list of link names with success and NULL with error.
*/
-int sysfs_read_dir_links(struct sysfs_directory *sysdir)
+struct dlist *read_dir_links(const char *path)
{
DIR *dir = NULL;
struct dirent *dirent = NULL;
- char file_path[SYSFS_PATH_MAX];
- int retval = 0;
+ char file_path[SYSFS_PATH_MAX], *linkname;
+ struct dlist *linklist = NULL;
- if (sysdir == NULL) {
+ if (!path) {
errno = EINVAL;
- return -1;
+ return NULL;
}
- dir = opendir(sysdir->path);
- if (dir == NULL) {
- dprintf("Error opening directory %s\n", sysdir->path);
- return -1;
+ dir = opendir(path);
+ if (!dir) {
+ dprintf("Error opening directory %s\n", path);
+ return NULL;
}
- while(((dirent = readdir(dir)) != NULL) && retval == 0) {
+ while ((dirent = readdir(dir)) != NULL) {
if (0 == strcmp(dirent->d_name, "."))
continue;
if (0 == strcmp(dirent->d_name, ".."))
continue;
memset(file_path, 0, SYSFS_PATH_MAX);
- safestrcpy(file_path, sysdir->path);
+ safestrcpy(file_path, path);
safestrcat(file_path, "/");
safestrcat(file_path, dirent->d_name);
- if ((sysfs_path_is_link(file_path)) == 0) {
- retval = add_link(sysdir, file_path);
- if (retval != 0)
- break;
+ if (!sysfs_path_is_link(file_path)) {
+ if (!linklist) {
+ linklist = dlist_new_with_delete
+ (SYSFS_NAME_LEN, sysfs_del_name);
+ if (!linklist) {
+ dprintf("Error creating list\n");
+ return NULL;
+ }
+ }
+ linkname = (char *)calloc(1, SYSFS_NAME_LEN);
+ safestrcpymax(linkname, dirent->d_name, SYSFS_NAME_LEN);
+ dlist_unshift_sorted(linklist, linkname, sort_char);
}
}
closedir(dir);
- if (!retval)
- errno = 0;
- return(retval);
+ return linklist;
}
/**
- * sysfs_read_dir_subdirs: grabs subdirs in a specific directory
- * @sysdir: sysfs directory to read links
- * returns 0 with success and -1 with error.
+ * read_dir_subdirs: grabs subdirs in a specific directory
+ * @sysdir: sysfs directory to read
+ * returns list of directory names with success and NULL with error.
*/
-int sysfs_read_dir_subdirs(struct sysfs_directory *sysdir)
+struct dlist *read_dir_subdirs(const char *path)
{
DIR *dir = NULL;
struct dirent *dirent = NULL;
- char file_path[SYSFS_PATH_MAX];
- int retval = 0;
+ char file_path[SYSFS_PATH_MAX], *dir_name;
+ struct dlist *dirlist = NULL;
- if (sysdir == NULL) {
+ if (!path) {
errno = EINVAL;
- return -1;
+ return NULL;
}
- dir = opendir(sysdir->path);
- if (dir == NULL) {
- dprintf("Error opening directory %s\n", sysdir->path);
- return -1;
+ dir = opendir(path);
+ if (!dir) {
+ dprintf("Error opening directory %s\n", path);
+ return NULL;
}
- while(((dirent = readdir(dir)) != NULL) && retval == 0) {
+ while ((dirent = readdir(dir)) != NULL) {
if (0 == strcmp(dirent->d_name, "."))
continue;
if (0 == strcmp(dirent->d_name, ".."))
continue;
memset(file_path, 0, SYSFS_PATH_MAX);
- safestrcpy(file_path, sysdir->path);
+ safestrcpy(file_path, path);
safestrcat(file_path, "/");
safestrcat(file_path, dirent->d_name);
- if ((sysfs_path_is_dir(file_path)) == 0)
- retval = add_subdirectory(sysdir, file_path);
+ if (!sysfs_path_is_dir(file_path)) {
+ if (!dirlist) {
+ dirlist = dlist_new_with_delete
+ (SYSFS_NAME_LEN, sysfs_del_name);
+ if (!dirlist) {
+ dprintf("Error creating list\n");
+ return NULL;
+ }
+ }
+ dir_name = (char *)calloc(1, SYSFS_NAME_LEN);
+ safestrcpymax(dir_name, dirent->d_name, SYSFS_NAME_LEN);
+ dlist_unshift_sorted(dirlist, dir_name, sort_char);
+ }
}
closedir(dir);
- if (!retval)
- errno = 0;
- return(retval);
+ return dirlist;
}
/**
- * sysfs_read_directory: grabs attributes, links, and subdirectories
- * @sysdir: sysfs directory to open
- * returns 0 with success and -1 with error.
+ * get_attributes_list: build a list of attributes for the given device
+ * @dev: devices whose attributes list is required
+ * returns dlist of attributes on success and NULL on failure
*/
-int sysfs_read_directory(struct sysfs_directory *sysdir)
+struct dlist *get_attributes_list(void *dev)
{
DIR *dir = NULL;
struct dirent *dirent = NULL;
- struct stat astats;
- char file_path[SYSFS_PATH_MAX];
- int retval = 0;
+ struct sysfs_attribute *attr = NULL;
+ char file_path[SYSFS_PATH_MAX], path[SYSFS_PATH_MAX];
- if (sysdir == NULL) {
+ if (!dev) {
errno = EINVAL;
- return -1;
+ return NULL;
}
- dir = opendir(sysdir->path);
- if (dir == NULL) {
- dprintf("Error opening directory %s\n", sysdir->path);
- return -1;
+ memset(path, 0, SYSFS_PATH_MAX);
+ safestrcpy(path, ((struct sysfs_device *)dev)->path);
+ dir = opendir(path);
+ if (!dir) {
+ dprintf("Error opening directory %s\n", path);
+ return NULL;
}
- while(((dirent = readdir(dir)) != NULL) && retval == 0) {
+ while ((dirent = readdir(dir)) != NULL) {
if (0 == strcmp(dirent->d_name, "."))
continue;
if (0 == strcmp(dirent->d_name, ".."))
continue;
memset(file_path, 0, SYSFS_PATH_MAX);
- safestrcpy(file_path, sysdir->path);
+ safestrcpy(file_path, path);
safestrcat(file_path, "/");
safestrcat(file_path, dirent->d_name);
- if ((lstat(file_path, &astats)) != 0) {
- dprintf("stat failed\n");
- continue;
- }
- if (S_ISDIR(astats.st_mode))
- retval = add_subdirectory(sysdir, file_path);
-
- else if (S_ISLNK(astats.st_mode))
- retval = add_link(sysdir, file_path);
-
- else if (S_ISREG(astats.st_mode))
- retval = add_attribute(sysdir, file_path);
- }
- closedir(dir);
- if (!retval)
- errno = 0;
- return(retval);
-}
-
-/**
- * sysfs_refresh_dir_attributes: Refresh attributes list
- * @sysdir: directory whose list of attributes to refresh
- * Returns 0 on success, 1 on failure
- */
-int sysfs_refresh_dir_attributes(struct sysfs_directory *sysdir)
-{
- if (sysdir == NULL) {
- errno = EINVAL;
- return 1;
- }
- if ((sysfs_path_is_dir(sysdir->path)) != 0) {
- dprintf("Invalid path to directory %s\n", sysdir->path);
- errno = EINVAL;
- return 1;
- }
- if (sysdir->attributes != NULL) {
- dlist_destroy(sysdir->attributes);
- sysdir->attributes = NULL;
- }
- if ((sysfs_read_dir_attributes(sysdir)) != 0) {
- dprintf("Error refreshing attributes for directory %s\n",
- sysdir->path);
- return 1;
- }
- errno = 0;
- return 0;
-}
-
-/**
- * sysfs_refresh_dir_links: Refresh links list
- * @sysdir: directory whose list of links to refresh
- * Returns 0 on success, 1 on failure
- */
-int sysfs_refresh_dir_links(struct sysfs_directory *sysdir)
-{
- if (sysdir == NULL) {
- errno = EINVAL;
- return 1;
- }
- if ((sysfs_path_is_dir(sysdir->path)) != 0) {
- dprintf("Invalid path to directory %s\n", sysdir->path);
- errno = EINVAL;
- return 1;
- }
- if (sysdir->links != NULL) {
- dlist_destroy(sysdir->links);
- sysdir->links = NULL;
- }
- if ((sysfs_read_dir_links(sysdir)) != 0) {
- dprintf("Error refreshing links for directory %s\n",
- sysdir->path);
- return 1;
- }
- errno = 0;
- return 0;
-}
-
-/**
- * sysfs_refresh_dir_subdirs: Refresh subdirs list
- * @sysdir: directory whose list of subdirs to refresh
- * Returns 0 on success, 1 on failure
- */
-int sysfs_refresh_dir_subdirs(struct sysfs_directory *sysdir)
-{
- if (sysdir == NULL) {
- errno = EINVAL;
- return 1;
- }
- if ((sysfs_path_is_dir(sysdir->path)) != 0) {
- dprintf("Invalid path to directory %s\n", sysdir->path);
- errno = EINVAL;
- return 1;
- }
- if (sysdir->subdirs != NULL) {
- dlist_destroy(sysdir->subdirs);
- sysdir->subdirs = NULL;
- }
- if ((sysfs_read_dir_subdirs(sysdir)) != 0) {
- dprintf("Error refreshing subdirs for directory %s\n",
- sysdir->path);
- return 1;
- }
- errno = 0;
- return 0;
-}
-
-/**
- * sysfs_get_directory_attribute: retrieves attribute attrname from current
- * directory only
- * @dir: directory to retrieve attribute from
- * @attrname: name of attribute to look for
- *
- * NOTE: Since we know the attribute to look for, this routine looks for the
- * attribute if it was created _after_ the attrlist was read initially.
- *
- * returns sysfs_attribute if found and NULL if not found
- */
-struct sysfs_attribute *sysfs_get_directory_attribute
- (struct sysfs_directory *dir, char *attrname)
-{
- struct sysfs_attribute *attr = NULL;
- char new_path[SYSFS_PATH_MAX];
-
- if (dir == NULL || attrname == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (dir->attributes == NULL)
- if ((sysfs_read_dir_attributes(dir) != 0)
- || (dir->attributes == NULL))
- return NULL;
-
- attr = (struct sysfs_attribute *)dlist_find_custom
- (dir->attributes, attrname, dir_attribute_name_equal);
- if (attr != NULL) {
- if ((attr->method & SYSFS_METHOD_SHOW) &&
- (sysfs_read_attribute(attr)) != 0) {
- dprintf("Error reading attribute %s\n", attr->name);
- return NULL;
- }
- } else {
- memset(new_path, 0, SYSFS_PATH_MAX);
- safestrcpy(new_path, dir->path);
- safestrcat(new_path, "/");
- safestrcat(new_path, attrname);
- if ((sysfs_path_is_file(new_path)) == 0) {
- if ((add_attribute(dir, new_path)) == 0) {
+ if (!sysfs_path_is_file(file_path)) {
+ if (((struct sysfs_device *)dev)->attrlist) {
+ /* check if attr is already in the list */
attr = (struct sysfs_attribute *)
- dlist_find_custom(dir->attributes,
- attrname, dir_attribute_name_equal);
- }
- }
- }
-
- return attr;
-}
-
-/**
- * sysfs_get_directory_link: retrieves link from one directory list
- * @dir: directory to retrieve link from
- * @linkname: name of link to look for
- * returns reference to sysfs_link if found and NULL if not found
- */
-struct sysfs_link *sysfs_get_directory_link
- (struct sysfs_directory *dir, char *linkname)
-{
- if (dir == NULL || linkname == NULL) {
- errno = EINVAL;
- return NULL;
- }
- if (dir->links == NULL) {
- if ((sysfs_read_dir_links(dir) != 0) || (dir->links == NULL))
- return NULL;
- } else {
- if ((sysfs_refresh_dir_links(dir)) != 0)
- return NULL;
- }
-
- return (struct sysfs_link *)dlist_find_custom(dir->links,
- linkname, dir_link_name_equal);
-}
-
-/**
- * sysfs_get_subdirectory: retrieves subdirectory by name.
- * @dir: directory to search for subdirectory.
- * @subname: subdirectory name to get.
- * returns reference to subdirectory or NULL if not found
- */
-struct sysfs_directory *sysfs_get_subdirectory(struct sysfs_directory *dir,
- char *subname)
-{
- struct sysfs_directory *sub = NULL, *cursub = NULL;
-
- if (dir == NULL || subname == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (dir->subdirs == NULL)
- if (sysfs_read_dir_subdirs(dir) != 0)
- return NULL;
-
- sub = (struct sysfs_directory *)dlist_find_custom(dir->subdirs,
- subname, dir_subdir_name_equal);
- if (sub != NULL)
- return sub;
-
- if (dir->subdirs != NULL) {
- dlist_for_each_data(dir->subdirs, cursub,
- struct sysfs_directory) {
- if (cursub->subdirs == NULL) {
- if (sysfs_read_dir_subdirs(cursub) != 0)
- continue;
- if (cursub->subdirs == NULL)
+ dlist_find_custom
+ ((((struct sysfs_device *)dev)->attrlist),
+ (void *)dirent->d_name, attr_name_equal);
+ if (attr)
continue;
- }
- sub = sysfs_get_subdirectory(cursub, subname);
- if (sub != NULL)
- return sub;
- }
- }
- return NULL;
-}
-
-/**
- * sysfs_get_subdirectory_link: looks through all subdirs for specific link.
- * @dir: directory and subdirectories to search for link.
- * @linkname: link name to get.
- * returns reference to link or NULL if not found
- */
-struct sysfs_link *sysfs_get_subdirectory_link(struct sysfs_directory *dir,
- char *linkname)
-{
- struct sysfs_directory *cursub = NULL;
- struct sysfs_link *ln = NULL;
-
- if (dir == NULL || linkname == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- ln = sysfs_get_directory_link(dir, linkname);
- if (ln != NULL)
- return ln;
-
- if (dir->subdirs == NULL)
- if (sysfs_read_dir_subdirs(dir) != 0)
- return NULL;
-
- if (dir->subdirs != NULL) {
- dlist_for_each_data(dir->subdirs, cursub,
- struct sysfs_directory) {
- ln = sysfs_get_subdirectory_link(cursub, linkname);
- if (ln != NULL)
- return ln;
+ else
+ add_attribute(dev, file_path);
+ } else
+ attr = add_attribute(dev, file_path);
}
}
- return NULL;
-}
-
-/**
- * sysfs_get_dir_attributes: returns dlist of directory attributes
- * @dir: directory to retrieve attributes from
- * returns dlist of attributes or NULL
- */
-struct dlist *sysfs_get_dir_attributes(struct sysfs_directory *dir)
-{
- if (dir == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (dir->attributes == NULL) {
- if (sysfs_read_dir_attributes(dir) != 0)
- return NULL;
- }
-
- return (dir->attributes);
-}
-
-/**
- * sysfs_get_dir_links: returns dlist of directory links
- * @dir: directory to return links for
- * returns dlist of links or NULL
- */
-struct dlist *sysfs_get_dir_links(struct sysfs_directory *dir)
-{
- if (dir == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (dir->links == NULL) {
- if (sysfs_read_dir_links(dir) != 0)
- return NULL;
- }
-
- return (dir->links);
-}
-
-/**
- * sysfs_get_dir_subdirs: returns dlist of directory subdirectories
- * @dir: directory to return subdirs for
- * returns dlist of subdirs or NULL
- */
-struct dlist *sysfs_get_dir_subdirs(struct sysfs_directory *dir)
-{
- if (dir == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- if (dir->subdirs == NULL) {
- if (sysfs_read_dir_subdirs(dir) != 0)
- return NULL;
- }
-
- return (dir->subdirs);
+ closedir(dir);
+ return ((struct sysfs_device *)dev)->attrlist;
}
diff --git a/lib/sysfs_driver.c b/lib/sysfs_driver.c
index 88d26b5..c2464fa 100644
--- a/lib/sysfs_driver.c
+++ b/lib/sysfs_driver.c
@@ -3,7 +3,7 @@
*
* Driver utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -28,42 +28,20 @@ static void sysfs_close_driver_device(void *device)
sysfs_close_device((struct sysfs_device *)device);
}
-/**
+/**
* sysfs_close_driver: closes driver and deletes device lists too
* @driver: driver to close
- */
+ */
void sysfs_close_driver(struct sysfs_driver *driver)
{
- if (driver != NULL) {
- if (driver->devices != NULL)
+ if (driver) {
+ if (driver->devices)
dlist_destroy(driver->devices);
- if (driver->directory != NULL)
- sysfs_close_directory(driver->directory);
+ if (driver->attrlist)
+ dlist_destroy(driver->attrlist);
free(driver);
}
}
-
-/**
- * open_driver_dir: Open the sysfs_directory for this driver
- * @driver: Driver whose directory to be opened
- * Returns 0 on success and 1 on failure
- */
-static int open_driver_dir(struct sysfs_driver *driver)
-{
- if (driver == NULL) {
- errno = EINVAL;
- return 1;
- }
- if (driver->directory == NULL) {
- driver->directory = sysfs_open_directory(driver->path);
- if (driver->directory == NULL) {
- dprintf("Error opening driver directory at %s\n",
- driver->path);
- return 1;
- }
- }
- return 0;
-}
/**
* alloc_driver: allocates and initializes driver
@@ -75,244 +53,104 @@ static struct sysfs_driver *alloc_driver(void)
}
/**
- * sysfs_open_driver_path: opens and initializes driver structure
- * @path: path to driver directory
- * returns struct sysfs_driver with success and NULL with error
+ * get_driver_bus: gets bus the driver is on
+ * Returns 0 on success and 1 on error
*/
-struct sysfs_driver *sysfs_open_driver_path(const char *path)
+static int get_driver_bus(struct sysfs_driver *drv)
{
- struct sysfs_driver *driver = NULL;
-
- if (path == NULL) {
- errno = EINVAL;
- return NULL;
- }
- if ((sysfs_path_is_dir(path)) != 0) {
- dprintf("Invalid path to driver: %s\n", path);
- return NULL;
- }
- driver = alloc_driver();
- if (driver == NULL) {
- dprintf("Error allocating driver at %s\n", path);
- return NULL;
- }
- if ((sysfs_get_name_from_path(path, driver->name,
- SYSFS_NAME_LEN)) != 0) {
- dprintf("Error getting driver name from path\n");
- free(driver);
- return NULL;
- }
- safestrcpy(driver->path, path);
- if ((sysfs_remove_trailing_slash(driver->path)) != 0) {
- dprintf("Invalid path to driver %s\n", driver->path);
- sysfs_close_driver(driver);
- return NULL;
- }
+ char drvpath[SYSFS_PATH_MAX], *c = NULL;
- return driver;
-}
-
-/**
- * sysfs_get_driver_attributes: gets list of attributes for the given driver
- * @driver: sysfs_driver for which attributes are required
- * returns a dlist of attributes corresponding to the driver if present
- * NULL otherwise
- */
-struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver)
-{
- if (driver == NULL) {
+ if (!drv) {
errno = EINVAL;
- return NULL;
- }
-
- if (driver->directory == NULL) {
- if ((open_driver_dir(driver)) == 1)
- return NULL;
- }
- if (driver->directory->attributes == NULL) {
- if ((sysfs_read_dir_attributes(driver->directory)) != 0)
- return NULL;
+ return 1;
}
- return(driver->directory->attributes);
-}
-/**
- * sysfs_refresh_driver_attributes: refreshes the driver's list of attributes
- * @driver: sysfs_driver whose attributes to refresh
- *
- * NOTE: Upon return, prior references to sysfs_attributes for this driver
- * _may_ not be valid
- *
- * Returns list of attributes on success and NULL on failure
- */
-struct dlist *sysfs_refresh_driver_attributes(struct sysfs_driver *driver)
-{
- if (driver == NULL) {
- errno = EINVAL;
- return NULL;
- }
- if (driver->directory == NULL)
- return (sysfs_get_driver_attributes(driver));
-
- if ((sysfs_refresh_dir_attributes(driver->directory)) != 0) {
- dprintf("Error refreshing driver attributes\n");
- return NULL;
- }
- return (driver->directory->attributes);
+ safestrcpy(drvpath, drv->path);
+ c = strstr(drvpath, SYSFS_DRIVERS_NAME);
+ if (c == NULL)
+ return 1;
+ *--c = '\0';
+ c = strstr(drvpath, SYSFS_BUS_NAME);
+ if (c == NULL)
+ return 1;
+ c = strstr(c, "/");
+ if (c == NULL)
+ return 1;
+ c++;
+ safestrcpy(drv->bus, c);
+ return 0;
}
/**
- * sysfs_get_driver_attr: searches driver's attributes by name
+ * sysfs_get_driver_attr: searches drv's attributes by name
* @drv: driver to look through
* @name: attribute name to get
- * returns sysfs_attribute reference on success or NULL with error
- */
+ * returns sysfs_attribute reference with success or NULL with error.
+ */
struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
- const char *name)
+ const char *name)
{
- struct dlist *attrlist = NULL;
-
- if (drv == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- attrlist = sysfs_get_driver_attributes(drv);
- if (attrlist == NULL)
+ if (!drv || !name) {
+ errno = EINVAL;
return NULL;
-
- return sysfs_get_directory_attribute(drv->directory, (char *)name);
+ }
+ return get_attribute(drv, (char *)name);
}
/**
- * sysfs_get_driver_links: gets list of links from the given driver
- * @driver: sysfs_driver for which links list is required
- * returns a dlist of links corresponding to the driver if present
- * NULL otherwise
+ * sysfs_get_driver_attributes: gets list of driver attributes
+ * @dev: driver whose attributes list is needed
+ * returns dlist of attributes on success or NULL on error
*/
-struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver)
+struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *drv)
{
- if (driver == NULL) {
+ if (!drv) {
errno = EINVAL;
return NULL;
}
-
- if (driver->directory == NULL)
- if ((open_driver_dir(driver)) == 1)
- return NULL;
-
- if (driver->directory->links == NULL)
- if ((sysfs_read_dir_links(driver->directory)) != 0)
- return NULL;
-
- return(driver->directory->links);
+ return get_attributes_list(drv);
}
/**
- * sysfs_get_driver_devices: open up the list of devices this driver supports
- * @driver: sysfs_driver for which devices are needed
- * Returns dlist of devices on SUCCESS or NULL with ERROR
- */
-struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver)
+ * sysfs_open_driver_path: opens and initializes driver structure
+ * @path: path to driver directory
+ * returns struct sysfs_driver with success and NULL with error
+ */
+struct sysfs_driver *sysfs_open_driver_path(const char *path)
{
- struct sysfs_link *curlink = NULL;
- struct sysfs_device *device = NULL;
+ struct sysfs_driver *driver = NULL;
- if (driver == NULL) {
+ if (!path) {
errno = EINVAL;
return NULL;
}
-
- if (driver->devices != NULL)
- return (driver->devices);
-
- if (driver->directory == NULL || driver->directory->links == NULL) {
- struct dlist *list = NULL;
- list = sysfs_get_driver_links(driver);
- }
-
- if (driver->directory->links != NULL) {
- dlist_for_each_data(driver->directory->links, curlink,
- struct sysfs_link) {
- device = sysfs_open_device_path(curlink->target);
- if (device == NULL) {
- dprintf("Error opening device at %s\n",
- curlink->target);
- return NULL;
- }
- if (driver->devices == NULL)
- driver->devices = dlist_new_with_delete
- (sizeof(struct sysfs_device),
- sysfs_close_driver_device);
- dlist_unshift_sorted(driver->devices, device,
- sort_list);
- }
+ if (sysfs_path_is_dir(path)) {
+ dprintf("Invalid path to driver: %s\n", path);
+ return NULL;
}
- return (driver->devices);
-}
-
-/**
- * sysfs_refresh_driver_devices: Refreshes drivers list of devices
- * @driver: sysfs_driver whose devices list needs to be refreshed
- *
- * NOTE: Upon return from this function, prior sysfs_device references from
- * this driver's list of devices _may_ not be valid
- *
- * Returns dlist of devices on success and NULL on failure
- */
-struct dlist *sysfs_refresh_driver_devices(struct sysfs_driver *driver)
-{
- if (driver == NULL) {
- errno = EINVAL;
+ driver = alloc_driver();
+ if (!driver) {
+ dprintf("Error allocating driver at %s\n", path);
return NULL;
}
-
- if (driver->devices != NULL) {
- dlist_destroy(driver->devices);
- driver->devices = NULL;
+ if (sysfs_get_name_from_path(path, driver->name, SYSFS_NAME_LEN)) {
+ dprintf("Error getting driver name from path\n");
+ free(driver);
+ return NULL;
}
-
- if (driver->directory == NULL)
- return (sysfs_get_driver_devices(driver));
-
- if ((sysfs_refresh_dir_links(driver->directory)) != 0) {
- dprintf("Error refreshing driver links\n");
+ safestrcpy(driver->path, path);
+ if (sysfs_remove_trailing_slash(driver->path)) {
+ dprintf("Invalid path to driver %s\n", driver->path);
+ sysfs_close_driver(driver);
return NULL;
}
-
- return (sysfs_get_driver_devices(driver));
-}
-
-/**
- * sysfs_get_driver_device: looks up a device from a list of driver's devices
- * and returns its sysfs_device corresponding to it
- * @driver: sysfs_driver on which to search
- * @name: name of the device to search
- * Returns a sysfs_device if found, NULL otherwise
- */
-struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver,
- const char *name)
-{
- struct sysfs_device *device = NULL;
- struct dlist *devlist = NULL;
-
- if (driver == NULL || name == NULL) {
- errno = EINVAL;
+ if (get_driver_bus(driver)) {
+ dprintf("Could not get the bus driver is on\n");
+ sysfs_close_driver(driver);
return NULL;
}
- if (driver->devices == NULL) {
- devlist = sysfs_get_driver_devices(driver);
- if (devlist == NULL) {
- dprintf("Error getting driver devices\n");
- return NULL;
- }
- }
- dlist_for_each_data(driver->devices, device, struct sysfs_device) {
- if (!(strncmp(device->name, name, SYSFS_NAME_LEN)))
- return device;
- }
- return NULL;
+ return driver;
}
/**
@@ -327,11 +165,11 @@ struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver,
static int get_driver_path(const char *bus, const char *drv,
char *path, size_t psize)
{
- if (bus == NULL || drv == NULL || path == NULL || psize == 0) {
+ if (!bus || !drv || !path || psize == 0) {
errno = EINVAL;
return -1;
}
- if (sysfs_get_mnt_path(path, psize) != 0) {
+ if (sysfs_get_mnt_path(path, psize)) {
dprintf("Error getting sysfs mount path\n");
return -1;
}
@@ -347,50 +185,6 @@ static int get_driver_path(const char *bus, const char *drv,
}
/**
- * sysfs_open_driver_attr: read the user supplied driver attribute
- * @bus: bus on which to look
- * @drv: driver whose attribute has to be read
- * @attrib: Attribute to be read
- * Returns struct sysfs_attribute on success and NULL on failure
- *
- * NOTE:
- * A call to sysfs_close_attribute() is required to close the
- * attribute returned and to free memory
- */
-struct sysfs_attribute *sysfs_open_driver_attr(const char *bus,
- const char *drv, const char *attrib)
-{
- struct sysfs_attribute *attribute = NULL;
- char path[SYSFS_PATH_MAX];
-
- if (bus == NULL || drv == NULL || attrib == NULL) {
- errno = EINVAL;
- return NULL;
- }
-
- memset(path, 0, SYSFS_PATH_MAX);
- if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) {
- dprintf("Error getting to driver %s\n", drv);
- return NULL;
- }
- safestrcat(path, "/");
- safestrcat(path, attrib);
- attribute = sysfs_open_attribute(path);
- if (attribute == NULL) {
- dprintf("Error opening attribute %s for driver %s\n",
- attrib, drv);
- return NULL;
- }
- if ((sysfs_read_attribute(attribute)) != 0) {
- dprintf("Error reading attribute %s for driver %s\n",
- attrib, drv);
- sysfs_close_attribute(attribute);
- return NULL;
- }
- return attribute;
-}
-
-/**
* sysfs_open_driver: open driver by name, given its bus
* @bus_name: Name of the bus
* @drv_name: Name of the driver
@@ -402,21 +196,66 @@ struct sysfs_driver *sysfs_open_driver(const char *bus_name,
char path[SYSFS_PATH_MAX];
struct sysfs_driver *driver = NULL;
- if (drv_name == NULL || bus_name == NULL) {
+ if (!drv_name || !bus_name) {
errno = EINVAL;
return NULL;
}
memset(path, 0, SYSFS_PATH_MAX);
- if ((get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) != 0) {
+ if (get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) {
dprintf("Error getting to driver %s\n", drv_name);
return NULL;
}
driver = sysfs_open_driver_path(path);
- if (driver == NULL) {
+ if (!driver) {
dprintf("Error opening driver at %s\n", path);
return NULL;
}
return driver;
}
+/**
+ * sysfs_get_driver_devices: gets list of devices that use the driver
+ * @drv: sysfs_driver whose device list is needed
+ * Returns dlist of struct sysfs_device on success and NULL on failure
+ */
+struct dlist *sysfs_get_driver_devices(struct sysfs_driver *drv)
+{
+ char *ln = NULL;
+ struct dlist *linklist = NULL;
+ struct sysfs_device *dev = NULL;
+
+ if (!drv) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ linklist = read_dir_links(drv->path);
+ if (linklist) {
+ dlist_for_each_data(linklist, ln, char) {
+
+ if (!strncmp(ln, SYSFS_MODULE_NAME, strlen(ln)))
+ continue;
+
+ dev = sysfs_open_device(drv->bus, ln);
+ if (!dev) {
+ dprintf("Error opening driver's device\n");
+ sysfs_close_list(linklist);
+ return NULL;
+ }
+ if (!drv->devices) {
+ drv->devices = dlist_new_with_delete
+ (sizeof(struct sysfs_device),
+ sysfs_close_driver_device);
+ if (!drv->devices) {
+ dprintf("Error creating device list\n");
+ sysfs_close_list(linklist);
+ return NULL;
+ }
+ }
+ dlist_unshift_sorted(drv->devices, dev, sort_list);
+ }
+ sysfs_close_list(linklist);
+ }
+ return drv->devices;
+}
diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c
index 8b1f56e..2044662 100644
--- a/lib/sysfs_utils.c
+++ b/lib/sysfs_utils.c
@@ -3,7 +3,7 @@
*
* System utility functions for libsysfs
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -23,12 +23,6 @@
#include "libsysfs.h"
#include "sysfs.h"
-static int sort_char(void *new, void *old)
-{
- return ((strncmp((char *)new, (char *)old,
- strlen((char *)new))) < 0 ? 1 : 0);
-}
-
/**
* sysfs_remove_trailing_slash: Removes any trailing '/' in the given path
* @path: Path to look for the trailing '/'
@@ -38,7 +32,7 @@ int sysfs_remove_trailing_slash(char *path)
{
char *c = NULL;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return 1;
}
@@ -53,54 +47,6 @@ int sysfs_remove_trailing_slash(char *path)
return 0;
}
-/**
- * sysfs_get_fs_mnt_path: Gets the mount point for specified filesystem.
- * @fs_type: filesystem type to retrieve mount point
- * @mnt_path: place to put the retrieved mount path
- * @len: size of mnt_path
- * returns 0 with success and -1 with error.
- */
-static int sysfs_get_fs_mnt_path(const char *fs_type,
- char *mnt_path, size_t len)
-{
- FILE *mnt;
- struct mntent *mntent;
- int ret = 0;
- size_t dirlen = 0;
-
- /* check arg */
- if (fs_type == NULL || mnt_path == NULL || len == 0) {
- errno = EINVAL;
- return -1;
- }
-
- if ((mnt = setmntent(SYSFS_PROC_MNTS, "r")) == NULL) {
- dprintf("Error getting mount information\n");
- return -1;
- }
- while (ret == 0 && dirlen == 0 && (mntent = getmntent(mnt)) != NULL) {
- if (strcmp(mntent->mnt_type, fs_type) == 0) {
- dirlen = strlen(mntent->mnt_dir);
- if (dirlen <= (len - 1)) {
- safestrcpymax(mnt_path, mntent->mnt_dir, len);
- } else {
- dprintf("Error - mount path too long\n");
- ret = -1;
- }
- }
- }
- endmntent(mnt);
- if (dirlen == 0 && ret == 0) {
- dprintf("Filesystem %s not found!\n", fs_type);
- errno = EINVAL;
- ret = -1;
- }
- if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
- ret = -1;
-
- return ret;
-}
-
/*
* sysfs_get_mnt_path: Gets the sysfs mount point.
* @mnt_path: place to put "sysfs" mount point
@@ -109,22 +55,21 @@ static int sysfs_get_fs_mnt_path(const char *fs_type,
*/
int sysfs_get_mnt_path(char *mnt_path, size_t len)
{
- char *sysfs_path = NULL;
- int ret = 0;
-
- if (mnt_path == NULL || len == 0) {
- errno = EINVAL;
- return -1;
+ static char sysfs_path[SYSFS_PATH_MAX] = "";
+ const char *sysfs_path_env;
+
+ /* evaluate only at the first call */
+ if (sysfs_path[0] == '\0') {
+ /* possible overrride of real mount path */
+ sysfs_path_env = getenv(SYSFS_PATH_ENV);
+ if (sysfs_path_env != NULL) {
+ safestrcpymax(mnt_path, sysfs_path_env, len);
+ return 0;
+ }
+ safestrcpymax(mnt_path, SYSFS_MNT_PATH, len);
}
- sysfs_path = getenv(SYSFS_PATH_ENV);
- if (sysfs_path != NULL) {
- safestrcpymax(mnt_path, sysfs_path, len);
- if ((sysfs_remove_trailing_slash(mnt_path)) != 0)
- return 1;
- } else
- ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
- return ret;
+ return 0;
}
/**
@@ -137,8 +82,8 @@ int sysfs_get_name_from_path(const char *path, char *name, size_t len)
{
char tmp[SYSFS_PATH_MAX];
char *n = NULL;
-
- if (path == NULL || name == NULL || len == 0) {
+
+ if (!path || !name || len == 0) {
errno = EINVAL;
return -1;
}
@@ -161,7 +106,7 @@ int sysfs_get_name_from_path(const char *path, char *name, size_t len)
safestrcpymax(name, n, len);
return 0;
}
-
+
/**
* sysfs_get_link: returns link source
* @path: symbolic link's path
@@ -176,7 +121,7 @@ int sysfs_get_link(const char *path, char *target, size_t len)
char *d = NULL, *s = NULL;
int slashes = 0, count = 0;
- if (path == NULL || target == NULL || len == 0) {
+ if (!path || !target || len == 0) {
errno = EINVAL;
return -1;
}
@@ -190,15 +135,15 @@ int sysfs_get_link(const char *path, char *target, size_t len)
return -1;
}
d = linkpath;
- /*
+ /*
* Three cases here:
* 1. relative path => format ../..
* 2. absolute path => format /abcd/efgh
* 3. relative path _from_ this dir => format abcd/efgh
- */
+ */
switch (*d) {
case '.':
- /*
+ /*
* handle the case where link is of type ./abcd/xxx
*/
safestrcpy(temp_path, devdir);
@@ -215,9 +160,8 @@ int sysfs_get_link(const char *path, char *target, size_t len)
}
safestrcpymax(target, temp_path, len);
break;
- /*
- * relative path
- * getting rid of leading "../.."
+ /*
+ * relative path, getting rid of leading "../.."
*/
parse_path:
while (*d == '/' || *d == '.') {
@@ -255,158 +199,28 @@ parse_path:
}
/**
- * sysfs_del_name: free function for sysfs_open_subsystem_list
- * @name: memory area to be freed
- */
-static void sysfs_del_name(void *name)
-{
- free(name);
-}
-
-
-/**
* sysfs_close_list: generic list free routine
* @list: dlist to free
* Returns nothing
*/
void sysfs_close_list(struct dlist *list)
{
- if (list != NULL)
+ if (list)
dlist_destroy(list);
}
/**
- * sysfs_open_subsystem_list: gets a list of all supported "name" subsystem
- * details from the system
- * @name: name of the subsystem, eg., "bus", "class", "devices"
- * Returns a dlist of supported names or NULL if subsystem not supported
- */
-struct dlist *sysfs_open_subsystem_list(char *name)
-{
- char sysfs_path[SYSFS_PATH_MAX], *subsys_name = NULL;
- char *c = NULL;
- struct sysfs_directory *dir = NULL, *cur = NULL;
- struct dlist *list = NULL;
-
- if (name == NULL)
- return NULL;
-
- if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
- dprintf("Error getting sysfs mount point\n");
- return NULL;
- }
-
- safestrcat(sysfs_path, "/");
- safestrcat(sysfs_path, name);
- dir = sysfs_open_directory(sysfs_path);
- if (dir == NULL) {
- dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
- return NULL;
- }
-
- if ((sysfs_read_dir_subdirs(dir)) != 0) {
- dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
- sysfs_close_directory(dir);
- return NULL;
- }
-
- if (dir->subdirs != NULL) {
- list = dlist_new_with_delete(SYSFS_NAME_LEN,
- sysfs_del_name);
- if (list == NULL) {
- dprintf("Error creating list\n");
- sysfs_close_directory(dir);
- return NULL;
- }
-
- dlist_for_each_data(dir->subdirs, cur,
- struct sysfs_directory) {
- subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrcpymax(subsys_name, cur->name, SYSFS_NAME_LEN);
- dlist_unshift_sorted(list, subsys_name, sort_char);
- }
- }
- sysfs_close_directory(dir);
- /*
- * We are now considering "block" as a "class". Hence, if the subsys
- * name requested here is "class", verify if "block" is supported on
- * this system and return the same.
- */
- if (strcmp(name, SYSFS_CLASS_NAME) == 0) {
- c = strstr(sysfs_path, SYSFS_CLASS_NAME);
- if (c == NULL)
- goto out;
- *c = '\0';
- safestrcpymax(c, SYSFS_BLOCK_NAME,
- sizeof(sysfs_path) - strlen(sysfs_path));
- if ((sysfs_path_is_dir(sysfs_path)) == 0) {
- subsys_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrcpymax(subsys_name, SYSFS_BLOCK_NAME,
- SYSFS_NAME_LEN);
- dlist_unshift_sorted(list, subsys_name, sort_char);
- }
- }
-out:
- return list;
-}
-
-
-/**
- * sysfs_open_bus_devices_list: gets a list of all devices on "name" bus
- * @name: name of the subsystem, eg., "pci", "scsi", "usb"
- * Returns a dlist of supported names or NULL if subsystem not supported
- */
-struct dlist *sysfs_open_bus_devices_list(char *name)
+ * sysfs_open_directory_list: gets a list of all directories under "path"
+ * @path: path to read
+ * Returns a dlist of supported names or NULL no directories (errno is set
+ * in case of error
+ */
+struct dlist *sysfs_open_directory_list(const char *path)
{
- char sysfs_path[SYSFS_PATH_MAX], *device_name = NULL;
- struct sysfs_directory *dir = NULL;
- struct sysfs_link *cur = NULL;
- struct dlist *list = NULL;
-
- if (name == NULL)
- return NULL;
-
- if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
- dprintf("Error getting sysfs mount point\n");
- return NULL;
- }
-
- safestrcat(sysfs_path, "/");
- safestrcat(sysfs_path, SYSFS_BUS_NAME);
- safestrcat(sysfs_path, "/");
- safestrcat(sysfs_path, name);
- safestrcat(sysfs_path, "/");
- safestrcat(sysfs_path, SYSFS_DEVICES_NAME);
- dir = sysfs_open_directory(sysfs_path);
- if (dir == NULL) {
- dprintf("Error opening sysfs_directory at %s\n", sysfs_path);
- return NULL;
- }
-
- if ((sysfs_read_dir_links(dir)) != 0) {
- dprintf("Error reading sysfs_directory at %s\n", sysfs_path);
- sysfs_close_directory(dir);
+ if (!path)
return NULL;
- }
-
- if (dir->links != NULL) {
- list = dlist_new_with_delete(SYSFS_NAME_LEN,
- sysfs_del_name);
- if (list == NULL) {
- dprintf("Error creating list\n");
- sysfs_close_directory(dir);
- return NULL;
- }
- dlist_for_each_data(dir->links, cur,
- struct sysfs_link) {
- device_name = (char *)calloc(1, SYSFS_NAME_LEN);
- safestrcpymax(device_name, cur->name, SYSFS_NAME_LEN);
- dlist_unshift_sorted(list, device_name, sort_char);
- }
- }
- sysfs_close_directory(dir);
- return list;
+ return (read_dir_subdirs(path));
}
/**
@@ -418,7 +232,7 @@ int sysfs_path_is_dir(const char *path)
{
struct stat astats;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return 1;
}
@@ -428,7 +242,7 @@ int sysfs_path_is_dir(const char *path)
}
if (S_ISDIR(astats.st_mode))
return 0;
-
+
return 1;
}
@@ -441,7 +255,7 @@ int sysfs_path_is_link(const char *path)
{
struct stat astats;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return 1;
}
@@ -451,7 +265,7 @@ int sysfs_path_is_link(const char *path)
}
if (S_ISLNK(astats.st_mode))
return 0;
-
+
return 1;
}
@@ -464,7 +278,7 @@ int sysfs_path_is_file(const char *path)
{
struct stat astats;
- if (path == NULL) {
+ if (!path) {
errno = EINVAL;
return 1;
}
@@ -474,6 +288,6 @@ int sysfs_path_is_file(const char *path)
}
if (S_ISREG(astats.st_mode))
return 0;
-
+
return 1;
}
diff --git a/test/Makefile.am b/test/Makefile.am
index e33528c..7a86584 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,19 +1,7 @@
-bin_PROGRAMS = dlist_test get_class_dev get_device get_driver write_attr \
- get_bus_devices_list get_classdev_parent testlibsysfs
-BUILT_SOURCES = test.h
-CLEANFILES = test.h
-test.h:
- ./create-test
-dlist_test_SOURCES = dlist_test.c
-get_class_dev_SOURCES = get_class_dev.c
+bin_PROGRAMS = dlist_test get_class_dev_attr get_device get_driver
+get_class_dev_attr_SOURCES = get_class_dev_attr.c
get_device_SOURCES = get_device.c
get_driver_SOURCES = get_driver.c
-get_bus_devices_list_SOURCES = get_bus_devices_list.c
-write_attr_SOURCES = write_attr.c
-get_classdev_parent_SOURCES = get_classdev_parent.c
-testlibsysfs_SOURCES = test.c test_bus.c test_class.c test_dir.c test_driver.c \
- test_root.c test_utils.c testout.c test-defs.h \
- libsysfs.conf create-test
INCLUDES = -I../include
LDADD = ../lib/libsysfs.la
CFLAGS = -Wall -Wshadow -W -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls
diff --git a/test/Makefile.in b/test/Makefile.in
index 4ab200e..448c615 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -83,22 +83,10 @@ VERSION = @VERSION@
am__include = @am__include@
am__quote = @am__quote@
install_sh = @install_sh@
-bin_PROGRAMS = dlist_test get_class_dev get_device get_driver write_attr \
- get_bus_devices_list get_classdev_parent testlibsysfs
-
-BUILT_SOURCES = test.h
-CLEANFILES = test.h
-dlist_test_SOURCES = dlist_test.c
-get_class_dev_SOURCES = get_class_dev.c
+bin_PROGRAMS = dlist_test get_class_dev_attr get_device get_driver
+get_class_dev_attr_SOURCES = get_class_dev_attr.c
get_device_SOURCES = get_device.c
get_driver_SOURCES = get_driver.c
-get_bus_devices_list_SOURCES = get_bus_devices_list.c
-write_attr_SOURCES = write_attr.c
-get_classdev_parent_SOURCES = get_classdev_parent.c
-testlibsysfs_SOURCES = test.c test_bus.c test_class.c test_dir.c test_driver.c \
- test_root.c test_utils.c testout.c test-defs.h \
- libsysfs.conf create-test
-
INCLUDES = -I../include
LDADD = ../lib/libsysfs.la
CFLAGS = -Wall -Wshadow -W -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls
@@ -106,32 +94,20 @@ subdir = test
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_HEADER = $(top_builddir)/config.h
CONFIG_CLEAN_FILES =
-bin_PROGRAMS = dlist_test$(EXEEXT) get_class_dev$(EXEEXT) \
- get_device$(EXEEXT) get_driver$(EXEEXT) write_attr$(EXEEXT) \
- get_bus_devices_list$(EXEEXT) get_classdev_parent$(EXEEXT) \
- testlibsysfs$(EXEEXT)
+bin_PROGRAMS = dlist_test$(EXEEXT) get_class_dev_attr$(EXEEXT) \
+ get_device$(EXEEXT) get_driver$(EXEEXT)
PROGRAMS = $(bin_PROGRAMS)
-am_dlist_test_OBJECTS = dlist_test.$(OBJEXT)
-dlist_test_OBJECTS = $(am_dlist_test_OBJECTS)
+dlist_test_SOURCES = dlist_test.c
+dlist_test_OBJECTS = dlist_test.$(OBJEXT)
dlist_test_LDADD = $(LDADD)
dlist_test_DEPENDENCIES = ../lib/libsysfs.la
dlist_test_LDFLAGS =
-am_get_bus_devices_list_OBJECTS = get_bus_devices_list.$(OBJEXT)
-get_bus_devices_list_OBJECTS = $(am_get_bus_devices_list_OBJECTS)
-get_bus_devices_list_LDADD = $(LDADD)
-get_bus_devices_list_DEPENDENCIES = ../lib/libsysfs.la
-get_bus_devices_list_LDFLAGS =
-am_get_class_dev_OBJECTS = get_class_dev.$(OBJEXT)
-get_class_dev_OBJECTS = $(am_get_class_dev_OBJECTS)
-get_class_dev_LDADD = $(LDADD)
-get_class_dev_DEPENDENCIES = ../lib/libsysfs.la
-get_class_dev_LDFLAGS =
-am_get_classdev_parent_OBJECTS = get_classdev_parent.$(OBJEXT)
-get_classdev_parent_OBJECTS = $(am_get_classdev_parent_OBJECTS)
-get_classdev_parent_LDADD = $(LDADD)
-get_classdev_parent_DEPENDENCIES = ../lib/libsysfs.la
-get_classdev_parent_LDFLAGS =
+am_get_class_dev_attr_OBJECTS = get_class_dev_attr.$(OBJEXT)
+get_class_dev_attr_OBJECTS = $(am_get_class_dev_attr_OBJECTS)
+get_class_dev_attr_LDADD = $(LDADD)
+get_class_dev_attr_DEPENDENCIES = ../lib/libsysfs.la
+get_class_dev_attr_LDFLAGS =
am_get_device_OBJECTS = get_device.$(OBJEXT)
get_device_OBJECTS = $(am_get_device_OBJECTS)
get_device_LDADD = $(LDADD)
@@ -142,18 +118,6 @@ get_driver_OBJECTS = $(am_get_driver_OBJECTS)
get_driver_LDADD = $(LDADD)
get_driver_DEPENDENCIES = ../lib/libsysfs.la
get_driver_LDFLAGS =
-am_testlibsysfs_OBJECTS = test.$(OBJEXT) test_bus.$(OBJEXT) \
- test_class.$(OBJEXT) test_dir.$(OBJEXT) test_driver.$(OBJEXT) \
- test_root.$(OBJEXT) test_utils.$(OBJEXT) testout.$(OBJEXT)
-testlibsysfs_OBJECTS = $(am_testlibsysfs_OBJECTS)
-testlibsysfs_LDADD = $(LDADD)
-testlibsysfs_DEPENDENCIES = ../lib/libsysfs.la
-testlibsysfs_LDFLAGS =
-am_write_attr_OBJECTS = write_attr.$(OBJEXT)
-write_attr_OBJECTS = $(am_write_attr_OBJECTS)
-write_attr_LDADD = $(LDADD)
-write_attr_DEPENDENCIES = ../lib/libsysfs.la
-write_attr_LDFLAGS =
DEFS = @DEFS@
DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)
@@ -163,15 +127,8 @@ LIBS = @LIBS@
depcomp = $(SHELL) $(top_srcdir)/depcomp
am__depfiles_maybe = depfiles
@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/dlist_test.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/get_bus_devices_list.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/get_class_dev.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/get_classdev_parent.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/get_device.Po ./$(DEPDIR)/get_driver.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/test.Po ./$(DEPDIR)/test_bus.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/test_class.Po ./$(DEPDIR)/test_dir.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/test_driver.Po ./$(DEPDIR)/test_root.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/test_utils.Po ./$(DEPDIR)/testout.Po \
-@AMDEP_TRUE@ ./$(DEPDIR)/write_attr.Po
+@AMDEP_TRUE@ ./$(DEPDIR)/get_class_dev_attr.Po \
+@AMDEP_TRUE@ ./$(DEPDIR)/get_device.Po ./$(DEPDIR)/get_driver.Po
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \
@@ -179,15 +136,12 @@ LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \
CCLD = $(CC)
LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
$(AM_LDFLAGS) $(LDFLAGS) -o $@
-DIST_SOURCES = $(dlist_test_SOURCES) $(get_bus_devices_list_SOURCES) \
- $(get_class_dev_SOURCES) $(get_classdev_parent_SOURCES) \
- $(get_device_SOURCES) $(get_driver_SOURCES) \
- $(testlibsysfs_SOURCES) $(write_attr_SOURCES)
+DIST_SOURCES = dlist_test.c $(get_class_dev_attr_SOURCES) \
+ $(get_device_SOURCES) $(get_driver_SOURCES)
DIST_COMMON = Makefile.am Makefile.in
-SOURCES = $(dlist_test_SOURCES) $(get_bus_devices_list_SOURCES) $(get_class_dev_SOURCES) $(get_classdev_parent_SOURCES) $(get_device_SOURCES) $(get_driver_SOURCES) $(testlibsysfs_SOURCES) $(write_attr_SOURCES)
+SOURCES = dlist_test.c $(get_class_dev_attr_SOURCES) $(get_device_SOURCES) $(get_driver_SOURCES)
-all: $(BUILT_SOURCES)
- $(MAKE) $(AM_MAKEFLAGS) all-am
+all: all-am
.SUFFIXES:
.SUFFIXES: .c .lo .o .obj
@@ -228,27 +182,15 @@ clean-binPROGRAMS:
dlist_test$(EXEEXT): $(dlist_test_OBJECTS) $(dlist_test_DEPENDENCIES)
@rm -f dlist_test$(EXEEXT)
$(LINK) $(dlist_test_LDFLAGS) $(dlist_test_OBJECTS) $(dlist_test_LDADD) $(LIBS)
-get_bus_devices_list$(EXEEXT): $(get_bus_devices_list_OBJECTS) $(get_bus_devices_list_DEPENDENCIES)
- @rm -f get_bus_devices_list$(EXEEXT)
- $(LINK) $(get_bus_devices_list_LDFLAGS) $(get_bus_devices_list_OBJECTS) $(get_bus_devices_list_LDADD) $(LIBS)
-get_class_dev$(EXEEXT): $(get_class_dev_OBJECTS) $(get_class_dev_DEPENDENCIES)
- @rm -f get_class_dev$(EXEEXT)
- $(LINK) $(get_class_dev_LDFLAGS) $(get_class_dev_OBJECTS) $(get_class_dev_LDADD) $(LIBS)
-get_classdev_parent$(EXEEXT): $(get_classdev_parent_OBJECTS) $(get_classdev_parent_DEPENDENCIES)
- @rm -f get_classdev_parent$(EXEEXT)
- $(LINK) $(get_classdev_parent_LDFLAGS) $(get_classdev_parent_OBJECTS) $(get_classdev_parent_LDADD) $(LIBS)
+get_class_dev_attr$(EXEEXT): $(get_class_dev_attr_OBJECTS) $(get_class_dev_attr_DEPENDENCIES)
+ @rm -f get_class_dev_attr$(EXEEXT)
+ $(LINK) $(get_class_dev_attr_LDFLAGS) $(get_class_dev_attr_OBJECTS) $(get_class_dev_attr_LDADD) $(LIBS)
get_device$(EXEEXT): $(get_device_OBJECTS) $(get_device_DEPENDENCIES)
@rm -f get_device$(EXEEXT)
$(LINK) $(get_device_LDFLAGS) $(get_device_OBJECTS) $(get_device_LDADD) $(LIBS)
get_driver$(EXEEXT): $(get_driver_OBJECTS) $(get_driver_DEPENDENCIES)
@rm -f get_driver$(EXEEXT)
$(LINK) $(get_driver_LDFLAGS) $(get_driver_OBJECTS) $(get_driver_LDADD) $(LIBS)
-testlibsysfs$(EXEEXT): $(testlibsysfs_OBJECTS) $(testlibsysfs_DEPENDENCIES)
- @rm -f testlibsysfs$(EXEEXT)
- $(LINK) $(testlibsysfs_LDFLAGS) $(testlibsysfs_OBJECTS) $(testlibsysfs_LDADD) $(LIBS)
-write_attr$(EXEEXT): $(write_attr_OBJECTS) $(write_attr_DEPENDENCIES)
- @rm -f write_attr$(EXEEXT)
- $(LINK) $(write_attr_LDFLAGS) $(write_attr_OBJECTS) $(write_attr_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT) core *.core
@@ -257,20 +199,9 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dlist_test.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_bus_devices_list.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_class_dev.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_classdev_parent.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_class_dev_attr.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_device.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/get_driver.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_bus.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_class.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_dir.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_driver.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_root.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_utils.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/testout.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/write_attr.Po@am__quote@
distclean-depend:
-rm -rf ./$(DEPDIR)
@@ -389,7 +320,6 @@ install-strip:
mostlyclean-generic:
clean-generic:
- -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
distclean-generic:
-rm -f Makefile $(CONFIG_CLEAN_FILES)
@@ -397,7 +327,6 @@ distclean-generic:
maintainer-clean-generic:
@echo "This command is intended for maintainers to use"
@echo "it deletes files that may require special tools to rebuild."
- -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES)
clean: clean-am
clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am
@@ -448,8 +377,6 @@ uninstall-am: uninstall-binPROGRAMS uninstall-info-am
mostlyclean-libtool tags uninstall uninstall-am \
uninstall-binPROGRAMS uninstall-info-am
-test.h:
- ./create-test
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
diff --git a/test/get_bus_devices_list.c b/test/get_bus_devices_list.c
index b828ef0..f0e75e5 100644
--- a/test/get_bus_devices_list.c
+++ b/test/get_bus_devices_list.c
@@ -3,7 +3,7 @@
*
* Utility to get the list of devices on a given bus
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/get_classdev_parent.c b/test/get_classdev_parent.c
index 56be44d..38c3ec9 100644
--- a/test/get_classdev_parent.c
+++ b/test/get_classdev_parent.c
@@ -3,7 +3,7 @@
*
* Utility to get a given class device as well as its parent if available
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/get_device.c b/test/get_device.c
index a2e72ca..499821f 100644
--- a/test/get_device.c
+++ b/test/get_device.c
@@ -3,7 +3,7 @@
*
* Utility to get details of a given device
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -33,8 +33,6 @@ static void print_usage(void)
int main(int argc, char *argv[])
{
struct sysfs_device *device = NULL;
- struct sysfs_attribute *attr = NULL;
- struct dlist *attrlist = NULL;
if (argc != 3) {
print_usage();
@@ -47,17 +45,13 @@ int main(int argc, char *argv[])
argv[2], argv[1]);
return 1;
}
-
- attrlist = sysfs_get_device_attributes(device);
- if (attrlist != NULL) {
- dlist_for_each_data(attrlist, attr,
- struct sysfs_attribute)
- fprintf(stdout, "\t%-20s : %s",
- attr->name, attr->value);
- }
- fprintf(stdout, "\n");
-
- sysfs_close_device(device);
+ struct sysfs_device *parent = sysfs_get_device_parent(device);
+ if (parent) {
+ fprintf(stdout, "parent is %s\n", parent->name);
+ fprintf(stdout, "parent is on bus %s, using driver %s\n",
+ parent->bus, parent->driver_name);
+ } else
+ fprintf(stdout, "no parent\n");
return 0;
}
diff --git a/test/get_driver.c b/test/get_driver.c
index 0a808a6..8594df7 100644
--- a/test/get_driver.c
+++ b/test/get_driver.c
@@ -3,7 +3,7 @@
*
* Utility to get details of the given driver
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -28,7 +28,7 @@
static void print_usage(void)
{
- fprintf(stdout, "Usage: get_driver [driver]\n");
+ fprintf(stdout, "Usage: get_driver [bus] [driver]\n");
}
int main(int argc, char *argv[])
@@ -37,21 +37,14 @@ int main(int argc, char *argv[])
struct sysfs_driver *driver = NULL;
struct sysfs_device *device = NULL;
struct dlist *devlist = NULL;
+ struct sysfs_attribute *attr = NULL;
- if (argc != 2) {
+ if (argc != 3) {
print_usage();
return 1;
}
memset(path, 0, SYSFS_PATH_MAX);
- bus = (char *)calloc(1, SYSFS_NAME_LEN);
- if ((sysfs_find_driver_bus(argv[1], bus, SYSFS_NAME_LEN)) < 0) {
- fprintf(stdout, "Driver %s not found\n", argv[1]);
- free(bus);
- return 1;
- }
- fprintf(stdout, "Driver %s is a member of bus %s\n", argv[1], bus);
-
if ((sysfs_get_mnt_path(path, SYSFS_PATH_MAX)) != 0) {
fprintf(stdout, "Sysfs not mounted?\n");
return 1;
@@ -59,25 +52,27 @@ int main(int argc, char *argv[])
strcat(path, "/");
strcat(path, SYSFS_BUS_NAME);
strcat(path, "/");
- strcat(path, bus);
+ strcat(path, argv[1]);
strcat(path, "/");
strcat(path, SYSFS_DRIVERS_NAME);
strcat(path, "/");
- strcat(path, argv[1]);
+ strcat(path, argv[2]);
driver = sysfs_open_driver_path(path);
if (driver == NULL) {
- fprintf(stdout, "Device %s not found\n", argv[1]);
+ fprintf(stdout, "Driver %s not found\n", argv[1]);
free(bus);
return 1;
}
devlist = sysfs_get_driver_devices(driver);
if (devlist != NULL) {
- fprintf(stdout, "%s is used by:\n", argv[1]);
+ fprintf(stdout, "%s is used by:\n", argv[2]);
dlist_for_each_data(devlist, device, struct sysfs_device)
fprintf(stdout, "\t\t%s\n", device->bus_id);
} else
- fprintf(stdout, "%s is presently not used by any device\n", argv[1]);
+ fprintf(stdout, "%s is presently not used by any device\n",
+ argv[2]);
+ fprintf(stdout, "driver %s is on bus %s\n", driver->name, driver->bus);
sysfs_close_driver(driver);
free(bus);
return 0;
diff --git a/test/test-defs.h b/test/test-defs.h
index b9996a3..6dac61f 100644
--- a/test/test-defs.h
+++ b/test/test-defs.h
@@ -3,7 +3,7 @@
*
* Generic defines/declarations for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test.c b/test/test.c
index 0f845c9..a98359b 100644
--- a/test/test.c
+++ b/test/test.c
@@ -3,7 +3,7 @@
*
* Main program for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_bus.c b/test/test_bus.c
index aaca5a6..a900991 100644
--- a/test/test_bus.c
+++ b/test/test_bus.c
@@ -3,7 +3,7 @@
*
* Tests for bus related functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_class.c b/test/test_class.c
index 2f6ef72..269f25e 100644
--- a/test/test_class.c
+++ b/test/test_class.c
@@ -3,7 +3,7 @@
*
* Tests for class related functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_dir.c b/test/test_dir.c
index a6cdc41..d28f0e0 100644
--- a/test/test_dir.c
+++ b/test/test_dir.c
@@ -3,7 +3,7 @@
*
* Tests for directory related functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_driver.c b/test/test_driver.c
index 2c7fafa..8f9c8b4 100644
--- a/test/test_driver.c
+++ b/test/test_driver.c
@@ -3,7 +3,7 @@
*
* Tests for driver related functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_root.c b/test/test_root.c
index 122b36d..443172f 100644
--- a/test/test_root.c
+++ b/test/test_root.c
@@ -3,7 +3,7 @@
*
* Tests for device/root device related functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/test_utils.c b/test/test_utils.c
index 56746be..98328ca 100644
--- a/test/test_utils.c
+++ b/test/test_utils.c
@@ -3,7 +3,7 @@
*
* Tests for utility functions for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/testout.c b/test/testout.c
index 547f9d8..3ad5b1c 100644
--- a/test/testout.c
+++ b/test/testout.c
@@ -3,7 +3,7 @@
*
* Display routines for the libsysfs testsuite
*
- * Copyright (C) IBM Corp. 2004
+ * Copyright (C) IBM Corp. 2004-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
diff --git a/test/write_attr.c b/test/write_attr.c
index 5c28c7a..6f71fae 100644
--- a/test/write_attr.c
+++ b/test/write_attr.c
@@ -3,7 +3,7 @@
*
* Utility to modify the value of a given class device attribute
*
- * Copyright (C) IBM Corp. 2003
+ * Copyright (C) IBM Corp. 2003-2005
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the