blob: a188127a0903c73773b6c49dd70a5598ac0bcde6 [file] [log] [blame]
Austin Schuh608514f2022-12-30 15:51:30 -08001#include <linux/securebits.h>
2#include <pwd.h>
3#include <sys/prctl.h>
Austin Schuha63163e2023-02-24 16:18:18 -08004#include <sys/resource.h>
Austin Schuh608514f2022-12-30 15:51:30 -08005#include <sys/types.h>
6
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "gflags/gflags.h"
8
Austin Schuh608514f2022-12-30 15:51:30 -08009#include "aos/events/shm_event_loop.h"
10#include "aos/init.h"
11#include "aos/starter/irq_affinity_lib.h"
12#include "aos/starter/kthread_generated.h"
13#include "aos/util/top.h"
Austin Schuh608514f2022-12-30 15:51:30 -080014
15DEFINE_string(config, "aos_config.json", "File path of aos configuration");
16
17DEFINE_string(user, "",
18 "Starter runs as though this user ran a SUID binary if set.");
Alexander Yeee61cac32023-02-11 19:40:40 -080019DEFINE_string(irq_config, "rockpi_config.json",
20 "File path of rockpi configuration");
Austin Schuh608514f2022-12-30 15:51:30 -080021
22namespace aos {
23
24cpu_set_t AffinityFromFlatbuffer(const flatbuffers::Vector<uint8_t> *v) {
25 cpu_set_t affinity;
26 CPU_ZERO(&affinity);
27 if (v == nullptr) {
28 for (int i = 0; i < CPU_SETSIZE; ++i) {
29 CPU_SET(i, &affinity);
30 }
31 } else {
32 for (uint8_t cpu : *v) {
33 CPU_SET(cpu, &affinity);
34 }
35 }
36 return affinity;
37}
38
39// Class to hold the configuration for an IRQ.
40struct ParsedIrqConfig {
41 std::string name;
42 cpu_set_t affinity;
43
44 void ConfigureIrq(int interrupt_number) const {
45 const std::string affinity_filename =
46 absl::StrCat("/proc/irq/", interrupt_number, "/smp_affinity");
47 const std::string contents = util::ReadFileToStringOrDie(affinity_filename);
48
49 std::string new_contents = std::string(contents.size() - 1, '0');
50
51 // Contents will be a padded string which is the size of the number of
52 // IRQs.
53 CHECK(!(CPU_SETSIZE & 0xf));
54 for (size_t i = 0; i < CPU_SETSIZE; i += 4) {
55 if (i / 4 >= new_contents.size()) {
56 break;
57 }
58 uint8_t byte = 0;
59 if (CPU_ISSET(i + 0, &affinity)) {
60 byte |= 1;
61 }
62 if (CPU_ISSET(i + 1, &affinity)) {
63 byte |= 2;
64 }
65 if (CPU_ISSET(i + 2, &affinity)) {
66 byte |= 4;
67 }
68 if (CPU_ISSET(i + 3, &affinity)) {
69 byte |= 8;
70 }
71 if (byte < 10) {
72 new_contents[new_contents.size() - 1 - i / 4] = '0' + byte;
73 } else {
74 new_contents[new_contents.size() - 1 - i / 4] = 'a' + (byte - 10);
75 }
76 }
77
78 if (contents != new_contents) {
79 util::WriteStringToFileOrDie(affinity_filename, new_contents);
80 }
81 }
82};
83
84// Class to hold the configuration for a kthread.
85struct ParsedKThreadConfig {
86 bool full_match = false;
87 std::string prefix;
88 std::string postfix;
89 starter::Scheduler scheduler;
90 int priority;
Austin Schuha63163e2023-02-24 16:18:18 -080091 std::optional<int> nice;
Austin Schuh608514f2022-12-30 15:51:30 -080092 cpu_set_t affinity;
93
94 bool Matches(std::string_view candidate) const {
95 if (full_match) {
96 return candidate == prefix;
97 } else {
98 if (candidate.size() < prefix.size() + postfix.size()) {
99 return false;
100 }
101 if (candidate.substr(0, prefix.size()) != prefix) {
102 return false;
103 }
104 if (candidate.substr(candidate.size() - postfix.size(), postfix.size()) !=
105 postfix) {
106 return false;
107 }
108 return true;
109 }
110 }
111
Austin Schuh19731b02024-03-02 16:53:19 -0800112 void ConfigurePid(pid_t pid, std::string_view name) const {
Austin Schuha63163e2023-02-24 16:18:18 -0800113 struct sched_param param;
114 param.sched_priority = priority;
115 int new_scheduler;
116 switch (scheduler) {
117 case starter::Scheduler::SCHEDULER_OTHER:
118 new_scheduler = SCHED_OTHER;
119 break;
120 case starter::Scheduler::SCHEDULER_RR:
121 new_scheduler = SCHED_RR;
122 break;
123 case starter::Scheduler::SCHEDULER_FIFO:
124 new_scheduler = SCHED_FIFO;
125 break;
126 default:
127 LOG(FATAL) << "Unknown scheduler";
Austin Schuh608514f2022-12-30 15:51:30 -0800128 }
Austin Schuh19731b02024-03-02 16:53:19 -0800129 PCHECK(sched_setscheduler(pid, new_scheduler, &param) == 0)
130 << ", Failed to set " << name << "(" << pid << ") to "
131 << (new_scheduler == SCHED_OTHER
132 ? "SCHED_OTHER"
133 : (new_scheduler == SCHED_RR ? "SCHED_RR" : "SCHED_FIFO"));
Austin Schuha63163e2023-02-24 16:18:18 -0800134
135 if (scheduler == starter::Scheduler::SCHEDULER_OTHER && nice.has_value()) {
136 PCHECK(setpriority(PRIO_PROCESS, pid, *nice) == 0)
137 << ": Failed to set priority";
138 }
139
Austin Schuh608514f2022-12-30 15:51:30 -0800140 PCHECK(sched_setaffinity(pid, sizeof(affinity), &affinity) == 0);
141 }
142};
143
144// TODO(austin): Clean this up a bit, and maybe we can add some tests.
145class IrqAffinity {
146 public:
147 IrqAffinity(
148 EventLoop *event_loop,
149 const aos::FlatbufferDetachedBuffer<aos::starter::IrqAffinityConfig>
150 &irq_affinity_config)
151 : top_(event_loop) {
152 if (irq_affinity_config.message().has_kthreads()) {
Austin Schuh4ec20622023-03-11 15:11:50 -0800153 PopulateThreads(irq_affinity_config.message().kthreads(), &kthreads_);
154 }
155 if (irq_affinity_config.message().has_threads()) {
156 PopulateThreads(irq_affinity_config.message().threads(), &threads_);
Austin Schuh608514f2022-12-30 15:51:30 -0800157 }
158
159 if (irq_affinity_config.message().has_irqs()) {
160 irqs_.reserve(irq_affinity_config.message().irqs()->size());
161 for (const starter::IrqConfig *irq_config :
162 *irq_affinity_config.message().irqs()) {
163 CHECK(irq_config->has_name()) << ": Name required";
164 LOG(INFO) << "IRQ " << aos::FlatbufferToJson(irq_config);
165 irqs_.push_back(ParsedIrqConfig{
166 .name = irq_config->name()->str(),
167 .affinity = AffinityFromFlatbuffer(irq_config->affinity()),
168 });
169 }
170 }
171
172 top_.set_track_top_processes(true);
173 top_.set_on_reading_update([this]() {
174 for (const std::pair<const pid_t, util::Top::ProcessReadings> &reading :
175 top_.readings()) {
176 if (reading.second.kthread) {
177 for (const ParsedKThreadConfig &match : kthreads_) {
178 if (match.Matches(reading.second.name)) {
Austin Schuh19731b02024-03-02 16:53:19 -0800179 match.ConfigurePid(reading.first, reading.second.name);
Austin Schuh608514f2022-12-30 15:51:30 -0800180 break;
181 }
182 }
Austin Schuh4ec20622023-03-11 15:11:50 -0800183 } else {
184 for (const ParsedKThreadConfig &match : threads_) {
185 if (match.Matches(reading.second.name)) {
Austin Schuh19731b02024-03-02 16:53:19 -0800186 match.ConfigurePid(reading.first, reading.second.name);
Austin Schuh4ec20622023-03-11 15:11:50 -0800187 break;
188 }
189 }
Austin Schuh608514f2022-12-30 15:51:30 -0800190 }
191 }
192
193 interrupts_status_.Update();
194
195 for (const InterruptsStatus::InterruptState &state :
196 interrupts_status_.states()) {
197 for (const ParsedIrqConfig &match : irqs_) {
198 bool matched = false;
199 for (const std::string &action : state.actions) {
200 if (match.name == action) {
201 matched = true;
202 break;
203 }
204 }
205 if (matched) {
206 match.ConfigureIrq(state.interrupt_number);
207 }
208 }
209 }
210 });
211 }
212
213 private:
Austin Schuh4ec20622023-03-11 15:11:50 -0800214 void PopulateThreads(
215 const flatbuffers::Vector<flatbuffers::Offset<starter::KthreadConfig>>
216 *threads_config,
217 std::vector<ParsedKThreadConfig> *threads) {
218 threads->reserve(threads_config->size());
219 for (const starter::KthreadConfig *kthread_config : *threads_config) {
220 LOG(INFO) << "Kthread " << aos::FlatbufferToJson(kthread_config);
221 CHECK(kthread_config->has_name()) << ": Name required";
222 const size_t star_position =
223 kthread_config->name()->string_view().find('*');
224 const bool has_star = star_position != std::string_view::npos;
225
226 threads->push_back(ParsedKThreadConfig{
227 .full_match = !has_star,
228 .prefix = std::string(
229 !has_star ? kthread_config->name()->string_view()
230 : kthread_config->name()->string_view().substr(
231 0, star_position)),
232 .postfix = std::string(
233 !has_star ? ""
234 : kthread_config->name()->string_view().substr(
235 star_position + 1)),
236 .scheduler = kthread_config->scheduler(),
237 .priority = kthread_config->priority(),
238 .nice = kthread_config->nice(),
239 .affinity = AffinityFromFlatbuffer(kthread_config->affinity()),
240 });
241 }
242 }
243
Austin Schuh608514f2022-12-30 15:51:30 -0800244 util::Top top_;
245
246 // TODO(austin): Publish message with everything in it.
247 // TODO(austin): Make Top report out affinity + priority + scheduler for
248 // posterity.
249
250 std::vector<ParsedKThreadConfig> kthreads_;
Austin Schuh4ec20622023-03-11 15:11:50 -0800251 std::vector<ParsedKThreadConfig> threads_;
Austin Schuh608514f2022-12-30 15:51:30 -0800252 std::vector<ParsedIrqConfig> irqs_;
253
254 InterruptsStatus interrupts_status_;
255};
256
257} // namespace aos
258
259int main(int argc, char **argv) {
260 aos::InitGoogle(&argc, &argv);
261
262 if (!FLAGS_user.empty()) {
263 // Maintain root permissions as we switch to become the user so we can
264 // actually manipulate priorities.
265 PCHECK(prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP | SECBIT_NOROOT) ==
266 0);
267
268 uid_t uid;
269 uid_t gid;
270 {
271 struct passwd *user_data = getpwnam(FLAGS_user.c_str());
272 if (user_data != nullptr) {
273 uid = user_data->pw_uid;
274 gid = user_data->pw_gid;
275 } else {
276 LOG(FATAL) << "Could not find user " << FLAGS_user;
277 return 1;
278 }
279 }
280 // Change the real and effective IDs to the user we're running as. The
281 // effective IDs mean files we access (like shared memory) will happen as
282 // that user. The real IDs allow child processes with an different effective
283 // ID to still participate in signal sending/receiving.
284 constexpr int kUnchanged = -1;
285 if (setresgid(/* ruid */ gid, /* euid */ gid,
286 /* suid */ kUnchanged) != 0) {
287 PLOG(FATAL) << "Failed to change GID to " << FLAGS_user;
288 }
289
290 if (setresuid(/* ruid */ uid, /* euid */ uid,
291 /* suid */ kUnchanged) != 0) {
292 PLOG(FATAL) << "Failed to change UID to " << FLAGS_user;
293 }
294 }
295
296 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
297 aos::configuration::ReadConfig(FLAGS_config);
298
Austin Schuh608514f2022-12-30 15:51:30 -0800299 aos::FlatbufferDetachedBuffer<aos::starter::IrqAffinityConfig>
300 irq_affinity_config =
Alexander Yeee61cac32023-02-11 19:40:40 -0800301 aos::JsonFileToFlatbuffer<aos::starter::IrqAffinityConfig>(
302 FLAGS_irq_config);
Austin Schuh608514f2022-12-30 15:51:30 -0800303
304 aos::ShmEventLoop shm_event_loop(&config.message());
305
306 aos::IrqAffinity irq_affinity(&shm_event_loop, irq_affinity_config);
307
308 shm_event_loop.Run();
309
310 return 0;
311}