blob: 1ac4514198277c62a7a31f2a51c504d1f7aa3e07 [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.");
James Kuszmaulb740f452023-11-14 17:44:29 -080013DEFINE_string(version_string, "",
14 "Version to report for starterd and subprocesses.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070015
Austin Schuh09ec0072023-02-21 14:17:02 -080016DECLARE_string(shm_base);
17DEFINE_bool(purge_shm_base, false,
18 "If true, delete everything in --shm_base before starting.");
19
Tyler Chatowa79419d2020-08-12 20:12:11 -070020int main(int argc, char **argv) {
21 aos::InitGoogle(&argc, &argv);
Austin Schuh09ec0072023-02-21 14:17:02 -080022
23 if (FLAGS_purge_shm_base) {
24 aos::util::UnlinkRecursive(FLAGS_shm_base);
25 }
26
Austin Schuh529ac592021-10-14 16:11:13 -070027 if (!FLAGS_user.empty()) {
28 uid_t uid;
29 uid_t gid;
30 {
31 struct passwd *user_data = getpwnam(FLAGS_user.c_str());
32 if (user_data != nullptr) {
33 uid = user_data->pw_uid;
34 gid = user_data->pw_gid;
35 } else {
36 LOG(FATAL) << "Could not find user " << FLAGS_user;
37 return 1;
38 }
39 }
James Kuszmaul4ff50272022-01-07 18:31:13 -080040 // Change the real and effective IDs to the user we're running as. The
41 // effective IDs mean files we access (like shared memory) will happen as
42 // that user. The real IDs allow child processes with an different effective
43 // ID to still participate in signal sending/receiving.
Austin Schuh529ac592021-10-14 16:11:13 -070044 constexpr int kUnchanged = -1;
45 if (setresgid(/* ruid */ gid, /* euid */ gid,
46 /* suid */ kUnchanged) != 0) {
Philipp Schrader790cb542023-07-05 21:06:52 -070047 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user << ", group "
48 << gid;
Austin Schuh529ac592021-10-14 16:11:13 -070049 }
50
51 if (setresuid(/* ruid */ uid, /* euid */ uid,
52 /* suid */ kUnchanged) != 0) {
53 PLOG(FATAL) << "Failed to change UID to " << FLAGS_user;
54 }
55 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070056
57 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
58 aos::configuration::ReadConfig(FLAGS_config);
59
60 const aos::Configuration *config_msg = &config.message();
61
62 aos::starter::Starter starter(config_msg);
James Kuszmaulb740f452023-11-14 17:44:29 -080063 if (!FLAGS_version_string.empty()) {
64 starter.event_loop()->SetVersionString(FLAGS_version_string);
65 }
Tyler Chatowa79419d2020-08-12 20:12:11 -070066
67 starter.Run();
68
69 return 0;
70}