Skip to content

Commit 96d3dfb

Browse files
committed
Merge branch 'fix/incorrect_console_open_and_close_behaviour_v5.3' into 'release/v5.3'
fix(storage/vfs_console): stop new console opens from overwriting existing fds (v5.3) See merge request espressif/esp-idf!35268
2 parents 10db90a + 299b93b commit 96d3dfb

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
components/esp_system/test_apps/console:
44
disable:
55
- if: CONFIG_NAME == "serial_jtag_only" and SOC_USB_SERIAL_JTAG_SUPPORTED != 1
6+
- if: CONFIG_NAME == "simple" and IDF_TARGET != "esp32"
67

78
components/esp_system/test_apps/esp_system_unity_tests:
89
disable:

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <stdio.h>
7+
#include <assert.h>
8+
#include <string.h>
9+
#include <fcntl.h>
10+
#include <unistd.h>
711
#include "sdkconfig.h"
812

913
#include "esp_rom_uart.h"
@@ -45,11 +49,34 @@ static void console_none_print(void)
4549
}
4650
#endif
4751

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

5274
#if CONFIG_ESP_CONSOLE_NONE
5375
console_none_print();
5476
#endif // CONFIG_ESP_CONSOLE_NONE
77+
78+
#if CONFIG_VFS_SUPPORT_IO
79+
console_open_close_check();
80+
#endif // CONFIG_VFS_SUPPORT_IO
81+
5582
}

components/esp_system/test_apps/console/pytest_esp_system_console_tests.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,46 @@ def test_esp_system_console_no_output_uart(dut: Dut) -> None:
3838

3939
@pytest.mark.usb_serial_jtag
4040
@pytest.mark.parametrize(
41-
'port, config',
41+
'port, flash_port, config',
4242
[
43-
pytest.param('/dev/serial_ports/ttyACM-esp32', 'serial_jtag_only', marks=JTAG_SERIAL_MARKS),
43+
pytest.param('/dev/serial_ports/ttyACM-esp32', '/dev/serial_ports/ttyUSB-esp32', 'serial_jtag_only', marks=JTAG_SERIAL_MARKS),
4444
],
4545
indirect=True,
4646
)
4747
def test_esp_system_console_only_serial_jtag(dut: Dut) -> None:
4848
dut.expect('2nd stage bootloader')
4949
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')

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

Whitespace-only changes.

components/esp_vfs_console/vfs_console.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "esp_vfs_console.h"
1616
#include "sdkconfig.h"
1717
#include "esp_private/startup_internal.h"
18+
#include <sys/errno.h>
1819

1920
#define STRINGIFY(s) STRINGIFY2(s)
2021
#define STRINGIFY2(s) #s
@@ -43,8 +44,18 @@ const static esp_vfs_t *primary_vfs = NULL;
4344

4445
static vfs_console_context_t vfs_console = {0};
4546

47+
static size_t s_open_count = 0;
48+
4649
int console_open(const char * path, int flags, int mode)
4750
{
51+
if (s_open_count > 0) {
52+
// Underlying fd is already open, so just increment the open count
53+
// and return the same fd
54+
55+
s_open_count++;
56+
return 0;
57+
}
58+
4859
// Primary port open
4960
#if CONFIG_ESP_CONSOLE_UART
5061
vfs_console.fd_primary = open("/dev/uart/"STRINGIFY(CONFIG_ESP_CONSOLE_UART_NUM), flags, mode);
@@ -58,6 +69,8 @@ int console_open(const char * path, int flags, int mode)
5869
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
5970
vfs_console.fd_secondary = open("/dev/secondary", flags, mode);
6071
#endif
72+
73+
s_open_count++;
6174
return 0;
6275
}
6376

@@ -78,6 +91,18 @@ int console_fstat(int fd, struct stat * st)
7891

7992
int console_close(int fd)
8093
{
94+
if (s_open_count == 0) {
95+
errno = EBADF;
96+
return -1;
97+
}
98+
99+
s_open_count--;
100+
101+
// We don't actually close the underlying fd until the open count reaches 0
102+
if (s_open_count > 0) {
103+
return 0;
104+
}
105+
81106
// All function calls are to primary, except from write and close, which will be forwarded to both primary and secondary.
82107
close(vfs_console.fd_primary);
83108
#if CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG

0 commit comments

Comments
 (0)