aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAyrton Munoz <ayrton@google.com>2022-10-25 18:41:18 -0400
committerAyrton Munoz <ayrton@google.com>2022-10-28 23:18:56 -0400
commit2e2bdc689d6306a3d1e61dadc0a11b0cda783ed3 (patch)
tree02801a170c64ed2d00be59ec0050aa66b27793e5 /lib
parent0583fc1296e21c2531f1e91e9ca001c07cc72f7d (diff)
downloadcommon-2e2bdc689d6306a3d1e61dadc0a11b0cda783ed3.tar.gz
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
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/include/stdio.h3
-rw-r--r--lib/libc/io_handle.c38
-rw-r--r--lib/libc/rules.mk3
3 files changed, 43 insertions, 1 deletions
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 <stdio.h>
+
+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 += \