blob: 57d90dd11dab49bb4965982a252902027ae3df42 [file] [log] [blame]
Brian Silvermanaf784862014-05-13 08:14:55 -07001// This has to come before anybody drags in <stdlib.h> or else we end up with
2// the wrong version of WIFEXITED etc (for one thing, they don't const-qualify
3// their casts) (sometimes at least).
4#include <sys/wait.h>
5
Brian Silvermand169fcd2013-02-27 13:18:47 -08006#include <stdio.h>
7#include <stdlib.h>
8#include <sys/types.h>
9#include <fcntl.h>
10#include <sys/inotify.h>
11#include <sys/stat.h>
12#include <sys/ioctl.h>
Brian Silvermand169fcd2013-02-27 13:18:47 -080013#include <signal.h>
14#include <stdint.h>
15#include <errno.h>
16#include <string.h>
Brian Silvermand90b5fe2013-03-10 18:34:42 -070017#include <inttypes.h>
Brian Silvermand169fcd2013-02-27 13:18:47 -080018
19#include <map>
20#include <functional>
21#include <deque>
22#include <fstream>
23#include <queue>
24#include <list>
25#include <string>
26#include <vector>
27#include <memory>
Brian Silvermand94642c2014-03-27 18:21:41 -070028#include <set>
Brian Silvermand169fcd2013-02-27 13:18:47 -080029
Brian Silverman258b9172015-09-19 14:32:57 -040030#include "third_party/libevent/event.h"
Brian Silvermand169fcd2013-02-27 13:18:47 -080031
32#include "aos/common/logging/logging.h"
Brian Silvermancb5da1f2015-12-05 22:19:58 -050033#include "aos/common/logging/implementations.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080034#include "aos/linux_code/init.h"
Brian Silvermand169fcd2013-02-27 13:18:47 -080035#include "aos/common/unique_malloc_ptr.h"
36#include "aos/common/time.h"
Brian Silverman5cc661b2013-02-27 15:23:36 -080037#include "aos/common/once.h"
Brian Silvermanaf784862014-05-13 08:14:55 -070038#include "aos/common/libc/aos_strsignal.h"
39#include "aos/common/util/run_command.h"
Brian Silvermand169fcd2013-02-27 13:18:47 -080040
41// This is the main piece of code that starts all of the rest of the code and
42// restarts it when the binaries are modified.
43//
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -080044// Throughout, the code is not terribly concerned with thread safety because
45// there is only 1 thread. It does some setup and then lets inotify run things
46// when appropriate.
47//
Brian Silverman5cc661b2013-02-27 15:23:36 -080048// NOTE: This program should never exit nicely. It catches all nice attempts to
49// exit, forwards them to all of the children that it has started, waits for
Brian Silvermand169fcd2013-02-27 13:18:47 -080050// them to exit nicely, and then SIGKILLs anybody left (which will always
51// include itself).
52
53using ::std::unique_ptr;
54
55namespace aos {
56namespace starter {
57
Austin Schuhf2a50ba2016-12-24 16:16:26 -080058namespace chrono = ::std::chrono;
59
Brian Silverman0eec9532013-02-27 20:24:16 -080060// TODO(brians): split out the c++ libevent wrapper stuff into its own file(s)
Brian Silvermand169fcd2013-02-27 13:18:47 -080061class EventBaseDeleter {
62 public:
63 void operator()(event_base *base) {
Brian Silverman8070a222013-02-28 15:01:36 -080064 if (base == NULL) return;
Brian Silvermand169fcd2013-02-27 13:18:47 -080065 event_base_free(base);
66 }
67};
68typedef unique_ptr<event_base, EventBaseDeleter> EventBaseUniquePtr;
Brian Silverman5cc661b2013-02-27 15:23:36 -080069EventBaseUniquePtr libevent_base;
Brian Silvermand169fcd2013-02-27 13:18:47 -080070
71class EventDeleter {
72 public:
73 void operator()(event *evt) {
Brian Silverman8070a222013-02-28 15:01:36 -080074 if (evt == NULL) return;
Brian Silvermand169fcd2013-02-27 13:18:47 -080075 if (event_del(evt) != 0) {
76 LOG(WARNING, "event_del(%p) failed\n", evt);
77 }
78 }
79};
80typedef unique_ptr<event, EventDeleter> EventUniquePtr;
81
Brian Silverman5cc661b2013-02-27 15:23:36 -080082// Watches a file path for modifications. Once created, keeps watching until
83// destroyed or RemoveWatch() is called.
Brian Silverman0eec9532013-02-27 20:24:16 -080084// TODO(brians): split this out into its own file + tests
Brian Silvermand169fcd2013-02-27 13:18:47 -080085class FileWatch {
86 public:
87 // Will call callback(value) when filename is modified.
88 // If value is NULL, then a pointer to this object will be passed instead.
Brian Silverman5cc661b2013-02-27 15:23:36 -080089 //
90 // Watching for file creations is slightly different. To do that, pass true
Brian Silverman8070a222013-02-28 15:01:36 -080091 // as create, the directory where the file will be created for filename, and
Brian Silverman5cc661b2013-02-27 15:23:36 -080092 // the name of the file (without directory name) for check_filename.
Brian Silvermand169fcd2013-02-27 13:18:47 -080093 FileWatch(std::string filename,
Brian Silverman8070a222013-02-28 15:01:36 -080094 std::function<void(void *)> callback,
95 void *value,
96 bool create = false,
97 std::string check_filename = "")
98 : filename_(filename),
99 callback_(callback),
100 value_(value),
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700101 create_(create),
102 check_filename_(check_filename),
103 watch_(-1) {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800104 init_once.Get();
105
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700106 CreateWatch();
Brian Silvermand169fcd2013-02-27 13:18:47 -0800107 }
108 // Cleans up everything.
109 ~FileWatch() {
110 if (watch_ != -1) {
111 RemoveWatch();
112 }
113 }
114
115 // After calling this method, this object won't really be doing much of
Brian Silverman5cc661b2013-02-27 15:23:36 -0800116 // anything besides possibly running its callback or something.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800117 void RemoveWatch() {
Brian Silvermanfe457de2014-05-26 22:04:08 -0700118 CHECK_NE(watch_, -1);
119 CHECK_EQ(watch_to_remove_, -1);
Brian Silverman5cc661b2013-02-27 15:23:36 -0800120
Brian Silvermand169fcd2013-02-27 13:18:47 -0800121 if (inotify_rm_watch(notify_fd, watch_) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700122 PLOG(WARNING, "inotify_rm_watch(%d, %d) failed", notify_fd, watch_);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800123 }
Brian Silvermand94642c2014-03-27 18:21:41 -0700124 watch_to_remove_ = watch_;
125 watch_ = -1;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800126 }
127
Brian Silverman5cc661b2013-02-27 15:23:36 -0800128 private:
129 // Performs the static initialization. Called by init_once from the
130 // constructor.
131 static void *Init() {
132 notify_fd = inotify_init1(IN_CLOEXEC);
133 EventUniquePtr notify_event(event_new(libevent_base.get(), notify_fd,
134 EV_READ | EV_PERSIST,
135 FileWatch::INotifyReadable, NULL));
136 event_add(notify_event.release(), NULL);
137 return NULL;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800138 }
139
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700140 void RemoveWatchFromMap() {
Brian Silvermand94642c2014-03-27 18:21:41 -0700141 int watch = watch_to_remove_;
142 if (watch == -1) {
Brian Silverman67550242014-07-19 16:58:19 -0700143 CHECK_NE(watch_, -1);
Brian Silvermand94642c2014-03-27 18:21:41 -0700144 watch = watch_;
145 }
146 if (watchers[watch] != this) {
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700147 LOG(WARNING, "watcher for %s (%p) didn't find itself in the map\n",
148 filename_.c_str(), this);
149 } else {
Brian Silvermand94642c2014-03-27 18:21:41 -0700150 watchers.erase(watch);
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700151 }
Brian Silvermand94642c2014-03-27 18:21:41 -0700152 LOG(DEBUG, "removed watch ID %d\n", watch);
153 if (watch_to_remove_ == -1) {
154 watch_ = -1;
155 } else {
156 watch_to_remove_ = -1;
157 }
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700158 }
159
160 void CreateWatch() {
Brian Silvermanfe457de2014-05-26 22:04:08 -0700161 CHECK_EQ(watch_, -1);
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700162 watch_ = inotify_add_watch(notify_fd, filename_.c_str(),
163 create_ ? IN_CREATE : (IN_ATTRIB |
164 IN_MODIFY |
165 IN_DELETE_SELF |
166 IN_MOVE_SELF));
167 if (watch_ == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700168 PLOG(FATAL, "inotify_add_watch(%d, %s,"
169 " %s ? IN_CREATE : (IN_ATTRIB | IN_MODIFY)) failed",
170 notify_fd, filename_.c_str(), create_ ? "true" : "false");
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700171 }
172 watchers[watch_] = this;
173 LOG(DEBUG, "watch for %s is %d\n", filename_.c_str(), watch_);
174 }
175
Brian Silvermand169fcd2013-02-27 13:18:47 -0800176 // This gets set up as the callback for EV_READ on the inotify file
Brian Silverman5cc661b2013-02-27 15:23:36 -0800177 // descriptor. It calls FileNotified on the appropriate instance.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800178 static void INotifyReadable(int /*fd*/, short /*events*/, void *) {
179 unsigned int to_read;
Brian Silverman5cc661b2013-02-27 15:23:36 -0800180 // Use FIONREAD to figure out how many bytes there are to read.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800181 if (ioctl(notify_fd, FIONREAD, &to_read) < 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700182 PLOG(FATAL, "FIONREAD(%d, %p) failed", notify_fd, &to_read);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800183 }
184 inotify_event *notifyevt = static_cast<inotify_event *>(malloc(to_read));
185 const char *end = reinterpret_cast<char *>(notifyevt) + to_read;
186 aos::unique_c_ptr<inotify_event> freer(notifyevt);
187
188 ssize_t ret = read(notify_fd, notifyevt, to_read);
189 if (ret < 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700190 PLOG(FATAL, "read(%d, %p, %u) failed", notify_fd, notifyevt, to_read);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800191 }
192 if (static_cast<size_t>(ret) != to_read) {
193 LOG(ERROR, "read(%d, %p, %u) returned %zd instead of %u\n",
194 notify_fd, notifyevt, to_read, ret, to_read);
195 return;
196 }
197
Brian Silverman5cc661b2013-02-27 15:23:36 -0800198 // Keep looping through until we get to the end because inotify does return
199 // multiple events at once.
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800200 while (reinterpret_cast<char *>(notifyevt) < end) {
Brian Silvermand169fcd2013-02-27 13:18:47 -0800201 if (watchers.count(notifyevt->wd) != 1) {
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800202 LOG(WARNING, "couldn't find whose watch ID %d is\n", notifyevt->wd);
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800203 } else {
Brian Silverman8efe23e2013-07-07 23:31:37 -0700204 LOG(DEBUG, "mask=%" PRIu32 "\n", notifyevt->mask);
Brian Silvermand94642c2014-03-27 18:21:41 -0700205 // If the watch was removed.
206 if (notifyevt->mask & IN_IGNORED) {
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700207 watchers[notifyevt->wd]->WatchDeleted();
208 } else {
Brian Silvermand94642c2014-03-27 18:21:41 -0700209 watchers[notifyevt->wd]
210 ->FileNotified((notifyevt->len > 0) ? notifyevt->name : NULL);
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700211 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800212 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800213
214 notifyevt = reinterpret_cast<inotify_event *>(
Brian Silvermandbdf1d02013-11-17 13:19:41 -0800215 __builtin_assume_aligned(reinterpret_cast<char *>(notifyevt) +
216 sizeof(*notifyevt) + notifyevt->len,
Brian Silvermanafc00a62014-04-21 17:51:23 -0700217 alignof(inotify_event)));
Brian Silvermand169fcd2013-02-27 13:18:47 -0800218 }
219 }
220
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700221 // INotifyReadable calls this method whenever the watch for our file gets
222 // removed somehow.
223 void WatchDeleted() {
224 LOG(DEBUG, "watch for %s deleted\n", filename_.c_str());
225 RemoveWatchFromMap();
226 CreateWatch();
227 }
228
Brian Silverman5cc661b2013-02-27 15:23:36 -0800229 // INotifyReadable calls this method whenever the watch for our file triggers.
230 void FileNotified(const char *filename) {
Brian Silvermanfe457de2014-05-26 22:04:08 -0700231 CHECK_NE(watch_, -1);
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800232 LOG(DEBUG, "got a notification for %s\n", filename_.c_str());
Brian Silverman5cc661b2013-02-27 15:23:36 -0800233
234 if (!check_filename_.empty()) {
235 if (filename == NULL) {
236 return;
237 }
238 if (std::string(filename) != check_filename_) {
239 return;
240 }
241 }
242
243 callback_((value_ == NULL) ? this : value_);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800244 }
245
Brian Silverman5cc661b2013-02-27 15:23:36 -0800246 // To make sure that Init gets called exactly once.
247 static ::aos::Once<void> init_once;
248
Brian Silvermand169fcd2013-02-27 13:18:47 -0800249 const std::string filename_;
250 const std::function<void(void *)> callback_;
251 void *const value_;
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700252 const bool create_;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800253 std::string check_filename_;
254
255 // The watch descriptor or -1 if we don't have one any more.
256 int watch_;
Brian Silvermand94642c2014-03-27 18:21:41 -0700257 // The watch that we still have to take out of the map once we get the
258 // IN_IGNORED or -1.
259 int watch_to_remove_ = -1;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800260
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800261 // Map from watch IDs to instances of this class.
262 // <https://patchwork.kernel.org/patch/73192/> ("inotify: do not reuse watch
263 // descriptors") says they won't get reused, but that shouldn't be counted on
264 // because we might have a modified/different version/whatever kernel.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800265 static std::map<int, FileWatch *> watchers;
Brian Silverman5cc661b2013-02-27 15:23:36 -0800266 // The inotify(7) file descriptor.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800267 static int notify_fd;
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800268
269 DISALLOW_COPY_AND_ASSIGN(FileWatch);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800270};
Brian Silverman5cc661b2013-02-27 15:23:36 -0800271::aos::Once<void> FileWatch::init_once(FileWatch::Init);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800272std::map<int, FileWatch *> FileWatch::watchers;
273int FileWatch::notify_fd;
274
Brian Silverman5cc661b2013-02-27 15:23:36 -0800275// Runs the given command and returns its first line of output (not including
276// the \n). LOG(FATAL)s if the command has an exit status other than 0 or does
277// not print out an entire line.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800278std::string RunCommand(std::string command) {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800279 // popen(3) might fail and not set it.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800280 errno = 0;
Brian Silverman5cc661b2013-02-27 15:23:36 -0800281 FILE *pipe = popen(command.c_str(), "r");
282 if (pipe == NULL) {
Brian Silverman01be0002014-05-10 15:44:38 -0700283 PLOG(FATAL, "popen(\"%s\", \"r\") failed", command.c_str());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800284 }
285
Brian Silverman5cc661b2013-02-27 15:23:36 -0800286 // result_size is how many bytes result is currently allocated to.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800287 size_t result_size = 128, read = 0;
288 unique_c_ptr<char> result(static_cast<char *>(malloc(result_size)));
289 while (true) {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800290 // If we filled up the buffer, then realloc(3) it bigger.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800291 if (read == result_size) {
292 result_size *= 2;
293 void *new_result = realloc(result.get(), result_size);
294 if (new_result == NULL) {
Brian Silverman01be0002014-05-10 15:44:38 -0700295 PLOG(FATAL, "realloc(%p, %zd) failed", result.get(), result_size);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800296 } else {
297 result.release();
298 result = unique_c_ptr<char>(static_cast<char *>(new_result));
299 }
300 }
301
Brian Silverman5cc661b2013-02-27 15:23:36 -0800302 size_t ret = fread(result.get() + read, 1, result_size - read, pipe);
303 // If the read didn't fill up the whole buffer, check to see if it was
304 // because of an error.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800305 if (ret < result_size - read) {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800306 if (ferror(pipe)) {
Brian Silverman01be0002014-05-10 15:44:38 -0700307 PLOG(FATAL, "couldn't finish reading output of \"%s\"\n",
308 command.c_str());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800309 }
310 }
311 read += ret;
312 if (read > 0 && result.get()[read - 1] == '\n') {
313 break;
314 }
315
Brian Silverman5cc661b2013-02-27 15:23:36 -0800316 if (feof(pipe)) {
317 LOG(FATAL, "`%s` failed. didn't print a whole line\n", command.c_str());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800318 }
319 }
320
Brian Silverman5cc661b2013-02-27 15:23:36 -0800321 // Get rid of the first \n and anything after it.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800322 *strchrnul(result.get(), '\n') = '\0';
323
Brian Silverman5cc661b2013-02-27 15:23:36 -0800324 int child_status = pclose(pipe);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800325 if (child_status == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700326 PLOG(FATAL, "pclose(%p) failed", pipe);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800327 }
328
329 if (child_status != 0) {
330 LOG(FATAL, "`%s` failed. return %d\n", command.c_str(), child_status);
331 }
332
333 return std::string(result.get());
334}
335
336// Will call callback(arg) after time.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800337void Timeout(monotonic_clock::duration time,
338 void (*callback)(int, short, void *), void *arg) {
Brian Silvermand169fcd2013-02-27 13:18:47 -0800339 EventUniquePtr timeout(evtimer_new(libevent_base.get(), callback, arg));
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800340 struct timeval time_timeval;
341 {
342 ::std::chrono::seconds sec =
343 ::std::chrono::duration_cast<::std::chrono::seconds>(time);
344 ::std::chrono::microseconds usec =
345 ::std::chrono::duration_cast<::std::chrono::microseconds>(time - sec);
346 time_timeval.tv_sec = sec.count();
347 time_timeval.tv_usec = usec.count();
348 }
Brian Silvermand94642c2014-03-27 18:21:41 -0700349 if (evtimer_add(timeout.release(), &time_timeval) != 0) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800350 LOG(FATAL, "evtimer_add(%p, %p) failed\n", timeout.release(),
351 &time_timeval);
Brian Silvermand94642c2014-03-27 18:21:41 -0700352 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800353}
354
Brian Silvermand94642c2014-03-27 18:21:41 -0700355class Child;
356// This is where all of the Child instances except core live.
357std::vector<unique_ptr<Child>> children;
358// A global place to hold on to which child is core.
359unique_ptr<Child> core;
360
Brian Silvermand169fcd2013-02-27 13:18:47 -0800361// Represents a child process. It will take care of restarting itself etc.
362class Child {
363 public:
Brian Silverman5cc661b2013-02-27 15:23:36 -0800364 // command is the (space-separated) command to run and its arguments.
365 Child(const std::string &command) : pid_(-1),
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800366 stat_at_start_valid_(false) {
Brian Silvermand94642c2014-03-27 18:21:41 -0700367 if (!restart_timeout) {
368 restart_timeout = EventUniquePtr(
369 evtimer_new(libevent_base.get(), StaticDoRestart, nullptr));
370 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800371 const char *start, *end;
372 start = command.c_str();
373 while (true) {
374 end = strchrnul(start, ' ');
375 args_.push_back(std::string(start, end - start));
376 start = end + 1;
377 if (*end == '\0') {
378 break;
379 }
380 }
381
Brian Silverman5cc661b2013-02-27 15:23:36 -0800382 original_binary_ = RunCommand("which " + args_[0]);
383 binary_ = original_binary_ + ".stm";
Brian Silvermand169fcd2013-02-27 13:18:47 -0800384
385 watcher_ = unique_ptr<FileWatch>(
Brian Silverman5cc661b2013-02-27 15:23:36 -0800386 new FileWatch(original_binary_, StaticFileModified, this));
Brian Silvermand169fcd2013-02-27 13:18:47 -0800387
388 Start();
389 }
390
391 pid_t pid() { return pid_; }
392
393 // This gets called whenever the actual process dies and should (probably) be
394 // restarted.
395 void ProcessDied() {
396 pid_ = -1;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800397 restarts_.push(monotonic_clock::now());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800398 if (restarts_.size() > kMaxRestartsNumber) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800399 monotonic_clock::time_point oldest = restarts_.front();
Brian Silvermand169fcd2013-02-27 13:18:47 -0800400 restarts_.pop();
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800401 if (monotonic_clock::now() <= kMaxRestartsTime + oldest) {
Brian Silvermand169fcd2013-02-27 13:18:47 -0800402 LOG(WARNING, "process %s getting restarted too often\n", name());
403 Timeout(kResumeWait, StaticStart, this);
404 return;
405 }
406 }
407 Start();
408 }
409
410 // Returns a name for logging purposes.
411 const char *name() {
412 return args_[0].c_str();
413 }
414
415 private:
416 struct CheckDiedStatus {
417 Child *self;
418 pid_t old_pid;
419 };
420
421 // How long to wait for a child to die nicely.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800422 static constexpr chrono::nanoseconds kProcessDieTime = chrono::seconds(2);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800423
424 // How long to wait after the file is modified to restart it.
425 // This is important because some programs like modifying the binaries by
426 // writing them in little bits, which results in attempting to start partial
427 // binaries without this.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800428 static constexpr chrono::nanoseconds kRestartWaitTime =
429 chrono::milliseconds(1500);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800430
Brian Silverman5cc661b2013-02-27 15:23:36 -0800431 // Only kMaxRestartsNumber restarts will be allowed in kMaxRestartsTime.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800432 static constexpr chrono::nanoseconds kMaxRestartsTime = chrono::seconds(4);
Brian Silverman52aeeac2013-08-28 16:20:53 -0700433 static const size_t kMaxRestartsNumber = 3;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800434 // How long to wait if it gets restarted too many times.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800435 static constexpr chrono::nanoseconds kResumeWait = chrono::seconds(5);
Brian Silverman5cc661b2013-02-27 15:23:36 -0800436
Brian Silvermand169fcd2013-02-27 13:18:47 -0800437 static void StaticFileModified(void *self) {
438 static_cast<Child *>(self)->FileModified();
439 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800440
Brian Silvermand169fcd2013-02-27 13:18:47 -0800441 void FileModified() {
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700442 LOG(DEBUG, "file for %s modified\n", name());
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800443 struct timeval restart_time_timeval;
444 {
445 ::std::chrono::seconds sec =
446 ::std::chrono::duration_cast<::std::chrono::seconds>(
447 kRestartWaitTime);
448 ::std::chrono::microseconds usec =
449 ::std::chrono::duration_cast<::std::chrono::microseconds>(
450 kRestartWaitTime - sec);
451 restart_time_timeval.tv_sec = sec.count();
452 restart_time_timeval.tv_usec = usec.count();
453 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800454 // This will reset the timeout again if it hasn't run yet.
Brian Silvermand94642c2014-03-27 18:21:41 -0700455 if (evtimer_add(restart_timeout.get(), &restart_time_timeval) != 0) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800456 LOG(FATAL, "evtimer_add(%p, %p) failed\n", restart_timeout.get(),
457 &restart_time_timeval);
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700458 }
Brian Silvermand94642c2014-03-27 18:21:41 -0700459 waiting_to_restart.insert(this);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800460 }
461
Brian Silvermand94642c2014-03-27 18:21:41 -0700462 static void StaticDoRestart(int, short, void *) {
463 LOG(DEBUG, "restarting everything that needs it\n");
464 if (waiting_to_restart.find(core.get()) != waiting_to_restart.end()) {
465 core->DoRestart();
466 waiting_to_restart.erase(core.get());
467 }
468 for (auto c : waiting_to_restart) {
469 c->DoRestart();
470 }
471 waiting_to_restart.clear();
Brian Silvermand169fcd2013-02-27 13:18:47 -0800472 }
473
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800474 // Called after somebody else has finished modifying the file.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800475 void DoRestart() {
Brian Silvermand94642c2014-03-27 18:21:41 -0700476 fprintf(stderr, "DoRestart(%s)\n", binary_.c_str());
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800477 if (stat_at_start_valid_) {
478 struct stat current_stat;
479 if (stat(original_binary_.c_str(), &current_stat) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700480 PLOG(FATAL, "stat(%s, %p) failed",
481 original_binary_.c_str(), &current_stat);
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800482 }
483 if (current_stat.st_mtime == stat_at_start_.st_mtime) {
484 LOG(DEBUG, "ignoring trigger for %s because mtime didn't change\n",
485 name());
486 return;
487 }
488 }
489
Brian Silvermand94642c2014-03-27 18:21:41 -0700490 if (this == core.get()) {
491 fprintf(stderr, "Restarting core -> exiting now.\n");
492 exit(0);
493 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800494 if (pid_ != -1) {
495 LOG(DEBUG, "sending SIGTERM to child %d to restart it\n", pid_);
496 if (kill(pid_, SIGTERM) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700497 PLOG(WARNING, "kill(%d, SIGTERM) failed", pid_);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800498 }
499 CheckDiedStatus *status = new CheckDiedStatus();
500 status->self = this;
501 status->old_pid = pid_;
502 Timeout(kProcessDieTime, StaticCheckDied, status);
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700503 } else {
504 LOG(WARNING, "%s restart attempted but not running\n", name());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800505 }
506 }
507
508 static void StaticCheckDied(int, short, void *status_in) {
509 CheckDiedStatus *status = static_cast<CheckDiedStatus *>(status_in);
510 status->self->CheckDied(status->old_pid);
511 delete status;
512 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800513
514 // Checks to see if the child using the PID old_pid is still running.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800515 void CheckDied(pid_t old_pid) {
516 if (pid_ == old_pid) {
517 LOG(WARNING, "child %d refused to die\n", old_pid);
518 if (kill(old_pid, SIGKILL) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700519 PLOG(WARNING, "kill(%d, SIGKILL) failed", old_pid);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800520 }
521 }
522 }
523
524 static void StaticStart(int, short, void *self) {
525 static_cast<Child *>(self)->Start();
526 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800527
528 // Actually starts the child.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800529 void Start() {
530 if (pid_ != -1) {
531 LOG(WARNING, "calling Start() but already have child %d running\n",
532 pid_);
Brian Silverman5cc661b2013-02-27 15:23:36 -0800533 if (kill(pid_, SIGKILL) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700534 PLOG(WARNING, "kill(%d, SIGKILL) failed", pid_);
Brian Silverman5cc661b2013-02-27 15:23:36 -0800535 return;
536 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800537 pid_ = -1;
538 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800539
540 // Remove the name that we run from (ie from a previous execution) and then
541 // hard link the real filename to it.
542 if (unlink(binary_.c_str()) != 0 && errno != ENOENT) {
Brian Silverman01be0002014-05-10 15:44:38 -0700543 PLOG(FATAL, "removing %s failed", binary_.c_str());
Brian Silverman5cc661b2013-02-27 15:23:36 -0800544 }
545 if (link(original_binary_.c_str(), binary_.c_str()) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700546 PLOG(FATAL, "link('%s', '%s') failed",
547 original_binary_.c_str(), binary_.c_str());
Brian Silverman5cc661b2013-02-27 15:23:36 -0800548 }
549
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800550 if (stat(original_binary_.c_str(), &stat_at_start_) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700551 PLOG(FATAL, "stat(%s, %p) failed",
552 original_binary_.c_str(), &stat_at_start_);
Brian Silvermanfe06fe12013-02-27 18:54:58 -0800553 }
554 stat_at_start_valid_ = true;
555
Brian Silvermand169fcd2013-02-27 13:18:47 -0800556 if ((pid_ = fork()) == 0) {
557 ssize_t args_size = args_.size();
558 const char **argv = new const char *[args_size + 1];
559 for (int i = 0; i < args_size; ++i) {
560 argv[i] = args_[i].c_str();
561 }
562 argv[args_size] = NULL;
563 // The const_cast is safe because no code that might care if it gets
564 // modified can run afterwards.
565 execv(binary_.c_str(), const_cast<char **>(argv));
Brian Silverman01be0002014-05-10 15:44:38 -0700566 PLOG(FATAL, "execv(%s, %p) failed", binary_.c_str(), argv);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800567 _exit(EXIT_FAILURE);
568 }
569 if (pid_ == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700570 PLOG(FATAL, "forking to run \"%s\" failed", binary_.c_str());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800571 }
Brian Silvermand90b5fe2013-03-10 18:34:42 -0700572 LOG(DEBUG, "started \"%s\" successfully\n", binary_.c_str());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800573 }
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800574
575 // A history of the times that this process has been restarted.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800576 std::queue<monotonic_clock::time_point,
577 std::list<monotonic_clock::time_point>> restarts_;
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800578
579 // The currently running child's PID or NULL.
580 pid_t pid_;
581
582 // All of the arguments (including the name of the binary).
583 std::deque<std::string> args_;
584
585 // The name of the real binary that we were told to run.
586 std::string original_binary_;
587 // The name of the file that we're actually running.
588 std::string binary_;
589
590 // Watches original_binary_.
591 unique_ptr<FileWatch> watcher_;
592
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800593 // Captured from the original file when we most recently started a new child
594 // process. Used to see if it actually changes or not.
595 struct stat stat_at_start_;
596 bool stat_at_start_valid_;
597
Brian Silvermand94642c2014-03-27 18:21:41 -0700598 // An event that restarts after kRestartWaitTime.
599 static EventUniquePtr restart_timeout;
600
601 // The set of children waiting to be restarted once all modifications stop.
602 static ::std::set<Child *> waiting_to_restart;
603
Brian Silvermanbc4fc2f2013-02-27 19:33:42 -0800604 DISALLOW_COPY_AND_ASSIGN(Child);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800605};
Brian Silverman52aeeac2013-08-28 16:20:53 -0700606
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800607constexpr chrono::nanoseconds Child::kProcessDieTime;
608constexpr chrono::nanoseconds Child::kRestartWaitTime;
609constexpr chrono::nanoseconds Child::kMaxRestartsTime;
610constexpr chrono::nanoseconds Child::kResumeWait;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800611
Brian Silvermand94642c2014-03-27 18:21:41 -0700612EventUniquePtr Child::restart_timeout;
613::std::set<Child *> Child::waiting_to_restart;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800614
Brian Silverman5cc661b2013-02-27 15:23:36 -0800615// Kills off the entire process group (including ourself).
616void KillChildren(bool try_nice) {
Brian Silvermand169fcd2013-02-27 13:18:47 -0800617 if (try_nice) {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800618 static constexpr int kNiceStopSignal = SIGTERM;
619 static constexpr auto kNiceWaitTime = chrono::seconds(1);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800620
621 // Make sure that we don't just nicely stop ourself...
622 sigset_t mask;
623 sigemptyset(&mask);
624 sigaddset(&mask, kNiceStopSignal);
625 sigprocmask(SIG_BLOCK, &mask, NULL);
626
Brian Silverman5cc661b2013-02-27 15:23:36 -0800627 kill(-getpid(), kNiceStopSignal);
628
629 fflush(NULL);
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800630 ::std::this_thread::sleep_for(kNiceWaitTime);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800631 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800632
Brian Silvermand169fcd2013-02-27 13:18:47 -0800633 // Send SIGKILL to our whole process group, which will forcibly terminate any
634 // of them that are still running (us for sure, maybe more too).
Brian Silverman5cc661b2013-02-27 15:23:36 -0800635 kill(-getpid(), SIGKILL);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800636}
637
Brian Silverman5cc661b2013-02-27 15:23:36 -0800638void ExitHandler() {
639 KillChildren(true);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800640}
Brian Silverman5cc661b2013-02-27 15:23:36 -0800641
642void KillChildrenSignalHandler(int signum) {
643 // If we get SIGSEGV or some other random signal who knows what's happening
644 // and we should just kill everybody immediately.
645 // This is a list of all of the signals that mean some form of "nicely stop".
646 KillChildren(signum == SIGHUP || signum == SIGINT || signum == SIGQUIT ||
Brian Silverman0eec9532013-02-27 20:24:16 -0800647 signum == SIGABRT || signum == SIGPIPE || signum == SIGTERM ||
648 signum == SIGXCPU);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800649}
650
Brian Silverman5cc661b2013-02-27 15:23:36 -0800651// Returns the currently running child with PID pid or an empty unique_ptr.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800652const unique_ptr<Child> &FindChild(pid_t pid) {
653 for (auto it = children.begin(); it != children.end(); ++it) {
654 if (pid == (*it)->pid()) {
655 return *it;
656 }
657 }
658
659 if (pid == core->pid()) {
660 return core;
661 }
662
Brian Silverman5cc661b2013-02-27 15:23:36 -0800663 static const unique_ptr<Child> kNothing;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800664 return kNothing;
665}
666
Brian Silverman5cc661b2013-02-27 15:23:36 -0800667// Gets set up as a libevent handler for SIGCHLD.
668// Handles calling Child::ProcessDied() on the appropriate one.
669void SigCHLDReceived(int /*fd*/, short /*events*/, void *) {
Brian Silvermand169fcd2013-02-27 13:18:47 -0800670 // In a while loop in case we miss any SIGCHLDs.
671 while (true) {
672 siginfo_t infop;
673 infop.si_pid = 0;
674 if (waitid(P_ALL, 0, &infop, WEXITED | WSTOPPED | WNOHANG) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700675 PLOG(WARNING, "waitid failed");
Brian Silverman5cc661b2013-02-27 15:23:36 -0800676 continue;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800677 }
Brian Silverman5cc661b2013-02-27 15:23:36 -0800678 // If there are no more child process deaths to process.
Brian Silvermand169fcd2013-02-27 13:18:47 -0800679 if (infop.si_pid == 0) {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800680 return;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800681 }
682
683 pid_t pid = infop.si_pid;
684 int status = infop.si_status;
685 const unique_ptr<Child> &child = FindChild(pid);
686 if (child) {
687 switch (infop.si_code) {
688 case CLD_EXITED:
689 LOG(WARNING, "child %d (%s) exited with status %d\n",
690 pid, child->name(), status);
691 break;
692 case CLD_DUMPED:
693 LOG(INFO, "child %d actually dumped core. "
694 "falling through to killed by signal case\n", pid);
695 case CLD_KILLED:
696 // If somebody (possibly us) sent it SIGTERM that means that they just
697 // want it to stop, so it stopping isn't a WARNING.
698 LOG((status == SIGTERM) ? DEBUG : WARNING,
699 "child %d (%s) was killed by signal %d (%s)\n",
Brian Silvermanaf784862014-05-13 08:14:55 -0700700 pid, child->name(), status, aos_strsignal(status));
Brian Silvermand169fcd2013-02-27 13:18:47 -0800701 break;
702 case CLD_STOPPED:
703 LOG(WARNING, "child %d (%s) was stopped by signal %d "
704 "(giving it a SIGCONT(%d))\n",
705 pid, child->name(), status, SIGCONT);
706 kill(pid, SIGCONT);
707 continue;
708 default:
709 LOG(WARNING, "something happened to child %d (%s) (killing it)\n",
710 pid, child->name());
711 kill(pid, SIGKILL);
712 continue;
713 }
714 } else {
Brian Silverman5cc661b2013-02-27 15:23:36 -0800715 LOG(WARNING, "couldn't find a Child for pid %d\n", pid);
716 return;
Brian Silvermand169fcd2013-02-27 13:18:47 -0800717 }
718
Brian Silverman5cc661b2013-02-27 15:23:36 -0800719 if (child == core) {
720 LOG(FATAL, "core died\n");
721 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800722 child->ProcessDied();
723 }
724}
725
Brian Silverman5cc661b2013-02-27 15:23:36 -0800726// This is used for communicating the name of the file to read processes to
727// start from main to Run.
728const char *child_list_file;
729
Brian Silverman8070a222013-02-28 15:01:36 -0800730void Run(void *watch);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800731void Main() {
732 logging::Init();
Brian Silvermand169fcd2013-02-27 13:18:47 -0800733
Comran Morshed7f6ba792016-02-21 16:54:05 +0000734 // Set UID to 0 so we can run things as root down below. Since the starter
735 // program on the roborio runs starter.sh under "lvuser", it will continuously
736 // fail due to lack of permissions if we do not manually set the UID to admin.
737#ifdef AOS_ARCHITECTURE_arm_frc
738 if (setuid(0) != 0) {
739 PLOG(FATAL, "setuid(0) failed");
740 }
741#endif
742
Brian Silverman5cc661b2013-02-27 15:23:36 -0800743 if (setpgid(0 /*self*/, 0 /*make PGID the same as PID*/) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -0700744 PLOG(FATAL, "setpgid(0, 0) failed");
Brian Silverman5cc661b2013-02-27 15:23:36 -0800745 }
Brian Silvermand169fcd2013-02-27 13:18:47 -0800746
747 // Make sure that we kill all children when we exit.
Brian Silverman5cc661b2013-02-27 15:23:36 -0800748 atexit(ExitHandler);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800749 // Do it on some signals too (ones that we otherwise tend to receive and then
750 // leave all of our children going).
Brian Silverman5cc661b2013-02-27 15:23:36 -0800751 signal(SIGHUP, KillChildrenSignalHandler);
752 signal(SIGINT, KillChildrenSignalHandler);
753 signal(SIGQUIT, KillChildrenSignalHandler);
754 signal(SIGILL, KillChildrenSignalHandler);
755 signal(SIGABRT, KillChildrenSignalHandler);
756 signal(SIGFPE, KillChildrenSignalHandler);
757 signal(SIGSEGV, KillChildrenSignalHandler);
758 signal(SIGPIPE, KillChildrenSignalHandler);
759 signal(SIGTERM, KillChildrenSignalHandler);
760 signal(SIGBUS, KillChildrenSignalHandler);
761 signal(SIGXCPU, KillChildrenSignalHandler);
Brian Silverman35df22f2015-12-27 17:57:10 -0800762
763#ifdef AOS_ARCHITECTURE_arm_frc
764 // Just allow overcommit memory like usual. Various processes map memory they
765 // will never use, and the roboRIO doesn't have enough RAM to handle it.
766 // This is in here instead of starter.sh because starter.sh doesn't run with
767 // permissions on a roboRIO.
768 CHECK(system("echo 0 > /proc/sys/vm/overcommit_memory") == 0);
769#endif
Brian Silvermand169fcd2013-02-27 13:18:47 -0800770
771 libevent_base = EventBaseUniquePtr(event_base_new());
772
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800773 std::string core_touch_file = "/tmp/starter.";
Brian Silvermand169fcd2013-02-27 13:18:47 -0800774 core_touch_file += std::to_string(static_cast<intmax_t>(getpid()));
775 core_touch_file += ".core_touch_file";
Brian Silvermanaf784862014-05-13 08:14:55 -0700776 const int result =
777 ::aos::util::RunCommand(("touch '" + core_touch_file + "'").c_str());
778 if (result == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700779 PLOG(FATAL, "running `touch '%s'` failed\n", core_touch_file.c_str());
Brian Silvermanaf784862014-05-13 08:14:55 -0700780 } else if (!WIFEXITED(result) || WEXITSTATUS(result) != 0) {
781 LOG(FATAL, "`touch '%s'` gave result %x\n", core_touch_file.c_str(),
782 result);
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800783 }
784 FileWatch core_touch_file_watch(core_touch_file, Run, NULL);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800785 core = unique_ptr<Child>(
Brian Silvermanb1e4f6c2013-02-27 15:42:02 -0800786 new Child("core " + core_touch_file));
Brian Silvermand169fcd2013-02-27 13:18:47 -0800787
788 FILE *pid_file = fopen("/tmp/starter.pid", "w");
789 if (pid_file == NULL) {
Brian Silverman01be0002014-05-10 15:44:38 -0700790 PLOG(FATAL, "fopen(\"/tmp/starter.pid\", \"w\") failed");
Brian Silvermand169fcd2013-02-27 13:18:47 -0800791 } else {
792 if (fprintf(pid_file, "%d", core->pid()) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700793 PLOG(WARNING, "fprintf(%p, \"%%d\", %d) failed",
794 pid_file, core->pid());
Brian Silvermand169fcd2013-02-27 13:18:47 -0800795 }
796 fclose(pid_file);
797 }
798
799 LOG(INFO, "waiting for %s to appear\n", core_touch_file.c_str());
800
801 event_base_dispatch(libevent_base.get());
802 LOG(FATAL, "event_base_dispatch(%p) returned\n", libevent_base.get());
803}
804
Brian Silverman0eec9532013-02-27 20:24:16 -0800805// This is the callback for when core creates the file indicating that it has
806// started.
807void Run(void *watch) {
808 // Make it so it doesn't keep on seeing random changes in /tmp.
809 static_cast<FileWatch *>(watch)->RemoveWatch();
810
811 // It's safe now because core is up.
812 aos::InitNRT();
813
814 std::ifstream list_file(child_list_file);
815
816 while (true) {
817 std::string child_name;
818 getline(list_file, child_name);
819 if ((list_file.rdstate() & std::ios_base::eofbit) != 0) {
820 break;
821 }
822 if (list_file.rdstate() != 0) {
823 LOG(FATAL, "reading input file %s failed\n", child_list_file);
824 }
825 children.push_back(unique_ptr<Child>(new Child(child_name)));
826 }
827
828 EventUniquePtr sigchld(event_new(libevent_base.get(), SIGCHLD,
829 EV_SIGNAL | EV_PERSIST,
830 SigCHLDReceived, NULL));
831 event_add(sigchld.release(), NULL);
832}
833
Brian Silverman8070a222013-02-28 15:01:36 -0800834const char *kArgsHelp = "[OPTION]... START_LIST\n"
835 "Start all of the robot code binaries in START_LIST.\n"
836 "\n"
837 "START_LIST is the file to read binaries (looked up on PATH) to run.\n"
838 " --help display this help and exit\n";
839void PrintHelp() {
840 fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp);
841}
842
Brian Silvermand169fcd2013-02-27 13:18:47 -0800843} // namespace starter
844} // namespace aos
845
846int main(int argc, char *argv[]) {
Brian Silverman8070a222013-02-28 15:01:36 -0800847 if (argc != 2) {
848 aos::starter::PrintHelp();
Brian Silvermand169fcd2013-02-27 13:18:47 -0800849 exit(EXIT_FAILURE);
Brian Silvermand169fcd2013-02-27 13:18:47 -0800850 }
Brian Silverman8070a222013-02-28 15:01:36 -0800851 if (strcmp(argv[1], "--help") == 0) {
852 aos::starter::PrintHelp();
853 exit(EXIT_SUCCESS);
854 }
855
Brian Silvermand169fcd2013-02-27 13:18:47 -0800856 aos::starter::child_list_file = argv[1];
857
858 aos::starter::Main();
859}