ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
core.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2
3#include "core.h"
4
6#include "preferences.h"
7
8#include <climits>
9#include <csignal>
10#include <cstdlib>
11#include <string>
12
13#ifdef __APPLE__
14#include <mach-o/dyld.h>
15#endif
16
17#ifdef __linux__
18#include <unistd.h>
19#endif
20
21namespace {
22volatile sig_atomic_t s_signal_received = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
23void signal_handler(int signal) { s_signal_received = signal; }
24
25char **s_argv = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
26std::string *s_exe_path = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
27std::string *s_reexec_path = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
28
29std::string resolve_exe_path(const char *argv0) {
30#ifdef __linux__
31 char buf[PATH_MAX];
32 ssize_t len = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
33 if (len > 0) {
34 buf[len] = '\0';
35 return std::string(buf);
36 }
37#endif
38#ifdef __APPLE__
39 char buf[PATH_MAX];
40 uint32_t size = sizeof(buf);
41 if (_NSGetExecutablePath(buf, &size) == 0) {
42 char real[PATH_MAX];
43 if (::realpath(buf, real) != nullptr)
44 return std::string(real);
45 return std::string(buf);
46 }
47#endif
48 if (argv0 == nullptr)
49 return {};
50 char real[PATH_MAX];
51 if (::realpath(argv0, real) != nullptr)
52 return std::string(real);
53 return std::string(argv0);
54}
55} // namespace
56
57namespace esphome::host {
58
59char **get_argv() { return s_argv; }
60
61const std::string &get_exe_path() {
62 static const std::string empty;
63 return s_exe_path != nullptr ? *s_exe_path : empty;
64}
65
66void arm_reexec(const std::string &path) {
67 if (s_reexec_path != nullptr)
68 *s_reexec_path = path;
69}
70
71const char *get_reexec_path() {
72 if (s_reexec_path == nullptr || s_reexec_path->empty())
73 return nullptr;
74 return s_reexec_path->c_str();
75}
76
77} // namespace esphome::host
78
79// HAL functions live in hal.cpp.
80
81void setup();
82void loop();
83int main(int argc, char **argv) {
84 s_argv = argv;
85 static std::string exe_path = resolve_exe_path(argc > 0 ? argv[0] : nullptr);
86 s_exe_path = &exe_path;
87 static std::string reexec_path;
88 s_reexec_path = &reexec_path;
89
90 // Install signal handlers for graceful shutdown (flushes preferences to disk)
91 std::signal(SIGINT, signal_handler);
92 std::signal(SIGTERM, signal_handler);
93
95 setup();
96 while (s_signal_received == 0) {
97 loop();
98 }
100 return 0;
101}
102
103#endif // USE_HOST
void setup()
void loop()
__int64 ssize_t
Definition httplib.h:178
void setup_preferences()
const char * get_reexec_path()
Armed re-exec path, or nullptr.
Definition core.cpp:71
const std::string & get_exe_path()
Absolute path to running exe (resolved at startup); empty on failure.
Definition core.cpp:61
char ** get_argv()
argv captured by main(); stable for process lifetime.
Definition core.cpp:59
void arm_reexec(const std::string &path)
Arm an execv on the next arch_restart(). Pass empty to disarm.
Definition core.cpp:66
uint16_t size
Definition helpers.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint32_t len
int main()
Definition core.cpp:48