diff options
author | nima <nima@abc39116-655e-4be6-ad55-d661dc543056> | 2008-12-21 07:53:54 +0000 |
---|---|---|
committer | nima <nima@abc39116-655e-4be6-ad55-d661dc543056> | 2008-12-21 07:53:54 +0000 |
commit | 0ff1b5b45facd7543529dab5711cf2812c47c218 (patch) | |
tree | 7cb4ba131f2ee5437079ae5b959889fac172f1e4 | |
parent | 42617d59e66684bff7272c9c49c0cadd2e53c7f9 (diff) | |
download | python-dmidecode-0ff1b5b45facd7543529dab5711cf2812c47c218.tar.gz python-dmidecode-0ff1b5b45facd7543529dab5711cf2812c47c218.tar.xz python-dmidecode-0ff1b5b45facd7543529dab5711cf2812c47c218.zip |
Handle cases where user does not have appropriate permission to access the
memory file or device.
git-svn-id: svn://svn.autonomy.net.au/python-dmidecode@155 abc39116-655e-4be6-ad55-d661dc543056
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/dmidecodemodule.c | 21 | ||||
-rwxr-xr-x | test.py | 27 |
3 files changed, 35 insertions, 15 deletions
@@ -64,7 +64,7 @@ $(SRCSRV)/$(PACKAGE)/$(PACKAGE)_$(VERSION).orig.tar.gz: ../$(PACKAGE)_$(VERSION) cd ../build-area/ && \ gpg --clearsign python-dmidecode_2.10-1_i386.changes && \ mv python-dmidecode_2.10-1_i386.changes.asc python-dmidecode_2.10-1_i386.changes && \ - dupload -t mentors python-dmidecode_2.10-1_i386.changes + echo dupload -t mentors python-dmidecode_2.10-1_i386.changes touch $@ $(SO): diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c index 31f4631..ef19ee5 100644 --- a/src/dmidecodemodule.c +++ b/src/dmidecodemodule.c @@ -135,6 +135,10 @@ static PyObject* dmidecode_get(PyObject *self, const char* section) { opt.type = parse_opt_type(opt.type, section); if(opt.type==NULL) return NULL; + const char *f = opt.dumpfile ? PyString_AsString(opt.dumpfile) : opt.devmem; + if(access(f, R_OK) < 0) + PyErr_SetString(PyExc_IOError, "Permission denied to memory file/device"); + PyObject* pydata = PyDict_New(); /***********************************/ @@ -173,11 +177,16 @@ static PyObject* dmidecode_get(PyObject *self, const char* section) { } } - if(ret==0) free(buf); free(opt.type); + if(ret==0) { + free(buf); + } else { + Py_DECREF(pydata); + pydata = NULL; + } //muntrace(); - return (ret != 1)?pydata:NULL; + return pydata; } static PyObject* dmidecode_get_bios(PyObject *self, PyObject *args) { return dmidecode_get(self, "bios"); } @@ -205,10 +214,10 @@ 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; - stat(f, &buf); + struct stat _buf; + stat(f, &_buf); - if((access(f, F_OK) != 0) || ((access(f, W_OK) == 0) && S_ISREG(buf.st_mode))) + if((access(f, F_OK) != 0) || ((access(f, W_OK) == 0) && S_ISREG(_buf.st_mode))) if(dump(PyString_AS_STRING(opt.dumpfile))) Py_RETURN_TRUE; Py_RETURN_FALSE; @@ -290,5 +299,5 @@ PyMODINIT_FUNC initdmidecode(void) { PyObject *dmi_version = NULL; dmidecode_set_version(&dmi_version); - PyModule_AddObject(module, "dmi", dmi_version); + PyModule_AddObject(module, "dmi", dmi_version?dmi_version:Py_None); } @@ -25,6 +25,7 @@ def test(r): return False total += 1 +print "-"*80 sys.stdout.write("Importing module...") try: import dmidecode @@ -33,6 +34,7 @@ try: sys.stdout.write(" * Version: %s\n"%dmidecode.version) sys.stdout.write(" * DMI Version String: %s\n"%dmidecode.dmi) + print "-"*80 sys.stdout.write("Testing that default device is /dev/mem...") test(dmidecode.get_dev() == "/dev/mem") @@ -54,11 +56,14 @@ try: os.unlink(DUMP) types = range(0, 42)+range(126, 128) - types = range(0, 42)+[126, 127] bad_types = [-1, -1000, 256] sections = ["bios", "system", "baseboard", "chassis", "processor", "memory", "cache", "connector", "slot"] - devices = [os.path.join("private", _) for _ in os.listdir("private")] - devices.remove('private/.svn') + devices = [] + if os.path.exists("private"): + devices.extend([os.path.join("private", _) for _ in os.listdir("private")]) + devices.remove('private/.svn') + else: + sys.stdout.write("If you have memory dumps to test, create a directory called `private' and drop them in there.\n") devices.append("/dev/mem") random.shuffle(types) random.shuffle(devices) @@ -67,6 +72,15 @@ try: for dev in devices: sys.stdout.write(" * Testing %s..."%dev); sys.stdout.flush() if test(dmidecode.set_dev(dev) and dmidecode.get_dev() == dev): + print "-"*80 + print sections + for section in sections: + sys.stdout.write(" * Testing %s..."%section); sys.stdout.flush() + output = getattr(dmidecode, section)() + test(output is not False) + if output: sys.stdout.write(" * %s\n"%output.keys()) + + print "-"*80 for i in bad_types: sys.stdout.write(" * Testing bad type %i..."%i); sys.stdout.flush() try: @@ -74,17 +88,14 @@ try: test(output is False) except SystemError: sys.stdout.write("FAILED\n") + + print "-"*80 for i in types: sys.stdout.write(" * Testing type %i..."%i); sys.stdout.flush() output = dmidecode.type(i) test(output is not False) if output: sys.stdout.write(" * %s\n"%output.keys()) - for section in sections: - sys.stdout.write(" * Testing %s..."%section); sys.stdout.flush() - output = getattr(dmidecode, section)() - test(output is not False) - if output: sys.stdout.write(" * %s\n"%output.keys()) except ImportError: sys.stdout.write("FAILED\n") |