Skip to content

Commit 85dd8c0

Browse files
author
Jiang Jiang Jian
committed
Merge branch 'fix/incorrect_console_open_and_close_behaviour' into 'master'
fix(storage/vfs_console): stop new console opens from overwriting existing fds Closes IDFGH-13502 See merge request espressif/esp-idf!32934
2 parents 5de734a + d5d295c commit 85dd8c0

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

components/esp_system/test_apps/.build-test-rules.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ components/esp_system/test_apps/console:
88
disable:
99
- if: CONFIG_NAME == "serial_jtag_only" and SOC_USB_SERIAL_JTAG_SUPPORTED != 1
1010
- if: CONFIG_NAME == "serial_jtag_only_no_vfs" and SOC_USB_SERIAL_JTAG_SUPPORTED != 1
11+
disable_test:
12+
- if: CONFIG_NAME == "simple" and IDF_TARGET != "esp32"
1113

1214
components/esp_system/test_apps/esp_system_unity_tests:
1315
disable:

components/esp_system/test_apps/console/main/test_app_main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#include <stdio.h>
77
#include <assert.h>
8+
#include <string.h>
89
#include "sdkconfig.h"
910

1011
#include "esp_rom_uart.h"
@@ -49,10 +50,29 @@ static void console_none_print(void)
4950
}
5051
#endif
5152

53+
#if CONFIG_VFS_SUPPORT_IO
54+
static void console_open_close_check(void)
55+
{
56+
printf("Opening /dev/console\n");
57+
int fd = open("/dev/console", O_RDWR);
58+
assert(fd >= 0 && "Could not open file");
59+
60+
const char *msg = "This should be printed to stdout\n";
61+
62+
write(fd, msg, strlen(msg));
63+
64+
printf("Closing /dev/console\n");
65+
close(fd);
66+
67+
printf("This should be printed to stdout\n");
68+
}
69+
#endif // CONFIG_VFS_SUPPORT_IO
70+
5271
void app_main(void)
5372
{
5473
printf("Hello World\n");
5574

75+
#if CONFIG_VFS_SUPPORT_IO
5676
int fd = open("/dev/null", O_RDWR);
5777
assert(fd >= 0 && "Could not open file"); // Standard check
5878

@@ -61,8 +81,14 @@ void app_main(void)
6181
assert(fd > 2 && "Incorrect file descriptor returned, stdin, stdout, stderr were not correctly assigned");
6282

6383
close(fd);
84+
#endif // CONFIG_VFS_SUPPORT_IO
6485

6586
#if CONFIG_ESP_CONSOLE_NONE
6687
console_none_print();
6788
#endif // CONFIG_ESP_CONSOLE_NONE
89+
90+
#if CONFIG_VFS_SUPPORT_IO
91+
console_open_close_check();
92+
#endif // CONFIG_VFS_SUPPORT_IO
93+
6894
}

components/esp_system/test_apps/console/pytest_esp_system_console_tests.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,43 @@ def test_esp_system_console_no_output_uart(dut: Dut) -> None:
4141
'port, flash_port, config',
4242
[
4343
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only', marks=JTAG_SERIAL_MARKS),
44-
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only_no_vfs', marks=JTAG_SERIAL_MARKS),
4544
],
4645
indirect=True,
4746
)
4847
def test_esp_system_console_only_serial_jtag(dut: Dut) -> None:
4948
dut.expect('2nd stage bootloader')
5049
dut.expect('Hello World')
50+
dut.expect('Opening /dev/console')
51+
dut.expect('This should be printed to stdout')
52+
dut.expect('Closing /dev/console')
53+
dut.expect('This should be printed to stdout')
54+
55+
56+
@pytest.mark.usb_serial_jtag
57+
@pytest.mark.parametrize(
58+
'port, flash_port, config',
59+
[
60+
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only_no_vfs', marks=JTAG_SERIAL_MARKS),
61+
],
62+
indirect=True,
63+
)
64+
def test_esp_system_console_only_serial_jtag_no_vfs(dut: Dut) -> None:
65+
dut.expect('2nd stage bootloader')
66+
dut.expect('Hello World')
67+
68+
69+
@pytest.mark.generic
70+
@pytest.mark.parametrize(
71+
'config',
72+
[
73+
pytest.param('simple', marks=pytest.mark.supported_targets),
74+
],
75+
indirect=True
76+
)
77+
def test_esp_system_console_correct_open_and_close(dut: Dut) -> None:
78+
dut.expect('2nd stage bootloader')
79+
dut.expect('Hello World')
80+
dut.expect('Opening /dev/console')
81+
dut.expect('This should be printed to stdout')
82+
dut.expect('Closing /dev/console')
83+
dut.expect('This should be printed to stdout')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONFIG_ESP_CONSOLE_NONE=y
2+
CONFIG_ESP_CONSOLE_SECONDARY_NONE=y
23
CONFIG_VFS_SUPPORT_IO=y

components/esp_system/test_apps/console/sdkconfig.ci.simple

Whitespace-only changes.

components/esp_vfs_console/vfs_console.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "esp_vfs_console.h"
1616
#include "sdkconfig.h"
1717
#include "esp_private/startup_internal.h"
18-
#include "esp_vfs_null.h"
1918
#include "esp_private/nullfs.h"
19+
#include <sys/errno.h>
2020

2121
#define STRINGIFY(s) STRINGIFY2(s)
2222
#define STRINGIFY2(s) #s
@@ -45,8 +45,18 @@ const static esp_vfs_fs_ops_t *primary_vfs = NULL;
4545

4646
static vfs_console_context_t vfs_console = {0};
4747

48+
static size_t s_open_count = 0;
49+
4850
int console_open(const char * path, int flags, int mode)
4951
{
52+
if (s_open_count > 0) {
53+
// Underlying fd is already open, so just increment the open count
54+
// and return the same fd
55+
56+
s_open_count++;
57+
return 0;
58+
}
59+
5060
// Primary port open
5161
#if CONFIG_ESP_CONSOLE_UART
5262
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)
6272
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
6373
vfs_console.fd_secondary = open("/dev/secondary", flags, mode);
6474
#endif
75+
76+
s_open_count++;
6577
return 0;
6678
}
6779

@@ -82,6 +94,18 @@ int console_fstat(int fd, struct stat * st)
8294

8395
int console_close(int fd)
8496
{
97+
if (s_open_count == 0) {
98+
errno = EBADF;
99+
return -1;
100+
}
101+
102+
s_open_count--;
103+
104+
// We don't actually close the underlying fd until the open count reaches 0
105+
if (s_open_count > 0) {
106+
return 0;
107+
}
108+
85109
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
86110
close(vfs_console.fd_primary);
87111
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG

0 commit comments

Comments
 (0)