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
This commit is contained in:
Mark M. Hoffman committed 2005-09-18 20:30:44 +00:00
1 parent 10337cf80b
commit 87fba1c487
7 files changed
+85 -35

No files matched your search

+1 -1
View File
@@ -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)
+2 -1
View File
@@ -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
+2
View File
@@ -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()))
+15 -30
View File
@@ -29,6 +29,7 @@
#include "general.h"
#include <limits.h>
#include <dirent.h>
#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);
-3
View File
@@ -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
+36
View File
@@ -0,0 +1,36 @@
/*
sysfs.c - Part of libsensors, a library for reading Linux sensor data
Copyright (c) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
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 <string.h>
#include <limits.h>
#include <sysfs/libsysfs.h>
#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;
}
+29
View File
@@ -0,0 +1,29 @@
/*
sysfs.h - part of libsensors, a library for reading Linux sensor data
Copyright (C) Mark M. Hoffman <mhoffman@lightlink.com>
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 */