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