mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-03 20:17:34 +00:00
fix(storage/vfs_console): stop new console opens from overwriting existing fds
This commit is contained in:

committed by
Tomas Rohlinek

parent
061ba0c99e
commit
c6d24bddf7
@@ -15,8 +15,8 @@
|
||||
#include "esp_vfs_console.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_private/startup_internal.h"
|
||||
#include "esp_vfs_null.h"
|
||||
#include "esp_private/nullfs.h"
|
||||
#include <sys/errno.h>
|
||||
|
||||
#define STRINGIFY(s) STRINGIFY2(s)
|
||||
#define STRINGIFY2(s) #s
|
||||
@@ -45,8 +45,18 @@ const static esp_vfs_fs_ops_t *primary_vfs = NULL;
|
||||
|
||||
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)
|
||||
{
|
||||
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
|
||||
#if CONFIG_ESP_CONSOLE_UART
|
||||
vfs_console.fd_primary = open("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
|
||||
@@ -62,6 +72,8 @@ int console_open(const char * path, int flags, int mode)
|
||||
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
||||
vfs_console.fd_secondary = open("/dev/secondary", flags, mode);
|
||||
#endif
|
||||
|
||||
s_open_count++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -82,6 +94,18 @@ int console_fstat(int fd, struct stat * st)
|
||||
|
||||
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.
|
||||
close(vfs_console.fd_primary);
|
||||
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
|
||||
|
Reference in New Issue
Block a user