From patchwork Fri May 7 11:16:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tremer X-Patchwork-Id: 4251 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4Fc7Ff48KMz44R5 for ; Fri, 7 May 2021 11:17:10 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4Fc7FY4wgrz36v; Fri, 7 May 2021 11:17:05 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4Fc7FY4dPLz2ylP; Fri, 7 May 2021 11:17:05 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4Fc7FW5Tpqz2xpc for ; Fri, 7 May 2021 11:17:03 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4Fc7FW3tRLz1Sm; Fri, 7 May 2021 11:17:03 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1620386223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BMqV465+z5qsosOMUh++gNaf2kusnDVq4poBJ36ySZQ=; b=Vse3u4jIsxjbrHbWaFZlaOwXydqXHFC/DLuDZIVu0odcvmPEzwRcuqjmV3+8wdsZ6MHItz sh2so7buiHnI+NAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1620386223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BMqV465+z5qsosOMUh++gNaf2kusnDVq4poBJ36ySZQ=; b=Mzj2nF1pphxQ/rc+o1FH7HDP0Srn3ayebhgeWJJQf2XVpAUWE/kB3rQji3Qt4ntyA9j3Nx +hHCEVa5lmWCyuyEVwMY0tKC8T0sbSnWH2uccb0lD66Rm6GddasLqvC1/ebsV4LyX+pJOn t3LhstciE0yUNRaA0gChQ3qgD3/AN9/UEVv9dugGfvIworVYRLa1ZyadRDu1tT1K7H5ep+ PcHdoBZDNYzosG4Cj8H8Ggs6MKDHAXWO675cWRwri4b+AHGA6xq1/zBH0E5DZGMm0Lrkbh hdTMXNoGiM2xIGkdK3ZTWqLnNBWH+rx9zPPl5RiWxc1YcELVYV4JCpd9LZ9ZBg== From: Michael Tremer To: development@lists.ipfire.org Subject: [PATCH 03/10] _fireinfo: Refactor some code Date: Fri, 7 May 2021 11:16:47 +0000 Message-Id: <20210507111654.2397-4-michael.tremer@ipfire.org> In-Reply-To: <20210507111654.2397-1-michael.tremer@ipfire.org> References: <74B08C60-EBDB-4C2E-83E0-A5EB5A0C3C5E@ipfire.org> <20210507111654.2397-1-michael.tremer@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Tremer Errors-To: development-bounces@lists.ipfire.org Sender: "Development" No functional changes. This only makes the code more similar to what I am writing in other projects and makes it tidier. Signed-off-by: Michael Tremer --- src/_fireinfo/fireinfo.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/_fireinfo/fireinfo.c b/src/_fireinfo/fireinfo.c index 58ee7e2..75ccb2c 100644 --- a/src/_fireinfo/fireinfo.c +++ b/src/_fireinfo/fireinfo.c @@ -58,23 +58,22 @@ const char *hypervisor_vendors[] = { #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; @@ -85,6 +84,7 @@ int read_one_line_file(const char *filename, char **line) { char *c = strdup(t); if (!c) return -ENOMEM; + truncate_nl(c); *line = c; @@ -171,7 +171,6 @@ int detect_hypervisor(int *hypervisor) { return 0; } - static PyObject * do_detect_hypervisor() { /* @@ -197,27 +196,29 @@ do_get_harddisk_serial(PyObject *o, PyObject *args) { 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; }