From e08ca78f40bb1948acffa60c0315083acfb02227 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 20 Apr 2020 23:48:34 +1000 Subject: [PATCH] py/stream: Remove mp_stream_errno and use system errno instead. This change is made for two reasons: 1. A 3rd-party library (eg berkeley-db-1.xx, axtls) may use the system provided errno for certain errors, and yet MicroPython stream objects that it calls will be using the internal mp_stream_errno. So if the library returns an error it is not known whether the corresponding errno code is stored in the system errno or mp_stream_errno. Using the system errno in all cases (eg in the mp_stream_posix_XXX wrappers) fixes this ambiguity. 2. For systems that have threading the system-provided errno should always be used because the errno value is thread-local. For systems that do not have an errno, the new lib/embed/__errno.c file is provided. --- extmod/extmod.mk | 2 +- lib/embed/__errno.c | 13 +++++++++++++ ports/esp32/mphalport.c | 7 ------- ports/esp8266/Makefile | 1 + ports/esp8266/esp_mphal.c | 5 ----- py/stream.c | 12 +++++------- 6 files changed, 20 insertions(+), 20 deletions(-) create mode 100644 lib/embed/__errno.c diff --git a/extmod/extmod.mk b/extmod/extmod.mk index 69d8cfad32..e312acba86 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -47,7 +47,7 @@ CFLAGS_MOD += -DMICROPY_PY_USSL=1 ifeq ($(MICROPY_SSL_AXTLS),1) CFLAGS_MOD += -DMICROPY_SSL_AXTLS=1 -I$(TOP)/lib/axtls/ssl -I$(TOP)/lib/axtls/crypto -I$(TOP)/extmod/axtls-include AXTLS_DIR = lib/axtls -$(BUILD)/$(AXTLS_DIR)/%.o: CFLAGS += -Wno-all -Wno-unused-parameter -Wno-uninitialized -Wno-sign-compare -Wno-old-style-definition $(AXTLS_DEFS_EXTRA) +$(BUILD)/$(AXTLS_DIR)/%.o: CFLAGS += -Wno-all -Wno-unused-parameter -Wno-uninitialized -Wno-sign-compare -Wno-old-style-definition -Dmp_stream_errno=errno $(AXTLS_DEFS_EXTRA) SRC_MOD += $(addprefix $(AXTLS_DIR)/,\ ssl/asn1.c \ ssl/loader.c \ diff --git a/lib/embed/__errno.c b/lib/embed/__errno.c new file mode 100644 index 0000000000..86417a02dd --- /dev/null +++ b/lib/embed/__errno.c @@ -0,0 +1,13 @@ +// This file provides a version of __errno() for embedded systems that do not have one. +// This function is needed for expressions of the form: &errno + +static int embed_errno; + +#if defined(__linux__) +int *__errno_location(void) +#else +int *__errno(void) +#endif +{ + return &embed_errno; +} diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 4f7785f24e..99548ad627 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -195,13 +195,6 @@ void mp_hal_delay_us(uint32_t us) { } } -/* -extern int mp_stream_errno; -int *__errno() { - return &mp_stream_errno; -} -*/ - // Wake up the main task if it is sleeping void mp_hal_wake_main_task_from_isr(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 85a04c070a..7fb1bc43ca 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -120,6 +120,7 @@ EXTMOD_SRC_C = $(addprefix extmod/,\ ) LIB_SRC_C = $(addprefix lib/,\ + embed/__errno.c \ libc/string0.c \ libm/math.c \ libm/fmodf.c \ diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c index 8859046d7e..20d0557d62 100644 --- a/ports/esp8266/esp_mphal.c +++ b/ports/esp8266/esp_mphal.c @@ -206,8 +206,3 @@ int ets_esf_free_bufs(int idx) { } return cnt; } - -extern int mp_stream_errno; -int *__errno() { - return &mp_stream_errno; -} diff --git a/py/stream.c b/py/stream.c index e6ffd3e2b0..dce64a44db 100644 --- a/py/stream.c +++ b/py/stream.c @@ -503,14 +503,12 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl); * POSIX-compatible software to work with MicroPython streams. */ -// errno-like variable. If any of the functions below returned with error -// status, this variable will contain error no. -int mp_stream_errno; +#include ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { mp_obj_base_t *o = stream; const mp_stream_p_t *stream_p = o->type->protocol; - mp_uint_t out_sz = stream_p->write(MP_OBJ_FROM_PTR(stream), buf, len, &mp_stream_errno); + mp_uint_t out_sz = stream_p->write(MP_OBJ_FROM_PTR(stream), buf, len, &errno); if (out_sz == MP_STREAM_ERROR) { return -1; } else { @@ -521,7 +519,7 @@ ssize_t mp_stream_posix_write(void *stream, const void *buf, size_t len) { ssize_t mp_stream_posix_read(void *stream, void *buf, size_t len) { mp_obj_base_t *o = stream; const mp_stream_p_t *stream_p = o->type->protocol; - mp_uint_t out_sz = stream_p->read(MP_OBJ_FROM_PTR(stream), buf, len, &mp_stream_errno); + mp_uint_t out_sz = stream_p->read(MP_OBJ_FROM_PTR(stream), buf, len, &errno); if (out_sz == MP_STREAM_ERROR) { return -1; } else { @@ -535,7 +533,7 @@ off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { struct mp_stream_seek_t seek_s; seek_s.offset = offset; seek_s.whence = whence; - mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &mp_stream_errno); + mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &errno); if (res == MP_STREAM_ERROR) { return -1; } @@ -545,7 +543,7 @@ off_t mp_stream_posix_lseek(void *stream, off_t offset, int whence) { int mp_stream_posix_fsync(void *stream) { mp_obj_base_t *o = stream; const mp_stream_p_t *stream_p = o->type->protocol; - mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_FLUSH, 0, &mp_stream_errno); + mp_uint_t res = stream_p->ioctl(MP_OBJ_FROM_PTR(stream), MP_STREAM_FLUSH, 0, &errno); if (res == MP_STREAM_ERROR) { return -1; }