mirror of
https://github.com/espressif/esp-idf
synced 2025-03-09 17:19:09 -04:00
Merge branch 'fix/incorrect_console_open_and_close_behaviour_v5.0' into 'release/v5.0'
fix(storage/vfs_console): stop new console opens from overwriting existing fds (v5.0) See merge request espressif/esp-idf!35271
This commit is contained in:
commit
e59cc9822c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -12,6 +12,7 @@
|
|||||||
#include "esp_vfs_dev.h"
|
#include "esp_vfs_dev.h"
|
||||||
#include "esp_private/usb_console.h"
|
#include "esp_private/usb_console.h"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
#include <sys/errno.h>
|
||||||
|
|
||||||
#define STRINGIFY(s) STRINGIFY2(s)
|
#define STRINGIFY(s) STRINGIFY2(s)
|
||||||
#define STRINGIFY2(s) #s
|
#define STRINGIFY2(s) #s
|
||||||
@ -49,8 +50,18 @@ static int primary_vfs_index;
|
|||||||
|
|
||||||
static vfs_console_context_t vfs_console= {0};
|
static vfs_console_context_t vfs_console= {0};
|
||||||
|
|
||||||
|
static size_t s_open_count = 0;
|
||||||
|
|
||||||
int console_open(const char * path, int flags, int mode)
|
int console_open(const char * path, int flags, int mode)
|
||||||
{
|
{
|
||||||
|
if (s_open_count > 0) {
|
||||||
|
// Underlying fd is already open, so just increment the open count
|
||||||
|
// and return the same fd
|
||||||
|
|
||||||
|
s_open_count++;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Primary port open
|
// Primary port open
|
||||||
#if CONFIG_ESP_CONSOLE_UART
|
#if CONFIG_ESP_CONSOLE_UART
|
||||||
vfs_console.fd_primary = get_vfs_for_path(primary_path)->vfs.open("/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
|
vfs_console.fd_primary = get_vfs_for_path(primary_path)->vfs.open("/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
|
||||||
@ -64,6 +75,8 @@ int console_open(const char * path, int flags, int mode)
|
|||||||
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
||||||
vfs_console.fd_secondary = get_vfs_for_path(secondary_path)->vfs.open("/", flags, mode);
|
vfs_console.fd_secondary = get_vfs_for_path(secondary_path)->vfs.open("/", flags, mode);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
s_open_count++;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +97,18 @@ int console_fstat(int fd, struct stat * st)
|
|||||||
|
|
||||||
int console_close(int fd)
|
int console_close(int fd)
|
||||||
{
|
{
|
||||||
|
if (s_open_count == 0) {
|
||||||
|
errno = EBADF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
s_open_count--;
|
||||||
|
|
||||||
|
// We don't actually close the underlying fd until the open count reaches 0
|
||||||
|
if (s_open_count > 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
|
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
|
||||||
get_vfs_for_index(primary_vfs_index)->vfs.close(vfs_console.fd_primary);
|
get_vfs_for_index(primary_vfs_index)->vfs.close(vfs_console.fd_primary);
|
||||||
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
||||||
|
Loading…
x
Reference in New Issue
Block a user