diff --git a/prog/sensord/chips.c b/prog/sensord/chips.c index c7dcb0b0..e7d5c60d 100644 --- a/prog/sensord/chips.c +++ b/prog/sensord/chips.c @@ -332,12 +332,11 @@ static void fillChipBeepEnable (FeatureDescriptor *beepen, beepen->dataNumbers[1] = -1; } -/* Note that alarms and beeps are no longer (or not yet) supported */ -ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip) +static +FeatureDescriptor * generateChipFeatures (const sensors_chip_name *chip) { int nr, count = 1; const sensors_feature_data *sensor; - ChipDescriptor *descriptor; FeatureDescriptor *features; /* How many main features do we have? */ @@ -348,14 +347,9 @@ ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip) } /* Allocate the memory we need */ - descriptor = calloc(1, sizeof(ChipDescriptor)); features = calloc(count, sizeof(FeatureDescriptor)); - if (!descriptor || !features) { - free(descriptor); - free(features); + if (!features) return NULL; - } - descriptor->features = features; /* Fill in the data structures */ count = 0; @@ -387,5 +381,43 @@ ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip) count++; } - return descriptor; + return features; +} + +ChipDescriptor * knownChips; + +int initKnownChips (void) +{ + int nr, count = 1; + const sensors_chip_name *name; + + /* How many chips do we have? */ + nr = 0; + while ((name = sensors_get_detected_chips(NULL, &nr))) + count++; + + /* Allocate the memory we need */ + knownChips = calloc(count, sizeof(ChipDescriptor)); + if (!knownChips) + return 1; + + /* Fill in the data structures */ + count = 0; + nr = 0; + while ((name = sensors_get_detected_chips(NULL, &nr))) { + knownChips[count].name = name; + if ((knownChips[count].features = generateChipFeatures(name))) + count++; + } + + return 0; +} + +void freeKnownChips (void) +{ + int index0; + + for (index0 = 0; knownChips[index0].features; index0++) + free (knownChips[index0].features); + free (knownChips); } diff --git a/prog/sensord/lib.c b/prog/sensord/lib.c index a3b2b0ac..d5459d63 100644 --- a/prog/sensord/lib.c +++ b/prog/sensord/lib.c @@ -116,18 +116,28 @@ loadConfig int loadLib (void) { - return loadConfig (0); + int ret; + ret = loadConfig (0); + if (!ret) + ret = initKnownChips (); + return ret; } int reloadLib (void) { - return loadConfig (1); + int ret; + freeKnownChips (); + ret = loadConfig (1); + if (!ret) + ret = initKnownChips (); + return ret; } int unloadLib (void) { + freeKnownChips (); sensors_cleanup (); return 0; } diff --git a/prog/sensord/rrd.c b/prog/sensord/rrd.c index ce0259cd..81885fa5 100644 --- a/prog/sensord/rrd.c +++ b/prog/sensord/rrd.c @@ -141,11 +141,18 @@ applyToFeatures for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { - ChipDescriptor *descriptor; - descriptor = generateChipDescriptor (chip); - if (descriptor) { + int index0, chipindex = -1; + for (index0 = 0; knownChips[index0].features; ++ index0) + /* Trick: we compare addresses here. We know it works because both + pointers were returned by sensors_get_detected_chips(), so they + refer to libsensors internal structures, which do not move. */ + if (knownChips[index0].name == chip) { + chipindex = index0; + break; + } + if (chipindex >= 0) { + const ChipDescriptor *descriptor = &knownChips[chipindex]; const FeatureDescriptor *features = descriptor->features; - int index0; for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) { const FeatureDescriptor *feature = features + index0; @@ -167,8 +174,6 @@ applyToFeatures if (label) free (label); } - free (descriptor->features); - free (descriptor); } } } diff --git a/prog/sensord/sense.c b/prog/sensord/sense.c index 14a816d6..ea6ef8cb 100644 --- a/prog/sensord/sense.c +++ b/prog/sensord/sense.c @@ -168,13 +168,17 @@ doChip if (action == DO_SET) { ret = setChip (chip); } else { - ChipDescriptor *descriptor; - descriptor = generateChipDescriptor (chip); - if (descriptor) { - ret = doKnownChip (chip, descriptor, action); - free (descriptor->features); - free (descriptor); - } + int index0, chipindex = -1; + for (index0 = 0; knownChips[index0].features; ++ index0) + /* Trick: we compare addresses here. We know it works because both + pointers were returned by sensors_get_detected_chips(), so they + refer to libsensors internal structures, which do not move. */ + if (knownChips[index0].name == chip) { + chipindex = index0; + break; + } + if (chipindex >= 0) + ret = doKnownChip (chip, &knownChips[chipindex], action); } return ret; } diff --git a/prog/sensord/sensord.h b/prog/sensord/sensord.h index 93bbbbe5..eee402a0 100644 --- a/prog/sensord/sensord.h +++ b/prog/sensord/sensord.h @@ -95,7 +95,10 @@ typedef struct { } FeatureDescriptor; typedef struct { + const sensors_chip_name *name; FeatureDescriptor *features; } ChipDescriptor; -extern ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip); +extern ChipDescriptor * knownChips; +extern int initKnownChips (void); +extern void freeKnownChips (void);