blob: b40776d826a8afe995d0eb799d5abf89dabe5414 [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 }
27 constexpr int kUnchanged = -1;
28 if (setresgid(/* ruid */ gid, /* euid */ gid,
29 /* suid */ kUnchanged) != 0) {
30 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user;
31 }
32
33 if (setresuid(/* ruid */ uid, /* euid */ uid,
34 /* suid */ kUnchanged) != 0) {
35 PLOG(FATAL) << "Failed to change UID to " << FLAGS_user;
36 }
37 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070038
39 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
40 aos::configuration::ReadConfig(FLAGS_config);
41
42 const aos::Configuration *config_msg = &config.message();
43
44 aos::starter::Starter starter(config_msg);
45
46 starter.Run();
47
48 return 0;
49}