cpukit: Raise internal error if we cannot open stdin with fileno 0

If someone manages to open a file before rtems_libio_post_driver is run,
open() may allocate a file number other than 0 for stdin. This leads to
a silent failure of the logic in rtems_libio_post_driver, and confusing
behavior because your BSP behaves as if it doesn't have a console.

Instead of failing silently, raise an internal error if open() succeeds
but gives us an unexpected file number for stdin.
This commit is contained in:
Jeremy Lorelli
2025-06-29 16:58:37 -07:00
committed by Chris Johns
parent 897a8d3094
commit d8ba01ec52
3 changed files with 14 additions and 4 deletions

View File

@@ -232,7 +232,8 @@ typedef enum {
INTERNAL_ERROR_IDLE_THREAD_CREATE_FAILED = 43, INTERNAL_ERROR_IDLE_THREAD_CREATE_FAILED = 43,
INTERNAL_ERROR_NO_MEMORY_FOR_IDLE_TASK_STORAGE = 44, INTERNAL_ERROR_NO_MEMORY_FOR_IDLE_TASK_STORAGE = 44,
INTERNAL_ERROR_IDLE_THREAD_STACK_TOO_SMALL = 45, INTERNAL_ERROR_IDLE_THREAD_STACK_TOO_SMALL = 45,
INTERNAL_ERROR_CANNOT_DISABLE_DATA_CACHE = 46 INTERNAL_ERROR_CANNOT_DISABLE_DATA_CACHE = 46,
INTERNAL_ERROR_LIBIO_STDIN_FD_OPEN_FAILED = 47,
} Internal_errors_Core_list; } Internal_errors_Core_list;
typedef CPU_Uint32ptr Internal_errors_t; typedef CPU_Uint32ptr Internal_errors_t;

View File

@@ -47,16 +47,24 @@
*/ */
void rtems_libio_post_driver(void) void rtems_libio_post_driver(void)
{ {
int fd = 0;
/* /*
* Attempt to open /dev/console. * Attempt to open /dev/console.
*/ */
if ( open( CONSOLE_DEVICE_NAME, O_RDONLY, 0 ) != STDIN_FILENO ) { if ( ( fd = open( CONSOLE_DEVICE_NAME, O_RDONLY, 0 ) ) != STDIN_FILENO ) {
/* /*
* There may not be a console driver so this is OK. * There may not be a console driver so this is OK.
*/ */
if ( fd < 0 ) {
return; return;
} }
/*
* If open succeeds, but doesn't give us the stdin fileno we expect, bail out...
*/
_Internal_error( INTERNAL_ERROR_LIBIO_STDIN_FD_OPEN_FAILED );
}
/* /*
* But if we find /dev/console once, we better find it twice more * But if we find /dev/console once, we better find it twice more
* or something is REALLY wrong. * or something is REALLY wrong.

View File

@@ -86,7 +86,8 @@ static const char *const internal_error_text[] = {
"INTERNAL_ERROR_RTEMS_INIT_TASK_CONSTRUCT_FAILED", "INTERNAL_ERROR_RTEMS_INIT_TASK_CONSTRUCT_FAILED",
"INTERNAL_ERROR_IDLE_THREAD_CREATE_FAILED", "INTERNAL_ERROR_IDLE_THREAD_CREATE_FAILED",
"INTERNAL_ERROR_NO_MEMORY_FOR_IDLE_TASK_STORAGE", "INTERNAL_ERROR_NO_MEMORY_FOR_IDLE_TASK_STORAGE",
"INTERNAL_ERROR_IDLE_THREAD_STACK_TOO_SMALL" "INTERNAL_ERROR_IDLE_THREAD_STACK_TOO_SMALL",
"INTERNAL_ERROR_LIBIO_STDIN_FD_OPEN_FAILED"
}; };
const char *rtems_internal_error_text( rtems_fatal_code error ) const char *rtems_internal_error_text( rtems_fatal_code error )