From 7890c77581f4494dbf6550ef60a7185d8e5ad5f6 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 28 Jan 2014 17:22:56 +0100 Subject: [PATCH] Catch exception when the hardware serial number cannot be read. --- src/_fireinfo/fireinfo.c | 6 ++++-- src/fireinfo/system.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/_fireinfo/fireinfo.c b/src/_fireinfo/fireinfo.c index 08d714b..4ab2bfa 100644 --- a/src/_fireinfo/fireinfo.c +++ b/src/_fireinfo/fireinfo.c @@ -197,13 +197,15 @@ do_get_harddisk_serial(PyObject *o, PyObject *args) { */ static struct hd_driveid hd; int fd; - char *device; + const char *device = NULL; if (!PyArg_ParseTuple(args, "s", &device)) return NULL; - if ((fd = open(device, O_RDONLY | O_NONBLOCK)) < 0) + if ((fd = open(device, O_RDONLY | O_NONBLOCK)) < 0) { + PyErr_Format(PyExc_OSError, "Could not open block device: %s", device); return NULL; + } if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) { char serial[21]; diff --git a/src/fireinfo/system.py b/src/fireinfo/system.py index 9db536b..800032e 100644 --- a/src/fireinfo/system.py +++ b/src/fireinfo/system.py @@ -397,7 +397,10 @@ class System(object): """ Return the serial number of the root disk (if any). """ - serial = _fireinfo.get_harddisk_serial("/dev/%s" % self.root_disk) + try: + serial = _fireinfo.get_harddisk_serial("/dev/%s" % self.root_disk) + except OSError: + return if serial: # Strip all spaces -- 2.39.2