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