summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2009-06-03 20:40:47 +0200
committerDavid Sommerseth <davids@redhat.com>2009-06-03 20:40:47 +0200
commita05daf8fde753404511e9f247ab4e9ac9a75bf1a (patch)
tree73b740c081a2c135f072860a47c26891330af322 /src
parentaea8d44d49a3b461194cc72b28cdbf2be51da8d8 (diff)
downloadpython-dmidecode-a05daf8fde753404511e9f247ab4e9ac9a75bf1a.tar.gz
python-dmidecode-a05daf8fde753404511e9f247ab4e9ac9a75bf1a.tar.xz
python-dmidecode-a05daf8fde753404511e9f247ab4e9ac9a75bf1a.zip
Rewrote bigger parts to avoid global variables as much as possible
Diffstat (limited to 'src')
-rw-r--r--src/dmidecode.c46
-rw-r--r--src/dmidecode.h9
-rw-r--r--src/dmidecodemodule.c205
-rw-r--r--src/dmidecodemodule.h10
-rw-r--r--src/dmihelper.h3
5 files changed, 152 insertions, 121 deletions
diff --git a/src/dmidecode.c b/src/dmidecode.c
index d2f340f..4a978a9 100644
--- a/src/dmidecode.c
+++ b/src/dmidecode.c
@@ -201,7 +201,7 @@ static int dmi_bcd_range(u8 value, u8 low, u8 high)
return 1;
}
-void dmi_dump(xmlNode *node, struct dmi_header * h)
+void dmi_dump(options *opt, xmlNode *node, struct dmi_header * h)
{
int row, i;
const char *s;
@@ -234,7 +234,7 @@ void dmi_dump(xmlNode *node, struct dmi_header * h)
while((s = dmi_string(h, i++)) != NULL) {
//. FIXME: DUMP
/*
- * if(opt.flags & FLAG_DUMP) {
+ * if(opt->flags & FLAG_DUMP) {
* int j, l = strlen(s)+1;
* for(row=0; row<((l-1)>>4)+1; row++) {
* for(j=0; j<16 && j<l-(row<<4); j++)
@@ -3684,7 +3684,7 @@ void dmi_additional_info(xmlNode *node, const struct dmi_header *h)
** Main
*/
-xmlNode *dmi_decode(xmlNode *prnt_n, struct dmi_header * h, u16 ver)
+xmlNode *dmi_decode(options *opt, xmlNode *prnt_n, struct dmi_header * h, u16 ver)
{
const u8 *data = h->data;
xmlNode *sect_n = NULL, *sub_n = NULL, *sub2_n = NULL;
@@ -4816,7 +4816,7 @@ xmlNode *dmi_decode(xmlNode *prnt_n, struct dmi_header * h, u16 ver)
dmixml_AddAttribute(sect_n, "Type", "%i", h->type);
dmixml_AddAttribute(sect_n, "InfoType", "%s", h->type >= 128 ? "OEM-specific" : "Unknown");
- dmi_dump(sect_n, h);
+ dmi_dump(opt, sect_n, h);
break;
}
return sect_n;
@@ -4830,10 +4830,10 @@ void to_dmi_header(struct dmi_header *h, u8 * data)
h->data = data;
}
-xmlNode *dmi_table_string(xmlNode *prnt_n, const struct dmi_header *h, const u8 *data, u16 ver)
+xmlNode *dmi_table_string(options *opt, xmlNode *prnt_n, const struct dmi_header *h, const u8 *data, u16 ver)
{
int key;
- u8 offset = opt.string->offset;
+ u8 offset = opt->string->offset;
xmlNode *handle_n = NULL, *dmi_n = NULL;
if(offset >= h->length)
@@ -4843,7 +4843,7 @@ xmlNode *dmi_table_string(xmlNode *prnt_n, const struct dmi_header *h, const u8
assert( handle_n != NULL );
//. TODO: These should have more meaningful dictionary names
- key = (opt.string->type << 8) | offset;
+ key = (opt->string->type << 8) | offset;
switch (key) {
case 0x108:
@@ -4877,7 +4877,7 @@ xmlNode *dmi_table_string(xmlNode *prnt_n, const struct dmi_header *h, const u8
}
/*
-static void dmi_table_dump(u32 base, u16 len, const char *devmem)
+static void dmi_table_dump(options *opt, u32 base, u16 len, const char *devmem)
{
u8 *buf;
@@ -4887,8 +4887,8 @@ static void dmi_table_dump(u32 base, u16 len, const char *devmem)
return;
}
- printf("# Writing %d bytes to %s.\n", len, PyString_AS_STRING(opt.dumpfile));
- write_dump(32, len, buf, PyString_AS_STRING(opt.dumpfile), 0);
+ printf("# Writing %d bytes to %s.\n", len, opt->dumpfile);
+ write_dump(32, len, buf, opt->dumpfile, 0);
free(buf);
}
*/
@@ -5011,13 +5011,13 @@ int dump(const char *dumpfile)
return ret == 0 ? found : ret;
}
-static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem, xmlNode *xmlnode)
+static void dmi_table(options *opt, u32 base, u16 len, u16 num, u16 ver, const char *devmem, xmlNode *xmlnode)
{
u8 *buf;
u8 *data;
int i = 0;
- if(opt.type == NULL) {
+ if(opt->type == NULL) {
xmlNode *info_n = NULL;
info_n = dmixml_AddTextChild(xmlnode, "DMIinfo", "%i structures occupying %i bytes", num, len);
@@ -5025,7 +5025,7 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem, x
dmixml_AddAttribute(info_n, "dmi_size", "%i", len);
/* TODO DUMP
- * if (!(opt.flags & FLAG_FROM_DUMP))
+ * if (!(opt->flags & FLAG_FROM_DUMP))
* dmixml_AddAttribute(info_n, "dmi_table_base", "0x%08x", base);
*/
@@ -5050,9 +5050,9 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem, x
int display;
to_dmi_header(&h, data);
- display = ((opt.type == NULL || opt.type[h.type])
+ display = ((opt->type == NULL || opt->type[h.type])
// && !(h.type>39 && h.type<=127)
- && !opt.string);
+ && !opt->string);
/*
** If a short entry is found (less than 4 bytes), not only it
@@ -5086,7 +5086,7 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem, x
if(display) {
if(next - buf <= len) {
/* TODO: ...
- * if(opt.flags & FLAG_DUMP) {
+ * if(opt->flags & FLAG_DUMP) {
* PyDict_SetItem(hDict, PyString_FromString("lookup"), dmi_dump(&h));
* } else {
* //. TODO: //. Is the value of `i' important?...
@@ -5095,11 +5095,11 @@ static void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem, x
* PyDict_SetItem(hDict, PyString_FromString("data"), dmi_decode(&h, ver));
* PyDict_SetItem(pydata, PyString_FromString(hid), hDict);
* } */
- handle_n = dmi_decode(xmlnode, &h, ver);
+ handle_n = dmi_decode(opt, xmlnode, &h, ver);
} else
fprintf(stderr, "<TRUNCATED>");
- } else if(opt.string != NULL && opt.string->type == h.type) {
- handle_n = dmi_table_string(xmlnode, &h, data, ver);
+ } else if(opt->string != NULL && opt->string->type == h.type) {
+ handle_n = dmi_table_string(opt, xmlnode, &h, data, ver);
}
if( handle_n != NULL ) {
dmixml_AddAttribute(handle_n, "handle", "0x%04x", h.handle);
@@ -5173,7 +5173,7 @@ xmlNode *smbios_decode_get_version(u8 * buf, const char *devmem)
return data_n;
}
-int smbios_decode(u8 * buf, const char *devmem, xmlNode *xmlnode)
+int smbios_decode(options *opt, u8 * buf, const char *devmem, xmlNode *xmlnode)
{
int check = _smbios_decode_check(buf);
@@ -5189,7 +5189,7 @@ int smbios_decode(u8 * buf, const char *devmem, xmlNode *xmlnode)
break;
}
//printf(">>%d @ %d, %d<<\n", DWORD(buf+0x18), WORD(buf+0x16), WORD(buf+0x1C));
- dmi_table(DWORD(buf + 0x18), WORD(buf + 0x16), WORD(buf + 0x1C), ver, devmem,
+ dmi_table(opt, DWORD(buf + 0x18), WORD(buf + 0x16), WORD(buf + 0x1C), ver, devmem,
xmlnode);
}
return check;
@@ -5228,12 +5228,12 @@ xmlNode *legacy_decode_get_version(u8 * buf, const char *devmem)
return data_n;
}
-int legacy_decode(u8 * buf, const char *devmem, xmlNode *xmlnode)
+int legacy_decode(options *opt, u8 * buf, const char *devmem, xmlNode *xmlnode)
{
int check = _legacy_decode_check(buf);
if(check == 1)
- dmi_table(DWORD(buf + 0x08), WORD(buf + 0x06), WORD(buf + 0x0C),
+ dmi_table(opt, DWORD(buf + 0x08), WORD(buf + 0x06), WORD(buf + 0x0C),
((buf[0x0E] & 0xF0) << 4) + (buf[0x0E] & 0x0F), devmem, xmlnode);
return check;
}
diff --git a/src/dmidecode.h b/src/dmidecode.h
index 135c264..c199b7a 100644
--- a/src/dmidecode.h
+++ b/src/dmidecode.h
@@ -20,6 +20,7 @@
*/
#include <libxml/tree.h>
+#include "dmihelper.h"
struct dmi_header {
u8 type;
@@ -28,14 +29,14 @@ struct dmi_header {
u8 *data;
};
-void dmi_dump(xmlNode *node, struct dmi_header * h);
-xmlNode *dmi_decode(xmlNode *parent_n, struct dmi_header * h, u16 ver);
+void dmi_dump(options *, xmlNode *node, struct dmi_header * h);
+xmlNode *dmi_decode(options *, xmlNode *parent_n, struct dmi_header * h, u16 ver);
int address_from_efi(size_t * address);
void to_dmi_header(struct dmi_header *h, u8 * data);
xmlNode *smbios_decode_get_version(u8 * buf, const char *devmem);
-int smbios_decode(u8 * buf, const char *devmem, xmlNode *xmlnode);
+int smbios_decode(options *, u8 * buf, const char *devmem, xmlNode *xmlnode);
xmlNode *legacy_decode_get_version(u8 * buf, const char *devmem);
-int legacy_decode(u8 * buf, const char *devmem, xmlNode *xmlnode);
+int legacy_decode(options *, u8 * buf, const char *devmem, xmlNode *xmlnode);
const char *dmi_string(const struct dmi_header *dm, u8 s);
void dmi_system_uuid(xmlNode *node, const u8 * p, u16 ver);
diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c
index 2564236..e77384a 100644
--- a/src/dmidecodemodule.c
+++ b/src/dmidecodemodule.c
@@ -48,21 +48,21 @@
#include "dmixml.h"
#include <mcheck.h>
-options opt;
+options *global_options = NULL;
-static void init(void)
+static void init(options *opt)
{
/* sanity check */
if(sizeof(u8) != 1 || sizeof(u16) != 2 || sizeof(u32) != 4 || '\0' != 0)
fprintf(stderr, "%s: compiler incompatibility\n", "dmidecodemodule");
- opt.devmem = DEFAULT_MEM_DEV;
- opt.dumpfile = NULL;
- opt.flags = 0;
- opt.type = NULL;
- opt.dmiversion_n = NULL;
- opt.mappingxml = NULL;
- opt.python_xml_map = strdup(PYTHON_XML_MAP);
+ opt->devmem = DEFAULT_MEM_DEV;
+ opt->dumpfile = NULL;
+ opt->flags = 0;
+ opt->type = NULL;
+ opt->dmiversion_n = NULL;
+ opt->mappingxml = NULL;
+ opt->python_xml_map = strdup(PYTHON_XML_MAP);
}
u8 *parse_opt_type(u8 * p, const char *arg)
@@ -115,7 +115,7 @@ u8 *parse_opt_type(u8 * p, const char *arg)
}
-xmlNode *dmidecode_get_version()
+xmlNode *dmidecode_get_version(options *opt)
{
int found = 0;
size_t fp;
@@ -124,21 +124,21 @@ xmlNode *dmidecode_get_version()
xmlNode *ver_n = NULL;
/* Set default option values */
- opt.devmem = DEFAULT_MEM_DEV;
+ if( opt->devmem == NULL ) {
+ opt->devmem = DEFAULT_MEM_DEV;
+ }
/* Read from dump if so instructed */
- if(opt.dumpfile != NULL) {
- const char *dumpfile = PyString_AS_STRING(opt.dumpfile);
-
+ if(opt->dumpfile != NULL) {
//. printf("Reading SMBIOS/DMI data from file %s.\n", dumpfile);
- if((buf = mem_chunk(0, 0x20, dumpfile)) != NULL) {
+ if((buf = mem_chunk(0, 0x20, opt->dumpfile)) != NULL) {
if(memcmp(buf, "_SM_", 4) == 0) {
- ver_n = smbios_decode_get_version(buf, dumpfile);
+ ver_n = smbios_decode_get_version(buf, opt->dumpfile);
if( dmixml_GetAttrValue(ver_n, "unknown") == NULL ) {
found++;
}
} else if(memcmp(buf, "_DMI_", 5) == 0) {
- ver_n = legacy_decode_get_version(buf, dumpfile);
+ ver_n = legacy_decode_get_version(buf, opt->dumpfile);
if( dmixml_GetAttrValue(ver_n, "unknown") == NULL ) {
found++;
}
@@ -149,16 +149,16 @@ xmlNode *dmidecode_get_version()
efi = address_from_efi(&fp);
if(efi == EFI_NOT_FOUND) {
/* Fallback to memory scan (x86, x86_64) */
- if((buf = mem_chunk(0xF0000, 0x10000, opt.devmem)) != NULL) {
+ if((buf = mem_chunk(0xF0000, 0x10000, opt->devmem)) != NULL) {
for(fp = 0; fp <= 0xFFF0; fp += 16) {
if(memcmp(buf + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
- ver_n = smbios_decode_get_version(buf + fp, opt.devmem);
+ ver_n = smbios_decode_get_version(buf + fp, opt->devmem);
if( dmixml_GetAttrValue(ver_n, "unknown") == NULL ) {
found++;
}
fp += 16;
} else if(memcmp(buf + fp, "_DMI_", 5) == 0) {
- ver_n = legacy_decode_get_version (buf + fp, opt.devmem);
+ ver_n = legacy_decode_get_version (buf + fp, opt->devmem);
if( dmixml_GetAttrValue(ver_n, "unknown") == NULL ) {
found++;
}
@@ -169,8 +169,8 @@ xmlNode *dmidecode_get_version()
ver_n = NULL;
} else {
// Process as EFI
- if((buf = mem_chunk(fp, 0x20, opt.devmem)) != NULL) {
- ver_n = smbios_decode_get_version(buf, opt.devmem);
+ if((buf = mem_chunk(fp, 0x20, opt->devmem)) != NULL) {
+ ver_n = smbios_decode_get_version(buf, opt->devmem);
if( dmixml_GetAttrValue(ver_n, "unknown") == NULL ) {
found++;
}
@@ -181,15 +181,16 @@ xmlNode *dmidecode_get_version()
if( buf != NULL ) {
free(buf);
}
-
if( !found ) {
fprintf(stderr, "No SMBIOS nor DMI entry point found, sorry.");
}
- free(opt.type);
+ if( opt->type != NULL ) {
+ free(opt->type);
+ }
return ver_n;
}
-int dmidecode_get_xml(xmlNode* dmixml_n)
+int dmidecode_get_xml(options *opt, xmlNode* dmixml_n)
{
assert(dmixml_n != NULL);
if(dmixml_n == NULL) {
@@ -201,23 +202,21 @@ int dmidecode_get_xml(xmlNode* dmixml_n)
int found = 0;
size_t fp;
int efi;
- u8 *buf;
+ u8 *buf = NULL;
- const char *f = opt.dumpfile ? PyString_AsString(opt.dumpfile) : opt.devmem;
+ const char *f = opt->dumpfile ? opt->dumpfile : opt->devmem;
if(access(f, R_OK) < 0)
PyErr_SetString(PyExc_IOError, "Permission denied to memory file/device");
/* Read from dump if so instructed */
- if(opt.dumpfile != NULL) {
- const char *dumpfile = PyString_AS_STRING(opt.dumpfile);
-
+ if(opt->dumpfile != NULL) {
// printf("Reading SMBIOS/DMI data from file %s.\n", dumpfile);
- if((buf = mem_chunk(0, 0x20, dumpfile)) != NULL) {
+ if((buf = mem_chunk(0, 0x20, opt->dumpfile)) != NULL) {
if(memcmp(buf, "_SM_", 4) == 0) {
- if(smbios_decode(buf, dumpfile, dmixml_n))
+ if(smbios_decode(opt, buf, opt->dumpfile, dmixml_n))
found++;
} else if(memcmp(buf, "_DMI_", 5) == 0) {
- if(legacy_decode(buf, dumpfile, dmixml_n))
+ if(legacy_decode(opt, buf, opt->dumpfile, dmixml_n))
found++;
}
} else {
@@ -228,15 +227,15 @@ int dmidecode_get_xml(xmlNode* dmixml_n)
efi = address_from_efi(&fp);
if(efi == EFI_NOT_FOUND) {
/* Fallback to memory scan (x86, x86_64) */
- if((buf = mem_chunk(0xF0000, 0x10000, opt.devmem)) != NULL) {
+ if((buf = mem_chunk(0xF0000, 0x10000, opt->devmem)) != NULL) {
for(fp = 0; fp <= 0xFFF0; fp += 16) {
if(memcmp(buf + fp, "_SM_", 4) == 0 && fp <= 0xFFE0) {
- if(smbios_decode(buf + fp, opt.devmem, dmixml_n)) {
+ if(smbios_decode(opt, buf + fp, opt->devmem, dmixml_n)) {
found++;
fp += 16;
}
} else if(memcmp(buf + fp, "_DMI_", 5) == 0) {
- if(legacy_decode(buf + fp, opt.devmem, dmixml_n))
+ if(legacy_decode(opt, buf + fp, opt->devmem, dmixml_n))
found++;
}
}
@@ -245,15 +244,16 @@ int dmidecode_get_xml(xmlNode* dmixml_n)
} else if(efi == EFI_NO_SMBIOS) {
ret = 1;
} else {
- if((buf = mem_chunk(fp, 0x20, opt.devmem)) == NULL)
+ if((buf = mem_chunk(fp, 0x20, opt->devmem)) == NULL)
ret = 1;
- else if(smbios_decode(buf, opt.devmem, dmixml_n))
+ else if(smbios_decode(opt, buf, opt->devmem, dmixml_n))
found++;
// TODO: dmixml_AddAttribute(dmixml_n, "efi_address", "0x%08x", efiAddress);
}
}
-
- free(opt.type);
+ if( opt->type != NULL ) {
+ free(opt->type);
+ }
if(ret == 0) {
free(buf);
}
@@ -261,39 +261,41 @@ int dmidecode_get_xml(xmlNode* dmixml_n)
return ret;
}
-static PyObject *dmidecode_get(const char *section)
+static PyObject *dmidecode_get(options *opt, const char *section)
{
PyObject *pydata = NULL;
xmlNode *dmixml_n = NULL;
/* Set default option values */
- opt.devmem = DEFAULT_MEM_DEV;
- opt.flags = 0;
- opt.type = NULL;
- opt.type = parse_opt_type(opt.type, section);
+ if( opt->devmem == NULL ) {
+ opt->devmem = DEFAULT_MEM_DEV;
+ }
+ opt->flags = 0;
+ opt->type = NULL;
+ opt->type = parse_opt_type(opt->type, section);
- if(opt.type == NULL) {
+ if(opt->type == NULL) {
return NULL;
}
dmixml_n = xmlNewNode(NULL, (xmlChar *) "dmidecode");
assert( dmixml_n != NULL );
// Append DMI version info
- if( opt.dmiversion_n != NULL ) {
- xmlAddChild(dmixml_n, xmlCopyNode(opt.dmiversion_n, 1));
+ if( opt->dmiversion_n != NULL ) {
+ xmlAddChild(dmixml_n, xmlCopyNode(opt->dmiversion_n, 1));
}
- if(dmidecode_get_xml(dmixml_n) == 0) {
+ if(dmidecode_get_xml(opt, dmixml_n) == 0) {
ptzMAP *mapping = NULL;
// Convert the retrieved XML nodes to Python dicts
- if( opt.mappingxml == NULL ) {
+ if( opt->mappingxml == NULL ) {
// Load mapping into memory
- opt.mappingxml = xmlReadFile(opt.python_xml_map, NULL, 0);
- assert( opt.mappingxml != NULL );
+ opt->mappingxml = xmlReadFile(opt->python_xml_map, NULL, 0);
+ assert( opt->mappingxml != NULL );
}
- mapping = dmiMAP_ParseMappingXML_GroupName(opt.mappingxml, section);
+ mapping = dmiMAP_ParseMappingXML_GroupName(opt->mappingxml, section);
if( mapping == NULL ) {
return NULL;
}
@@ -317,42 +319,41 @@ static PyObject *dmidecode_get(const char *section)
}
-
static PyObject *dmidecode_get_bios(PyObject * self, PyObject * args)
{
- return dmidecode_get("bios");
+ return dmidecode_get(global_options, "bios");
}
static PyObject *dmidecode_get_system(PyObject * self, PyObject * args)
{
- return dmidecode_get("system");
+ return dmidecode_get(global_options, "system");
}
static PyObject *dmidecode_get_baseboard(PyObject * self, PyObject * args)
{
- return dmidecode_get("baseboard");
+ return dmidecode_get(global_options, "baseboard");
}
static PyObject *dmidecode_get_chassis(PyObject * self, PyObject * args)
{
- return dmidecode_get("chassis");
+ return dmidecode_get(global_options, "chassis");
}
static PyObject *dmidecode_get_processor(PyObject * self, PyObject * args)
{
- return dmidecode_get("processor");
+ return dmidecode_get(global_options, "processor");
}
static PyObject *dmidecode_get_memory(PyObject * self, PyObject * args)
{
- return dmidecode_get("memory");
+ return dmidecode_get(global_options, "memory");
}
static PyObject *dmidecode_get_cache(PyObject * self, PyObject * args)
{
- return dmidecode_get("cache");
+ return dmidecode_get(global_options, "cache");
}
static PyObject *dmidecode_get_connector(PyObject * self, PyObject * args)
{
- return dmidecode_get("connector");
+ return dmidecode_get(global_options, "connector");
}
static PyObject *dmidecode_get_slot(PyObject * self, PyObject * args)
{
- return dmidecode_get("slot");
+ return dmidecode_get(global_options, "slot");
}
static PyObject *dmidecode_get_type(PyObject * self, PyObject * args)
{
@@ -364,7 +365,7 @@ static PyObject *dmidecode_get_type(PyObject * self, PyObject * args)
if(lu < 256) {
char s[8];
sprintf(s, "%lu", lu);
- return dmidecode_get(s);
+ return dmidecode_get(global_options, s);
}
e = 1;
//return Py_False;
@@ -382,26 +383,22 @@ static PyObject *dmidecode_get_type(PyObject * self, PyObject * args)
static PyObject *dmidecode_dump(PyObject * self, PyObject * null)
{
const char *f;
-
- f = opt.dumpfile ? PyString_AsString(opt.dumpfile) : opt.devmem;
struct stat _buf;
+ f = (global_options->dumpfile ? global_options->dumpfile : global_options->devmem);
stat(f, &_buf);
if((access(f, F_OK) != 0) || ((access(f, W_OK) == 0) && S_ISREG(_buf.st_mode)))
- if(dump(PyString_AS_STRING(opt.dumpfile)))
+ if(dump(PyString_AS_STRING(global_options->dumpfile)))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
static PyObject *dmidecode_get_dev(PyObject * self, PyObject * null)
{
- PyObject *dev;
-
- if(opt.dumpfile != NULL)
- dev = opt.dumpfile;
- else
- dev = PyString_FromString(opt.devmem);
+ PyObject *dev = NULL;
+ dev = PyString_FromString((global_options->dumpfile != NULL
+ ? global_options->dumpfile : global_options->devmem));
Py_INCREF(dev);
return dev;
}
@@ -409,27 +406,26 @@ static PyObject *dmidecode_get_dev(PyObject * self, PyObject * null)
static PyObject *dmidecode_set_dev(PyObject * self, PyObject * arg)
{
if(PyString_Check(arg)) {
- if(opt.dumpfile == arg)
- Py_RETURN_TRUE;
-
struct stat buf;
char *f = PyString_AsString(arg);
- stat(f, &buf);
- if(opt.dumpfile) {
- Py_DECREF(opt.dumpfile);
+ if( (f != NULL) && (strcmp(global_options->dumpfile, f) == 0) ) {
+ Py_RETURN_TRUE;
}
+ stat(f, &buf);
if(S_ISCHR(buf.st_mode)) {
if(memcmp(PyString_AsString(arg), "/dev/mem", 8) == 0) {
- opt.dumpfile = NULL;
+ if( global_options->dumpfile != NULL ) {
+ free(global_options->dumpfile);
+ global_options->dumpfile = NULL;
+ }
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
} else if(!S_ISDIR(buf.st_mode)) {
- opt.dumpfile = arg;
- Py_INCREF(opt.dumpfile);
+ global_options->dumpfile = strdup(f);
Py_RETURN_TRUE;
}
}
@@ -450,8 +446,8 @@ static PyObject *dmidecode_set_pythonxmlmap(PyObject * self, PyObject * arg)
return NULL;
}
- free(opt.python_xml_map);
- opt.python_xml_map = strdup(fname);
+ free(global_options->python_xml_map);
+ global_options->python_xml_map = strdup(fname);
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
@@ -484,13 +480,43 @@ static PyMethodDef DMIDataMethods[] = {
{NULL, NULL, 0, NULL}
};
+void destruct_options(void *ptr) {
+ options *opt = (options *) ptr;
+
+ if( opt->mappingxml != NULL ) {
+ xmlFreeDoc(opt->mappingxml);
+ opt->mappingxml = NULL;
+ }
+
+ if( opt->python_xml_map != NULL ) {
+ free(opt->python_xml_map);
+ opt->python_xml_map = NULL;
+ }
+
+ if( opt->dmiversion_n != NULL ) {
+ xmlFreeNode(opt->dmiversion_n);
+ opt->dmiversion_n = NULL;
+ }
+
+ if( opt->dumpfile != NULL ) {
+ free(opt->dumpfile);
+ opt->dumpfile = NULL;
+ }
+
+ free(ptr);
+}
+
+
PyMODINIT_FUNC initdmidecode(void)
{
char *dmiver = NULL;
PyObject *module = NULL;
PyObject *version = NULL;
+ options *opt;
- init();
+ opt = (options *) malloc(sizeof(options)+2);
+ memset(opt, 0, sizeof(options)+2);
+ init(opt);
module = Py_InitModule3((char *)"dmidecode", DMIDataMethods,
"Python extension module for dmidecode");
@@ -498,7 +524,12 @@ PyMODINIT_FUNC initdmidecode(void)
Py_INCREF(version);
PyModule_AddObject(module, "version", version);
- opt.dmiversion_n = dmidecode_get_version();
- dmiver = dmixml_GetContent(opt.dmiversion_n);
+ opt->dmiversion_n = dmidecode_get_version(opt);
+ dmiver = dmixml_GetContent(opt->dmiversion_n);
PyModule_AddObject(module, "dmi", dmiver ? PyString_FromString(dmiver) : Py_None);
+
+ // Assign this options struct to the module as well with a destructor, that way it will
+ // clean up the memory for us.
+ PyModule_AddObject(module, "options", PyCObject_FromVoidPtr(opt, destruct_options));
+ global_options = opt;
}
diff --git a/src/dmidecodemodule.h b/src/dmidecodemodule.h
index 0f5c75d..04d1203 100644
--- a/src/dmidecodemodule.h
+++ b/src/dmidecodemodule.h
@@ -21,16 +21,16 @@
#include "dmihelper.h"
-xmlNode *dmidecode_get_version(void);
+xmlNode *dmidecode_get_version(options *);
//extern void dmi_decode(struct dmi_header *h, u16 ver, PyObject* pydata);
-extern void dmi_dump(xmlNode *node, struct dmi_header *h);
-extern xmlNode *dmi_decode(xmlNode *parent_n, struct dmi_header * h, u16 ver);
+extern void dmi_dump(options *, xmlNode *node, struct dmi_header *h);
+extern xmlNode *dmi_decode(options *, xmlNode *parent_n, struct dmi_header * h, u16 ver);
extern int address_from_efi(size_t * address);
extern void to_dmi_header(struct dmi_header *h, u8 * data);
extern void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem);
-extern int smbios_decode(u8 * buf, const char *devmem, xmlNode *node);
-extern int legacy_decode(u8 * buf, const char *devmem, xmlNode *node);
+extern int smbios_decode(options *, u8 * buf, const char *devmem, xmlNode *node);
+extern int legacy_decode(options *, u8 * buf, const char *devmem, xmlNode *node);
extern xmlNode *smbios_decode_get_version(u8 * buf, const char *devmem);
extern xmlNode *legacy_decode_get_version(u8 * buf, const char *devmem);
extern void *mem_chunk(size_t base, size_t len, const char *devmem);
diff --git a/src/dmihelper.h b/src/dmihelper.h
index 6dccd94..bd2b7c9 100644
--- a/src/dmihelper.h
+++ b/src/dmihelper.h
@@ -111,8 +111,7 @@ typedef struct _options {
xmlDoc *mappingxml;
char *python_xml_map;
xmlNode *dmiversion_n;
- PyObject *dumpfile;
+ char *dumpfile;
} options;
-extern options opt;
#endif