diff --git a/boards/native/native_sim/CMakeLists.txt b/boards/native/native_sim/CMakeLists.txt index 2bb964f49e86c..014b0bb049cb1 100644 --- a/boards/native/native_sim/CMakeLists.txt +++ b/boards/native/native_sim/CMakeLists.txt @@ -13,6 +13,15 @@ zephyr_library_sources( posix_arch_if.c ) +if(CONFIG_NATIVE_SIM_REBOOT) + zephyr_library_sources(reboot.c) + if(CONFIG_NATIVE_LIBRARY) + target_sources(native_simulator INTERFACE reboot_bottom.c) + else() + zephyr_library_sources(reboot_bottom.c) + endif() +endif() + zephyr_include_directories( ${NSI_DIR}/common/src/include ${NSI_DIR}/native/src/include diff --git a/boards/native/native_sim/Kconfig b/boards/native/native_sim/Kconfig index 4ecdbf702f58a..b04c637469166 100644 --- a/boards/native/native_sim/Kconfig +++ b/boards/native/native_sim/Kconfig @@ -1,4 +1,5 @@ # Copyright (c) 2023 Nordic Semiconductor ASA +# Copyright (c) 2025 GARDENA GmbH # SPDX-License-Identifier: Apache-2.0 config BOARD_NATIVE_SIM @@ -50,6 +51,12 @@ config NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME Transitional option which allows applications which targeted native_posix to set the correct native_sim option (CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME) +config NATIVE_SIM_REBOOT + bool "Reboot support" + depends on REBOOT + help + Enables the reboot implementation for the native sim executable. + source "boards/native/common/sdl/Kconfig" source "boards/native/common/extra_args/Kconfig" diff --git a/boards/native/native_sim/reboot.c b/boards/native/native_sim/reboot.c new file mode 100644 index 0000000000000..fb864cb202232 --- /dev/null +++ b/boards/native/native_sim/reboot.c @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 GARDENA GmbH + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "reboot_bottom.h" +#include "posix_board_if.h" + +void sys_arch_reboot(int type) +{ + (void)type; + native_set_reboot_on_exit(); + posix_exit(0); +} diff --git a/boards/native/native_sim/reboot_bottom.c b/boards/native/native_sim/reboot_bottom.c new file mode 100644 index 0000000000000..21ec7a2634f25 --- /dev/null +++ b/boards/native/native_sim/reboot_bottom.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 GARDENA GmbH + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static const char module[] = "native_sim_reboot"; + +static long close_open_fds(void) +{ + /* close all open file descriptors except 0-2 */ + errno = 0; + + long max_fd = sysconf(_SC_OPEN_MAX); + + if (max_fd < 0) { + if (errno != 0) { + nsi_print_error_and_exit("%s: %s\n", module, strerror(errno)); + } else { + nsi_print_warning("%s: Cannot determine maximum number of file descriptors" + "\n", + module); + } + return max_fd; + } + for (int fd = 3; fd < max_fd; fd++) { + (void)close(fd); + } + return 0; +} + +static bool reboot_on_exit; + +void native_set_reboot_on_exit(void) +{ + reboot_on_exit = true; +} + +void maybe_reboot(void) +{ + char **argv; + int argc; + + if (!reboot_on_exit) { + return; + } + + reboot_on_exit = false; /* If we reenter it means we failed to reboot */ + + nsi_get_cmd_line_args(&argc, &argv); + + if (close_open_fds() < 0) { + nsi_exit(1); + } + + nsi_print_warning("%s: Restarting process.\n", module); + + (void)execv("/proc/self/exe", argv); + + nsi_print_error_and_exit("%s: Failed to restart process, exiting (%s)\n", module, + strerror(errno)); +} + +NSI_TASK(maybe_reboot, ON_EXIT_POST, 999); diff --git a/boards/native/native_sim/reboot_bottom.h b/boards/native/native_sim/reboot_bottom.h new file mode 100644 index 0000000000000..3e4eef6d420bc --- /dev/null +++ b/boards/native/native_sim/reboot_bottom.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 GARDENA GmbH + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H +#define BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H + +#ifdef __cplusplus +extern "C" { +#endif + +void native_set_reboot_on_exit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H */