blob: 64858d4b844fbb1a7f4da8a8ead593d669bccff4 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "gflags/gflags.h"
Stephan Pleinesf581a072024-05-23 20:59:27 -07008#include "glog/logging.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07009
Stephan Pleinesf581a072024-05-23 20:59:27 -070010#include "aos/configuration.h"
11#include "aos/events/event_loop.h"
12#include "aos/flatbuffers.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070013#include "aos/init.h"
Austin Schuh09ec0072023-02-21 14:17:02 -080014#include "aos/starter/starterd_lib.h"
15#include "aos/util/file.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070016
Austin Schuh8e2dfc62022-08-17 16:36:00 -070017DEFINE_string(config, "aos_config.json", "File path of aos configuration");
Austin Schuh529ac592021-10-14 16:11:13 -070018DEFINE_string(user, "",
19 "Starter runs as though this user ran a SUID binary if set.");
James Kuszmaulb740f452023-11-14 17:44:29 -080020DEFINE_string(version_string, "",
21 "Version to report for starterd and subprocesses.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070022
Austin Schuh09ec0072023-02-21 14:17:02 -080023DECLARE_string(shm_base);
24DEFINE_bool(purge_shm_base, false,
25 "If true, delete everything in --shm_base before starting.");
26
Tyler Chatowa79419d2020-08-12 20:12:11 -070027int main(int argc, char **argv) {
28 aos::InitGoogle(&argc, &argv);
Austin Schuh09ec0072023-02-21 14:17:02 -080029
30 if (FLAGS_purge_shm_base) {
31 aos::util::UnlinkRecursive(FLAGS_shm_base);
32 }
33
Austin Schuh529ac592021-10-14 16:11:13 -070034 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 Kuszmaul4ff50272022-01-07 18:31:13 -080047 // 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 Schuh529ac592021-10-14 16:11:13 -070051 constexpr int kUnchanged = -1;
52 if (setresgid(/* ruid */ gid, /* euid */ gid,
53 /* suid */ kUnchanged) != 0) {
Philipp Schrader790cb542023-07-05 21:06:52 -070054 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user << ", group "
55 << gid;
Austin Schuh529ac592021-10-14 16:11:13 -070056 }
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 Chatowa79419d2020-08-12 20:12:11 -070063
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 Kuszmaulb740f452023-11-14 17:44:29 -080070 if (!FLAGS_version_string.empty()) {
71 starter.event_loop()->SetVersionString(FLAGS_version_string);
72 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070073
74 starter.Run();
75
76 return 0;
77}