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