library: introduced 'Item_table' validation provisions

The recent work on updating the <meminfo> and <vmstat>
modules with some newly added linux fields reminded me
(again) of a need for some mechanism guaranteeing that
a header file agrees with the source file assumptions.

Sadly, in the past, if a table entry was omitted or if
the table and header are ordered differently, then the
library would silently return the wrong results values
or even potentially experience a SIGSEGV abnormal end.

This patch offers a much needed development assist for
ensuring that Item_table entries are synchronized with
header file enumerators in terms of number plus order.

It's intended solely for our use as libprocps evolves.

Now, by activating ITEMTABLE_DEBUG, either directly or
via ./configure CFLAGS='-DITEMTABLE_DEBUG', the number
and order will be verified. It is envisioned that this
feature will be used at least once prior to a release.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2020-08-10 00:00:00 -05:00
committed by Craig Small
parent c865b06c30
commit 92d0297e1e
6 changed files with 199 additions and 23 deletions

View File

@@ -50,6 +50,14 @@
#define STACKS_INCR 64 // amount reap stack allocations grow
#define STR_COMPARE strverscmp
/* ----------------------------------------------------------------------- +
this provision can help ensure that our Item_table remains synchronized |
with the enumerators found in the associated header file. It's intended |
to only be used locally (& temporarily) at some point before a release! | */
// #define ITEMTABLE_DEBUG //--------------------------------------------- |
// ----------------------------------------------------------------------- +
struct dev_data {
unsigned long reads;
unsigned long reads_merged;
@@ -200,7 +208,11 @@ srtDECL(noop) { \
// ___ Controlling Table ||||||||||||||||||||||||||||||||||||||||||||||||||||||
typedef void (*SET_t)(struct diskstats_result *, struct dev_node *);
#ifdef ITEMTABLE_DEBUG
#define RS(e) (SET_t)setNAME(e), DISKSTATS_ ## e, STRINGIFY(DISKSTATS_ ## e)
#else
#define RS(e) (SET_t)setNAME(e)
#endif
typedef int (*QSR_t)(const void *, const void *, void *);
#define QS(t) (QSR_t)srtNAME(t)
@@ -214,6 +226,10 @@ typedef int (*QSR_t)(const void *, const void *, void *);
* those *enum diskstats_item* guys ! */
static struct {
SET_t setsfunc; // the actual result setting routine
#ifdef ITEMTABLE_DEBUG
int enumnumb; // enumerator (must match position!)
char *enum2str; // enumerator name as a char* string
#endif
QSR_t sortfunc; // sort cmp func for a specific type
char *type2str; // the result type as a string value
} Item_table[] = {
@@ -250,9 +266,6 @@ static struct {
{ RS(DELTA_WRITE_TIME), QS(s_int), TS(s_int) },
{ RS(DELTA_IO_TIME), QS(s_int), TS(s_int) },
{ RS(DELTA_WEIGHTED_TIME), QS(s_int), TS(s_int) },
// dummy entry corresponding to DISKSTATS_logical_end ...
{ NULL, NULL, NULL }
};
/* please note,
@@ -703,6 +716,23 @@ PROCPS_EXPORT int procps_diskstats_new (
{
struct diskstats_info *p;
#ifdef ITEMTABLE_DEBUG
int i, failed = 0;
for (i = 0; i < MAXTABLE(Item_table); i++) {
if (i != Item_table[i].enumnumb) {
fprintf(stderr, "%s: enum/table error: Item_table[%d] was %s, but its value is %d\n"
, __FILE__, i, Item_table[i].enum2str, Item_table[i].enumnumb);
failed = 1;
}
}
if (i != DISKSTATS_logical_end) {
fprintf(stderr, "%s: DISKSTATS_logical_end is %d, expected %d\n"
, __FILE__, DISKSTATS_logical_end, i);
failed = 1;
}
if (failed) _Exit(EXIT_FAILURE);
#endif
if (info == NULL)
return -EINVAL;
if (!(p = calloc(1, sizeof(struct diskstats_info))))