diff --git a/lib/Module.mk b/lib/Module.mk index 8b534b6d..de0de030 100644 --- a/lib/Module.mk +++ b/lib/Module.mk @@ -36,7 +36,8 @@ 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)/proc.c $(MODULE_DIR)/access.c \ + $(MODULE_DIR)/init.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 f5568a47..7d77702c 100644 --- a/lib/access.c +++ b/lib/access.c @@ -141,10 +141,12 @@ int sensors_get_label(sensors_chip_name name, int feature, char **result) for (chip = NULL; (chip = sensors_for_all_config_chips(name,chip));) for (i = 0; i < chip->labels_count; i++) if (!strcmp(featureptr->name, chip->labels[i].name)) { - *result = strdup(chip->labels[i].name); + if (! (*result = strdup(chip->labels[i].name))) + sensors_fatal_error("sensors_get_label","Allocating label text"); return 0; } - *result = strdup(featureptr->name); + if (! (*result = strdup(featureptr->name))) + sensors_fatal_error("sensors_get_label","Allocating label text"); return 0; } diff --git a/lib/data.c b/lib/data.c index 0e78b4b1..50c8543b 100644 --- a/lib/data.c +++ b/lib/data.c @@ -66,6 +66,8 @@ int sensors_parse_chip_name(const char *orig_name, sensors_chip_name *res) char *name = strdup(orig_name); int i; + if (! name) + sensors_fatal_error("sensors_parse_chip_name","Allocating new name"); /* First split name in upto four pieces. */ if ((part4 = strrchr(name,'-'))) *part4++ = '\0'; @@ -155,8 +157,8 @@ DONE2: if (!strcmp(name,"*")) res->prefix = SENSORS_CHIP_NAME_PREFIX_ANY; - else - res->prefix = strdup(name); + else if (! (res->prefix = strdup(name))) + sensors_fatal_error("sensors_parse_chip_name","Allocating new name"); goto SUCCES; SUCCES: diff --git a/lib/data.h b/lib/data.h index 24ff9646..02f88343 100644 --- a/lib/data.h +++ b/lib/data.h @@ -22,8 +22,9 @@ #include "sensors.h" -/* This header file contains all kinds of data structures which are - internally used. */ +/* This header file contains all kinds of data structures which are used + for the representation of the config file data and the /proc/.../chips + data. */ typedef enum sensors_operation { sensors_add, sensors_sub, sensors_multiply, sensors_divide, diff --git a/lib/error.c b/lib/error.c index 32f0bcf4..3ea6f094 100644 --- a/lib/error.c +++ b/lib/error.c @@ -41,7 +41,7 @@ static const char *errorlist[] = #define ERROR_LIST_LEN (sizeof(errorlist) / sizeof(char *)) -char *sensors_strerror(int errnum) +const char *sensors_strerror(int errnum) { if (errnum < 0) errnum = -errnum; diff --git a/lib/error.h b/lib/error.h index 13ab56db..06eac7cf 100644 --- a/lib/error.h +++ b/lib/error.h @@ -27,11 +27,12 @@ #define SENSORS_ERR_DIV_ZERO 5 /* Divide by zero */ #define SENSORS_ERR_CHIP_NAME 6 /* Can't parse chip name */ #define SENSORS_ERR_BUS_NAME 7 /* Can't parse bus name */ +#define SENSORS_ERR_PARSE 8 /* General parse error */ /* This function returns a pointer to a string which describes the error. errnum may be negative (the corresponding positive error is returned). You may not modify the result! */ -extern char *sensors_strerror(int errnum); +extern const char *sensors_strerror(int errnum); /* This function is called when a parse error is detected. Give it a new value, and your own function is called instead of the default (which diff --git a/lib/init.c b/lib/init.c new file mode 100644 index 00000000..431c424c --- /dev/null +++ b/lib/init.c @@ -0,0 +1,137 @@ +/* + init.c - Part of libsensors, a Linux library for reading sensor data. + Copyright (c) 1998 Frodo Looijaard + + 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 "sensors.h" +#include "data.h" +#include "proc.h" +#include "error.h" + +extern int sensors_yyparse(void); + +static void free_proc_chips_entry(sensors_proc_chips_entry entry); +static void free_chip_name(sensors_chip_name name); +static void free_bus(sensors_bus bus); +static void free_chip(sensors_chip chip); +static void free_label(sensors_label label); +static void free_set(sensors_set set); +static void free_compute(sensors_compute compute); +static void free_expr(sensors_expr *expr); + +int sensors_init(void) +{ + int res; + sensors_cleanup(); + if ((res = sensors_read_proc_chips())) + return res; + if ((res = sensors_yyparse())) + return SENSORS_ERR_PARSE; + return 0; +} + +void sensors_cleanup(void) +{ + int i; + + for (i = 0; i < sensors_proc_chips_count; i++) + free_proc_chips_entry(sensors_proc_chips[i]); + free(sensors_proc_chips); + sensors_proc_chips = NULL; + sensors_proc_chips_count = sensors_proc_chips_max = 0; + + for (i = 0; i < sensors_config_busses_count; i++) + free_bus(sensors_config_busses[i]); + free(sensors_config_busses); + sensors_config_busses = NULL; + sensors_config_busses_count = sensors_config_busses_max = 0; + + for (i = 0; i < sensors_config_chips_count; i++) + free_chip(sensors_config_chips[i]); + free(sensors_config_chips); + sensors_config_chips = NULL; + sensors_config_chips_count = sensors_config_chips_max = 0; +} + +void free_proc_chips_entry(sensors_proc_chips_entry entry) +{ + free_chip_name(entry.name); +} + +void free_chip_name(sensors_chip_name name) +{ + free(name.prefix); +} + +void free_bus(sensors_bus bus) +{ + free(bus.adapter); + free(bus.algorithm); +} + +void free_chip(sensors_chip chip) +{ + int i; + + for (i = 0; i < chip.labels_count; i++) + free_label(chip.labels[i]); + free(chip.labels); + chip.labels_count = chip.labels_max = 0; + + for (i = 0; i < chip.sets_count; i++) + free_set(chip.sets[i]); + free(chip.sets); + chip.sets_count = chip.sets_max = 0; + + for (i = 0; i < chip.computes_count; i++) + free_compute(chip.computes[i]); + free(chip.computes); + chip.computes_count = chip.computes_max = 0; +} + +void free_label(sensors_label label) +{ + free(label.name); + free(label.value); +} + +void free_set(sensors_set set) +{ + free(set.name); + free_expr(set.value); +} + +void free_compute(sensors_compute compute) +{ + free(compute.name); + free_expr(compute.from_proc); + free_expr(compute.to_proc); +} + +void free_expr(sensors_expr *expr) +{ + if ((expr->kind) == sensors_kind_var) + free(expr->data.var); + else if ((expr->kind) == sensors_kind_sub) { + if (expr->data.subexpr.sub1) + free_expr(expr->data.subexpr.sub1); + if (expr->data.subexpr.sub2) + free_expr(expr->data.subexpr.sub2); + } + free(expr); +} diff --git a/lib/sensors.h b/lib/sensors.h index 0f5c04f9..04d2b108 100644 --- a/lib/sensors.h +++ b/lib/sensors.h @@ -34,6 +34,14 @@ typedef struct sensors_chip_name { int addr; } sensors_chip_name; +/* (Re)load the configuration file and the detected chips list. */ +extern int sensors_init(void); + +/* Strictly optional clean-up function: You can't access anything after + this, until the next sensors_init() call! */ +extern void sensors_cleanup(void); + + /* Parse a chip name to the internal representation. Return 0 on succes, <0 on error. */ extern int sensors_parse_chip_name(const char *orig_name,