From 08286ac20f28a58fffe7acacc34c3566dededdef Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sun, 1 Dec 2019 19:20:52 +0100 Subject: [PATCH 1/3] newlib: add definitions of system, raise Closes IDF-1220 Closes IDFGH-1811 Closes https://github.com/espressif/esp-idf/pull/4020 --- components/newlib/syscalls.c | 11 +++++++++++ components/newlib/test/test_newlib.c | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/components/newlib/syscalls.c b/components/newlib/syscalls.c index c3da628da..56a12293d 100644 --- a/components/newlib/syscalls.c +++ b/components/newlib/syscalls.c @@ -21,12 +21,23 @@ #include +int system(const char* str) +{ + errno = ENOSYS; + return -1; +} + int _system_r(struct _reent *r, const char *str) { __errno_r(r) = ENOSYS; return -1; } +int raise(int sig) +{ + abort(); +} + int _raise_r(struct _reent *r, int sig) { abort(); diff --git a/components/newlib/test/test_newlib.c b/components/newlib/test/test_newlib.c index 41f72329b..4cfec3e40 100644 --- a/components/newlib/test/test_newlib.c +++ b/components/newlib/test/test_newlib.c @@ -182,3 +182,8 @@ TEST_CASE("fmod and fmodf work as expected", "[newlib]") TEST_ASSERT_EQUAL(0.1, fmod(10.1, 2.0)); TEST_ASSERT_EQUAL(0.1f, fmodf(10.1f, 2.0f)); } + +TEST_CASE("newlib: can link 'system', 'raise'", "[newlib]") +{ + printf("system: %p, raise: %p\n", &system, &raise); +} From 23848fd1b92588f5b8babf8d21c671e8185aac48 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Dec 2019 13:50:04 +0100 Subject: [PATCH 2/3] newlib: define fcntl as strong symbol Closes https://github.com/espressif/esp-idf/issues/3694 Closes https://github.com/espressif/esp-idf/issues/4407 --- components/newlib/syscalls.c | 15 ++++++++++++++- components/vfs/test/test_vfs_uart.c | 11 +++++++++++ components/vfs/vfs.c | 10 ---------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/components/newlib/syscalls.c b/components/newlib/syscalls.c index 56a12293d..8b4b7aece 100644 --- a/components/newlib/syscalls.c +++ b/components/newlib/syscalls.c @@ -15,10 +15,12 @@ #include #include #include +#include #include #include #include -#include +#include +#include int system(const char* str) @@ -65,6 +67,17 @@ void _exit(int __status) abort(); } +/* Replaces newlib fcntl, which has been compiled without HAVE_FCNTL */ +int fcntl(int fd, int cmd, ...) +{ + va_list args; + va_start(args, cmd); + int arg = va_arg(args, int); + va_end(args); + struct _reent* r = __getreent(); + return _fcntl_r(r, fd, cmd, arg); +} + /* No-op function, used to force linking this file, instead of the syscalls implementation from libgloss. */ diff --git a/components/vfs/test/test_vfs_uart.c b/components/vfs/test/test_vfs_uart.c index 77815aed7..255e54f83 100644 --- a/components/vfs/test/test_vfs_uart.c +++ b/components/vfs/test/test_vfs_uart.c @@ -207,6 +207,17 @@ TEST_CASE("can write to UART while another task is reading", "[vfs]") vSemaphoreDelete(write_arg.done); } +TEST_CASE("fcntl supported in UART VFS", "[vfs]") +{ + int flags = fcntl(STDIN_FILENO, F_GETFL, 0); + TEST_ASSERT_NOT_EQUAL(-1, flags); + int res = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); + TEST_ASSERT_NOT_EQUAL(-1, res); + /* revert */ + res = fcntl(STDIN_FILENO, F_SETFL, flags); + TEST_ASSERT_NOT_EQUAL(-1, res); +} + #ifdef CONFIG_VFS_SUPPORT_TERMIOS TEST_CASE("Can use termios for UART", "[vfs]") { diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 65b558b6d..690896c57 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -725,16 +725,6 @@ int _fcntl_r(struct _reent *r, int fd, int cmd, int arg) return ret; } -int __attribute__((weak)) fcntl(int fd, int cmd, ...) -{ - va_list args; - va_start(args, cmd); - int arg = va_arg(args, int); - va_end(args); - struct _reent* r = __getreent(); - return _fcntl_r(r, fd, cmd, arg); -} - int ioctl(int fd, int cmd, ...) { const vfs_entry_t* vfs = get_vfs_for_fd(fd); From 87a41fabfa9431b136dd94e01180aab98cf2dbcf Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Dec 2019 15:28:21 +0100 Subject: [PATCH 3/3] esp-tls: check return value of fcntl --- components/esp-tls/esp_tls.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index af2be302d..ba9e75812 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -189,7 +189,11 @@ static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *s } if (cfg->non_block) { int flags = fcntl(fd, F_GETFL, 0); - fcntl(fd, F_SETFL, flags | O_NONBLOCK); + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + if (ret < 0) { + ESP_LOGE(TAG, "Failed to configure the socket as non-blocking (errno %d)", errno); + goto err_freesocket; + } } }