blob: a5a340ad3a36c430687cba75224bcbecd368c142 [file] [log] [blame]
Austin Schuh529ac592021-10-14 16:11:13 -07001#include <pwd.h>
2#include <sys/types.h>
3
Tyler Chatowa79419d2020-08-12 20:12:11 -07004#include "aos/init.h"
5#include "gflags/gflags.h"
6#include "starterd_lib.h"
7
8DEFINE_string(config, "./config.json", "File path of aos configuration");
Austin Schuh529ac592021-10-14 16:11:13 -07009DEFINE_string(user, "",
10 "Starter runs as though this user ran a SUID binary if set.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070011
12int main(int argc, char **argv) {
13 aos::InitGoogle(&argc, &argv);
Austin Schuh529ac592021-10-14 16:11:13 -070014 if (!FLAGS_user.empty()) {
15 uid_t uid;
16 uid_t gid;
17 {
18 struct passwd *user_data = getpwnam(FLAGS_user.c_str());
19 if (user_data != nullptr) {
20 uid = user_data->pw_uid;
21 gid = user_data->pw_gid;
22 } else {
23 LOG(FATAL) << "Could not find user " << FLAGS_user;
24 return 1;
25 }
26 }
James Kuszmaul4ff50272022-01-07 18:31:13 -080027 // Change the real and effective IDs to the user we're running as. The
28 // effective IDs mean files we access (like shared memory) will happen as
29 // that user. The real IDs allow child processes with an different effective
30 // ID to still participate in signal sending/receiving.
Austin Schuh529ac592021-10-14 16:11:13 -070031 constexpr int kUnchanged = -1;
32 if (setresgid(/* ruid */ gid, /* euid */ gid,
33 /* suid */ kUnchanged) != 0) {
34 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user;
35 }
36
37 if (setresuid(/* ruid */ uid, /* euid */ uid,
38 /* suid */ kUnchanged) != 0) {
39 PLOG(FATAL) << "Failed to change UID to " << FLAGS_user;
40 }
41 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070042
43 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
44 aos::configuration::ReadConfig(FLAGS_config);
45
46 const aos::Configuration *config_msg = &config.message();
47
48 aos::starter::Starter starter(config_msg);
49
50 starter.Run();
51
52 return 0;
53}