Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <sys/inotify.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/ioctl.h> |
| 8 | #include <assert.h> |
| 9 | #include <signal.h> |
| 10 | #include <stdint.h> |
| 11 | #include <errno.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/wait.h> |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 14 | #include <inttypes.h> |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 15 | |
| 16 | #include <map> |
| 17 | #include <functional> |
| 18 | #include <deque> |
| 19 | #include <fstream> |
| 20 | #include <queue> |
| 21 | #include <list> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | #include <memory> |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 25 | #include <set> |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 26 | |
| 27 | #include <event2/event.h> |
| 28 | |
| 29 | #include "aos/common/logging/logging.h" |
| 30 | #include "aos/common/logging/logging_impl.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 31 | #include "aos/linux_code/init.h" |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 32 | #include "aos/common/unique_malloc_ptr.h" |
| 33 | #include "aos/common/time.h" |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 34 | #include "aos/common/once.h" |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 35 | |
| 36 | // This is the main piece of code that starts all of the rest of the code and |
| 37 | // restarts it when the binaries are modified. |
| 38 | // |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 39 | // Throughout, the code is not terribly concerned with thread safety because |
| 40 | // there is only 1 thread. It does some setup and then lets inotify run things |
| 41 | // when appropriate. |
| 42 | // |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 43 | // NOTE: This program should never exit nicely. It catches all nice attempts to |
| 44 | // 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] | 45 | // them to exit nicely, and then SIGKILLs anybody left (which will always |
| 46 | // include itself). |
| 47 | |
| 48 | using ::std::unique_ptr; |
| 49 | |
| 50 | namespace aos { |
| 51 | namespace starter { |
| 52 | |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 53 | // 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] | 54 | class EventBaseDeleter { |
| 55 | public: |
| 56 | void operator()(event_base *base) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 57 | if (base == NULL) return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 58 | event_base_free(base); |
| 59 | } |
| 60 | }; |
| 61 | typedef unique_ptr<event_base, EventBaseDeleter> EventBaseUniquePtr; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 62 | EventBaseUniquePtr libevent_base; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 63 | |
| 64 | class EventDeleter { |
| 65 | public: |
| 66 | void operator()(event *evt) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 67 | if (evt == NULL) return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 68 | if (event_del(evt) != 0) { |
| 69 | LOG(WARNING, "event_del(%p) failed\n", evt); |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | typedef unique_ptr<event, EventDeleter> EventUniquePtr; |
| 74 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 75 | // Watches a file path for modifications. Once created, keeps watching until |
| 76 | // destroyed or RemoveWatch() is called. |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 77 | // TODO(brians): split this out into its own file + tests |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 78 | class FileWatch { |
| 79 | public: |
| 80 | // Will call callback(value) when filename is modified. |
| 81 | // 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] | 82 | // |
| 83 | // Watching for file creations is slightly different. To do that, pass true |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 84 | // 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] | 85 | // the name of the file (without directory name) for check_filename. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 86 | FileWatch(std::string filename, |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 87 | std::function<void(void *)> callback, |
| 88 | void *value, |
| 89 | bool create = false, |
| 90 | std::string check_filename = "") |
| 91 | : filename_(filename), |
| 92 | callback_(callback), |
| 93 | value_(value), |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 94 | create_(create), |
| 95 | check_filename_(check_filename), |
| 96 | watch_(-1) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 97 | init_once.Get(); |
| 98 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 99 | CreateWatch(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 100 | } |
| 101 | // Cleans up everything. |
| 102 | ~FileWatch() { |
| 103 | if (watch_ != -1) { |
| 104 | RemoveWatch(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // 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] | 109 | // anything besides possibly running its callback or something. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 110 | void RemoveWatch() { |
| 111 | assert(watch_ != -1); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 112 | assert(watch_to_remove_ == -1); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 113 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 114 | if (inotify_rm_watch(notify_fd, watch_) == -1) { |
| 115 | LOG(WARNING, "inotify_rm_watch(%d, %d) failed with %d: %s\n", |
| 116 | notify_fd, watch_, errno, strerror(errno)); |
| 117 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 118 | watch_to_remove_ = watch_; |
| 119 | watch_ = -1; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 122 | private: |
| 123 | // Performs the static initialization. Called by init_once from the |
| 124 | // constructor. |
| 125 | static void *Init() { |
| 126 | notify_fd = inotify_init1(IN_CLOEXEC); |
| 127 | EventUniquePtr notify_event(event_new(libevent_base.get(), notify_fd, |
| 128 | EV_READ | EV_PERSIST, |
| 129 | FileWatch::INotifyReadable, NULL)); |
| 130 | event_add(notify_event.release(), NULL); |
| 131 | return NULL; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 134 | void RemoveWatchFromMap() { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 135 | int watch = watch_to_remove_; |
| 136 | if (watch == -1) { |
| 137 | assert(watch_ != -1); |
| 138 | watch = watch_; |
| 139 | } |
| 140 | if (watchers[watch] != this) { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 141 | LOG(WARNING, "watcher for %s (%p) didn't find itself in the map\n", |
| 142 | filename_.c_str(), this); |
| 143 | } else { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 144 | watchers.erase(watch); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 145 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 146 | LOG(DEBUG, "removed watch ID %d\n", watch); |
| 147 | if (watch_to_remove_ == -1) { |
| 148 | watch_ = -1; |
| 149 | } else { |
| 150 | watch_to_remove_ = -1; |
| 151 | } |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void CreateWatch() { |
| 155 | assert(watch_ == -1); |
| 156 | watch_ = inotify_add_watch(notify_fd, filename_.c_str(), |
| 157 | create_ ? IN_CREATE : (IN_ATTRIB | |
| 158 | IN_MODIFY | |
| 159 | IN_DELETE_SELF | |
| 160 | IN_MOVE_SELF)); |
| 161 | if (watch_ == -1) { |
| 162 | LOG(FATAL, "inotify_add_watch(%d, %s," |
| 163 | " %s ? IN_CREATE : (IN_ATTRIB | IN_MODIFY)) failed with %d: %s\n", |
| 164 | notify_fd, filename_.c_str(), create_ ? "true" : "false", |
| 165 | errno, strerror(errno)); |
| 166 | } |
| 167 | watchers[watch_] = this; |
| 168 | LOG(DEBUG, "watch for %s is %d\n", filename_.c_str(), watch_); |
| 169 | } |
| 170 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 171 | // 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] | 172 | // descriptor. It calls FileNotified on the appropriate instance. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 173 | static void INotifyReadable(int /*fd*/, short /*events*/, void *) { |
| 174 | unsigned int to_read; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 175 | // Use FIONREAD to figure out how many bytes there are to read. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 176 | if (ioctl(notify_fd, FIONREAD, &to_read) < 0) { |
| 177 | LOG(FATAL, "FIONREAD(%d, %p) failed with %d: %s\n", |
| 178 | notify_fd, &to_read, errno, strerror(errno)); |
| 179 | } |
| 180 | inotify_event *notifyevt = static_cast<inotify_event *>(malloc(to_read)); |
| 181 | const char *end = reinterpret_cast<char *>(notifyevt) + to_read; |
| 182 | aos::unique_c_ptr<inotify_event> freer(notifyevt); |
| 183 | |
| 184 | ssize_t ret = read(notify_fd, notifyevt, to_read); |
| 185 | if (ret < 0) { |
| 186 | LOG(FATAL, "read(%d, %p, %u) failed with %d: %s\n", |
| 187 | notify_fd, notifyevt, to_read, errno, strerror(errno)); |
| 188 | } |
| 189 | if (static_cast<size_t>(ret) != to_read) { |
| 190 | LOG(ERROR, "read(%d, %p, %u) returned %zd instead of %u\n", |
| 191 | notify_fd, notifyevt, to_read, ret, to_read); |
| 192 | return; |
| 193 | } |
| 194 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 195 | // Keep looping through until we get to the end because inotify does return |
| 196 | // multiple events at once. |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 197 | while (reinterpret_cast<char *>(notifyevt) < end) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 198 | if (watchers.count(notifyevt->wd) != 1) { |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 199 | 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] | 200 | } else { |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 201 | LOG(DEBUG, "mask=%" PRIu32 "\n", notifyevt->mask); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 202 | // If the watch was removed. |
| 203 | if (notifyevt->mask & IN_IGNORED) { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 204 | watchers[notifyevt->wd]->WatchDeleted(); |
| 205 | } else { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 206 | watchers[notifyevt->wd] |
| 207 | ->FileNotified((notifyevt->len > 0) ? notifyevt->name : NULL); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 208 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 209 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 210 | |
| 211 | notifyevt = reinterpret_cast<inotify_event *>( |
Brian Silverman | dbdf1d0 | 2013-11-17 13:19:41 -0800 | [diff] [blame] | 212 | __builtin_assume_aligned(reinterpret_cast<char *>(notifyevt) + |
| 213 | sizeof(*notifyevt) + notifyevt->len, |
Brian Silverman | afc00a6 | 2014-04-21 17:51:23 -0700 | [diff] [blame^] | 214 | alignof(inotify_event))); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 218 | // INotifyReadable calls this method whenever the watch for our file gets |
| 219 | // removed somehow. |
| 220 | void WatchDeleted() { |
| 221 | LOG(DEBUG, "watch for %s deleted\n", filename_.c_str()); |
| 222 | RemoveWatchFromMap(); |
| 223 | CreateWatch(); |
| 224 | } |
| 225 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 226 | // INotifyReadable calls this method whenever the watch for our file triggers. |
| 227 | void FileNotified(const char *filename) { |
| 228 | assert(watch_ != -1); |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 229 | LOG(DEBUG, "got a notification for %s\n", filename_.c_str()); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 230 | |
| 231 | if (!check_filename_.empty()) { |
| 232 | if (filename == NULL) { |
| 233 | return; |
| 234 | } |
| 235 | if (std::string(filename) != check_filename_) { |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | callback_((value_ == NULL) ? this : value_); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 243 | // To make sure that Init gets called exactly once. |
| 244 | static ::aos::Once<void> init_once; |
| 245 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 246 | const std::string filename_; |
| 247 | const std::function<void(void *)> callback_; |
| 248 | void *const value_; |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 249 | const bool create_; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 250 | std::string check_filename_; |
| 251 | |
| 252 | // The watch descriptor or -1 if we don't have one any more. |
| 253 | int watch_; |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 254 | // The watch that we still have to take out of the map once we get the |
| 255 | // IN_IGNORED or -1. |
| 256 | int watch_to_remove_ = -1; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 257 | |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 258 | // Map from watch IDs to instances of this class. |
| 259 | // <https://patchwork.kernel.org/patch/73192/> ("inotify: do not reuse watch |
| 260 | // descriptors") says they won't get reused, but that shouldn't be counted on |
| 261 | // because we might have a modified/different version/whatever kernel. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 262 | static std::map<int, FileWatch *> watchers; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 263 | // The inotify(7) file descriptor. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 264 | static int notify_fd; |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 265 | |
| 266 | DISALLOW_COPY_AND_ASSIGN(FileWatch); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 267 | }; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 268 | ::aos::Once<void> FileWatch::init_once(FileWatch::Init); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 269 | std::map<int, FileWatch *> FileWatch::watchers; |
| 270 | int FileWatch::notify_fd; |
| 271 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 272 | // Runs the given command and returns its first line of output (not including |
| 273 | // the \n). LOG(FATAL)s if the command has an exit status other than 0 or does |
| 274 | // not print out an entire line. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 275 | std::string RunCommand(std::string command) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 276 | // popen(3) might fail and not set it. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 277 | errno = 0; |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 278 | FILE *pipe = popen(command.c_str(), "r"); |
| 279 | if (pipe == NULL) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 280 | LOG(FATAL, "popen(\"%s\", \"r\") failed with %d: %s\n", |
| 281 | command.c_str(), errno, strerror(errno)); |
| 282 | } |
| 283 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 284 | // result_size is how many bytes result is currently allocated to. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 285 | size_t result_size = 128, read = 0; |
| 286 | unique_c_ptr<char> result(static_cast<char *>(malloc(result_size))); |
| 287 | while (true) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 288 | // If we filled up the buffer, then realloc(3) it bigger. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 289 | if (read == result_size) { |
| 290 | result_size *= 2; |
| 291 | void *new_result = realloc(result.get(), result_size); |
| 292 | if (new_result == NULL) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 293 | LOG(FATAL, "realloc(%p, %zd) failed because of %d: %s\n", |
| 294 | result.get(), result_size, errno, strerror(errno)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 295 | } else { |
| 296 | result.release(); |
| 297 | result = unique_c_ptr<char>(static_cast<char *>(new_result)); |
| 298 | } |
| 299 | } |
| 300 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 301 | size_t ret = fread(result.get() + read, 1, result_size - read, pipe); |
| 302 | // If the read didn't fill up the whole buffer, check to see if it was |
| 303 | // because of an error. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 304 | if (ret < result_size - read) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 305 | if (ferror(pipe)) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 306 | LOG(FATAL, "couldn't finish reading output of \"%s\"\n", |
| 307 | command.c_str()); |
| 308 | } |
| 309 | } |
| 310 | read += ret; |
| 311 | if (read > 0 && result.get()[read - 1] == '\n') { |
| 312 | break; |
| 313 | } |
| 314 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 315 | if (feof(pipe)) { |
| 316 | 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] | 317 | } |
| 318 | } |
| 319 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 320 | // Get rid of the first \n and anything after it. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 321 | *strchrnul(result.get(), '\n') = '\0'; |
| 322 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 323 | int child_status = pclose(pipe); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 324 | if (child_status == -1) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 325 | LOG(FATAL, "pclose(%p) failed with %d: %s\n", pipe, |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 326 | errno, strerror(errno)); |
| 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. |
| 337 | void Timeout(time::Time time, void (*callback)(int, short, void *), void *arg) { |
| 338 | EventUniquePtr timeout(evtimer_new(libevent_base.get(), callback, arg)); |
| 339 | struct timeval time_timeval = time.ToTimeval(); |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 340 | if (evtimer_add(timeout.release(), &time_timeval) != 0) { |
| 341 | LOG(FATAL, "evtimer_add(%p, %p) failed\n", |
| 342 | timeout.release(), &time_timeval); |
| 343 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 346 | class Child; |
| 347 | // This is where all of the Child instances except core live. |
| 348 | std::vector<unique_ptr<Child>> children; |
| 349 | // A global place to hold on to which child is core. |
| 350 | unique_ptr<Child> core; |
| 351 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 352 | // Represents a child process. It will take care of restarting itself etc. |
| 353 | class Child { |
| 354 | public: |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 355 | // command is the (space-separated) command to run and its arguments. |
| 356 | Child(const std::string &command) : pid_(-1), |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 357 | stat_at_start_valid_(false) { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 358 | if (!restart_timeout) { |
| 359 | restart_timeout = EventUniquePtr( |
| 360 | evtimer_new(libevent_base.get(), StaticDoRestart, nullptr)); |
| 361 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 362 | const char *start, *end; |
| 363 | start = command.c_str(); |
| 364 | while (true) { |
| 365 | end = strchrnul(start, ' '); |
| 366 | args_.push_back(std::string(start, end - start)); |
| 367 | start = end + 1; |
| 368 | if (*end == '\0') { |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 373 | original_binary_ = RunCommand("which " + args_[0]); |
| 374 | binary_ = original_binary_ + ".stm"; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 375 | |
| 376 | watcher_ = unique_ptr<FileWatch>( |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 377 | new FileWatch(original_binary_, StaticFileModified, this)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 378 | |
| 379 | Start(); |
| 380 | } |
| 381 | |
| 382 | pid_t pid() { return pid_; } |
| 383 | |
| 384 | // This gets called whenever the actual process dies and should (probably) be |
| 385 | // restarted. |
| 386 | void ProcessDied() { |
| 387 | pid_ = -1; |
| 388 | restarts_.push(time::Time::Now()); |
| 389 | if (restarts_.size() > kMaxRestartsNumber) { |
| 390 | time::Time oldest = restarts_.front(); |
| 391 | restarts_.pop(); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 392 | if ((time::Time::Now() - oldest) <= kMaxRestartsTime) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 393 | LOG(WARNING, "process %s getting restarted too often\n", name()); |
| 394 | Timeout(kResumeWait, StaticStart, this); |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | Start(); |
| 399 | } |
| 400 | |
| 401 | // Returns a name for logging purposes. |
| 402 | const char *name() { |
| 403 | return args_[0].c_str(); |
| 404 | } |
| 405 | |
| 406 | private: |
| 407 | struct CheckDiedStatus { |
| 408 | Child *self; |
| 409 | pid_t old_pid; |
| 410 | }; |
| 411 | |
| 412 | // How long to wait for a child to die nicely. |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 413 | static constexpr time::Time kProcessDieTime = time::Time::InSeconds(2); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 414 | |
| 415 | // How long to wait after the file is modified to restart it. |
| 416 | // This is important because some programs like modifying the binaries by |
| 417 | // writing them in little bits, which results in attempting to start partial |
| 418 | // binaries without this. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 419 | static constexpr time::Time kRestartWaitTime = time::Time::InSeconds(1.5); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 420 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 421 | // Only kMaxRestartsNumber restarts will be allowed in kMaxRestartsTime. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 422 | static constexpr time::Time kMaxRestartsTime = time::Time::InSeconds(4); |
| 423 | static const size_t kMaxRestartsNumber = 3; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 424 | // How long to wait if it gets restarted too many times. |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 425 | static constexpr time::Time kResumeWait = time::Time::InSeconds(5); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 426 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 427 | static void StaticFileModified(void *self) { |
| 428 | static_cast<Child *>(self)->FileModified(); |
| 429 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 430 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 431 | void FileModified() { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 432 | LOG(DEBUG, "file for %s modified\n", name()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 433 | struct timeval restart_time_timeval = kRestartWaitTime.ToTimeval(); |
| 434 | // This will reset the timeout again if it hasn't run yet. |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 435 | if (evtimer_add(restart_timeout.get(), &restart_time_timeval) != 0) { |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 436 | LOG(FATAL, "evtimer_add(%p, %p) failed\n", |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 437 | restart_timeout.get(), &restart_time_timeval); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 438 | } |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 439 | waiting_to_restart.insert(this); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 442 | static void StaticDoRestart(int, short, void *) { |
| 443 | LOG(DEBUG, "restarting everything that needs it\n"); |
| 444 | if (waiting_to_restart.find(core.get()) != waiting_to_restart.end()) { |
| 445 | core->DoRestart(); |
| 446 | waiting_to_restart.erase(core.get()); |
| 447 | } |
| 448 | for (auto c : waiting_to_restart) { |
| 449 | c->DoRestart(); |
| 450 | } |
| 451 | waiting_to_restart.clear(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 454 | // Called after somebody else has finished modifying the file. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 455 | void DoRestart() { |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 456 | fprintf(stderr, "DoRestart(%s)\n", binary_.c_str()); |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 457 | if (stat_at_start_valid_) { |
| 458 | struct stat current_stat; |
| 459 | if (stat(original_binary_.c_str(), ¤t_stat) == -1) { |
| 460 | LOG(FATAL, "stat(%s, %p) failed with %d: %s\n", |
| 461 | original_binary_.c_str(), ¤t_stat, errno, strerror(errno)); |
| 462 | } |
| 463 | if (current_stat.st_mtime == stat_at_start_.st_mtime) { |
| 464 | LOG(DEBUG, "ignoring trigger for %s because mtime didn't change\n", |
| 465 | name()); |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 470 | if (this == core.get()) { |
| 471 | fprintf(stderr, "Restarting core -> exiting now.\n"); |
| 472 | exit(0); |
| 473 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 474 | if (pid_ != -1) { |
| 475 | LOG(DEBUG, "sending SIGTERM to child %d to restart it\n", pid_); |
| 476 | if (kill(pid_, SIGTERM) == -1) { |
| 477 | LOG(WARNING, "kill(%d, SIGTERM) failed with %d: %s\n", |
| 478 | pid_, errno, strerror(errno)); |
| 479 | } |
| 480 | CheckDiedStatus *status = new CheckDiedStatus(); |
| 481 | status->self = this; |
| 482 | status->old_pid = pid_; |
| 483 | Timeout(kProcessDieTime, StaticCheckDied, status); |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 484 | } else { |
| 485 | LOG(WARNING, "%s restart attempted but not running\n", name()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | static void StaticCheckDied(int, short, void *status_in) { |
| 490 | CheckDiedStatus *status = static_cast<CheckDiedStatus *>(status_in); |
| 491 | status->self->CheckDied(status->old_pid); |
| 492 | delete status; |
| 493 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 494 | |
| 495 | // 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] | 496 | void CheckDied(pid_t old_pid) { |
| 497 | if (pid_ == old_pid) { |
| 498 | LOG(WARNING, "child %d refused to die\n", old_pid); |
| 499 | if (kill(old_pid, SIGKILL) == -1) { |
| 500 | LOG(WARNING, "kill(%d, SIGKILL) failed with %d: %s\n", |
| 501 | old_pid, errno, strerror(errno)); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void StaticStart(int, short, void *self) { |
| 507 | static_cast<Child *>(self)->Start(); |
| 508 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 509 | |
| 510 | // Actually starts the child. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 511 | void Start() { |
| 512 | if (pid_ != -1) { |
| 513 | LOG(WARNING, "calling Start() but already have child %d running\n", |
| 514 | pid_); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 515 | if (kill(pid_, SIGKILL) == -1) { |
| 516 | LOG(WARNING, "kill(%d, SIGKILL) failed with %d: %s\n", |
| 517 | pid_, errno, strerror(errno)); |
| 518 | return; |
| 519 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 520 | pid_ = -1; |
| 521 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 522 | |
| 523 | // Remove the name that we run from (ie from a previous execution) and then |
| 524 | // hard link the real filename to it. |
| 525 | if (unlink(binary_.c_str()) != 0 && errno != ENOENT) { |
| 526 | LOG(FATAL, "removing %s failed because of %d: %s\n", |
| 527 | binary_.c_str(), errno, strerror(errno)); |
| 528 | } |
| 529 | if (link(original_binary_.c_str(), binary_.c_str()) != 0) { |
| 530 | LOG(FATAL, "link('%s', '%s') failed because of %d: %s\n", |
| 531 | original_binary_.c_str(), binary_.c_str(), errno, strerror(errno)); |
| 532 | } |
| 533 | |
Brian Silverman | fe06fe1 | 2013-02-27 18:54:58 -0800 | [diff] [blame] | 534 | if (stat(original_binary_.c_str(), &stat_at_start_) == -1) { |
| 535 | LOG(FATAL, "stat(%s, %p) failed with %d: %s\n", |
| 536 | original_binary_.c_str(), &stat_at_start_, errno, strerror(errno)); |
| 537 | } |
| 538 | stat_at_start_valid_ = true; |
| 539 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 540 | if ((pid_ = fork()) == 0) { |
| 541 | ssize_t args_size = args_.size(); |
| 542 | const char **argv = new const char *[args_size + 1]; |
| 543 | for (int i = 0; i < args_size; ++i) { |
| 544 | argv[i] = args_[i].c_str(); |
| 545 | } |
| 546 | argv[args_size] = NULL; |
| 547 | // The const_cast is safe because no code that might care if it gets |
| 548 | // modified can run afterwards. |
| 549 | execv(binary_.c_str(), const_cast<char **>(argv)); |
| 550 | LOG(FATAL, "execv(%s, %p) failed with %d: %s\n", |
| 551 | binary_.c_str(), argv, errno, strerror(errno)); |
| 552 | _exit(EXIT_FAILURE); |
| 553 | } |
| 554 | if (pid_ == -1) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 555 | LOG(FATAL, "forking to run \"%s\" failed with %d: %s\n", |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 556 | binary_.c_str(), errno, strerror(errno)); |
| 557 | } |
Brian Silverman | d90b5fe | 2013-03-10 18:34:42 -0700 | [diff] [blame] | 558 | LOG(DEBUG, "started \"%s\" successfully\n", binary_.c_str()); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 559 | } |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 560 | |
| 561 | // A history of the times that this process has been restarted. |
| 562 | std::queue<time::Time, std::list<time::Time>> restarts_; |
| 563 | |
| 564 | // The currently running child's PID or NULL. |
| 565 | pid_t pid_; |
| 566 | |
| 567 | // All of the arguments (including the name of the binary). |
| 568 | std::deque<std::string> args_; |
| 569 | |
| 570 | // The name of the real binary that we were told to run. |
| 571 | std::string original_binary_; |
| 572 | // The name of the file that we're actually running. |
| 573 | std::string binary_; |
| 574 | |
| 575 | // Watches original_binary_. |
| 576 | unique_ptr<FileWatch> watcher_; |
| 577 | |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 578 | // Captured from the original file when we most recently started a new child |
| 579 | // process. Used to see if it actually changes or not. |
| 580 | struct stat stat_at_start_; |
| 581 | bool stat_at_start_valid_; |
| 582 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 583 | // An event that restarts after kRestartWaitTime. |
| 584 | static EventUniquePtr restart_timeout; |
| 585 | |
| 586 | // The set of children waiting to be restarted once all modifications stop. |
| 587 | static ::std::set<Child *> waiting_to_restart; |
| 588 | |
Brian Silverman | bc4fc2f | 2013-02-27 19:33:42 -0800 | [diff] [blame] | 589 | DISALLOW_COPY_AND_ASSIGN(Child); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 590 | }; |
Brian Silverman | 52aeeac | 2013-08-28 16:20:53 -0700 | [diff] [blame] | 591 | |
| 592 | constexpr time::Time Child::kProcessDieTime; |
| 593 | constexpr time::Time Child::kRestartWaitTime; |
| 594 | constexpr time::Time Child::kMaxRestartsTime; |
| 595 | constexpr time::Time Child::kResumeWait; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 596 | |
Brian Silverman | d94642c | 2014-03-27 18:21:41 -0700 | [diff] [blame] | 597 | EventUniquePtr Child::restart_timeout; |
| 598 | ::std::set<Child *> Child::waiting_to_restart; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 599 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 600 | // Kills off the entire process group (including ourself). |
| 601 | void KillChildren(bool try_nice) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 602 | if (try_nice) { |
| 603 | static const int kNiceStopSignal = SIGTERM; |
| 604 | static const time::Time kNiceWaitTime = time::Time::InSeconds(1); |
| 605 | |
| 606 | // Make sure that we don't just nicely stop ourself... |
| 607 | sigset_t mask; |
| 608 | sigemptyset(&mask); |
| 609 | sigaddset(&mask, kNiceStopSignal); |
| 610 | sigprocmask(SIG_BLOCK, &mask, NULL); |
| 611 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 612 | kill(-getpid(), kNiceStopSignal); |
| 613 | |
| 614 | fflush(NULL); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 615 | time::SleepFor(kNiceWaitTime); |
| 616 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 617 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 618 | // Send SIGKILL to our whole process group, which will forcibly terminate any |
| 619 | // of them that are still running (us for sure, maybe more too). |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 620 | kill(-getpid(), SIGKILL); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 623 | void ExitHandler() { |
| 624 | KillChildren(true); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 625 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 626 | |
| 627 | void KillChildrenSignalHandler(int signum) { |
| 628 | // If we get SIGSEGV or some other random signal who knows what's happening |
| 629 | // and we should just kill everybody immediately. |
| 630 | // This is a list of all of the signals that mean some form of "nicely stop". |
| 631 | KillChildren(signum == SIGHUP || signum == SIGINT || signum == SIGQUIT || |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 632 | signum == SIGABRT || signum == SIGPIPE || signum == SIGTERM || |
| 633 | signum == SIGXCPU); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 636 | // 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] | 637 | const unique_ptr<Child> &FindChild(pid_t pid) { |
| 638 | for (auto it = children.begin(); it != children.end(); ++it) { |
| 639 | if (pid == (*it)->pid()) { |
| 640 | return *it; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (pid == core->pid()) { |
| 645 | return core; |
| 646 | } |
| 647 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 648 | static const unique_ptr<Child> kNothing; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 649 | return kNothing; |
| 650 | } |
| 651 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 652 | // Gets set up as a libevent handler for SIGCHLD. |
| 653 | // Handles calling Child::ProcessDied() on the appropriate one. |
| 654 | void SigCHLDReceived(int /*fd*/, short /*events*/, void *) { |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 655 | // In a while loop in case we miss any SIGCHLDs. |
| 656 | while (true) { |
| 657 | siginfo_t infop; |
| 658 | infop.si_pid = 0; |
| 659 | if (waitid(P_ALL, 0, &infop, WEXITED | WSTOPPED | WNOHANG) != 0) { |
| 660 | LOG(WARNING, "waitid failed with %d: %s", errno, strerror(errno)); |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 661 | continue; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 662 | } |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 663 | // If there are no more child process deaths to process. |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 664 | if (infop.si_pid == 0) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 665 | return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | pid_t pid = infop.si_pid; |
| 669 | int status = infop.si_status; |
| 670 | const unique_ptr<Child> &child = FindChild(pid); |
| 671 | if (child) { |
| 672 | switch (infop.si_code) { |
| 673 | case CLD_EXITED: |
| 674 | LOG(WARNING, "child %d (%s) exited with status %d\n", |
| 675 | pid, child->name(), status); |
| 676 | break; |
| 677 | case CLD_DUMPED: |
| 678 | LOG(INFO, "child %d actually dumped core. " |
| 679 | "falling through to killed by signal case\n", pid); |
| 680 | case CLD_KILLED: |
| 681 | // If somebody (possibly us) sent it SIGTERM that means that they just |
| 682 | // want it to stop, so it stopping isn't a WARNING. |
| 683 | LOG((status == SIGTERM) ? DEBUG : WARNING, |
| 684 | "child %d (%s) was killed by signal %d (%s)\n", |
| 685 | pid, child->name(), status, |
| 686 | strsignal(status)); |
| 687 | break; |
| 688 | case CLD_STOPPED: |
| 689 | LOG(WARNING, "child %d (%s) was stopped by signal %d " |
| 690 | "(giving it a SIGCONT(%d))\n", |
| 691 | pid, child->name(), status, SIGCONT); |
| 692 | kill(pid, SIGCONT); |
| 693 | continue; |
| 694 | default: |
| 695 | LOG(WARNING, "something happened to child %d (%s) (killing it)\n", |
| 696 | pid, child->name()); |
| 697 | kill(pid, SIGKILL); |
| 698 | continue; |
| 699 | } |
| 700 | } else { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 701 | LOG(WARNING, "couldn't find a Child for pid %d\n", pid); |
| 702 | return; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 705 | if (child == core) { |
| 706 | LOG(FATAL, "core died\n"); |
| 707 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 708 | child->ProcessDied(); |
| 709 | } |
| 710 | } |
| 711 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 712 | // This is used for communicating the name of the file to read processes to |
| 713 | // start from main to Run. |
| 714 | const char *child_list_file; |
| 715 | |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 716 | void Run(void *watch); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 717 | void Main() { |
| 718 | logging::Init(); |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 719 | // TODO(brians): tell logging that using the root logger from here until we |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 720 | // bring up shm is ok |
| 721 | |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 722 | if (setpgid(0 /*self*/, 0 /*make PGID the same as PID*/) != 0) { |
| 723 | LOG(FATAL, "setpgid(0, 0) failed with %d: %s\n", errno, strerror(errno)); |
| 724 | } |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 725 | |
| 726 | // Make sure that we kill all children when we exit. |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 727 | atexit(ExitHandler); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 728 | // Do it on some signals too (ones that we otherwise tend to receive and then |
| 729 | // leave all of our children going). |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 730 | signal(SIGHUP, KillChildrenSignalHandler); |
| 731 | signal(SIGINT, KillChildrenSignalHandler); |
| 732 | signal(SIGQUIT, KillChildrenSignalHandler); |
| 733 | signal(SIGILL, KillChildrenSignalHandler); |
| 734 | signal(SIGABRT, KillChildrenSignalHandler); |
| 735 | signal(SIGFPE, KillChildrenSignalHandler); |
| 736 | signal(SIGSEGV, KillChildrenSignalHandler); |
| 737 | signal(SIGPIPE, KillChildrenSignalHandler); |
| 738 | signal(SIGTERM, KillChildrenSignalHandler); |
| 739 | signal(SIGBUS, KillChildrenSignalHandler); |
| 740 | signal(SIGXCPU, KillChildrenSignalHandler); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 741 | |
| 742 | libevent_base = EventBaseUniquePtr(event_base_new()); |
| 743 | |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 744 | std::string core_touch_file = "/tmp/starter."; |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 745 | core_touch_file += std::to_string(static_cast<intmax_t>(getpid())); |
| 746 | core_touch_file += ".core_touch_file"; |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 747 | if (system(("touch '" + core_touch_file + "'").c_str()) != 0) { |
| 748 | LOG(FATAL, "running `touch '%s'` failed\n", core_touch_file.c_str()); |
| 749 | } |
| 750 | FileWatch core_touch_file_watch(core_touch_file, Run, NULL); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 751 | core = unique_ptr<Child>( |
Brian Silverman | b1e4f6c | 2013-02-27 15:42:02 -0800 | [diff] [blame] | 752 | new Child("core " + core_touch_file)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 753 | |
| 754 | FILE *pid_file = fopen("/tmp/starter.pid", "w"); |
| 755 | if (pid_file == NULL) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 756 | LOG(FATAL, "fopen(\"/tmp/starter.pid\", \"w\") failed with %d: %s\n", |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 757 | errno, strerror(errno)); |
| 758 | } else { |
| 759 | if (fprintf(pid_file, "%d", core->pid()) == -1) { |
Brian Silverman | 5cc661b | 2013-02-27 15:23:36 -0800 | [diff] [blame] | 760 | LOG(WARNING, "fprintf(%p, \"%%d\", %d) failed with %d: %s\n", |
| 761 | pid_file, core->pid(), errno, strerror(errno)); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 762 | } |
| 763 | fclose(pid_file); |
| 764 | } |
| 765 | |
| 766 | LOG(INFO, "waiting for %s to appear\n", core_touch_file.c_str()); |
| 767 | |
| 768 | event_base_dispatch(libevent_base.get()); |
| 769 | LOG(FATAL, "event_base_dispatch(%p) returned\n", libevent_base.get()); |
| 770 | } |
| 771 | |
Brian Silverman | 0eec953 | 2013-02-27 20:24:16 -0800 | [diff] [blame] | 772 | // This is the callback for when core creates the file indicating that it has |
| 773 | // started. |
| 774 | void Run(void *watch) { |
| 775 | // Make it so it doesn't keep on seeing random changes in /tmp. |
| 776 | static_cast<FileWatch *>(watch)->RemoveWatch(); |
| 777 | |
| 778 | // It's safe now because core is up. |
| 779 | aos::InitNRT(); |
| 780 | |
| 781 | std::ifstream list_file(child_list_file); |
| 782 | |
| 783 | while (true) { |
| 784 | std::string child_name; |
| 785 | getline(list_file, child_name); |
| 786 | if ((list_file.rdstate() & std::ios_base::eofbit) != 0) { |
| 787 | break; |
| 788 | } |
| 789 | if (list_file.rdstate() != 0) { |
| 790 | LOG(FATAL, "reading input file %s failed\n", child_list_file); |
| 791 | } |
| 792 | children.push_back(unique_ptr<Child>(new Child(child_name))); |
| 793 | } |
| 794 | |
| 795 | EventUniquePtr sigchld(event_new(libevent_base.get(), SIGCHLD, |
| 796 | EV_SIGNAL | EV_PERSIST, |
| 797 | SigCHLDReceived, NULL)); |
| 798 | event_add(sigchld.release(), NULL); |
| 799 | } |
| 800 | |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 801 | const char *kArgsHelp = "[OPTION]... START_LIST\n" |
| 802 | "Start all of the robot code binaries in START_LIST.\n" |
| 803 | "\n" |
| 804 | "START_LIST is the file to read binaries (looked up on PATH) to run.\n" |
| 805 | " --help display this help and exit\n"; |
| 806 | void PrintHelp() { |
| 807 | fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp); |
| 808 | } |
| 809 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 810 | } // namespace starter |
| 811 | } // namespace aos |
| 812 | |
| 813 | int main(int argc, char *argv[]) { |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 814 | if (argc != 2) { |
| 815 | aos::starter::PrintHelp(); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 816 | exit(EXIT_FAILURE); |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 817 | } |
Brian Silverman | 8070a22 | 2013-02-28 15:01:36 -0800 | [diff] [blame] | 818 | if (strcmp(argv[1], "--help") == 0) { |
| 819 | aos::starter::PrintHelp(); |
| 820 | exit(EXIT_SUCCESS); |
| 821 | } |
| 822 | |
Brian Silverman | d169fcd | 2013-02-27 13:18:47 -0800 | [diff] [blame] | 823 | aos::starter::child_list_file = argv[1]; |
| 824 | |
| 825 | aos::starter::Main(); |
| 826 | } |