From 2e2bdc689d6306a3d1e61dadc0a11b0cda783ed3 Mon Sep 17 00:00:00 2001 From: Ayrton Munoz Date: Tue, 25 Oct 2022 18:41:18 -0400 Subject: lib/libc: Add fd_io_handle and file_io_handle LK's libc exposes the FILE struct's definition which app/consoletest and libtrusty use to access the `io_handle_t`s corresponding to stdout/stderr. On the other hande, musl only exposes an opaque type for FILE. This commit adds two new functions for accessing the `io_handle_t`s given a file descriptor or a FILE*. These functions also have equivalents for musl defined in libc-trusty. This allows us to modify the modules that map fd or FILE* to io_handle_t with a call to one of the new functions and select the definition based on which libc implementation is chosen. Bug: 230134581 Change-Id: I2052258614b42413cc6ea040bd29b42249cc7c04 --- lib/libc/include/stdio.h | 3 +++ lib/libc/io_handle.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/libc/rules.mk | 3 ++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/libc/io_handle.c (limited to 'lib') diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h index 3ef8932e..05b604b5 100644 --- a/lib/libc/include/stdio.h +++ b/lib/libc/include/stdio.h @@ -83,6 +83,9 @@ int vsnprintf_filtered(char *str, size_t len, const char *fmt, va_list ap); // sccanf is not implemented. int sscanf(const char* str, const char* format, ...); +io_handle_t* fd_io_handle(int fd); +io_handle_t* file_io_handle(FILE* file); + __END_CDECLS #endif diff --git a/lib/libc/io_handle.c b/lib/libc/io_handle.c new file mode 100644 index 00000000..ed911496 --- /dev/null +++ b/lib/libc/io_handle.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Google, Inc. All rights reserved + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include + +io_handle_t* fd_io_handle(int fd) { + if ((fd == 1) || (fd == 2)) { + return &console_io; + } + return NULL; +} + +io_handle_t* file_io_handle(FILE* file) { + if ((file == stdout) || (file == stderr)) { + return &console_io; + } + return NULL; +} diff --git a/lib/libc/rules.mk b/lib/libc/rules.mk index 0cf7cd1b..ed35164e 100644 --- a/lib/libc/rules.mk +++ b/lib/libc/rules.mk @@ -32,7 +32,8 @@ MODULE_SRCS += \ $(LOCAL_DIR)/stdio.c \ $(LOCAL_DIR)/qsort.c \ $(LOCAL_DIR)/eabi.c \ - $(LOCAL_DIR)/eabi_unwind_stubs.c + $(LOCAL_DIR)/eabi_unwind_stubs.c \ + $(LOCAL_DIR)/io_handle.c ifeq ($(WITH_CPP_SUPPORT),true) MODULE_SRCS += \ -- cgit v1.2.3