Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 1 | // 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 6 | #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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 13 | #include <signal.h> |
| 14 | #include <stdint.h> |
| 15 | #include <errno.h> |
| 16 | #include <string.h> |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 18 | |
| 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 Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 28 | #include <set> |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 29 | |
Brian Silverman | 258b917 | 2015-09-19 14:32:57 -0400 | [diff] [blame] | 30 | #include "third_party/libevent/event.h" |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 31 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 32 | #include "aos/libc/aos_strsignal.h" |
| 33 | #include "aos/logging/implementations.h" |
| 34 | #include "aos/logging/logging.h" |
| 35 | #include "aos/time/time.h" |
| 36 | #include "aos/unique_malloc_ptr.h" |
| 37 | #include "aos/util/run_command.h" |
Sabina Davis | 2ed5ea2 | 2017-09-26 22:27:42 -0700 | [diff] [blame] | 38 | #include "aos/linux_code/init.h" |
| 39 | #include "aos/once.h" |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 40 | |
| 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 Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 44 | // 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 48 | // 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 50 | // them to exit nicely, and then SIGKILLs anybody left (which will always |
| 51 | // include itself). |
| 52 | |
| 53 | using ::std::unique_ptr; |
| 54 | |
| 55 | namespace aos { |
| 56 | namespace starter { |
| 57 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 58 | namespace chrono = ::std::chrono; |
| 59 | |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 60 | // TODO(brians): split out the c++ libevent wrapper stuff into its own file(s) |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 61 | class EventBaseDeleter { |
| 62 | public: |
| 63 | void operator()(event_base *base) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 64 | if (base == NULL) return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 65 | event_base_free(base); |
| 66 | } |
| 67 | }; |
| 68 | typedef unique_ptr<event_base, EventBaseDeleter> EventBaseUniquePtr; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 69 | EventBaseUniquePtr libevent_base; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 70 | |
| 71 | class EventDeleter { |
| 72 | public: |
| 73 | void operator()(event *evt) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 74 | if (evt == NULL) return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 75 | if (event_del(evt) != 0) { |
| 76 | LOG(WARNING, "event_del(%p) failed\n", evt); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | typedef unique_ptr<event, EventDeleter> EventUniquePtr; |
| 81 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 82 | // Watches a file path for modifications. Once created, keeps watching until |
| 83 | // destroyed or RemoveWatch() is called. |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 84 | // TODO(brians): split this out into its own file + tests |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 85 | class 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 89 | // |
| 90 | // Watching for file creations is slightly different. To do that, pass true |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 91 | // as create, the directory where the file will be created for filename, and |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 92 | // the name of the file (without directory name) for check_filename. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 93 | FileWatch(std::string filename, |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 94 | 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 Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 101 | create_(create), |
| 102 | check_filename_(check_filename), |
| 103 | watch_(-1) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 104 | init_once.Get(); |
| 105 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 106 | CreateWatch(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 107 | } |
| 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 116 | // anything besides possibly running its callback or something. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 117 | void RemoveWatch() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 118 | CHECK_NE(watch_, -1); |
| 119 | CHECK_EQ(watch_to_remove_, -1); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 120 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 121 | if (inotify_rm_watch(notify_fd, watch_) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 122 | PLOG(WARNING, "inotify_rm_watch(%d, %d) failed", notify_fd, watch_); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 123 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 124 | watch_to_remove_ = watch_; |
| 125 | watch_ = -1; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 128 | 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 140 | void RemoveWatchFromMap() { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 141 | int watch = watch_to_remove_; |
| 142 | if (watch == -1) { |
Brian Silverman | 6755024 | 2014-07-19 16:58:19 -0700 | [diff] [blame] | 143 | CHECK_NE(watch_, -1); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 144 | watch = watch_; |
| 145 | } |
| 146 | if (watchers[watch] != this) { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 147 | LOG(WARNING, "watcher for %s (%p) didn't find itself in the map\n", |
| 148 | filename_.c_str(), this); |
| 149 | } else { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 150 | watchers.erase(watch); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 151 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 152 | 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 Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void CreateWatch() { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 161 | CHECK_EQ(watch_, -1); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 162 | 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 168 | 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 Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 171 | } |
| 172 | watchers[watch_] = this; |
| 173 | LOG(DEBUG, "watch for %s is %d\n", filename_.c_str(), watch_); |
| 174 | } |
| 175 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 176 | // This gets set up as the callback for EV_READ on the inotify file |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 177 | // descriptor. It calls FileNotified on the appropriate instance. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 178 | static void INotifyReadable(int /*fd*/, short /*events*/, void *) { |
| 179 | unsigned int to_read; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 180 | // Use FIONREAD to figure out how many bytes there are to read. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 181 | if (ioctl(notify_fd, FIONREAD, &to_read) < 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 182 | PLOG(FATAL, "FIONREAD(%d, %p) failed", notify_fd, &to_read); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 183 | } |
| 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 190 | PLOG(FATAL, "read(%d, %p, %u) failed", notify_fd, notifyevt, to_read); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 191 | } |
| 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 198 | // Keep looping through until we get to the end because inotify does return |
| 199 | // multiple events at once. |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 200 | while (reinterpret_cast<char *>(notifyevt) < end) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 201 | if (watchers.count(notifyevt->wd) != 1) { |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 202 | LOG(WARNING, "couldn't find whose watch ID %d is\n", notifyevt->wd); |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 203 | } else { |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 204 | LOG(DEBUG, "mask=%" PRIu32 "\n", notifyevt->mask); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 205 | // If the watch was removed. |
| 206 | if (notifyevt->mask & IN_IGNORED) { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 207 | watchers[notifyevt->wd]->WatchDeleted(); |
| 208 | } else { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 209 | watchers[notifyevt->wd] |
| 210 | ->FileNotified((notifyevt->len > 0) ? notifyevt->name : NULL); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 211 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 212 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 213 | |
| 214 | notifyevt = reinterpret_cast<inotify_event *>( |
Brian Silverman | dbdf1d0 | 2013-11-17 13:19:41 -0800 | [diff] [blame] | 215 | __builtin_assume_aligned(reinterpret_cast<char *>(notifyevt) + |
| 216 | sizeof(*notifyevt) + notifyevt->len, |
Brian Silverman | afc00a6 | 2014-04-21 17:51:23 -0700 | [diff] [blame] | 217 | alignof(inotify_event))); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 221 | // 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 229 | // INotifyReadable calls this method whenever the watch for our file triggers. |
| 230 | void FileNotified(const char *filename) { |
Brian Silverman | fe457de | 2014-05-26 22:04:08 -0700 | [diff] [blame] | 231 | CHECK_NE(watch_, -1); |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 232 | LOG(DEBUG, "got a notification for %s\n", filename_.c_str()); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 233 | |
| 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 246 | // To make sure that Init gets called exactly once. |
| 247 | static ::aos::Once<void> init_once; |
| 248 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 249 | const std::string filename_; |
| 250 | const std::function<void(void *)> callback_; |
| 251 | void *const value_; |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 252 | const bool create_; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 253 | std::string check_filename_; |
| 254 | |
| 255 | // The watch descriptor or -1 if we don't have one any more. |
| 256 | int watch_; |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 257 | // 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 260 | |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 261 | // 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 265 | static std::map<int, FileWatch *> watchers; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 266 | // The inotify(7) file descriptor. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 267 | static int notify_fd; |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 268 | |
| 269 | DISALLOW_COPY_AND_ASSIGN(FileWatch); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 270 | }; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 271 | ::aos::Once<void> FileWatch::init_once(FileWatch::Init); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 272 | std::map<int, FileWatch *> FileWatch::watchers; |
| 273 | int FileWatch::notify_fd; |
| 274 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 275 | // 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 278 | std::string RunCommand(std::string command) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 279 | // popen(3) might fail and not set it. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 280 | errno = 0; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 281 | FILE *pipe = popen(command.c_str(), "r"); |
| 282 | if (pipe == NULL) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 283 | PLOG(FATAL, "popen(\"%s\", \"r\") failed", command.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 286 | // result_size is how many bytes result is currently allocated to. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 287 | size_t result_size = 128, read = 0; |
| 288 | unique_c_ptr<char> result(static_cast<char *>(malloc(result_size))); |
| 289 | while (true) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 290 | // If we filled up the buffer, then realloc(3) it bigger. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 291 | if (read == result_size) { |
| 292 | result_size *= 2; |
| 293 | void *new_result = realloc(result.get(), result_size); |
| 294 | if (new_result == NULL) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 295 | PLOG(FATAL, "realloc(%p, %zd) failed", result.get(), result_size); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 296 | } else { |
| 297 | result.release(); |
| 298 | result = unique_c_ptr<char>(static_cast<char *>(new_result)); |
| 299 | } |
| 300 | } |
| 301 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 302 | 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 305 | if (ret < result_size - read) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 306 | if (ferror(pipe)) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 307 | PLOG(FATAL, "couldn't finish reading output of \"%s\"\n", |
| 308 | command.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | read += ret; |
| 312 | if (read > 0 && result.get()[read - 1] == '\n') { |
| 313 | break; |
| 314 | } |
| 315 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 316 | if (feof(pipe)) { |
| 317 | LOG(FATAL, "`%s` failed. didn't print a whole line\n", command.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 321 | // Get rid of the first \n and anything after it. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 322 | *strchrnul(result.get(), '\n') = '\0'; |
| 323 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 324 | int child_status = pclose(pipe); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 325 | if (child_status == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 326 | PLOG(FATAL, "pclose(%p) failed", pipe); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 327 | } |
| 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 337 | void Timeout(monotonic_clock::duration time, |
| 338 | void (*callback)(int, short, void *), void *arg) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 339 | EventUniquePtr timeout(evtimer_new(libevent_base.get(), callback, arg)); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 340 | 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 Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 349 | if (evtimer_add(timeout.release(), &time_timeval) != 0) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 350 | LOG(FATAL, "evtimer_add(%p, %p) failed\n", timeout.release(), |
| 351 | &time_timeval); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 352 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 355 | class Child; |
| 356 | // This is where all of the Child instances except core live. |
| 357 | std::vector<unique_ptr<Child>> children; |
| 358 | // A global place to hold on to which child is core. |
| 359 | unique_ptr<Child> core; |
| 360 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 361 | // Represents a child process. It will take care of restarting itself etc. |
| 362 | class Child { |
| 363 | public: |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 364 | // command is the (space-separated) command to run and its arguments. |
| 365 | Child(const std::string &command) : pid_(-1), |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 366 | stat_at_start_valid_(false) { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 367 | if (!restart_timeout) { |
| 368 | restart_timeout = EventUniquePtr( |
| 369 | evtimer_new(libevent_base.get(), StaticDoRestart, nullptr)); |
| 370 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 371 | 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 382 | original_binary_ = RunCommand("which " + args_[0]); |
| 383 | binary_ = original_binary_ + ".stm"; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 384 | |
| 385 | watcher_ = unique_ptr<FileWatch>( |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 386 | new FileWatch(original_binary_, StaticFileModified, this)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 387 | |
| 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 397 | restarts_.push(monotonic_clock::now()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 398 | if (restarts_.size() > kMaxRestartsNumber) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 399 | monotonic_clock::time_point oldest = restarts_.front(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 400 | restarts_.pop(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 401 | if (monotonic_clock::now() <= kMaxRestartsTime + oldest) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 402 | 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 422 | static constexpr chrono::nanoseconds kProcessDieTime = chrono::seconds(2); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 423 | |
| 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 Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 428 | static constexpr chrono::nanoseconds kRestartWaitTime = |
| 429 | chrono::milliseconds(1500); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 430 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 431 | // Only kMaxRestartsNumber restarts will be allowed in kMaxRestartsTime. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 432 | static constexpr chrono::nanoseconds kMaxRestartsTime = chrono::seconds(4); |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 433 | static const size_t kMaxRestartsNumber = 3; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 434 | // How long to wait if it gets restarted too many times. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 435 | static constexpr chrono::nanoseconds kResumeWait = chrono::seconds(5); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 436 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 437 | static void StaticFileModified(void *self) { |
| 438 | static_cast<Child *>(self)->FileModified(); |
| 439 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 440 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 441 | void FileModified() { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 442 | LOG(DEBUG, "file for %s modified\n", name()); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 443 | 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 454 | // This will reset the timeout again if it hasn't run yet. |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 455 | if (evtimer_add(restart_timeout.get(), &restart_time_timeval) != 0) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 456 | LOG(FATAL, "evtimer_add(%p, %p) failed\n", restart_timeout.get(), |
| 457 | &restart_time_timeval); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 458 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 459 | waiting_to_restart.insert(this); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 462 | 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 474 | // Called after somebody else has finished modifying the file. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 475 | void DoRestart() { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 476 | fprintf(stderr, "DoRestart(%s)\n", binary_.c_str()); |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 477 | if (stat_at_start_valid_) { |
| 478 | struct stat current_stat; |
| 479 | if (stat(original_binary_.c_str(), ¤t_stat) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 480 | PLOG(FATAL, "stat(%s, %p) failed", |
| 481 | original_binary_.c_str(), ¤t_stat); |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 482 | } |
| 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 Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 490 | if (this == core.get()) { |
| 491 | fprintf(stderr, "Restarting core -> exiting now.\n"); |
| 492 | exit(0); |
| 493 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 494 | if (pid_ != -1) { |
| 495 | LOG(DEBUG, "sending SIGTERM to child %d to restart it\n", pid_); |
| 496 | if (kill(pid_, SIGTERM) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 497 | PLOG(WARNING, "kill(%d, SIGTERM) failed", pid_); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 498 | } |
| 499 | CheckDiedStatus *status = new CheckDiedStatus(); |
| 500 | status->self = this; |
| 501 | status->old_pid = pid_; |
| 502 | Timeout(kProcessDieTime, StaticCheckDied, status); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 503 | } else { |
| 504 | LOG(WARNING, "%s restart attempted but not running\n", name()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 505 | } |
| 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 513 | |
| 514 | // Checks to see if the child using the PID old_pid is still running. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 515 | 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 519 | PLOG(WARNING, "kill(%d, SIGKILL) failed", old_pid); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static void StaticStart(int, short, void *self) { |
| 525 | static_cast<Child *>(self)->Start(); |
| 526 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 527 | |
| 528 | // Actually starts the child. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 529 | void Start() { |
| 530 | if (pid_ != -1) { |
| 531 | LOG(WARNING, "calling Start() but already have child %d running\n", |
| 532 | pid_); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 533 | if (kill(pid_, SIGKILL) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 534 | PLOG(WARNING, "kill(%d, SIGKILL) failed", pid_); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 535 | return; |
| 536 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 537 | pid_ = -1; |
| 538 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 539 | |
| 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 543 | PLOG(FATAL, "removing %s failed", binary_.c_str()); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 544 | } |
| 545 | if (link(original_binary_.c_str(), binary_.c_str()) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 546 | PLOG(FATAL, "link('%s', '%s') failed", |
| 547 | original_binary_.c_str(), binary_.c_str()); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 550 | if (stat(original_binary_.c_str(), &stat_at_start_) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 551 | PLOG(FATAL, "stat(%s, %p) failed", |
| 552 | original_binary_.c_str(), &stat_at_start_); |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 553 | } |
| 554 | stat_at_start_valid_ = true; |
| 555 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 556 | 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 566 | PLOG(FATAL, "execv(%s, %p) failed", binary_.c_str(), argv); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 567 | _exit(EXIT_FAILURE); |
| 568 | } |
| 569 | if (pid_ == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 570 | PLOG(FATAL, "forking to run \"%s\" failed", binary_.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 571 | } |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 572 | LOG(DEBUG, "started \"%s\" successfully\n", binary_.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 573 | } |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 574 | |
| 575 | // A history of the times that this process has been restarted. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 576 | std::queue<monotonic_clock::time_point, |
| 577 | std::list<monotonic_clock::time_point>> restarts_; |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 578 | |
| 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 Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 593 | // 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 Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 598 | // 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 Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 604 | DISALLOW_COPY_AND_ASSIGN(Child); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 605 | }; |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 606 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 607 | constexpr chrono::nanoseconds Child::kProcessDieTime; |
| 608 | constexpr chrono::nanoseconds Child::kRestartWaitTime; |
| 609 | constexpr chrono::nanoseconds Child::kMaxRestartsTime; |
| 610 | constexpr chrono::nanoseconds Child::kResumeWait; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 611 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 612 | EventUniquePtr Child::restart_timeout; |
| 613 | ::std::set<Child *> Child::waiting_to_restart; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 614 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 615 | // Kills off the entire process group (including ourself). |
| 616 | void KillChildren(bool try_nice) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 617 | if (try_nice) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 618 | static constexpr int kNiceStopSignal = SIGTERM; |
| 619 | static constexpr auto kNiceWaitTime = chrono::seconds(1); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 620 | |
| 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 627 | kill(-getpid(), kNiceStopSignal); |
| 628 | |
| 629 | fflush(NULL); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 630 | ::std::this_thread::sleep_for(kNiceWaitTime); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 631 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 632 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 633 | // 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 635 | kill(-getpid(), SIGKILL); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 638 | void ExitHandler() { |
| 639 | KillChildren(true); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 640 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 641 | |
| 642 | void 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 Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 647 | signum == SIGABRT || signum == SIGPIPE || signum == SIGTERM || |
| 648 | signum == SIGXCPU); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 651 | // Returns the currently running child with PID pid or an empty unique_ptr. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 652 | const 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 663 | static const unique_ptr<Child> kNothing; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 664 | return kNothing; |
| 665 | } |
| 666 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 667 | // Gets set up as a libevent handler for SIGCHLD. |
| 668 | // Handles calling Child::ProcessDied() on the appropriate one. |
| 669 | void SigCHLDReceived(int /*fd*/, short /*events*/, void *) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 670 | // 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 675 | PLOG(WARNING, "waitid failed"); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 676 | continue; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 677 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 678 | // If there are no more child process deaths to process. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 679 | if (infop.si_pid == 0) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 680 | return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 681 | } |
| 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 Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 700 | pid, child->name(), status, aos_strsignal(status)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 701 | 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 715 | LOG(WARNING, "couldn't find a Child for pid %d\n", pid); |
| 716 | return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 719 | if (child == core) { |
| 720 | LOG(FATAL, "core died\n"); |
| 721 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 722 | child->ProcessDied(); |
| 723 | } |
| 724 | } |
| 725 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 726 | // This is used for communicating the name of the file to read processes to |
| 727 | // start from main to Run. |
| 728 | const char *child_list_file; |
| 729 | |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 730 | void Run(void *watch); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 731 | void Main() { |
| 732 | logging::Init(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 733 | |
Comran Morshed | 7f6ba79 | 2016-02-21 16:54:05 +0000 | [diff] [blame] | 734 | // 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 Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 743 | if (setpgid(0 /*self*/, 0 /*make PGID the same as PID*/) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 744 | PLOG(FATAL, "setpgid(0, 0) failed"); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 745 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 746 | |
| 747 | // Make sure that we kill all children when we exit. |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 748 | atexit(ExitHandler); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 749 | // Do it on some signals too (ones that we otherwise tend to receive and then |
| 750 | // leave all of our children going). |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 751 | 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 Silverman | 35df22f | 2015-12-27 17:57:10 -0800 | [diff] [blame] | 762 | |
| 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 Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 770 | |
| 771 | libevent_base = EventBaseUniquePtr(event_base_new()); |
| 772 | |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 773 | std::string core_touch_file = "/tmp/starter."; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 774 | core_touch_file += std::to_string(static_cast<intmax_t>(getpid())); |
| 775 | core_touch_file += ".core_touch_file"; |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 776 | const int result = |
| 777 | ::aos::util::RunCommand(("touch '" + core_touch_file + "'").c_str()); |
| 778 | if (result == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 779 | PLOG(FATAL, "running `touch '%s'` failed\n", core_touch_file.c_str()); |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 780 | } else if (!WIFEXITED(result) || WEXITSTATUS(result) != 0) { |
| 781 | LOG(FATAL, "`touch '%s'` gave result %x\n", core_touch_file.c_str(), |
| 782 | result); |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 783 | } |
| 784 | FileWatch core_touch_file_watch(core_touch_file, Run, NULL); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 785 | core = unique_ptr<Child>( |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 786 | new Child("core " + core_touch_file)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 787 | |
| 788 | FILE *pid_file = fopen("/tmp/starter.pid", "w"); |
| 789 | if (pid_file == NULL) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 790 | PLOG(FATAL, "fopen(\"/tmp/starter.pid\", \"w\") failed"); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 791 | } else { |
| 792 | if (fprintf(pid_file, "%d", core->pid()) == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 793 | PLOG(WARNING, "fprintf(%p, \"%%d\", %d) failed", |
| 794 | pid_file, core->pid()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 795 | } |
| 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 Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 805 | // This is the callback for when core creates the file indicating that it has |
| 806 | // started. |
| 807 | void 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 Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 834 | const 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"; |
| 839 | void PrintHelp() { |
| 840 | fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp); |
| 841 | } |
| 842 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 843 | } // namespace starter |
| 844 | } // namespace aos |
| 845 | |
| 846 | int main(int argc, char *argv[]) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 847 | if (argc != 2) { |
| 848 | aos::starter::PrintHelp(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 849 | exit(EXIT_FAILURE); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 850 | } |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 851 | if (strcmp(argv[1], "--help") == 0) { |
| 852 | aos::starter::PrintHelp(); |
| 853 | exit(EXIT_SUCCESS); |
| 854 | } |
| 855 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 856 | aos::starter::child_list_file = argv[1]; |
| 857 | |
| 858 | aos::starter::Main(); |
| 859 | } |