summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorseteiro <[email protected]>2026-11-12 14:04:58 +0800
committerRbb666 <[email protected]>2026-11-13 09:36:13 +0800
commit4b433918cdeb71ac9af08a47ee1e9923e8a09f85 (patch)
treefce3b20cf9622dd23190b767a9fdef62f3f77058 /components
parent9548847a06d5d87f93cf461e1e74dc83a1212018 (diff)
[libc][stdio] avoid musl fopen for kernel console setup
Fix #11716 Use the kernel DFS open() and close() path for the MUSLLIBC console setup, matching the non-musl std_fd implementation. This prevents the kernel-side call from entering musl's Linux-numbered openat syscall path and leaves the syscall table and user-space musl ABI unchanged.
Diffstat (limited to 'components')
-rw-r--r--components/libc/posix/io/stdio/stdio.c42
1 files changed, 10 insertions, 32 deletions
diff --git a/components/libc/posix/io/stdio/stdio.c b/components/libc/posix/io/stdio/stdio.c
index 06d341d722..76b97fe560 100644
--- a/components/libc/posix/io/stdio/stdio.c
+++ b/components/libc/posix/io/stdio/stdio.c
@@ -129,53 +129,31 @@ int rt_posix_stdio_get_console(void)
#elif defined(RT_USING_MUSLLIBC)
-static FILE* std_console = NULL;
-
-int rt_posix_stdio_set_console(const char* device_name, int mode)
+static int std_fd = -1;
+int rt_posix_stdio_set_console(const char *device_name, int mode)
{
- FILE *fp;
+ int fd;
char name[STDIO_DEVICE_NAME_MAX];
- char *file_mode;
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
- if (mode == O_RDWR) file_mode = "r+";
- else if (mode == O_WRONLY) file_mode = "wb";
- else file_mode = "rb";
-
- fp = fopen(name, file_mode);
- if (fp)
+ fd = open(name, mode, 0);
+ if (fd >= 0)
{
- setvbuf(fp, NULL, _IONBF, 0);
-
- if (std_console)
+ if (std_fd >= 0)
{
- fclose(std_console);
- std_console = NULL;
+ close(std_fd);
}
- std_console = fp;
- }
-
- if (std_console)
- {
- int fd = fileno(std_console);
-
- return fd;
+ std_fd = fd;
}
- return -1;
+ return std_fd;
}
int rt_posix_stdio_get_console(void)
{
- int ret = -1;
- if (std_console)
- {
- ret = fileno(std_console);
- }
-
- return ret;
+ return std_fd;
}
#else