blob: 127bf41a51064d20bae40b9c650f5ff4cece3e29 [file] [log] [blame]
Austin Schuh529ac592021-10-14 16:11:13 -07001#include <pwd.h>
Stephan Pleinesf581a072024-05-23 20:59:27 -07002#include <unistd.h>
3
4#include <ostream>
5#include <string>
Austin Schuh529ac592021-10-14 16:11:13 -07006
Austin Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/flags/flag.h"
8#include "absl/log/check.h"
9#include "absl/log/log.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070010
Stephan Pleinesf581a072024-05-23 20:59:27 -070011#include "aos/configuration.h"
12#include "aos/events/event_loop.h"
13#include "aos/flatbuffers.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070014#include "aos/init.h"
Austin Schuh09ec0072023-02-21 14:17:02 -080015#include "aos/starter/starterd_lib.h"
16#include "aos/util/file.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070017
Austin Schuh99f7c6a2024-06-25 22:07:44 -070018ABSL_FLAG(std::string, config, "aos_config.json",
19 "File path of aos configuration");
20ABSL_FLAG(std::string, user, "",
21 "Starter runs as though this user ran a SUID binary if set.");
22ABSL_FLAG(std::string, version_string, "",
23 "Version to report for starterd and subprocesses.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070024
Austin Schuh99f7c6a2024-06-25 22:07:44 -070025ABSL_DECLARE_FLAG(std::string, shm_base);
26ABSL_FLAG(bool, purge_shm_base, false,
27 "If true, delete everything in --shm_base before starting.");
Austin Schuh09ec0072023-02-21 14:17:02 -080028
Tyler Chatowa79419d2020-08-12 20:12:11 -070029int main(int argc, char **argv) {
30 aos::InitGoogle(&argc, &argv);
Austin Schuh09ec0072023-02-21 14:17:02 -080031
Austin Schuh99f7c6a2024-06-25 22:07:44 -070032 if (absl::GetFlag(FLAGS_purge_shm_base)) {
33 aos::util::UnlinkRecursive(absl::GetFlag(FLAGS_shm_base));
Austin Schuh09ec0072023-02-21 14:17:02 -080034 }
35
Austin Schuh99f7c6a2024-06-25 22:07:44 -070036 if (!absl::GetFlag(FLAGS_user).empty()) {
Austin Schuh529ac592021-10-14 16:11:13 -070037 uid_t uid;
38 uid_t gid;
39 {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070040 struct passwd *user_data = getpwnam(absl::GetFlag(FLAGS_user).c_str());
Austin Schuh529ac592021-10-14 16:11:13 -070041 if (user_data != nullptr) {
42 uid = user_data->pw_uid;
43 gid = user_data->pw_gid;
44 } else {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070045 LOG(FATAL) << "Could not find user " << absl::GetFlag(FLAGS_user);
Austin Schuh529ac592021-10-14 16:11:13 -070046 return 1;
47 }
48 }
James Kuszmaul4ff50272022-01-07 18:31:13 -080049 // 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 Schuh529ac592021-10-14 16:11:13 -070053 constexpr int kUnchanged = -1;
54 if (setresgid(/* ruid */ gid, /* euid */ gid,
55 /* suid */ kUnchanged) != 0) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070056 PLOG(FATAL) << "Failed to change GID to " << absl::GetFlag(FLAGS_user)
57 << ", group " << gid;
Austin Schuh529ac592021-10-14 16:11:13 -070058 }
59
60 if (setresuid(/* ruid */ uid, /* euid */ uid,
61 /* suid */ kUnchanged) != 0) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070062 PLOG(FATAL) << "Failed to change UID to " << absl::GetFlag(FLAGS_user);
Austin Schuh529ac592021-10-14 16:11:13 -070063 }
64 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070065
66 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuh99f7c6a2024-06-25 22:07:44 -070067 aos::configuration::ReadConfig(absl::GetFlag(FLAGS_config));
Tyler Chatowa79419d2020-08-12 20:12:11 -070068
69 const aos::Configuration *config_msg = &config.message();
70
71 aos::starter::Starter starter(config_msg);
Austin Schuh99f7c6a2024-06-25 22:07:44 -070072 if (!absl::GetFlag(FLAGS_version_string).empty()) {
73 starter.event_loop()->SetVersionString(absl::GetFlag(FLAGS_version_string));
James Kuszmaulb740f452023-11-14 17:44:29 -080074 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070075
76 starter.Run();
77
78 return 0;
79}