#define NEWLINE "\n\r"
-char *truncate_nl(char *s) {
+static void truncate_nl(char *s) {
assert(s);
- s[strcspn(s, NEWLINE)] = 0;
- return s;
+ s[strcspn(s, NEWLINE)] = '\0';
}
-int read_one_line_file(const char *filename, char **line) {
- assert(filename);
- assert(line);
+static int read_one_line_file(const char *filename, char **line) {
+ char t[2048];
+
+ if (!filename || !line)
+ return -EINVAL;
- FILE *f = NULL;
- f = fopen(filename, "re");
+ FILE* f = fopen(filename, "re");
if (!f)
return -errno;
- char t[2048];
if (!fgets(t, sizeof(t), f)) {
if (ferror(f))
return errno ? -errno : -EIO;
char *c = strdup(t);
if (!c)
return -ENOMEM;
+
truncate_nl(c);
*line = c;
return 0;
}
-
static PyObject *
do_detect_hypervisor() {
/*
Python wrapper around read_harddisk_serial.
*/
static struct hd_driveid hd;
- int fd;
const char *device = NULL;
+ char serial[21];
if (!PyArg_ParseTuple(args, "s", &device))
return NULL;
- if ((fd = open(device, O_RDONLY | O_NONBLOCK)) < 0) {
+ int fd = open(device, O_RDONLY | O_NONBLOCK);
+ if (fd < 0) {
PyErr_Format(PyExc_OSError, "Could not open block device: %s", device);
return NULL;
}
if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
- char serial[21];
- memset(serial, 0, sizeof(serial));
-
- strncpy(serial, (const char *)hd.serial_no, sizeof(serial) - 1);
+ snprintf(serial, sizeof(serial) - 1, "%s", (const char *)hd.serial_no);
- if (serial[0])
+ if (*serial) {
+ close(fd);
return PyUnicode_FromString(serial);
+ }
}
+ close(fd);
+
Py_RETURN_NONE;
}