blob: e7ded5913839f1a60567dd8997709d13441417d1 [file] [log] [blame]
Tyler Chatowa79419d2020-08-12 20:12:11 -07001#ifndef AOS_STARTER_STARTERD_LIB_H_
2#define AOS_STARTER_STARTERD_LIB_H_
3
Tyler Chatowa79419d2020-08-12 20:12:11 -07004#include <sys/signalfd.h>
5#include <sys/wait.h>
6
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <csignal>
8#include <cstdio>
Tyler Chatowa79419d2020-08-12 20:12:11 -07009#include <string>
10#include <unordered_map>
11#include <vector>
12
13#include "aos/configuration.h"
14#include "aos/events/shm_event_loop.h"
15#include "aos/ipc_lib/signalfd.h"
16#include "aos/macros.h"
17#include "aos/starter/starter_generated.h"
18#include "aos/starter/starter_rpc_generated.h"
James Kuszmaul3224b8e2022-01-07 19:00:39 -080019#include "aos/starter/subprocess.h"
James Kuszmaul6295a642022-03-22 15:23:59 -070020#include "aos/util/top.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070021
22namespace aos {
23namespace starter {
24
James Kuszmaul293b2172021-11-10 16:20:48 -080025const aos::Channel *StatusChannelForNode(const aos::Configuration *config,
26 const aos::Node *node);
27const aos::Channel *StarterRpcChannelForNode(const aos::Configuration *config,
28 const aos::Node *node);
29
Tyler Chatowa79419d2020-08-12 20:12:11 -070030class Starter {
31 public:
32 Starter(const aos::Configuration *event_loop_config);
33
34 // Inserts a new application from config. Returns the inserted application if
35 // it was successful, otherwise nullptr if an application already exists
36 // with the given name.
37 Application *AddApplication(const aos::Application *application);
38
39 // Runs the event loop and starts all applications
40 void Run();
41
42 void Cleanup();
43
44 private:
45 // Signals which indicate starter has died
46 static const inline std::vector<int> kStarterDeath = {
47 SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE,
48 SIGSEGV, SIGPIPE, SIGTERM, SIGBUS, SIGXCPU};
49
50 void OnSignal(signalfd_siginfo signal);
James Kuszmaul293b2172021-11-10 16:20:48 -080051 void HandleStarterRpc(const StarterRpc &command);
Tyler Chatowa79419d2020-08-12 20:12:11 -070052
James Kuszmaul6295a642022-03-22 15:23:59 -070053 // Handles any potential state change in the child applications.
54 // In particular, sends the Status message if it wouldn't exceed the rate
55 // limit.
56 void HandleStateChange();
Austin Schuhfc304942021-10-16 14:20:05 -070057
Tyler Chatowa79419d2020-08-12 20:12:11 -070058 void SendStatus();
59
60 const std::string config_path_;
61 const aos::Configuration *config_msg_;
62
63 aos::ShmEventLoop event_loop_;
64 aos::Sender<aos::starter::Status> status_sender_;
65 aos::TimerHandler *status_timer_;
66 aos::TimerHandler *cleanup_timer_;
67
Austin Schuhfc304942021-10-16 14:20:05 -070068 int status_count_ = 0;
69 const int max_status_count_;
70
Tyler Chatowa79419d2020-08-12 20:12:11 -070071 std::unordered_map<std::string, Application> applications_;
72
73 // Set to true on cleanup to block rpc commands and ensure cleanup only
74 // happens once.
75 bool exiting_ = false;
76
77 SignalListener listener_;
78
James Kuszmaul6295a642022-03-22 15:23:59 -070079 util::Top top_;
80
Tyler Chatowa79419d2020-08-12 20:12:11 -070081 DISALLOW_COPY_AND_ASSIGN(Starter);
82};
83
84} // namespace starter
85} // namespace aos
86
87#endif // AOS_STARTER_STARTERD_LIB_H_