From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 20396A034F; Wed, 9 Feb 2022 07:54:26 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C33CC41165; Wed, 9 Feb 2022 07:54:11 +0100 (CET) Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mails.dpdk.org (Postfix) with ESMTP id BAA2B4115A for ; Wed, 9 Feb 2022 07:54:10 +0100 (CET) Received: by mail-pl1-f172.google.com with SMTP id x3so1432374pll.3 for ; Tue, 08 Feb 2022 22:54:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b6Lg72jErA2YiAaxtEzDxgYdm5Ko2AxdI3US1yOOAG8=; b=z5ucmloyuXQBfOEE59fzfC2sPwSxS+0gaxVmYiSAglKRKn82g7W/YREjHFlewJ3cs5 sAE+WJQWxgZDyDAaFhG9Ke1pdlytsjPmKrnhM8ZUXbhcx4zQtU/Fs4wDimVZsVOm3kL0 fVBw4hqu+NokCTC3MMXENWOAqT1/knQ9nXMnbt1P2wcweOU3x8mgLT0UM18bq5wsVp1F So9dF4GR0pGLxZCWpgE8wqjLkzZOytvV4aIEaEjkfvZQAzGbAqPU8nenDk5V8jBrJIoQ Vw+ugWFuhqAqvPD8y4HhazVGZg5hFmjotw0A08j13Iw5GPVvm4l1cUniB9B49CVBH/fm vprw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b6Lg72jErA2YiAaxtEzDxgYdm5Ko2AxdI3US1yOOAG8=; b=f6ZXL6SOdVWl7mIMLjV+dZhrzwmPoY5qjb84cWyM+WBCsTvFxdaWPYk/xlBAaWGchN 5jNAIDIKT1eFIEkYlU1gGz65kk0NGec3VMKgXe5cMTG0xvB3hynZHZoaUi8d+bEqyDOo UaAYcMTW5MVoQEAy+jajBxmzvEPhjLc5Y/VJq/FUENEzzlzwjvIoA0SCHcgu8JuROqos qKN/4+upcG8Srw+I+wrmNAkAKCPEQpimkYB66CN5Z+MO9lF+Ws7xSEaRwOzIrHjSrbvs QJMsG03u48hXg8r6QttL+APFXW1cbLJWQJJmZse6qsyeCTh/qqp+XXK95o59V5qd3tnh jmYA== X-Gm-Message-State: AOAM53042lVKil4plVWkG4TRfH5DluccCJAqjOBYsCAsUSP0FiLvZj3+ Z/HTxsSDCFJ4R/yzYmAJ95w5ijeXZLXwqJmf X-Google-Smtp-Source: ABdhPJwtye0VSIbSAxr25BX+xagOtO/9J7G9Ky+bwEEZ6WJwp0IObZr/+jmzpNLQda6Ihsb1z/U8GQ== X-Received: by 2002:a17:90a:5503:: with SMTP id b3mr1858470pji.169.1644389649357; Tue, 08 Feb 2022 22:54:09 -0800 (PST) Received: from hermes.local (204-195-112-199.wavecable.com. [204.195.112.199]) by smtp.gmail.com with ESMTPSA id oa5sm5208892pjb.0.2022.02.08.22.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 22:54:08 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v3 3/3] eal: move common filesystem setup code into one file Date: Tue, 8 Feb 2022 22:54:03 -0800 Message-Id: <20220209065403.168475-4-stephen@networkplumber.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220209065403.168475-1-stephen@networkplumber.org> References: <20211223233907.181033-1-stephen@networkplumber.org> <20220209065403.168475-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Both Linux and FreeBSD have same code for creating runtime directory and reading sysfs files. Put them in the new lib/eal/unix subdirectory. Signed-off-by: Stephen Hemminger --- lib/eal/freebsd/eal.c | 88 --------------------------- lib/eal/linux/eal.c | 90 ---------------------------- lib/eal/unix/eal_filesystem.c | 110 ++++++++++++++++++++++++++++++++++ lib/eal/unix/meson.build | 1 + 4 files changed, 111 insertions(+), 178 deletions(-) create mode 100644 lib/eal/unix/eal_filesystem.c diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c index 503e276dc27f..eacd432ab621 100644 --- a/lib/eal/freebsd/eal.c +++ b/lib/eal/freebsd/eal.c @@ -72,62 +72,6 @@ struct lcore_config lcore_config[RTE_MAX_LCORE]; /* used by rte_rdtsc() */ int rte_cycles_vmware_tsc_map; -static const char *default_runtime_dir = "/var/run"; - -int -eal_create_runtime_dir(void) -{ - const char *directory = default_runtime_dir; - const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR"); - const char *fallback = "/tmp"; - char run_dir[PATH_MAX]; - char tmp[PATH_MAX]; - int ret; - - if (getuid() != 0) { - /* try XDG path first, fall back to /tmp */ - if (xdg_runtime_dir != NULL) - directory = xdg_runtime_dir; - else - directory = fallback; - } - /* create DPDK subdirectory under runtime dir */ - ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory); - if (ret < 0 || ret == sizeof(tmp)) { - RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n"); - return -1; - } - - /* create prefix-specific subdirectory under DPDK runtime dir */ - ret = snprintf(run_dir, sizeof(run_dir), "%s/%s", - tmp, eal_get_hugefile_prefix()); - if (ret < 0 || ret == sizeof(run_dir)) { - RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n"); - return -1; - } - - /* create the path if it doesn't exist. no "mkdir -p" here, so do it - * step by step. - */ - ret = mkdir(tmp, 0700); - if (ret < 0 && errno != EEXIST) { - RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", - tmp, strerror(errno)); - return -1; - } - - ret = mkdir(run_dir, 0700); - if (ret < 0 && errno != EEXIST) { - RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", - run_dir, strerror(errno)); - return -1; - } - - if (eal_set_runtime_dir(run_dir)) - return -1; - - return 0; -} int eal_clean_runtime_dir(void) @@ -138,38 +82,6 @@ eal_clean_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) -{ - 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", - __func__, filename); - return -1; - } - - if (fgets(buf, sizeof(buf), f) == NULL) { - RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n", - __func__, filename); - fclose(f); - return -1; - } - *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; -} - - /* create memory configuration in shared/mmap memory. Take out * a write lock on the memsegs, so we can auto-detect primary/secondary. * This means we never close the file while running (auto-close on exit). diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c index e9764f6c9f37..b99cab25ce48 100644 --- a/lib/eal/linux/eal.c +++ b/lib/eal/linux/eal.c @@ -86,65 +86,6 @@ struct lcore_config lcore_config[RTE_MAX_LCORE]; /* used by rte_rdtsc() */ int rte_cycles_vmware_tsc_map; -int -eal_create_runtime_dir(void) -{ - const char *directory; - char run_dir[PATH_MAX]; - char tmp[PATH_MAX]; - int ret; - - /* from RuntimeDirectory= see systemd.exec */ - directory = getenv("RUNTIME_DIRECTORY"); - if (directory == NULL) { - /* - * Used standard convention defined in - * XDG Base Directory Specification and - * Filesystem Hierarchy Standard. - */ - if (getuid() == 0) - directory = "/var/run"; - else - directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp"; - } - - /* create DPDK subdirectory under runtime dir */ - ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory); - if (ret < 0 || ret == sizeof(tmp)) { - RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n"); - return -1; - } - - /* create prefix-specific subdirectory under DPDK runtime dir */ - ret = snprintf(run_dir, sizeof(run_dir), "%s/%s", - tmp, eal_get_hugefile_prefix()); - if (ret < 0 || ret == sizeof(run_dir)) { - RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n"); - return -1; - } - - /* create the path if it doesn't exist. no "mkdir -p" here, so do it - * step by step. - */ - ret = mkdir(tmp, 0700); - if (ret < 0 && errno != EEXIST) { - RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", - tmp, strerror(errno)); - return -1; - } - - ret = mkdir(run_dir, 0700); - if (ret < 0 && errno != EEXIST) { - RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", - run_dir, strerror(errno)); - return -1; - } - - if (eal_set_runtime_dir(run_dir)) - return -1; - - return 0; -} int eal_clean_runtime_dir(void) @@ -232,37 +173,6 @@ eal_clean_runtime_dir(void) return -1; } -/* parse a sysfs (or other) file containing one integer value */ -int -eal_parse_sysfs_value(const char *filename, unsigned long *val) -{ - 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", - __func__, filename); - return -1; - } - - if (fgets(buf, sizeof(buf), f) == NULL) { - RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n", - __func__, filename); - fclose(f); - return -1; - } - *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; -} - /* create memory configuration in shared/mmap memory. Take out * a write lock on the memsegs, so we can auto-detect primary/secondary. diff --git a/lib/eal/unix/eal_filesystem.c b/lib/eal/unix/eal_filesystem.c new file mode 100644 index 000000000000..4759651a9581 --- /dev/null +++ b/lib/eal/unix/eal_filesystem.c @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation. + * Copyright(c) 2012-2014 6WIND S.A. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "eal_private.h" +#include "eal_filesystem.h" + +int +eal_create_runtime_dir(void) +{ + const char *directory; + char run_dir[PATH_MAX]; + char tmp[PATH_MAX]; + int ret; + + /* from RuntimeDirectory= see systemd.exec */ + directory = getenv("RUNTIME_DIRECTORY"); + if (directory == NULL) { + /* + * Used standard convention defined in + * XDG Base Directory Specification and + * Filesystem Hierarchy Standard. + */ + if (getuid() == 0) + directory = "/var/run"; + else + directory = getenv("XDG_RUNTIME_DIR") ? : "/tmp"; + } + + /* create DPDK subdirectory under runtime dir */ + ret = snprintf(tmp, sizeof(tmp), "%s/dpdk", directory); + if (ret < 0 || ret == sizeof(tmp)) { + RTE_LOG(ERR, EAL, "Error creating DPDK runtime path name\n"); + return -1; + } + + /* create prefix-specific subdirectory under DPDK runtime dir */ + ret = snprintf(run_dir, sizeof(run_dir), "%s/%s", + tmp, eal_get_hugefile_prefix()); + if (ret < 0 || ret == sizeof(run_dir)) { + RTE_LOG(ERR, EAL, "Error creating prefix-specific runtime path name\n"); + return -1; + } + + /* create the path if it doesn't exist. no "mkdir -p" here, so do it + * step by step. + */ + ret = mkdir(tmp, 0700); + if (ret < 0 && errno != EEXIST) { + RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", + tmp, strerror(errno)); + return -1; + } + + ret = mkdir(run_dir, 0700); + if (ret < 0 && errno != EEXIST) { + RTE_LOG(ERR, EAL, "Error creating '%s': %s\n", + run_dir, strerror(errno)); + return -1; + } + + if (eal_set_runtime_dir(run_dir)) + return -1; + + return 0; +} +/* parse a sysfs (or other) file containing one integer value */ +int +eal_parse_sysfs_value(const char *filename, unsigned long *val) +{ + 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", + __func__, filename); + return -1; + } + + if (fgets(buf, sizeof(buf), f) == NULL) { + RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n", + __func__, filename); + fclose(f); + return -1; + } + *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/unix/meson.build b/lib/eal/unix/meson.build index e3ecd3e956b2..a22ea7cabc46 100644 --- a/lib/eal/unix/meson.build +++ b/lib/eal/unix/meson.build @@ -6,5 +6,6 @@ sources += files( 'eal_unix_memory.c', 'eal_unix_timer.c', 'eal_firmware.c', + 'eal_filesystem.c', 'rte_thread.c', ) -- 2.34.1