Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions boards/native/native_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions boards/native/native_sim/Kconfig
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"

Expand Down
16 changes: 16 additions & 0 deletions boards/native/native_sim/reboot.c
Original file line number Diff line number Diff line change
@@ -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);
}
74 changes: 74 additions & 0 deletions boards/native/native_sim/reboot_bottom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2025 GARDENA GmbH
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <nsi_main.h>
#include <nsi_tasks.h>
#include <nsi_tracing.h>
#include <nsi_cmdline.h>

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);
21 changes: 21 additions & 0 deletions boards/native/native_sim/reboot_bottom.h
Original file line number Diff line number Diff line change
@@ -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 */