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