1998-12-20 21:57:39 +00:00
|
|
|
/*
|
|
|
|
init.c - Part of libsensors, a Linux library for reading sensor data.
|
1999-02-08 22:50:29 +00:00
|
|
|
Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
|
2007-09-29 19:18:15 +00:00
|
|
|
Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
|
1998-12-20 21:57:39 +00:00
|
|
|
|
|
|
|
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
|
2008-03-26 13:37:12 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301 USA.
|
1998-12-20 21:57:39 +00:00
|
|
|
*/
|
|
|
|
|
2008-04-08 14:50:13 +00:00
|
|
|
#include <locale.h>
|
1998-12-20 21:57:39 +00:00
|
|
|
#include <stdlib.h>
|
1998-12-21 14:14:25 +00:00
|
|
|
#include <stdio.h>
|
2008-04-08 14:50:13 +00:00
|
|
|
#include <string.h>
|
2007-10-25 09:59:05 +00:00
|
|
|
#include <errno.h>
|
1998-12-20 21:57:39 +00:00
|
|
|
#include "sensors.h"
|
|
|
|
#include "data.h"
|
|
|
|
#include "error.h"
|
1998-12-24 01:13:44 +00:00
|
|
|
#include "access.h"
|
1998-12-25 21:51:59 +00:00
|
|
|
#include "conf.h"
|
2005-09-18 20:30:44 +00:00
|
|
|
#include "sysfs.h"
|
2006-05-20 15:24:37 +00:00
|
|
|
#include "scanner.h"
|
2007-09-05 20:10:00 +00:00
|
|
|
#include "init.h"
|
1998-12-20 21:57:39 +00:00
|
|
|
|
2007-10-25 09:59:05 +00:00
|
|
|
#define DEFAULT_CONFIG_FILE ETCDIR "/sensors3.conf"
|
|
|
|
#define ALT_CONFIG_FILE ETCDIR "/sensors.conf"
|
2007-10-25 09:48:18 +00:00
|
|
|
|
2008-04-08 14:50:13 +00:00
|
|
|
/* Wrapper around sensors_yyparse(), which clears the locale so that
|
|
|
|
the decimal numbers are always parsed properly. */
|
|
|
|
static int sensors_parse(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
char *locale;
|
|
|
|
|
|
|
|
/* Remember the current locale and clear it */
|
|
|
|
locale = setlocale(LC_ALL, NULL);
|
|
|
|
if (locale) {
|
|
|
|
locale = strdup(locale);
|
|
|
|
setlocale(LC_ALL, "C");
|
|
|
|
}
|
|
|
|
|
|
|
|
res = sensors_yyparse();
|
|
|
|
|
|
|
|
/* Restore the old locale */
|
|
|
|
if (locale) {
|
|
|
|
setlocale(LC_ALL, locale);
|
|
|
|
free(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1998-12-21 14:14:25 +00:00
|
|
|
int sensors_init(FILE *input)
|
1998-12-20 21:57:39 +00:00
|
|
|
{
|
2007-08-31 14:42:20 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!sensors_init_sysfs())
|
2007-09-23 13:53:11 +00:00
|
|
|
return -SENSORS_ERR_KERNEL;
|
2007-08-31 14:42:20 +00:00
|
|
|
if ((res = sensors_read_sysfs_bus()) ||
|
|
|
|
(res = sensors_read_sysfs_chips()))
|
2007-10-25 09:51:19 +00:00
|
|
|
goto exit_cleanup;
|
2007-10-25 09:48:18 +00:00
|
|
|
|
2007-10-25 09:51:19 +00:00
|
|
|
res = -SENSORS_ERR_PARSE;
|
2007-10-25 09:48:18 +00:00
|
|
|
if (input) {
|
|
|
|
if (sensors_scanner_init(input) ||
|
2008-04-08 14:50:13 +00:00
|
|
|
sensors_parse())
|
2007-10-25 09:51:19 +00:00
|
|
|
goto exit_cleanup;
|
2007-10-25 09:48:18 +00:00
|
|
|
} else {
|
|
|
|
/* No configuration provided, use default */
|
|
|
|
input = fopen(DEFAULT_CONFIG_FILE, "r");
|
2007-10-25 09:59:05 +00:00
|
|
|
if (!input && errno == ENOENT)
|
|
|
|
input = fopen(ALT_CONFIG_FILE, "r");
|
2007-10-25 09:52:46 +00:00
|
|
|
if (input) {
|
|
|
|
if (sensors_scanner_init(input) ||
|
2008-04-08 14:50:13 +00:00
|
|
|
sensors_parse()) {
|
2007-10-25 09:52:46 +00:00
|
|
|
fclose(input);
|
|
|
|
goto exit_cleanup;
|
|
|
|
}
|
2007-10-25 09:48:18 +00:00
|
|
|
fclose(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-31 14:42:20 +00:00
|
|
|
if ((res = sensors_substitute_busses()))
|
2007-10-25 09:51:19 +00:00
|
|
|
goto exit_cleanup;
|
2007-08-31 14:42:20 +00:00
|
|
|
return 0;
|
2007-10-25 09:51:19 +00:00
|
|
|
|
|
|
|
exit_cleanup:
|
|
|
|
sensors_cleanup();
|
|
|
|
return res;
|
1998-12-20 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
static void free_chip_name(sensors_chip_name *name)
|
1998-12-20 21:57:39 +00:00
|
|
|
{
|
2007-08-31 14:42:20 +00:00
|
|
|
free(name->prefix);
|
|
|
|
free(name->path);
|
1998-12-20 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-05 08:19:42 +00:00
|
|
|
static void free_chip_features(sensors_chip_features *features)
|
2007-06-28 14:39:34 +00:00
|
|
|
{
|
2007-08-31 14:42:20 +00:00
|
|
|
int i;
|
2007-06-28 14:39:34 +00:00
|
|
|
|
2007-09-23 12:02:22 +00:00
|
|
|
for (i = 0; i < features->subfeature_count; i++)
|
|
|
|
free(features->subfeature[i].name);
|
|
|
|
free(features->subfeature);
|
2007-09-23 12:05:16 +00:00
|
|
|
for (i = 0; i < features->feature_count; i++)
|
|
|
|
free(features->feature[i].name);
|
|
|
|
free(features->feature);
|
2007-06-28 14:39:34 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
static void free_bus(sensors_bus *bus)
|
1998-12-20 21:57:39 +00:00
|
|
|
{
|
2007-08-31 14:42:20 +00:00
|
|
|
free(bus->adapter);
|
1998-12-20 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
static void free_label(sensors_label *label)
|
|
|
|
{
|
|
|
|
free(label->name);
|
|
|
|
free(label->value);
|
|
|
|
}
|
|
|
|
|
2007-09-05 12:30:39 +00:00
|
|
|
void free_expr(sensors_expr *expr)
|
2007-08-31 15:09:26 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_set(sensors_set *set)
|
|
|
|
{
|
|
|
|
free(set->name);
|
|
|
|
free_expr(set->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_compute(sensors_compute *compute)
|
|
|
|
{
|
|
|
|
free(compute->name);
|
|
|
|
free_expr(compute->from_proc);
|
|
|
|
free_expr(compute->to_proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_ignore(sensors_ignore *ignore)
|
|
|
|
{
|
|
|
|
free(ignore->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_chip(sensors_chip *chip)
|
1998-12-20 21:57:39 +00:00
|
|
|
{
|
2007-08-31 14:42:20 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < chip->chips.fits_count; i++)
|
|
|
|
free_chip_name(&chip->chips.fits[i]);
|
|
|
|
free(chip->chips.fits);
|
|
|
|
chip->chips.fits_count = chip->chips.fits_max = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < chip->ignores_count; i++)
|
|
|
|
free_ignore(&chip->ignores[i]);
|
|
|
|
free(chip->ignores);
|
|
|
|
chip->ignores_count = chip->ignores_max = 0;
|
1998-12-20 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
void sensors_cleanup(void)
|
1998-12-20 21:57:39 +00:00
|
|
|
{
|
2007-08-31 15:09:26 +00:00
|
|
|
int i;
|
1998-12-20 21:57:39 +00:00
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
sensors_scanner_exit();
|
1998-12-20 21:57:39 +00:00
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
for (i = 0; i < sensors_proc_chips_count; i++) {
|
|
|
|
free_chip_name(&sensors_proc_chips[i].chip);
|
2007-09-05 08:19:42 +00:00
|
|
|
free_chip_features(&sensors_proc_chips[i]);
|
2007-08-31 15:09:26 +00:00
|
|
|
}
|
|
|
|
free(sensors_proc_chips);
|
|
|
|
sensors_proc_chips = NULL;
|
|
|
|
sensors_proc_chips_count = sensors_proc_chips_max = 0;
|
1998-12-20 21:57:39 +00:00
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
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;
|
1999-12-24 22:44:41 +00:00
|
|
|
|
2007-08-31 15:09:26 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < sensors_proc_bus_count; i++)
|
|
|
|
free_bus(&sensors_proc_bus[i]);
|
|
|
|
free(sensors_proc_bus);
|
|
|
|
sensors_proc_bus = NULL;
|
|
|
|
sensors_proc_bus_count = sensors_proc_bus_max = 0;
|
1998-12-20 21:57:39 +00:00
|
|
|
}
|