Skip to content

Commit d0772e8

Browse files
koalatuxaescolar
andcommitted
boards: native_sim: add reboot for native_sim
Add a reboot implementation for the native_sim target which restarts the current executable keeping the command line arguments after closing all open file descriptors. Signed-off-by: Adrian Friedli <adrian.friedli@husqvarnagroup.com> Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no> Co-Authored-By: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
1 parent c3646a3 commit d0772e8

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

boards/native/native_sim/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ zephyr_library_sources(
1313
posix_arch_if.c
1414
)
1515

16+
if(CONFIG_NATIVE_SIM_REBOOT)
17+
zephyr_library_sources(reboot.c)
18+
if(CONFIG_NATIVE_LIBRARY)
19+
target_sources(native_simulator INTERFACE reboot_bottom.c)
20+
else()
21+
zephyr_library_sources(reboot_bottom.c)
22+
endif()
23+
endif()
24+
1625
zephyr_include_directories(
1726
${NSI_DIR}/common/src/include
1827
${NSI_DIR}/native/src/include

boards/native/native_sim/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2023 Nordic Semiconductor ASA
2+
# Copyright (c) 2025 GARDENA GmbH
23
# SPDX-License-Identifier: Apache-2.0
34

45
config BOARD_NATIVE_SIM
@@ -50,6 +51,12 @@ config NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME
5051
Transitional option which allows applications which targeted native_posix
5152
to set the correct native_sim option (CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME)
5253

54+
config NATIVE_SIM_REBOOT
55+
bool "Reboot native sim"
56+
depends on REBOOT
57+
help
58+
Enables the reboot implementation for the native sim executable.
59+
5360
source "boards/native/common/sdl/Kconfig"
5461
source "boards/native/common/extra_args/Kconfig"
5562

boards/native/native_sim/reboot.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025 GARDENA GmbH
3+
* Copyright (c) 2025 Nordic Semiconductor ASA
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include "reboot_bottom.h"
9+
#include "posix_board_if.h"
10+
11+
void sys_arch_reboot(int type)
12+
{
13+
(void)type;
14+
native_set_reboot_on_exit();
15+
posix_exit(0);
16+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2025 GARDENA GmbH
3+
* Copyright (c) 2025 Nordic Semiconductor ASA
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <stdbool.h>
9+
#include <errno.h>
10+
#include <string.h>
11+
#include <unistd.h>
12+
#include <nsi_main.h>
13+
#include <nsi_tasks.h>
14+
#include <nsi_tracing.h>
15+
#include <nsi_cmdline.h>
16+
17+
static const char module[] = "native_sim_reboot";
18+
19+
static long close_open_fds(void)
20+
{
21+
/* close all open file descriptors except 0-2 */
22+
errno = 0;
23+
24+
long max_fd = sysconf(_SC_OPEN_MAX);
25+
26+
if (max_fd < 0) {
27+
if (errno != 0) {
28+
nsi_print_error_and_exit("%s: %s\n", module, strerror(errno));
29+
} else {
30+
nsi_print_warning("%s: Cannot determine maximum number of file descriptors"
31+
"\n",
32+
module);
33+
}
34+
return max_fd;
35+
}
36+
for (int fd = 3; fd < max_fd; fd++) {
37+
(void)close(fd);
38+
}
39+
return 0;
40+
}
41+
42+
static bool reboot_on_exit;
43+
44+
void native_set_reboot_on_exit(void)
45+
{
46+
reboot_on_exit = true;
47+
}
48+
49+
void maybe_reboot(void)
50+
{
51+
char **argv;
52+
int argc;
53+
54+
if (!reboot_on_exit) {
55+
return;
56+
}
57+
58+
reboot_on_exit = false; /* If we reenter it means we failed to reboot */
59+
60+
nsi_get_cmd_line_args(&argc, &argv);
61+
62+
if (close_open_fds() < 0) {
63+
nsi_exit(1);
64+
}
65+
66+
nsi_print_warning("%s: Restarting process.\n", module);
67+
68+
(void)execv("/proc/self/exe", argv);
69+
70+
nsi_print_error_and_exit("%s: Failed to restart process, exiting (%s)\n", module,
71+
strerror(errno));
72+
}
73+
74+
NSI_TASK(maybe_reboot, ON_EXIT_POST, 999);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 GARDENA GmbH
3+
* Copyright (c) 2025 Nordic Semiconductor ASA
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H
9+
#define BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
void native_set_reboot_on_exit(void);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif
20+
21+
#endif /* BOARDS_NATIVE_NATIVE_SIM_REBOOT_BOTTOM_H */

0 commit comments

Comments
 (0)