From 3d91e6d993c2f76d1c44b2b3d1db8e2a503e55d6 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 15 Sep 2021 14:49:57 +0200 Subject: [PATCH 1/2] newlib: implement _fstat_r stub for console When CONFIG_VFS_SUPPORT_IO is disabled, _read_r and _write_r implementations in syscalls.c are used to provide console I/O via esp_rom_uart_tx_one_char/esp_rom_uart_rx_one_char. When newlib opens a (FILE*) stream, it calls fstat to check if the underlying file is character-oriented. In this case, it configures the stream to use line buffering. Otherwise (or if fstat fails) the stream is opened as block buffered. Since fstat wasn't provided, stdin/stdout/stderr streams got opened in block buffered mode. For console, we need line buffered output so that the stream buffer is flushed each time a complete line (ending with '\n') is sent to stdout or stderr. Fix by implementing _fstat_r stub, setting st->st_mdoe=S_IFCHR. --- components/newlib/syscalls.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/components/newlib/syscalls.c b/components/newlib/syscalls.c index d6c3098592..f9e5300fd9 100644 --- a/components/newlib/syscalls.c +++ b/components/newlib/syscalls.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -64,6 +65,18 @@ ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size) return -1; } +static ssize_t _fstat_r_console(struct _reent *r, int fd, struct stat * st) +{ + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { + memset(st, 0, sizeof(*st)); + /* This needs to be set so that stdout and stderr are line buffered. */ + st->st_mode = S_IFCHR; + return 0; + } + __errno_r(r) = EBADF; + return -1; +} + /* The following weak definitions of syscalls will be used unless * another definition is provided. That definition may come from @@ -73,6 +86,8 @@ ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size) __attribute__((weak,alias("_read_r_console"))); ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size) __attribute__((weak,alias("_write_r_console"))); +int _fstat_r (struct _reent *r, int fd, struct stat *st) + __attribute__((weak,alias("_fstat_r_console"))); /* The aliases below are to "syscall_not_implemented", which @@ -90,8 +105,6 @@ off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode) __attribute__((weak,alias("syscall_not_implemented"))); int _fcntl_r(struct _reent *r, int fd, int cmd, int arg) __attribute__((weak,alias("syscall_not_implemented"))); -int _fstat_r(struct _reent *r, int fd, struct stat * st) - __attribute__((weak,alias("syscall_not_implemented"))); int _stat_r(struct _reent *r, const char * path, struct stat * st) __attribute__((weak,alias("syscall_not_implemented"))); int _link_r(struct _reent *r, const char* n1, const char* n2) From 8e7b11f6d14f768b1472689766ddaad0980226d6 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 15 Sep 2021 14:56:31 +0200 Subject: [PATCH 2/2] newlib: set errno in the explicitly provided reent structure Since these functions receive the pointer to reent structure, they should set errno in it rather than using thread-local errno. This didn't cause practical issues because console functions in IDF are only called from threads, and in that case 'r' is a pointer to the thread-local structure, so &errno is the same thing as &__errno_r(r). Still, fixing this for consistency. --- components/newlib/syscalls.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/newlib/syscalls.c b/components/newlib/syscalls.c index f9e5300fd9..9cf5827166 100644 --- a/components/newlib/syscalls.c +++ b/components/newlib/syscalls.c @@ -24,9 +24,9 @@ #include "sdkconfig.h" #include "esp_rom_uart.h" -static int syscall_not_implemented(void) +static int syscall_not_implemented(struct _reent *r, ...) { - errno = ENOSYS; + __errno_r(r) = ENOSYS; return -1; } @@ -44,7 +44,7 @@ ssize_t _write_r_console(struct _reent *r, int fd, const void * data, size_t siz } return size; } - errno = EBADF; + __errno_r(r) = EBADF; return -1; } @@ -61,7 +61,7 @@ ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size) } return received; } - errno = EBADF; + __errno_r(r) = EBADF; return -1; } @@ -118,8 +118,6 @@ int _isatty_r(struct _reent *r, int fd) /* These functions are not expected to be overridden */ -int system(const char* str) - __attribute__((alias("syscall_not_implemented"))); int _system_r(struct _reent *r, const char *str) __attribute__((alias("syscall_not_implemented"))); int raise(int sig) @@ -137,6 +135,13 @@ void _exit(int __status) #pragma GCC diagnostic pop +/* Similar to syscall_not_implemented, but not taking struct _reent argument */ +int system(const char* str) +{ + errno = ENOSYS; + return -1; +} + /* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */ int fcntl(int fd, int cmd, ...) {