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