John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/init.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | #include <sched.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <sys/mman.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | #include <sys/resource.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | |
| 9 | #include <cerrno> |
| 10 | #include <cstdio> |
| 11 | #include <cstdlib> |
| 12 | #include <cstring> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 14 | #include "gflags/gflags.h" |
| 15 | #include "glog/logging.h" |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 16 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 17 | #include "aos/realtime.h" |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 18 | #include "aos/uuid.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 19 | |
Austin Schuh | 707c835 | 2020-03-15 14:27:25 -0700 | [diff] [blame] | 20 | DEFINE_bool(coredump, false, "If true, write core dumps on failure."); |
| 21 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | namespace { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 24 | bool initialized = false; |
Austin Schuh | 3d4d5df | 2015-10-17 15:51:41 -0700 | [diff] [blame] | 25 | } // namespace |
| 26 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 27 | bool IsInitialized() { return initialized; } |
| 28 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | void InitGoogle(int *argc, char ***argv) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 30 | CHECK(!IsInitialized()) << "Only initialize once."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | FLAGS_logtostderr = true; |
| 32 | google::InitGoogleLogging((*argv)[0]); |
| 33 | gflags::ParseCommandLineFlags(argc, argv, true); |
| 34 | google::InstallFailureSignalHandler(); |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 36 | if (FLAGS_coredump) { |
| 37 | WriteCoreDumps(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 40 | RegisterMallocHook(); |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 41 | // Ensure that the random number generator for the UUID code is initialized |
| 42 | // (it does some potentially expensive random number generation). |
| 43 | UUID::Random(); |
| 44 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 45 | initialized = true; |
Brian Silverman | e4d8b28 | 2015-12-24 13:44:48 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Brian Silverman | e4c79ce | 2022-08-15 05:57:28 -0700 | [diff] [blame] | 48 | void InitFromRust(const char *argv0) { |
| 49 | CHECK(!IsInitialized()) << "Only initialize once."; |
| 50 | |
| 51 | FLAGS_logtostderr = true; |
| 52 | |
| 53 | google::InitGoogleLogging(argv0); |
| 54 | |
| 55 | // TODO(Brian): Provide a way for Rust to configure C++ flags. |
| 56 | char fake_argv0_val[] = "rust"; |
| 57 | char *fake_argv0 = fake_argv0_val; |
| 58 | char **fake_argv = &fake_argv0; |
| 59 | int fake_argc = 1; |
| 60 | gflags::ParseCommandLineFlags(&fake_argc, &fake_argv, true); |
| 61 | |
| 62 | // TODO(Brian): Where should Rust binaries be configured to write coredumps? |
| 63 | |
| 64 | // TODO(Brian): Figure out what to do with allocator hooks for C++ and Rust. |
| 65 | |
| 66 | initialized = true; |
| 67 | } |
| 68 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 69 | } // namespace aos |