diff options
author | nima <nima@abc39116-655e-4be6-ad55-d661dc543056> | 2008-07-01 23:14:17 +0000 |
---|---|---|
committer | nima <nima@abc39116-655e-4be6-ad55-d661dc543056> | 2008-07-01 23:14:17 +0000 |
commit | c10adcd728be238d8579bf98075a2468f0143cc2 (patch) | |
tree | 4fa1bce13ff08de36ff95023af9356fa29c4c5bd | |
parent | a7a5cb531b86a75265f106b91d7033e45d754612 (diff) | |
download | python-dmidecode-c10adcd728be238d8579bf98075a2468f0143cc2.tar.gz python-dmidecode-c10adcd728be238d8579bf98075a2468f0143cc2.tar.xz python-dmidecode-c10adcd728be238d8579bf98075a2468f0143cc2.zip |
Brought main() back into the python module and fixed malloc/free problems.
git-svn-id: svn://svn.autonomy.net.au/python-dmidecode@7 abc39116-655e-4be6-ad55-d661dc543056
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | dmidecode.c | 8 | ||||
-rw-r--r-- | dmidecode.h | 2 | ||||
-rw-r--r-- | dmidecodemodule.c | 75 | ||||
-rw-r--r-- | dmidecodemodule.h | 2 | ||||
-rw-r--r-- | dmiopt.c | 9 | ||||
-rwxr-xr-x | example.py | 14 |
7 files changed, 101 insertions, 16 deletions
@@ -46,12 +46,9 @@ PROGRAMS != echo dmidecode ; test `uname -m 2>/dev/null` != ia64 && echo biosdec all : $(PROGRAMS) python setup.py clean python setup.py build - #sudo python setup.py install - #python -c 'import dmidecode' - #ldd /usr/lib/python2.4/site-packages/dmidecode.so - gcc -pthread -shared -fPIC build/temp.linux-i686-2.4/dmidecodemodule.o -L. -I/usr/include/python2.4 catsprintf.o dmidecode.o dmiopt.o dmioem.o util.o -o build/lib.linux-i686-2.4/dmidecode.so + #. FIXME: setup.py should be configured such that the following workaround is no longer required... + $(CC) $(LDFLAGS) -pthread -shared -fPIC build/temp.linux-i686-2.4/dmidecodemodule.o -L. -I/usr/include/python2.4 catsprintf.o dmidecode.o dmiopt.o dmioem.o util.o -o build/lib.linux-i686-2.4/dmidecode.so sudo python setup.py install - ldd /usr/lib/python2.4/site-packages/dmidecode.so python -c 'import dmidecode' # diff --git a/dmidecode.c b/dmidecode.c index 974f3f0..a00de7b 100644 --- a/dmidecode.c +++ b/dmidecode.c @@ -4049,7 +4049,7 @@ int submain(int argc, char * const argv[]) size_t fp; int efi; u8 *buf; - + if(sizeof(u8)!=1 || sizeof(u16)!=2 || sizeof(u32)!=4 || '\0'!=0) { fprintf(stderr, "%s: compiler incompatibility\n", argv[0]); @@ -4057,8 +4057,8 @@ int submain(int argc, char * const argv[]) } /* Set default option values */ - opt.devmem=DEFAULT_MEM_DEV; - opt.flags=0; + //. opt.devmem=DEFAULT_MEM_DEV; + //. opt.flags=0; if(parse_command_line(argc, argv)<0) { @@ -4132,7 +4132,7 @@ done: catsprintf(buffer, "# No SMBIOS nor DMI entry point found, sorry.\n"); exit_free: - free(opt.type); + //. free(opt.type); return ret; } diff --git a/dmidecode.h b/dmidecode.h index 62779ca..ddf9897 100644 --- a/dmidecode.h +++ b/dmidecode.h @@ -38,3 +38,5 @@ void dmi_system_uuid(u8 *p); const char *dmi_chassis_type(u8 code); const char *dmi_processor_family(u8 code); void dmi_processor_frequency(u8 *p); + +int submain(int argc, char * const argv[]); diff --git a/dmidecodemodule.c b/dmidecodemodule.c index 278a13c..4748e40 100644 --- a/dmidecodemodule.c +++ b/dmidecodemodule.c @@ -3,17 +3,84 @@ static PyObject* dmidecode_get(PyObject *self, char* section) { bzero(buffer, 50000); - PyObject *list = PyList_New(0); + //Py_Initialize(); + //if(!Py_IsInitialized()) + // return NULL; + int argc = 3; char *argv[4]; argv[0] = "dmidecode"; argv[1] = "--type"; argv[2] = section; argv[3] = NULL; - submain(3, argv); - PyList_Append(list, PyUnicode_Splitlines(Py_BuildValue("s", buffer), 1)); - return list; + int ret=0; /* Returned value */ + int found=0; + size_t fp; + int efi; + u8 *buf; + + if(sizeof(u8)!=1 || sizeof(u16)!=2 || sizeof(u32)!=4 || '\0'!=0) { + fprintf(stderr, "%s: compiler incompatibility\n", argv[0]); + exit(255); + } + + /* 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.type==NULL) return -1; + + /* First try EFI (ia64, Intel-based Mac) */ + efi = address_from_efi(&fp); + switch(efi) { + case EFI_NOT_FOUND: + //. XXX + goto memory_scan; + case EFI_NO_SMBIOS: + ret = 1; + goto exit_free; + } + + if((buf=mem_chunk(fp, 0x20, opt.devmem))==NULL) { + ret = 1; + goto exit_free; + } + + if(smbios_decode(buf, opt.devmem)) found++; + + goto done; + +memory_scan: + /* Fallback to memory scan (x86, x86_64) */ + if((buf=mem_chunk(0xF0000, 0x10000, opt.devmem))==NULL) { + ret = 1; + goto exit_free; + } + + for(fp=0; fp<=0xFFF0; fp+=16) { + if(memcmp(buf+fp, "_SM_", 4)==0 && fp<=0xFFE0) { + if(smbios_decode(buf+fp, opt.devmem)) found++; + fp+=16; + } else if(memcmp(buf+fp, "_DMI_", 5)==0) { + if(legacy_decode(buf+fp, opt.devmem)) found++; + } + } + +done: + free(buf); + + if(!found && !(opt.flags & FLAG_QUIET)) + catsprintf(buffer, "# No SMBIOS nor DMI entry point found, sorry.\n"); + +exit_free: + //Py_Finalize(); + + //. FIXME: Why does this cause crash? + free(opt.type); + + return PyUnicode_Splitlines(Py_BuildValue("s", buffer), 1); } static PyObject* dmidecode_get_bios(PyObject *self, PyObject *args) { return dmidecode_get(self, "bios"); } diff --git a/dmidecodemodule.h b/dmidecodemodule.h index a1acf78..7ae0b4f 100644 --- a/dmidecodemodule.h +++ b/dmidecodemodule.h @@ -27,4 +27,4 @@ extern void dmi_table(u32 base, u16 len, u16 num, u16 ver, const char *devmem); extern int smbios_decode(u8 *buf, const char *devmem); extern int legacy_decode(u8 *buf, const char *devmem); extern void *mem_chunk(size_t base, size_t len, const char *devmem); -extern int main(int argc, char* const argv[]); +extern u8 *parse_opt_type(u8 *p, const char *arg); @@ -78,7 +78,7 @@ static void print_opt_type_list(void) } } -static u8 *parse_opt_type(u8 *p, const char *arg) +u8 *parse_opt_type(u8 *p, const char *arg) { unsigned int i; @@ -229,7 +229,9 @@ int parse_command_line(int argc, char * const argv[]) { 0, 0, 0, 0 } }; - while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1) + int i = 0; + while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1) { + i++; switch(option) { case 'd': @@ -272,6 +274,9 @@ int parse_command_line(int argc, char * const argv[]) return -1; } + } + printf("%d: %s, %s, %s --> %d loops\n", argc, argv[0], argv[1], argv[2], i); + if(opt.type!=NULL && opt.string!=NULL) { fprintf(stderr, "Options --string and --type are mutually exclusive\n"); diff --git a/example.py b/example.py new file mode 100755 index 0000000..c7f0056 --- /dev/null +++ b/example.py @@ -0,0 +1,14 @@ +#!/usr/bin/python + +def l(x): + return len(x) + +import dmidecode, time +print(dir(dmidecode)) + +print "proc", l(dmidecode.processor()) +print "sys", l(dmidecode.system()) +print "bios", l(dmidecode.bios()) +print "proc", l(dmidecode.processor()) +print "sys", l(dmidecode.system()) +print "bios", l(dmidecode.bios()) |