Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 1 | #include <pwd.h> |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | |
| 4 | #include <ostream> |
| 5 | #include <string> |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 7 | #include "absl/flags/flag.h" |
| 8 | #include "absl/log/check.h" |
| 9 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 11 | #include "aos/configuration.h" |
| 12 | #include "aos/events/event_loop.h" |
| 13 | #include "aos/flatbuffers.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 14 | #include "aos/init.h" |
Austin Schuh | 09ec007 | 2023-02-21 14:17:02 -0800 | [diff] [blame] | 15 | #include "aos/starter/starterd_lib.h" |
| 16 | #include "aos/util/file.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 17 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 18 | ABSL_FLAG(std::string, config, "aos_config.json", |
| 19 | "File path of aos configuration"); |
| 20 | ABSL_FLAG(std::string, user, "", |
| 21 | "Starter runs as though this user ran a SUID binary if set."); |
| 22 | ABSL_FLAG(std::string, version_string, "", |
| 23 | "Version to report for starterd and subprocesses."); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 24 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 25 | ABSL_DECLARE_FLAG(std::string, shm_base); |
| 26 | ABSL_FLAG(bool, purge_shm_base, false, |
| 27 | "If true, delete everything in --shm_base before starting."); |
Austin Schuh | 09ec007 | 2023-02-21 14:17:02 -0800 | [diff] [blame] | 28 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 29 | int main(int argc, char **argv) { |
| 30 | aos::InitGoogle(&argc, &argv); |
Austin Schuh | 09ec007 | 2023-02-21 14:17:02 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 32 | if (absl::GetFlag(FLAGS_purge_shm_base)) { |
| 33 | aos::util::UnlinkRecursive(absl::GetFlag(FLAGS_shm_base)); |
Austin Schuh | 09ec007 | 2023-02-21 14:17:02 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 36 | if (!absl::GetFlag(FLAGS_user).empty()) { |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 37 | uid_t uid; |
| 38 | uid_t gid; |
| 39 | { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 40 | struct passwd *user_data = getpwnam(absl::GetFlag(FLAGS_user).c_str()); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 41 | if (user_data != nullptr) { |
| 42 | uid = user_data->pw_uid; |
| 43 | gid = user_data->pw_gid; |
| 44 | } else { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 45 | LOG(FATAL) << "Could not find user " << absl::GetFlag(FLAGS_user); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | } |
James Kuszmaul | 4ff5027 | 2022-01-07 18:31:13 -0800 | [diff] [blame] | 49 | // Change the real and effective IDs to the user we're running as. The |
| 50 | // effective IDs mean files we access (like shared memory) will happen as |
| 51 | // that user. The real IDs allow child processes with an different effective |
| 52 | // ID to still participate in signal sending/receiving. |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 53 | constexpr int kUnchanged = -1; |
| 54 | if (setresgid(/* ruid */ gid, /* euid */ gid, |
| 55 | /* suid */ kUnchanged) != 0) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 56 | PLOG(FATAL) << "Failed to change GID to " << absl::GetFlag(FLAGS_user) |
| 57 | << ", group " << gid; |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (setresuid(/* ruid */ uid, /* euid */ uid, |
| 61 | /* suid */ kUnchanged) != 0) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 62 | PLOG(FATAL) << "Failed to change UID to " << absl::GetFlag(FLAGS_user); |
Austin Schuh | 529ac59 | 2021-10-14 16:11:13 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 65 | |
| 66 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 67 | aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 68 | |
| 69 | const aos::Configuration *config_msg = &config.message(); |
| 70 | |
| 71 | aos::starter::Starter starter(config_msg); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 72 | if (!absl::GetFlag(FLAGS_version_string).empty()) { |
| 73 | starter.event_loop()->SetVersionString(absl::GetFlag(FLAGS_version_string)); |
James Kuszmaul | b740f45 | 2023-11-14 17:44:29 -0800 | [diff] [blame] | 74 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 75 | |
| 76 | starter.Run(); |
| 77 | |
| 78 | return 0; |
| 79 | } |