From patchwork Thu Dec 22 23:24:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Duszynski X-Patchwork-Id: 121294 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C8E90A0093; Fri, 23 Dec 2022 00:24:54 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2B717427EB; Fri, 23 Dec 2022 00:24:50 +0100 (CET) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by mails.dpdk.org (Postfix) with ESMTP id 47B1340685 for ; Fri, 23 Dec 2022 00:24:48 +0100 (CET) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2BMGxxOv026098; Thu, 22 Dec 2022 15:24:47 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=3jbrJUZUmZXTtLPKbZG9aDofLiAzyT6WEMu7rKa1geU=; b=gcBbFsg5f8gQ8htKGHJ//RRmwltHZGHah40gash2TxuDTuULN8MxxKRuD38qvju6r4B2 VB74hp+f3CuowaqiRwKDo6N0GozYgJsaBUYQQDNiybCZRqSYowQxuHRCLeNT0Z3H+S7x AlxY3Xg+AER/f8eCwnQFgblnQ0WJDEiXWo4sV09Wbe1O/Ri2LPwxgFkIg1+9OS+Arvpb hYZ9Nv9d7ejzIqEBwLPypn9X+V+X+f2yr/jKjcaHG3uaOGJIEm2GrtWtXyq7QlZ7EvGK tyAcq2rx/8qdwfduIHU/c+k7ICm+ZaqbF6gF2fA1riBtgKt6T251gxnha2KogUoXw+8P 4A== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3mhe5runw7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Thu, 22 Dec 2022 15:24:47 -0800 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.42; Thu, 22 Dec 2022 15:24:45 -0800 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.42 via Frontend Transport; Thu, 22 Dec 2022 15:24:45 -0800 Received: from localhost.localdomain (unknown [10.28.34.39]) by maili.marvell.com (Postfix) with ESMTP id B93AB3F704E; Thu, 22 Dec 2022 15:24:43 -0800 (PST) From: Tomasz Duszynski To: CC: , , Tomasz Duszynski Subject: [RFC PATCH 1/7] lib: add helper to read strings from sysfs files Date: Fri, 23 Dec 2022 00:24:29 +0100 Message-ID: <20221222232436.643514-2-tduszynski@marvell.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221222232436.643514-1-tduszynski@marvell.com> References: <20221222232436.643514-1-tduszynski@marvell.com> MIME-Version: 1.0 X-Proofpoint-GUID: HxPP2LqLkCkzGEo-AnRVM1abvwjwrN57 X-Proofpoint-ORIG-GUID: HxPP2LqLkCkzGEo-AnRVM1abvwjwrN57 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-12-22_10,2022-12-22_03,2022-06-22_01 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Reading strings from sysfs files is a re-occurring pattern hence add helper for doing that. Signed-off-by: Tomasz Duszynski --- app/test/test_eal_fs.c | 108 ++++++++++++++++++++++++++++---- lib/eal/common/eal_filesystem.h | 6 ++ lib/eal/unix/eal_filesystem.c | 24 ++++--- lib/eal/version.map | 3 + 4 files changed, 123 insertions(+), 18 deletions(-) diff --git a/app/test/test_eal_fs.c b/app/test/test_eal_fs.c index b3686edcb4..6c373fc7f1 100644 --- a/app/test/test_eal_fs.c +++ b/app/test/test_eal_fs.c @@ -20,12 +20,33 @@ test_eal_fs(void) #else +static int +temp_create(char *filename, size_t len) +{ + char file_template[] = "/tmp/eal_test_XXXXXX"; + char proc_path[PATH_MAX]; + int fd; + + fd = mkstemp(file_template); + if (fd == -1) { + perror("mkstemp() failure"); + return -1; + } + + snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd); + if (readlink(proc_path, filename, len) < 0) { + perror("readlink() failure"); + close(fd); + return -1; + } + + return fd; +} + static int test_parse_sysfs_value(void) { char filename[PATH_MAX] = ""; - char proc_path[PATH_MAX]; - char file_template[] = "/tmp/eal_test_XXXXXX"; int tmp_file_handle = -1; FILE *fd = NULL; unsigned valid_number; @@ -40,16 +61,10 @@ test_parse_sysfs_value(void) /* get a temporary filename to use for all tests - create temp file handle and then * use /proc to get the actual file that we can open */ - tmp_file_handle = mkstemp(file_template); - if (tmp_file_handle == -1) { - perror("mkstemp() failure"); + tmp_file_handle = temp_create(filename, sizeof(filename)); + if (tmp_file_handle < 0) goto error; - } - snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", tmp_file_handle); - if (readlink(proc_path, filename, sizeof(filename)) < 0) { - perror("readlink() failure"); - goto error; - } + printf("Temporary file is: %s\n", filename); /* test we get an error value if we use file before it's created */ @@ -175,11 +190,82 @@ test_parse_sysfs_value(void) return -1; } +static int +test_parse_sysfs_string(void) +{ + const char *teststr = "the quick brown dog jumps over the lazy fox\n"; + char filename[PATH_MAX] = ""; + char buf[BUFSIZ] = { }; + int tmp_file_handle; + FILE *fd = NULL; + +#ifdef RTE_EXEC_ENV_FREEBSD + /* BSD doesn't have /proc/pid/fd */ + return 0; +#endif + printf("Testing function eal_parse_sysfs_string()\n"); + + /* get a temporary filename to use for all tests - create temp file handle and then + * use /proc to get the actual file that we can open + */ + tmp_file_handle = temp_create(filename, sizeof(filename)); + if (tmp_file_handle < 0) + goto error; + + printf("Temporary file is: %s\n", filename); + + /* test we get an error value if we use file before it's created */ + printf("Test reading a missing file ...\n"); + if (eal_parse_sysfs_string("/dev/not-quite-null", buf, sizeof(buf)) == 0) { + printf("Error with eal_parse_sysfs_string() - returned success on reading empty file\n"); + goto error; + } + printf("Confirmed return error when reading empty file\n"); + + /* test reading a string from file */ + printf("Test reading string ...\n"); + fd = fopen(filename, "w"); + if (fd == NULL) { + printf("line %d, Error opening %s: %s\n", __LINE__, filename, strerror(errno)); + goto error; + } + fprintf(fd, "%s", teststr); + fclose(fd); + fd = NULL; + if (eal_parse_sysfs_string(filename, buf, sizeof(buf) - 1) < 0) { + printf("eal_parse_sysfs_string() returned error - test failed\n"); + goto error; + } + if (strcmp(teststr, buf)) { + printf("Invalid string read by eal_parse_sysfs_string() - test failed\n"); + goto error; + } + /* don't print newline */ + buf[strlen(buf) - 1] = '\0'; + printf("Read '%s\\n' ok\n", buf); + + close(tmp_file_handle); + unlink(filename); + printf("eal_parse_sysfs_string() - OK\n"); + return 0; + +error: + if (fd) + fclose(fd); + if (tmp_file_handle > 0) + close(tmp_file_handle); + if (filename[0] != '\0') + unlink(filename); + return -1; +} + static int test_eal_fs(void) { if (test_parse_sysfs_value() < 0) return -1; + if (test_parse_sysfs_string() < 0) + return -1; return 0; } diff --git a/lib/eal/common/eal_filesystem.h b/lib/eal/common/eal_filesystem.h index 5d21f07c20..79bcc5ca1f 100644 --- a/lib/eal/common/eal_filesystem.h +++ b/lib/eal/common/eal_filesystem.h @@ -104,4 +104,10 @@ eal_get_hugefile_path(char *buffer, size_t buflen, const char *hugedir, int f_id * Used to read information from files on /sys */ int eal_parse_sysfs_value(const char *filename, unsigned long *val); +/** Function to read a string from a file on the filesystem. + * Used to read information for files in /sys + */ +__rte_experimental +int eal_parse_sysfs_string(const char *filename, char *str, size_t size); + #endif /* EAL_FILESYSTEM_H */ diff --git a/lib/eal/unix/eal_filesystem.c b/lib/eal/unix/eal_filesystem.c index afbab9368a..8ed10094be 100644 --- a/lib/eal/unix/eal_filesystem.c +++ b/lib/eal/unix/eal_filesystem.c @@ -76,12 +76,9 @@ int eal_create_runtime_dir(void) return 0; } -/* parse a sysfs (or other) file containing one integer value */ -int eal_parse_sysfs_value(const char *filename, unsigned long *val) +int eal_parse_sysfs_string(const char *filename, char *str, size_t size) { FILE *f; - char buf[BUFSIZ]; - char *end = NULL; if ((f = fopen(filename, "r")) == NULL) { RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n", @@ -89,19 +86,32 @@ int eal_parse_sysfs_value(const char *filename, unsigned long *val) return -1; } - if (fgets(buf, sizeof(buf), f) == NULL) { + if (fgets(str, size, f) == NULL) { RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n", __func__, filename); fclose(f); return -1; } + fclose(f); + return 0; +} + +/* parse a sysfs (or other) file containing one integer value */ +int eal_parse_sysfs_value(const char *filename, unsigned long *val) +{ + char buf[BUFSIZ]; + char *end = NULL; + int ret; + + ret = eal_parse_sysfs_string(filename, buf, sizeof(buf)); + if (ret < 0) + return ret; + *val = strtoul(buf, &end, 0); if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) { RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n", __func__, filename); - fclose(f); return -1; } - fclose(f); return 0; } diff --git a/lib/eal/version.map b/lib/eal/version.map index 7ad12a7dc9..2dbc10e6be 100644 --- a/lib/eal/version.map +++ b/lib/eal/version.map @@ -440,6 +440,9 @@ EXPERIMENTAL { rte_thread_detach; rte_thread_equal; rte_thread_join; + + # added in 23.03 + eal_parse_sysfs_string; # WINDOWS_NO_EXPORT }; INTERNAL {