From 87fba1c487fc3f2cfad2a2a874ff2d4c4253c423 Mon Sep 17 00:00:00 2001 From: "Mark M. Hoffman" Date: Sun, 18 Sep 2005 20:30:44 +0000 Subject: [PATCH] (mmh) This patch adds the files lib/sysfs.h and lib/sysfs.c, beginning with a rewrite of the sysfs detection code (using libsyfs). In the process, I replaced two poorly named globals with better names, and moved them to sysfs.c. The sysfs detection now happens very early during library init, rather than later. The long term plan is for the two global variables to disappear anyway, as everything to do with sysfs should get moved into lib/sysfs.c. git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@3093 7894878c-1315-0410-8ee3-d5d059ff63e0 --- lib/Module.mk | 2 +- lib/access.c | 3 ++- lib/init.c | 2 ++ lib/proc.c | 45 +++++++++++++++------------------------------ lib/proc.h | 3 --- lib/sysfs.c | 36 ++++++++++++++++++++++++++++++++++++ lib/sysfs.h | 29 +++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 lib/sysfs.c create mode 100644 lib/sysfs.h diff --git a/lib/Module.mk b/lib/Module.mk index d7e5cb43..a8500292 100644 --- a/lib/Module.mk +++ b/lib/Module.mk @@ -49,7 +49,7 @@ LIBTARGETS := $(MODULE_DIR)/$(LIBSTLIBNAME) $(MODULE_DIR)/$(LIBSHLIBNAME) \ LIBCSOURCES := $(MODULE_DIR)/data.c $(MODULE_DIR)/general.c \ $(MODULE_DIR)/error.c $(MODULE_DIR)/chips.c \ $(MODULE_DIR)/proc.c $(MODULE_DIR)/access.c \ - $(MODULE_DIR)/init.c + $(MODULE_DIR)/init.c $(MODULE_DIR)/sysfs.c LIBOTHEROBJECTS := $(MODULE_DIR)/conf-parse.o $(MODULE_DIR)/conf-lex.o LIBSHOBJECTS := $(LIBCSOURCES:.c=.lo) $(LIBOTHEROBJECTS:.o=.lo) LIBSTOBJECTS := $(LIBCSOURCES:.c=.ao) $(LIBOTHEROBJECTS:.o=.ao) diff --git a/lib/access.c b/lib/access.c index 62f3165e..f83fe174 100644 --- a/lib/access.c +++ b/lib/access.c @@ -26,6 +26,7 @@ #include "error.h" #include "proc.h" #include "general.h" +#include "sysfs.h" static int sensors_do_this_chip_sets(sensors_chip_name name); @@ -120,7 +121,7 @@ const sensors_chip_feature *sensors_lookup_feature_name(const char *prefix, if there are wildcards. */ int sensors_chip_name_has_wildcards(sensors_chip_name chip) { - if(foundsysfs) { + if (sensors_found_sysfs) { if (chip.prefix == SENSORS_CHIP_NAME_PREFIX_ANY) return 1; else diff --git a/lib/init.c b/lib/init.c index eceec29c..0bcd0d8e 100644 --- a/lib/init.c +++ b/lib/init.c @@ -25,6 +25,7 @@ #include "error.h" #include "access.h" #include "conf.h" +#include "sysfs.h" static void free_proc_chips_entry(sensors_proc_chips_entry entry); static void free_chip_name(sensors_chip_name name); @@ -40,6 +41,7 @@ int sensors_init(FILE *input) { int res; sensors_cleanup(); + sensors_init_sysfs(); if ((res = sensors_read_proc_chips())) return res; if ((res = sensors_read_proc_bus())) diff --git a/lib/proc.c b/lib/proc.c index 8009ade4..5adaa461 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -29,6 +29,7 @@ #include "general.h" #include #include +#include "sysfs.h" /* OK, this proves one thing: if there are too many chips detected, we get in trouble. The limit is around 4096/sizeof(struct sensors_chip_data), which @@ -42,9 +43,6 @@ static char buf[BUF_LEN]; static int sensors_get_chip_id(sensors_chip_name name); -int foundsysfs=0; -char sysfsmount[NAME_MAX]; - static int getsysname(const sensors_chip_feature *feature, char *sysname, int *sysmag, char *altsysname); @@ -85,7 +83,7 @@ static int sensors_read_one_sysfs_chip(char *name, char *dirname, char *id) /* Find out if ISA or not */ sprintf(name, "%s/class/i2c-adapter/i2c-%d/device/name", - sysfsmount, entry.name.bus); + sensors_sysfs_mount, entry.name.bus); if ((f = fopen(name, "r")) != NULL) { if (fgets(x, 5, f) != NULL && !strncmp(x, "ISA ", 4)) @@ -103,7 +101,6 @@ int sensors_read_proc_chips(void) { struct dirent *de; DIR *dir; - FILE *f; char sysfs[NAME_MAX], n[NAME_MAX]; char dirname[NAME_MAX]; int res; @@ -114,26 +111,11 @@ int sensors_read_proc_chips(void) sensors_proc_chips_entry entry; int lineno; - /* First figure out where sysfs was mounted */ - if ((f = fopen("/proc/mounts", "r")) == NULL) + if (!sensors_found_sysfs) goto proc; - while (fgets(n, NAME_MAX, f)) { - char *fstype = dirname; /* alias to keep the code readable */ - - if (sscanf(n, "%*[^ ] %[^ ] %[^ ] %*s\n", sysfsmount, fstype) - == 2 && !strcasecmp(fstype, "sysfs")) { - foundsysfs++; - break; - } - } - fclose(f); - if (!foundsysfs) { - memset(sysfsmount, '\0', sizeof(sysfsmount)); - goto proc; - } /* Try /sys/class/hwmon first (Linux 2.6.14 and up) */ - strncpy(sysfs, sysfsmount, sizeof(sysfs) - 1); + strncpy(sysfs, sensors_sysfs_mount, sizeof(sysfs) - 1); sysfs[sizeof(sysfs) - 1] = '\0'; strncat(sysfs, "/class/hwmon", sizeof(sysfs) - strlen(sysfs) - 1); @@ -181,7 +163,7 @@ int sensors_read_proc_chips(void) oldsys: /* Fall back to /sys/bus/i2c (Linux 2.5 to 2.6.13) */ - strncpy(sysfs, sysfsmount, sizeof(sysfs) - 1); + strncpy(sysfs, sensors_sysfs_mount, sizeof(sysfs) - 1); sysfs[sizeof(sysfs) - 1] = '\0'; strncat(sysfs, "/bus/i2c/devices", sizeof(sysfs) - strlen(sysfs) - 1); @@ -242,9 +224,12 @@ int sensors_read_proc_bus(void) char sysfs[NAME_MAX], n[NAME_MAX]; char dirname[NAME_MAX]; - if(foundsysfs) { - strcpy(sysfs, sysfsmount); - strcat(sysfs, "/class/i2c-adapter"); + if (sensors_found_sysfs) { + strncpy(sysfs, sensors_sysfs_mount, sizeof(sysfs) - 1); + sysfs[sizeof(sysfs) - 1] = 0; + strncat(sysfs, "/class/i2c-adapter", + sizeof(sysfs) - strlen(sysfs) - 1); + /* Then read from it */ dir = opendir(sysfs); if (! dir) @@ -352,12 +337,12 @@ int sensors_read_proc(sensors_chip_name name, int feature, double *value) int buflen = BUF_LEN; int mag; - if(!foundsysfs) + if (!sensors_found_sysfs) if ((sysctl_name[2] = sensors_get_chip_id(name)) < 0) return sysctl_name[2]; if (! (the_feature = sensors_lookup_feature_nr(name.prefix,feature))) return -SENSORS_ERR_NO_ENTRY; - if(foundsysfs) { + if (sensors_found_sysfs) { char n[NAME_MAX], altn[NAME_MAX]; FILE *f; strcpy(n, name.busname); @@ -394,12 +379,12 @@ int sensors_write_proc(sensors_chip_name name, int feature, double value) int buflen = BUF_LEN; int mag; - if(!foundsysfs) + if (!sensors_found_sysfs) if ((sysctl_name[2] = sensors_get_chip_id(name)) < 0) return sysctl_name[2]; if (! (the_feature = sensors_lookup_feature_nr(name.prefix,feature))) return -SENSORS_ERR_NO_ENTRY; - if(foundsysfs) { + if (sensors_found_sysfs) { char n[NAME_MAX], altn[NAME_MAX]; FILE *f; strcpy(n, name.busname); diff --git a/lib/proc.h b/lib/proc.h index a2a0c9c1..05638b58 100644 --- a/lib/proc.h +++ b/lib/proc.h @@ -34,7 +34,4 @@ extern int sensors_read_proc(sensors_chip_name name, int feature, extern int sensors_write_proc(sensors_chip_name name, int feature, double value); -/* Linux 2.6 uses sysfs, some code needs to know */ -extern int foundsysfs; - #endif diff --git a/lib/sysfs.c b/lib/sysfs.c new file mode 100644 index 00000000..5960edce --- /dev/null +++ b/lib/sysfs.c @@ -0,0 +1,36 @@ +/* + sysfs.c - Part of libsensors, a library for reading Linux sensor data + Copyright (c) 2005 Mark M. Hoffman + + 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 Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include +#include "sysfs.h" + +int sensors_found_sysfs = 0; + +char sensors_sysfs_mount[NAME_MAX]; + +/* returns !0 if sysfs filesystem was found, 0 otherwise */ +int sensors_init_sysfs(void) +{ + if (sysfs_get_mnt_path(sensors_sysfs_mount, NAME_MAX) == 0) + sensors_found_sysfs = 1; + + return sensors_found_sysfs; +} diff --git a/lib/sysfs.h b/lib/sysfs.h new file mode 100644 index 00000000..933232e2 --- /dev/null +++ b/lib/sysfs.h @@ -0,0 +1,29 @@ +/* + sysfs.h - part of libsensors, a library for reading Linux sensor data + Copyright (C) Mark M. Hoffman + + 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 Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef SENSORS_LIB_SYSFS_H +#define SENSORS_LIB_SYSFS_H + +extern int sensors_found_sysfs; + +extern char sensors_sysfs_mount[]; + +extern int sensors_init_sysfs(void); + +#endif /* !SENSORS_LIB_SYSFS_H */