blob: 19117de838250410ea4f62d2df38294ac1027854 [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"
Austin Schuh09ec0072023-02-21 14:17:02 -08005#include "aos/starter/starterd_lib.h"
6#include "aos/util/file.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -07007#include "gflags/gflags.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -07008
Austin Schuh8e2dfc62022-08-17 16:36:00 -07009DEFINE_string(config, "aos_config.json", "File path of aos configuration");
Austin Schuh529ac592021-10-14 16:11:13 -070010DEFINE_string(user, "",
11 "Starter runs as though this user ran a SUID binary if set.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070012
Austin Schuh09ec0072023-02-21 14:17:02 -080013DECLARE_string(shm_base);
14DEFINE_bool(purge_shm_base, false,
15 "If true, delete everything in --shm_base before starting.");
16
Tyler Chatowa79419d2020-08-12 20:12:11 -070017int main(int argc, char **argv) {
18 aos::InitGoogle(&argc, &argv);
Austin Schuh09ec0072023-02-21 14:17:02 -080019
20 if (FLAGS_purge_shm_base) {
21 aos::util::UnlinkRecursive(FLAGS_shm_base);
22 }
23
Austin Schuh529ac592021-10-14 16:11:13 -070024 if (!FLAGS_user.empty()) {
25 uid_t uid;
26 uid_t gid;
27 {
28 struct passwd *user_data = getpwnam(FLAGS_user.c_str());
29 if (user_data != nullptr) {
30 uid = user_data->pw_uid;
31 gid = user_data->pw_gid;
32 } else {
33 LOG(FATAL) << "Could not find user " << FLAGS_user;
34 return 1;
35 }
36 }
James Kuszmaul4ff50272022-01-07 18:31:13 -080037 // Change the real and effective IDs to the user we're running as. The
38 // effective IDs mean files we access (like shared memory) will happen as
39 // that user. The real IDs allow child processes with an different effective
40 // ID to still participate in signal sending/receiving.
Austin Schuh529ac592021-10-14 16:11:13 -070041 constexpr int kUnchanged = -1;
42 if (setresgid(/* ruid */ gid, /* euid */ gid,
43 /* suid */ kUnchanged) != 0) {
Austin Schuh3e1d3b62023-01-08 13:52:31 -080044 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user << ", group " << gid;
Austin Schuh529ac592021-10-14 16:11:13 -070045 }
46
47 if (setresuid(/* ruid */ uid, /* euid */ uid,
48 /* suid */ kUnchanged) != 0) {
49 PLOG(FATAL) << "Failed to change UID to " << FLAGS_user;
50 }
51 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070052
53 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
54 aos::configuration::ReadConfig(FLAGS_config);
55
56 const aos::Configuration *config_msg = &config.message();
57
58 aos::starter::Starter starter(config_msg);
59
60 starter.Run();
61
62 return 0;
63}