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