From 0e5793b270d8377f9b9dc12a0c05adfe1ca9bad5 Mon Sep 17 00:00:00 2001 From: "sonika.rathi" Date: Wed, 21 Feb 2024 09:49:50 +0100 Subject: [PATCH] feat(vfs): Add function to dump VFS file descriptors --- components/vfs/include/esp_vfs.h | 19 +++++++++ components/vfs/test_apps/main/test_vfs_fd.c | 47 ++++++++++++++++++++- components/vfs/vfs.c | 22 +++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/components/vfs/include/esp_vfs.h b/components/vfs/include/esp_vfs.h index f7cca42e71..527b4d7e62 100644 --- a/components/vfs/include/esp_vfs.h +++ b/components/vfs/include/esp_vfs.h @@ -469,6 +469,25 @@ ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset); */ ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset); +/** + * + * @brief Dump the existing VFS FDs data to FILE* fp + * + * Dump the FDs in the format: + @verbatim + -- + + where: + VFS Path Prefix : file prefix used in the esp_vfs_register call + FD seen by App : file descriptor returned by the vfs to the application for the path prefix + FD seen by driver : file descriptor used by the driver for the same file prefix. + + @endverbatim + * + * @param fp File descriptor where data will be dumped + */ +void esp_vfs_dump_fds(FILE *fp); + #ifdef __cplusplus } // extern "C" #endif diff --git a/components/vfs/test_apps/main/test_vfs_fd.c b/components/vfs/test_apps/main/test_vfs_fd.c index 4eeebb5f7d..94814539cd 100644 --- a/components/vfs/test_apps/main/test_vfs_fd.c +++ b/components/vfs/test_apps/main/test_vfs_fd.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -18,6 +18,10 @@ #include "esp_log.h" #include "test_utils.h" #include "ccomp_timer.h" +#include "driver/uart.h" +#include "driver/uart_vfs.h" +#include "lwip/sockets.h" +#include "lwip/netdb.h" #define VFS_PREF1 "/vfs1" #define VFS_PREF2 "/vfs2" @@ -289,3 +293,44 @@ TEST_CASE("esp_vfs_register_fd_range checks for overlap", "[vfs]") TEST_ESP_OK(esp_vfs_unregister("/test")); TEST_ESP_ERR(ESP_ERR_INVALID_ARG, err); } + +static void socket_init(int *socket_fd) +{ + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_DGRAM, + }; + struct addrinfo *res; + int err; + + err = getaddrinfo("localhost", "80", &hints, &res); + TEST_ASSERT_EQUAL(err, 0); + TEST_ASSERT_NOT_NULL(res); + + *socket_fd = socket(res->ai_family, res->ai_socktype, 0); + TEST_ASSERT(*socket_fd >= 0); + freeaddrinfo(res); +} + +TEST_CASE("esp_vfs_dump_fds displays all registered fds in vfs", "[vfs]") +{ + int uart_fd0, uart_fd1, uart_fd2; + int socket_fd=-1; + + uart_fd0 = open("/dev/uart/0", O_RDWR); + TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd0, -1, "Cannot open UART"); + uart_fd1 = open("/dev/uart/1", O_RDWR); + TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd1, -1, "Cannot open UART"); + uart_fd2 = open("/dev/uart/1", O_RDWR); + TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd2, -1, "Cannot open UART"); + + test_case_uses_tcpip(); + socket_init(&socket_fd); + + esp_vfs_dump_fds(stdout); + + close(uart_fd0); + close(uart_fd1); + close(uart_fd2); + close(socket_fd); +} diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index ef66a54323..906e8684d9 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -270,6 +270,26 @@ esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd) return ret; } +void esp_vfs_dump_fds(FILE *fp) +{ + const vfs_entry_t* vfs; + fprintf(fp, "------------------------------------------------------\n"); + fprintf(fp, "--\n"); + fprintf(fp, "------------------------------------------------------\n"); + _lock_acquire(&s_fd_table_lock); + for (int index = 0; index < MAX_FDS; index++) { + if (s_fd_table[index].vfs_index != -1) { + vfs = s_vfs[s_fd_table[index].vfs_index]; + if (strcmp(vfs->path_prefix, "")) { + fprintf(fp, "(%s) - 0x%x - 0x%x\n", vfs->path_prefix, index, s_fd_table[index].local_fd); + } else { + fprintf(fp, "(socket) - 0x%x - 0x%x\n", index, s_fd_table[index].local_fd); + } + } + } + _lock_release(&s_fd_table_lock); +} + /* * Set ESP_VFS_FLAG_READONLY_FS read-only flag for a registered virtual filesystem * for given path prefix. Should be only called from the esp_vfs_*filesystem* register