blob: d6caf3f3b65b4d50b341d1865981f8d5cbb578a9 [file] [log] [blame]
Austin Schuh529ac592021-10-14 16:11:13 -07001#include <pwd.h>
2#include <sys/types.h>
3
Philipp Schrader790cb542023-07-05 21:06:52 -07004#include "gflags/gflags.h"
5
Tyler Chatowa79419d2020-08-12 20:12:11 -07006#include "aos/init.h"
Austin Schuh09ec0072023-02-21 14:17:02 -08007#include "aos/starter/starterd_lib.h"
8#include "aos/util/file.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -07009
Austin Schuh8e2dfc62022-08-17 16:36:00 -070010DEFINE_string(config, "aos_config.json", "File path of aos configuration");
Austin Schuh529ac592021-10-14 16:11:13 -070011DEFINE_string(user, "",
12 "Starter runs as though this user ran a SUID binary if set.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070013
Austin Schuh09ec0072023-02-21 14:17:02 -080014DECLARE_string(shm_base);
15DEFINE_bool(purge_shm_base, false,
16 "If true, delete everything in --shm_base before starting.");
17
Tyler Chatowa79419d2020-08-12 20:12:11 -070018int main(int argc, char **argv) {
19 aos::InitGoogle(&argc, &argv);
Austin Schuh09ec0072023-02-21 14:17:02 -080020
21 if (FLAGS_purge_shm_base) {
22 aos::util::UnlinkRecursive(FLAGS_shm_base);
23 }
24
Austin Schuh529ac592021-10-14 16:11:13 -070025 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 Kuszmaul4ff50272022-01-07 18:31:13 -080038 // 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 Schuh529ac592021-10-14 16:11:13 -070042 constexpr int kUnchanged = -1;
43 if (setresgid(/* ruid */ gid, /* euid */ gid,
44 /* suid */ kUnchanged) != 0) {
Philipp Schrader790cb542023-07-05 21:06:52 -070045 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user << ", group "
46 << gid;
Austin Schuh529ac592021-10-14 16:11:13 -070047 }
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 Chatowa79419d2020-08-12 20:12:11 -070054
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}