Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // blaze.cc: bootstrap and client code for Blaze server. |
| 16 | // |
| 17 | // Responsible for: |
| 18 | // - extracting the Python, C++ and Java components. |
| 19 | // - starting the server or finding the existing one. |
| 20 | // - client options parsing. |
| 21 | // - passing the argv array, and printing the out/err streams. |
| 22 | // - signal handling. |
| 23 | // - exiting with the right error/WTERMSIG code. |
| 24 | // - debugger + profiler support. |
| 25 | // - mutual exclusion between batch invocations. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 26 | #include "src/main/cpp/blaze.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | |
| 28 | #include <assert.h> |
| 29 | #include <ctype.h> |
| 30 | #include <dirent.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <limits.h> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | #include <sched.h> |
| 35 | #include <signal.h> |
| 36 | #include <stdarg.h> |
Thiago Farina | 8a67da4 | 2015-05-05 18:04:50 +0000 | [diff] [blame] | 37 | #include <stdint.h> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/resource.h> |
| 42 | #include <sys/select.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <sys/statvfs.h> |
| 46 | #include <sys/time.h> |
| 47 | #include <sys/un.h> |
| 48 | #include <time.h> |
| 49 | #include <unistd.h> |
| 50 | #include <utime.h> |
Lukacs Berki | e21e592 | 2016-04-12 12:22:20 +0000 | [diff] [blame] | 51 | |
| 52 | #include <grpc/grpc.h> |
Googler | 197547b | 2016-09-26 22:25:14 +0000 | [diff] [blame] | 53 | #include <grpc/support/log.h> |
Lukacs Berki | e21e592 | 2016-04-12 12:22:20 +0000 | [diff] [blame] | 54 | #include <grpc++/channel.h> |
| 55 | #include <grpc++/client_context.h> |
| 56 | #include <grpc++/create_channel.h> |
| 57 | #include <grpc++/security/credentials.h> |
| 58 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | #include <algorithm> |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 60 | #include <chrono> // NOLINT (gRPC requires this) |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 61 | #include <mutex> // NOLINT |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | #include <set> |
| 63 | #include <string> |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 64 | #include <thread> // NOLINT |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | #include <utility> |
| 66 | #include <vector> |
| 67 | |
Lukacs Berki | e21e592 | 2016-04-12 12:22:20 +0000 | [diff] [blame] | 68 | |
Nathan Harmata | bf98f39 | 2016-01-07 22:58:29 +0000 | [diff] [blame] | 69 | #include "src/main/cpp/blaze_abrupt_exit.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 70 | #include "src/main/cpp/blaze_util.h" |
| 71 | #include "src/main/cpp/blaze_util_platform.h" |
Thiago Farina | 676cb9f | 2016-10-06 11:00:43 +0000 | [diff] [blame] | 72 | #include "src/main/cpp/global_variables.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 73 | #include "src/main/cpp/option_processor.h" |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 74 | #include "src/main/cpp/startup_options.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 75 | #include "src/main/cpp/util/errors.h" |
Thiago Farina | 7f9357f | 2015-04-23 13:57:43 +0000 | [diff] [blame] | 76 | #include "src/main/cpp/util/exit_code.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 77 | #include "src/main/cpp/util/file.h" |
| 78 | #include "src/main/cpp/util/md5.h" |
| 79 | #include "src/main/cpp/util/numbers.h" |
| 80 | #include "src/main/cpp/util/port.h" |
| 81 | #include "src/main/cpp/util/strings.h" |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 82 | #include "src/main/cpp/workspace_layout.h" |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 83 | #include "third_party/ijar/zip.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 84 | |
Lukacs Berki | e21e592 | 2016-04-12 12:22:20 +0000 | [diff] [blame] | 85 | #include "src/main/protobuf/command_server.grpc.pb.h" |
| 86 | |
Thiago Farina | 7390ddb | 2015-04-09 13:53:53 +0000 | [diff] [blame] | 87 | using blaze_util::Md5Digest; |
Thiago Farina | 241f46c | 2015-04-13 14:33:30 +0000 | [diff] [blame] | 88 | using blaze_util::die; |
| 89 | using blaze_util::pdie; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | |
| 91 | // This should already be defined in sched.h, but it's not. |
| 92 | #ifndef SCHED_BATCH |
| 93 | #define SCHED_BATCH 3 |
| 94 | #endif |
| 95 | |
| 96 | namespace blaze { |
| 97 | |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 98 | using std::set; |
| 99 | using std::string; |
| 100 | using std::vector; |
| 101 | |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 102 | static void WriteFileToStreamOrDie(FILE *stream, const char *file_name); |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 103 | static int GetServerPid(const string &server_dir); |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 104 | static void VerifyJavaVersionAndSetJvm(); |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 105 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 106 | // The following is a treatise on how the interaction between the client and the |
| 107 | // server works. |
| 108 | // |
| 109 | // First, the client unconditionally acquires an flock() lock on |
| 110 | // $OUTPUT_BASE/lock then verifies if it has already extracted itself by |
| 111 | // checking if the directory it extracts itself to (install base + a checksum) |
| 112 | // is present. If not, then it does the extraction. Care is taken that this |
| 113 | // process is atomic so that Blazen in multiple output bases do not clash. |
| 114 | // |
| 115 | // Then the client tries to connect to the currently executing server and kills |
| 116 | // it if at least one of the following conditions is true: |
| 117 | // |
| 118 | // - The server is of the wrong version (as determined by the |
| 119 | // $OUTPUT_BASE/install symlink) |
| 120 | // - The server has different startup options than the client wants |
| 121 | // - The client wants to run the command in batch mode |
| 122 | // |
| 123 | // Then, if needed, the client adjusts the install link to indicate which |
| 124 | // version of the server it is running. |
| 125 | // |
| 126 | // In batch mode, the client then simply executes the server while taking care |
| 127 | // that the output base lock is kept until it finishes. |
| 128 | // |
| 129 | // If in server mode, the client starts up a server if needed then sends the |
Thiago Farina | 69dac86 | 2016-11-02 09:48:27 +0000 | [diff] [blame^] | 130 | // command to the client and streams back stdout and stderr. The output base |
| 131 | // lock is released after the command is sent to the server (the server |
| 132 | // implements its own locking mechanism). |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 133 | |
| 134 | // Synchronization between the client and the server is a little precarious |
| 135 | // because the client needs to know the PID of the server and it is not |
| 136 | // available using a Java API and we don't have JNI on Windows at the moment, |
| 137 | // so the server can't just communicate this over the communication channel. |
| 138 | // Thus, a PID file is used, but care needs to be taken that the contents of |
| 139 | // this PID file are right. |
| 140 | // |
| 141 | // Upon server startup, the PID file is written before the client spawns the |
| 142 | // server. Thus, when the client can connect, it can be certain that the PID |
| 143 | // file is up to date. |
| 144 | // |
| 145 | // Upon server shutdown, the PID file is deleted using a server shutdown hook. |
| 146 | // However, this happens *after* the server stopped listening, so it's possible |
| 147 | // that a client has already started up a server and written a new PID file. |
| 148 | // In order to avoid this, when the client starts up a new server, it reads the |
| 149 | // contents of the PID file and kills the process indicated in it (it could do |
| 150 | // with a bit more care, since PIDs can be reused, but for now, we just believe |
| 151 | // the PID file) |
| 152 | // |
| 153 | // Some more interesting scenarios: |
| 154 | // |
| 155 | // - The server receives a kill signal and it does not have a chance to delete |
| 156 | // the PID file: the client cannot connect, reads the PID file, kills the |
| 157 | // process indicated in it and starts up a new server. |
| 158 | // |
| 159 | // - The server stopped accepting connections but hasn't quit yet and a new |
| 160 | // client comes around: the new client will kill the server based on the |
| 161 | // PID file before a new server is started up. |
| 162 | // |
| 163 | // Alternative implementations: |
| 164 | // |
| 165 | // - Don't deal with PIDs at all. This would make it impossible for the client |
| 166 | // to deliver a SIGKILL to the server after three SIGINTs. It would only be |
| 167 | // possible with gRPC anyway. |
| 168 | // |
| 169 | // - Have the server check that the PID file containts the correct things |
| 170 | // before deleting them: there is a window of time between checking the file |
| 171 | // and deleting it in which a new server can overwrite the PID file. The |
| 172 | // output base lock cannot be acquired, either, because when starting up a |
| 173 | // new server, the client already holds it. |
| 174 | // |
| 175 | // - Delete the PID file before stopping to accept connections: then a client |
| 176 | // could come about after deleting the PID file but before stopping accepting |
| 177 | // connections. It would also not be resilient against a dead server that |
| 178 | // left a PID file around. |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 179 | class BlazeServer { |
| 180 | public: |
| 181 | virtual ~BlazeServer() {} |
| 182 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 183 | // Acquire a lock for the server running in this output base. Returns the |
| 184 | // number of milliseconds spent waiting for the lock. |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 185 | uint64_t AcquireLock(); |
| 186 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 187 | // Whether there is an active connection to a server. |
| 188 | bool Connected() const { return connected_; } |
| 189 | |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 190 | // Connect to the server. Returns if the connection was successful. Only |
| 191 | // call this when this object is in disconnected state. If it returns true, |
| 192 | // this object will be in connected state. |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 193 | virtual bool Connect() = 0; |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 194 | |
| 195 | // Disconnects from an existing server. Only call this when this object is in |
| 196 | // connected state. After this call returns, the object will be in connected |
| 197 | // state. |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 198 | virtual void Disconnect() = 0; |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 199 | |
| 200 | // Send the command line to the server and forward whatever it says to stdout |
| 201 | // and stderr. Returns the desired exit code. Only call this when the server |
| 202 | // is in connected state. |
| 203 | virtual unsigned int Communicate() = 0; |
| 204 | |
| 205 | // Disconnects and kills an existing server. Only call this when this object |
| 206 | // is in connected state. |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 207 | virtual void KillRunningServer() = 0; |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 208 | |
| 209 | // Cancel the currently running command. If there is no command currently |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 210 | // running, the result is unspecified. When called, this object must be in |
| 211 | // connected state. |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 212 | virtual void Cancel() = 0; |
Thiago Farina | 69dac86 | 2016-11-02 09:48:27 +0000 | [diff] [blame^] | 213 | |
| 214 | protected: |
| 215 | BlazeLock blaze_lock_; |
| 216 | bool connected_; |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 219 | //////////////////////////////////////////////////////////////////////// |
| 220 | // Global Variables |
| 221 | static GlobalVariables *globals; |
| 222 | static BlazeServer *blaze_server; |
| 223 | |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 224 | uint64_t BlazeServer::AcquireLock() { |
| 225 | return blaze::AcquireLock( |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 226 | globals->options->output_base, globals->options->batch, |
| 227 | globals->options->block_for_lock, &blaze_lock_); |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 230 | // Communication method that uses gRPC on a socket bound to localhost. More |
| 231 | // documentation is in command_server.proto . |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 232 | class GrpcBlazeServer : public BlazeServer { |
| 233 | public: |
| 234 | GrpcBlazeServer(); |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 235 | virtual ~GrpcBlazeServer(); |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 236 | |
Lukacs Berki | 9d52bc5 | 2016-06-07 11:11:04 +0000 | [diff] [blame] | 237 | virtual bool Connect(); |
| 238 | virtual void Disconnect(); |
| 239 | virtual unsigned int Communicate(); |
| 240 | virtual void KillRunningServer(); |
| 241 | virtual void Cancel(); |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 242 | |
| 243 | private: |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 244 | enum CancelThreadAction { NOTHING, JOIN, CANCEL, COMMAND_ID_RECEIVED }; |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 245 | |
| 246 | std::unique_ptr<command_server::CommandServer::Stub> client_; |
| 247 | std::string request_cookie_; |
| 248 | std::string response_cookie_; |
| 249 | std::string command_id_; |
| 250 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 251 | // protects command_id_ . Although we always set it before making the cancel |
| 252 | // thread do something with it, the mutex is still useful because it provides |
| 253 | // a memory fence. |
| 254 | std::mutex cancel_thread_mutex_; |
Lukacs Berki | 8b99998 | 2016-04-26 15:40:38 +0000 | [diff] [blame] | 255 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 256 | int recv_socket_; // Socket the cancel thread reads actions from |
| 257 | int send_socket_; // Socket the main thread writes actions to |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 258 | |
| 259 | void CancelThread(); |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 260 | void SendAction(CancelThreadAction action); |
| 261 | void SendCancelMessage(); |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 265 | //////////////////////////////////////////////////////////////////////// |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 266 | // Logic |
| 267 | |
| 268 | |
Dmitry Lomov | 8e2e4b3 | 2016-04-20 08:04:17 +0000 | [diff] [blame] | 269 | #if !defined(__CYGWIN__) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 270 | // Returns the canonical form of the base dir given a root and a hashable |
| 271 | // string. The resulting dir is composed of the root + md5(hashable) |
| 272 | static string GetHashedBaseDir(const string &root, |
| 273 | const string &hashable) { |
Thiago Farina | 7390ddb | 2015-04-09 13:53:53 +0000 | [diff] [blame] | 274 | unsigned char buf[Md5Digest::kDigestLength]; |
| 275 | Md5Digest digest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 276 | digest.Update(hashable.data(), hashable.size()); |
| 277 | digest.Finish(buf); |
| 278 | return root + "/" + digest.String(); |
| 279 | } |
| 280 | |
Dmitry Lomov | 8e2e4b3 | 2016-04-20 08:04:17 +0000 | [diff] [blame] | 281 | #else |
Dmitry Lomov | bc84cc8 | 2016-04-15 14:05:24 +0000 | [diff] [blame] | 282 | // Builds a shorter output base dir name for Windows. |
| 283 | // This MD5s together user name and workspace directory, |
| 284 | // and only uses 1/3 of the bits to get 8-char alphanumeric |
| 285 | // file name. |
| 286 | static string GetHashedBaseDirForWindows(const string &root, |
| 287 | const string &product_name, |
| 288 | const string &user_name, |
| 289 | const string &workspace_directory) { |
| 290 | static const char* alphabet |
| 291 | // Exactly 64 characters. |
| 292 | = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789$-"; |
| 293 | |
| 294 | // The length of the resulting filename (8 characters). |
| 295 | static const int filename_length = Md5Digest::kDigestLength / 2; |
| 296 | unsigned char buf[Md5Digest::kDigestLength]; |
| 297 | char coded_name[filename_length + 1]; |
| 298 | Md5Digest digest; |
| 299 | digest.Update(user_name.data(), user_name.size()); |
| 300 | digest.Update(workspace_directory.data(), workspace_directory.size()); |
| 301 | digest.Finish(buf); |
| 302 | for (int i = 0; i < filename_length; i++) { |
| 303 | coded_name[i] = alphabet[buf[i] & 0x3F]; |
| 304 | } |
| 305 | coded_name[filename_length] = '\0'; |
| 306 | return root + "/" + product_name + "/" + string(coded_name); |
| 307 | } |
Dmitry Lomov | 8e2e4b3 | 2016-04-20 08:04:17 +0000 | [diff] [blame] | 308 | #endif |
Dmitry Lomov | bc84cc8 | 2016-04-15 14:05:24 +0000 | [diff] [blame] | 309 | |
| 310 | |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 311 | // A devtools_ijar::ZipExtractorProcessor to extract the InstallKeyFile |
| 312 | class GetInstallKeyFileProcessor : public devtools_ijar::ZipExtractorProcessor { |
| 313 | public: |
Thiago Farina | 9cb3275 | 2015-06-03 15:34:19 +0000 | [diff] [blame] | 314 | explicit GetInstallKeyFileProcessor(string *install_base_key) |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 315 | : install_base_key_(install_base_key) {} |
| 316 | |
| 317 | virtual bool Accept(const char *filename, const devtools_ijar::u4 attr) { |
| 318 | globals->extracted_binaries.push_back(filename); |
| 319 | return strcmp(filename, "install_base_key") == 0; |
| 320 | } |
| 321 | |
| 322 | virtual void Process(const char *filename, const devtools_ijar::u4 attr, |
| 323 | const devtools_ijar::u1 *data, const size_t size) { |
| 324 | string str(reinterpret_cast<const char *>(data), size); |
| 325 | blaze_util::StripWhitespace(&str); |
Lukacs Berki | 58c29ae | 2015-10-16 14:48:33 +0000 | [diff] [blame] | 326 | if (str.size() != 32) { |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 327 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
Lukacs Berki | 58c29ae | 2015-10-16 14:48:33 +0000 | [diff] [blame] | 328 | "\nFailed to extract install_base_key: file size mismatch " |
| 329 | "(should be 32, is %zd)", str.size()); |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 330 | } |
| 331 | *install_base_key_ = str; |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | string *install_base_key_; |
| 336 | }; |
| 337 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 338 | // Returns the install base (the root concatenated with the contents of the file |
| 339 | // 'install_base_key' contained as a ZIP entry in the Blaze binary); as a side |
| 340 | // effect, it also populates the extracted_binaries global variable. |
| 341 | static string GetInstallBase(const string &root, const string &self_path) { |
Eric Fellheimer | 4c5eb0f | 2015-08-12 15:02:24 +0000 | [diff] [blame] | 342 | GetInstallKeyFileProcessor processor(&globals->install_md5); |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 343 | std::unique_ptr<devtools_ijar::ZipExtractor> extractor( |
| 344 | devtools_ijar::ZipExtractor::Create(self_path.c_str(), &processor)); |
| 345 | if (extractor.get() == NULL) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 346 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 347 | "\nFailed to open %s as a zip file: (%d) %s", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 348 | globals->options->product_name.c_str(), errno, strerror(errno)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 349 | } |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 350 | if (extractor->ProcessAll() < 0) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 351 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 352 | "\nFailed to extract install_base_key: %s", extractor->GetError()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Eric Fellheimer | 4c5eb0f | 2015-08-12 15:02:24 +0000 | [diff] [blame] | 355 | if (globals->install_md5.empty()) { |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 356 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 357 | "\nFailed to find install_base_key's in zip file"); |
| 358 | } |
Eric Fellheimer | 4c5eb0f | 2015-08-12 15:02:24 +0000 | [diff] [blame] | 359 | return root + "/" + globals->install_md5; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Escapes colons by replacing them with '_C' and underscores by replacing them |
| 363 | // with '_U'. E.g. "name:foo_bar" becomes "name_Cfoo_Ubar" |
| 364 | static string EscapeForOptionSource(const string& input) { |
| 365 | string result = input; |
| 366 | blaze_util::Replace("_", "_U", &result); |
| 367 | blaze_util::Replace(":", "_C", &result); |
| 368 | return result; |
| 369 | } |
| 370 | |
Thiago Farina | 6a2dc2b | 2016-10-28 13:05:22 +0000 | [diff] [blame] | 371 | // Returns the installed embedded binaries directory, under the shared |
| 372 | // install_base location. |
| 373 | string GetEmbeddedBinariesRoot(const string &install_base) { |
| 374 | return blaze_util::JoinPath(install_base, "_embedded_binaries"); |
| 375 | } |
| 376 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 377 | // Returns the JVM command argument array. |
| 378 | static vector<string> GetArgumentArray() { |
| 379 | vector<string> result; |
| 380 | |
| 381 | // e.g. A Blaze server process running in ~/src/build_root (where there's a |
| 382 | // ~/src/build_root/WORKSPACE file) will appear in ps(1) as "blaze(src)". |
| 383 | string workspace = |
| 384 | blaze_util::Basename(blaze_util::Dirname(globals->workspace)); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 385 | string product = globals->options->product_name; |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 386 | blaze_util::ToLower(&product); |
| 387 | result.push_back(product + "(" + workspace + ")"); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 388 | globals->options->AddJVMArgumentPrefix( |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 389 | blaze_util::Dirname(blaze_util::Dirname(globals->jvm_path)), |
| 390 | &result); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 391 | |
| 392 | result.push_back("-XX:+HeapDumpOnOutOfMemoryError"); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 393 | string heap_crash_path = globals->options->output_base; |
Dmitry Lomov | 7608db5 | 2016-07-14 11:27:10 +0000 | [diff] [blame] | 394 | result.push_back("-XX:HeapDumpPath=" + ConvertPath(heap_crash_path)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 395 | |
| 396 | result.push_back("-Xverify:none"); |
| 397 | |
Janak Ramakrishnan | de735c0 | 2015-06-02 16:38:57 +0000 | [diff] [blame] | 398 | vector<string> user_options; |
| 399 | |
Janak Ramakrishnan | 0acd154 | 2016-01-06 18:42:30 +0000 | [diff] [blame] | 400 | user_options.insert(user_options.begin(), |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 401 | globals->options->host_jvm_args.begin(), |
| 402 | globals->options->host_jvm_args.end()); |
Janak Ramakrishnan | de735c0 | 2015-06-02 16:38:57 +0000 | [diff] [blame] | 403 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 404 | // Add JVM arguments particular to building blaze64 and particular JVM |
| 405 | // versions. |
| 406 | string error; |
| 407 | blaze_exit_code::ExitCode jvm_args_exit_code = |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 408 | globals->options->AddJVMArguments(globals->options->GetHostJavabase(), |
Janak Ramakrishnan | de735c0 | 2015-06-02 16:38:57 +0000 | [diff] [blame] | 409 | &result, user_options, &error); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 410 | if (jvm_args_exit_code != blaze_exit_code::SUCCESS) { |
| 411 | die(jvm_args_exit_code, "%s", error.c_str()); |
| 412 | } |
| 413 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 414 | if (globals->options->batch && globals->options->oom_more_eagerly) { |
Janak Ramakrishnan | 70c5790 | 2016-03-10 00:58:59 +0000 | [diff] [blame] | 415 | // Put this OOM trigger with kill after --host_jvm_args, in case |
| 416 | // --host_jvm_args contains user-specified OOM triggers since we want those |
| 417 | // to execute first. |
| 418 | result.push_back("-XX:OnOutOfMemoryError=kill -USR2 %p"); |
| 419 | } |
| 420 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 421 | // We put all directories on the java.library.path that contain .so files. |
| 422 | string java_library_path = "-Djava.library.path="; |
Thiago Farina | 6a2dc2b | 2016-10-28 13:05:22 +0000 | [diff] [blame] | 423 | string real_install_dir = |
| 424 | GetEmbeddedBinariesRoot(globals->options->install_base); |
| 425 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 426 | bool first = true; |
| 427 | for (const auto& it : globals->extracted_binaries) { |
Thiago Farina | 01f3600 | 2015-04-08 15:59:08 +0000 | [diff] [blame] | 428 | if (IsSharedLibrary(it)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 429 | if (!first) { |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 430 | java_library_path += blaze::ListSeparator(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 431 | } |
| 432 | first = false; |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 433 | java_library_path += blaze::ConvertPath( |
| 434 | blaze_util::JoinPath(real_install_dir, blaze_util::Dirname(it))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | result.push_back(java_library_path); |
| 438 | |
| 439 | // Force use of latin1 for file names. |
| 440 | result.push_back("-Dfile.encoding=ISO-8859-1"); |
| 441 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 442 | if (globals->options->host_jvm_debug) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 443 | fprintf(stderr, |
| 444 | "Running host JVM under debugger (listening on TCP port 5005).\n"); |
| 445 | // Start JVM so that it listens for a connection from a |
| 446 | // JDWP-compliant debugger: |
| 447 | result.push_back("-Xdebug"); |
| 448 | result.push_back("-Xrunjdwp:transport=dt_socket,server=y,address=5005"); |
| 449 | } |
Janak Ramakrishnan | de735c0 | 2015-06-02 16:38:57 +0000 | [diff] [blame] | 450 | result.insert(result.end(), user_options.begin(), user_options.end()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 451 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 452 | globals->options->AddJVMArgumentSuffix(real_install_dir, |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 453 | globals->extracted_binaries[0], |
| 454 | &result); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 455 | |
Lukacs Berki | 3d48683 | 2016-10-26 12:51:38 +0000 | [diff] [blame] | 456 | // JVM arguments are complete. Now pass in Blaze startup options. |
| 457 | // Note that we always use the --flag=ARG form (instead of the --flag ARG one) |
| 458 | // so that BlazeRuntime#splitStartupOptions has an easy job. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 459 | if (!globals->options->batch) { |
Lukacs Berki | 3d48683 | 2016-10-26 12:51:38 +0000 | [diff] [blame] | 460 | result.push_back("--max_idle_secs=" + |
| 461 | ToString(globals->options->max_idle_secs)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 462 | } else { |
Googler | c8c64e7 | 2015-03-23 23:22:18 +0000 | [diff] [blame] | 463 | // --batch must come first in the arguments to Java main() because |
| 464 | // the code expects it to be at args[0] if it's been set. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | result.push_back("--batch"); |
| 466 | } |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 467 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 468 | if (globals->options->command_port != 0) { |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 469 | result.push_back( |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 470 | "--command_port=" + ToString(globals->options->command_port)); |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 473 | result.push_back("--install_base=" + |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 474 | blaze::ConvertPath(globals->options->install_base)); |
Eric Fellheimer | 4c5eb0f | 2015-08-12 15:02:24 +0000 | [diff] [blame] | 475 | result.push_back("--install_md5=" + globals->install_md5); |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 476 | result.push_back("--output_base=" + |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 477 | blaze::ConvertPath(globals->options->output_base)); |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 478 | result.push_back("--workspace_directory=" + |
| 479 | blaze::ConvertPath(globals->workspace)); |
Marian Lobur | 6dcdd60 | 2015-04-09 09:28:40 +0000 | [diff] [blame] | 480 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 481 | if (globals->options->allow_configurable_attributes) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 482 | result.push_back("--allow_configurable_attributes"); |
| 483 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 484 | if (globals->options->deep_execroot) { |
Lukacs Berki | 5fb98d1 | 2015-12-09 15:29:46 +0000 | [diff] [blame] | 485 | result.push_back("--deep_execroot"); |
| 486 | } else { |
| 487 | result.push_back("--nodeep_execroot"); |
| 488 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 489 | if (globals->options->oom_more_eagerly) { |
Janak Ramakrishnan | adc706f | 2016-03-07 19:12:48 +0000 | [diff] [blame] | 490 | result.push_back("--experimental_oom_more_eagerly"); |
| 491 | } |
Janak Ramakrishnan | 19fde1f | 2016-05-23 21:20:16 +0000 | [diff] [blame] | 492 | result.push_back("--experimental_oom_more_eagerly_threshold=" + |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 493 | ToString(globals->options->oom_more_eagerly_threshold)); |
Janak Ramakrishnan | 8cc772e | 2016-03-23 17:26:12 +0000 | [diff] [blame] | 494 | |
Michajlo Matijkiw | af79a32 | 2016-09-16 15:44:35 +0000 | [diff] [blame] | 495 | if (!globals->options->write_command_log) { |
| 496 | result.push_back("--nowrite_command_log"); |
| 497 | } |
| 498 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 499 | if (globals->options->watchfs) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 500 | result.push_back("--watchfs"); |
| 501 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 502 | if (globals->options->fatal_event_bus_exceptions) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 503 | result.push_back("--fatal_event_bus_exceptions"); |
| 504 | } else { |
| 505 | result.push_back("--nofatal_event_bus_exceptions"); |
| 506 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 507 | |
| 508 | // This is only for Blaze reporting purposes; the real interpretation of the |
| 509 | // jvm flags occurs when we set up the java command line. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 510 | if (globals->options->host_jvm_debug) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 511 | result.push_back("--host_jvm_debug"); |
| 512 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 513 | if (!globals->options->host_jvm_profile.empty()) { |
| 514 | result.push_back("--host_jvm_profile=" + |
| 515 | globals->options->host_jvm_profile); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 516 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 517 | if (!globals->options->host_jvm_args.empty()) { |
| 518 | for (const auto &arg : globals->options->host_jvm_args) { |
Janak Ramakrishnan | 533657e | 2015-11-13 23:34:14 +0000 | [diff] [blame] | 519 | result.push_back("--host_jvm_args=" + arg); |
| 520 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 521 | } |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 522 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 523 | if (globals->options->invocation_policy != NULL && |
| 524 | strlen(globals->options->invocation_policy) > 0) { |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 525 | result.push_back(string("--invocation_policy=") + |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 526 | globals->options->invocation_policy); |
Alex Humesky | 2f3f4cf | 2015-09-29 01:42:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 529 | result.push_back("--product_name=" + globals->options->product_name); |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 530 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 531 | globals->options->AddExtraOptions(&result); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 532 | |
| 533 | // The option sources are transmitted in the following format: |
| 534 | // --option_sources=option1:source1:option2:source2:... |
| 535 | string option_sources = "--option_sources="; |
| 536 | first = true; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 537 | for (const auto& it : globals->options->option_sources) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 538 | if (!first) { |
| 539 | option_sources += ":"; |
| 540 | } |
| 541 | |
| 542 | first = false; |
| 543 | option_sources += EscapeForOptionSource(it.first) + ":" + |
| 544 | EscapeForOptionSource(it.second); |
| 545 | } |
| 546 | |
| 547 | result.push_back(option_sources); |
| 548 | return result; |
| 549 | } |
| 550 | |
Thiago Farina | 5735c25 | 2016-04-27 16:16:27 +0000 | [diff] [blame] | 551 | // Add common command options for logging to the given argument array. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 552 | static void AddLoggingArgs(vector<string>* args) { |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 553 | args->push_back("--startup_time=" + ToString(globals->startup_time)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 554 | if (globals->command_wait_time != 0) { |
| 555 | args->push_back("--command_wait_time=" + |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 556 | ToString(globals->command_wait_time)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 557 | } |
| 558 | if (globals->extract_data_time != 0) { |
| 559 | args->push_back("--extract_data_time=" + |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 560 | ToString(globals->extract_data_time)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 561 | } |
| 562 | if (globals->restart_reason != NO_RESTART) { |
| 563 | const char *reasons[] = { |
| 564 | "no_restart", "no_daemon", "new_version", "new_options" |
| 565 | }; |
| 566 | args->push_back( |
| 567 | string("--restart_reason=") + reasons[globals->restart_reason]); |
| 568 | } |
| 569 | args->push_back( |
| 570 | string("--binary_path=") + globals->binary_path); |
| 571 | } |
| 572 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 573 | // Join the elements of the specified array with NUL's (\0's), akin to the |
| 574 | // format of /proc/$PID/cmdline. |
Thiago Farina | 0b6963e | 2015-04-28 20:26:45 +0000 | [diff] [blame] | 575 | static string GetArgumentString(const vector<string>& argument_array) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 576 | string result; |
| 577 | blaze_util::JoinStrings(argument_array, '\0', &result); |
| 578 | return result; |
| 579 | } |
| 580 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 581 | // Do a chdir into the workspace, and die if it fails. |
| 582 | static void GoToWorkspace() { |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 583 | if (WorkspaceLayout::InWorkspace(globals->workspace) && |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 584 | chdir(globals->workspace.c_str()) != 0) { |
| 585 | pdie(blaze_exit_code::INTERNAL_ERROR, |
| 586 | "chdir() into %s failed", globals->workspace.c_str()); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Check the java version if a java version specification is bundled. On |
Thiago Farina | 5735c25 | 2016-04-27 16:16:27 +0000 | [diff] [blame] | 591 | // success, returns the executable path of the java command. |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 592 | static void VerifyJavaVersionAndSetJvm() { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 593 | string exe = globals->options->GetJvm(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 594 | |
| 595 | string version_spec_file = blaze_util::JoinPath( |
Thiago Farina | 6a2dc2b | 2016-10-28 13:05:22 +0000 | [diff] [blame] | 596 | GetEmbeddedBinariesRoot(globals->options->install_base), "java.version"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 597 | string version_spec = ""; |
| 598 | if (ReadFile(version_spec_file, &version_spec)) { |
| 599 | blaze_util::StripWhitespace(&version_spec); |
| 600 | // A version specification is given, get version of java. |
| 601 | string jvm_version = GetJvmVersion(exe); |
| 602 | |
| 603 | // Compare that jvm_version is found and at least the one specified. |
| 604 | if (jvm_version.size() == 0) { |
| 605 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 606 | "Java version not detected while at least %s is needed.\n" |
| 607 | "Please set JAVA_HOME.", version_spec.c_str()); |
| 608 | } else if (!CheckJavaVersionIsAtLeast(jvm_version, version_spec)) { |
| 609 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 610 | "Java version is %s while at least %s is needed.\n" |
| 611 | "Please set JAVA_HOME.", |
| 612 | jvm_version.c_str(), version_spec.c_str()); |
| 613 | } |
| 614 | } |
| 615 | |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 616 | globals->jvm_path = exe; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // Starts the Blaze server. Returns a readable fd connected to the server. |
| 620 | // This is currently used only to detect liveness. |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 621 | static void StartServer(BlazeServerStartup** server_startup) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 622 | vector<string> jvm_args_vector = GetArgumentArray(); |
| 623 | string argument_string = GetArgumentString(jvm_args_vector); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 624 | string server_dir = globals->options->output_base + "/server"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 625 | // Write the cmdline argument string to the server dir. If we get to this |
| 626 | // point, there is no server running, so we don't overwrite the cmdline file |
| 627 | // for the existing server. If might be that the server dies and the cmdline |
| 628 | // file stays there, but that is not a problem, since we always check the |
| 629 | // server, too. |
Lukacs Berki | 5a78166 | 2016-04-25 11:17:31 +0000 | [diff] [blame] | 630 | WriteFile(argument_string, server_dir + "/cmdline"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 631 | |
| 632 | // unless we restarted for a new-version, mark this as initial start |
| 633 | if (globals->restart_reason == NO_RESTART) { |
| 634 | globals->restart_reason = NO_DAEMON; |
| 635 | } |
| 636 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 637 | string exe = globals->options->GetExe(globals->jvm_path, |
| 638 | globals->extracted_binaries[0]); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 639 | // Go to the workspace before we daemonize, so |
| 640 | // we can still print errors to the terminal. |
| 641 | GoToWorkspace(); |
| 642 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 643 | ExecuteDaemon(exe, jvm_args_vector, globals->jvm_log_file.c_str(), |
| 644 | server_dir, server_startup); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 645 | } |
| 646 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 647 | // Replace this process with blaze in standalone/batch mode. |
| 648 | // The batch mode blaze process handles the command and exits. |
| 649 | // |
| 650 | // This function passes the commands array to the blaze process. |
| 651 | // This array should start with a command ("build", "info", etc.). |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 652 | static void StartStandalone(BlazeServer* server) { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 653 | if (server->Connected()) { |
| 654 | server->KillRunningServer(); |
| 655 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 656 | |
| 657 | // Wall clock time since process startup. |
| 658 | globals->startup_time = ProcessClock() / 1000000LL; |
| 659 | |
| 660 | if (VerboseLogging()) { |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 661 | fprintf(stderr, "Starting %s in batch mode.\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 662 | globals->options->product_name.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 663 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 664 | string command = globals->option_processor->GetCommand(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 665 | vector<string> command_arguments; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 666 | globals->option_processor->GetCommandArguments(&command_arguments); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 667 | |
| 668 | if (!command_arguments.empty() && command == "shutdown") { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 669 | string product = globals->options->product_name; |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 670 | blaze_util::ToLower(&product); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 671 | fprintf(stderr, |
| 672 | "WARNING: Running command \"shutdown\" in batch mode. Batch mode " |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 673 | "is triggered\nwhen not running %s within a workspace. If you " |
| 674 | "intend to shutdown an\nexisting %s server, run \"%s " |
| 675 | "shutdown\" from the directory where\nit was started.\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 676 | globals->options->product_name.c_str(), |
| 677 | globals->options->product_name.c_str(), product.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 678 | } |
| 679 | vector<string> jvm_args_vector = GetArgumentArray(); |
| 680 | if (command != "") { |
| 681 | jvm_args_vector.push_back(command); |
| 682 | AddLoggingArgs(&jvm_args_vector); |
| 683 | } |
| 684 | |
| 685 | jvm_args_vector.insert(jvm_args_vector.end(), |
| 686 | command_arguments.begin(), |
| 687 | command_arguments.end()); |
| 688 | |
| 689 | GoToWorkspace(); |
| 690 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 691 | string exe = globals->options->GetExe(globals->jvm_path, |
Eric Fellheimer | 3a695f3 | 2016-05-11 17:26:30 +0000 | [diff] [blame] | 692 | globals->extracted_binaries[0]); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 693 | ExecuteProgram(exe, jvm_args_vector); |
| 694 | pdie(blaze_exit_code::INTERNAL_ERROR, "execv of '%s' failed", exe.c_str()); |
| 695 | } |
| 696 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 697 | // Write the contents of file_name to stream. |
| 698 | static void WriteFileToStreamOrDie(FILE *stream, const char *file_name) { |
| 699 | FILE *fp = fopen(file_name, "r"); |
| 700 | if (fp == NULL) { |
| 701 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 702 | "opening %s failed", file_name); |
| 703 | } |
| 704 | char buffer[255]; |
| 705 | int num_read; |
| 706 | while ((num_read = fread(buffer, 1, sizeof buffer, fp)) > 0) { |
| 707 | if (ferror(fp)) { |
| 708 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 709 | "failed to read from '%s'", file_name); |
| 710 | } |
| 711 | fwrite(buffer, 1, num_read, stream); |
| 712 | } |
| 713 | fclose(fp); |
| 714 | } |
| 715 | |
Lukacs Berki | 4912f7f | 2016-06-17 16:12:22 +0000 | [diff] [blame] | 716 | // After connecting to the Blaze server, return its PID, or -1 if there was an |
| 717 | // error. |
Lukacs Berki | d9da60f | 2016-04-26 11:40:24 +0000 | [diff] [blame] | 718 | static int GetServerPid(const string &server_dir) { |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 719 | // Note: there is no race here on startup since the server creates |
| 720 | // the pid file strictly before it binds the socket. |
Lukacs Berki | ea4c42e | 2016-04-25 07:22:11 +0000 | [diff] [blame] | 721 | char buf[33]; |
| 722 | |
| 723 | // The server writes a file, but we need to handle old servers that still |
| 724 | // write a symlink. |
Lukacs Berki | ea4c42e | 2016-04-25 07:22:11 +0000 | [diff] [blame] | 725 | int len; |
Thiago Farina | 048bbfc | 2016-09-21 08:20:41 +0000 | [diff] [blame] | 726 | string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile); |
| 727 | string pid_symlink = blaze_util::JoinPath(server_dir, kServerPidSymlink); |
Lukacs Berki | d9da60f | 2016-04-26 11:40:24 +0000 | [diff] [blame] | 728 | len = readlink(pid_symlink.c_str(), buf, sizeof(buf) - 1); |
Lukacs Berki | ea4c42e | 2016-04-25 07:22:11 +0000 | [diff] [blame] | 729 | if (len < 0) { |
| 730 | int fd = open(pid_file.c_str(), O_RDONLY); |
| 731 | if (fd < 0) { |
| 732 | return -1; |
| 733 | } |
| 734 | len = read(fd, buf, 32); |
| 735 | close(fd); |
| 736 | if (len < 0) { |
| 737 | return -1; |
| 738 | } |
Doug Rabson | d655f2a | 2015-08-13 14:41:50 +0000 | [diff] [blame] | 739 | } |
Lukacs Berki | ea4c42e | 2016-04-25 07:22:11 +0000 | [diff] [blame] | 740 | |
| 741 | int result; |
| 742 | buf[len] = 0; |
| 743 | if (!blaze_util::safe_strto32(string(buf), &result)) { |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | return result; |
Doug Rabson | d655f2a | 2015-08-13 14:41:50 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 750 | // Starts up a new server and connects to it. Exits if it didn't work not. |
| 751 | static void StartServerAndConnect(BlazeServer *server) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 752 | string server_dir = globals->options->output_base + "/server"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 753 | |
| 754 | // The server dir has the socket, so we don't allow access by other |
| 755 | // users. |
| 756 | if (MakeDirectories(server_dir, 0700) == -1) { |
| 757 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 758 | "server directory '%s' could not be created", server_dir.c_str()); |
| 759 | } |
| 760 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 761 | // If we couldn't connect to the server check if there is still a PID file |
| 762 | // and if so, kill the server that wrote it. This can happen e.g. if the |
| 763 | // server is in a GC pause and therefore cannot respond to ping requests and |
| 764 | // having two server instances running in the same output base is a |
| 765 | // disaster. |
| 766 | int server_pid = GetServerPid(server_dir); |
| 767 | if (server_pid > 0) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 768 | if (VerifyServerProcess(server_pid, globals->options->output_base, |
| 769 | globals->options->install_base) && |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 770 | KillServerProcess(server_pid)) { |
Lukacs Berki | 119dd4b | 2016-07-13 15:28:42 +0000 | [diff] [blame] | 771 | fprintf(stderr, "Killed non-responsive server process (pid=%d)\n", |
| 772 | server_pid); |
| 773 | } |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 776 | SetScheduling(globals->options->batch_cpu_scheduling, |
| 777 | globals->options->io_nice_level); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 778 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 779 | BlazeServerStartup* server_startup; |
| 780 | StartServer(&server_startup); |
| 781 | // Give the server one minute to start up. |
| 782 | for (int ii = 0; ii < 600; ++ii) { |
| 783 | // 60s; enough time to connect with debugger |
| 784 | if (server->Connect()) { |
| 785 | if (ii) { |
| 786 | fputc('\n', stderr); |
| 787 | fflush(stderr); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 788 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 789 | delete server_startup; |
| 790 | return; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 791 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 792 | fputc('.', stderr); |
| 793 | fflush(stderr); |
Chongyu Zhu | fefd232 | 2016-09-21 11:01:14 +0000 | [diff] [blame] | 794 | struct timespec ts; |
| 795 | ts.tv_sec = 0; |
| 796 | ts.tv_nsec = 100 * 1000 * 1000; |
| 797 | nanosleep(&ts, NULL); |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 798 | if (!server_startup->IsStillAlive()) { |
| 799 | fprintf(stderr, "\nunexpected pipe read status: %s\n" |
| 800 | "Server presumed dead. Now printing '%s':\n", |
| 801 | strerror(errno), globals->jvm_log_file.c_str()); |
| 802 | WriteFileToStreamOrDie(stderr, globals->jvm_log_file.c_str()); |
| 803 | exit(blaze_exit_code::INTERNAL_ERROR); |
| 804 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 805 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 806 | die(blaze_exit_code::INTERNAL_ERROR, |
Thiago Farina | 8fc795a | 2016-10-31 08:54:25 +0000 | [diff] [blame] | 807 | "\nError: couldn't connect to server after 60 seconds."); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 810 | // Calls fsync() on the file (or directory) specified in 'file_path'. |
| 811 | // pdie()'s if syncing fails. |
| 812 | static void SyncFile(const char *file_path) { |
| 813 | // fsync always fails on Cygwin with "Permission denied" for some reason. |
| 814 | #ifndef __CYGWIN__ |
| 815 | int fd = open(file_path, O_RDONLY); |
| 816 | if (fd < 0) { |
| 817 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 818 | "failed to open '%s' for syncing", file_path); |
| 819 | } |
| 820 | if (fsync(fd) < 0) { |
| 821 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 822 | "failed to sync '%s'", file_path); |
| 823 | } |
| 824 | close(fd); |
| 825 | #endif |
| 826 | } |
| 827 | |
| 828 | // Walks the temporary directory recursively and collects full file paths. |
| 829 | static void CollectExtractedFiles(const string &dir_path, vector<string> &files) { |
| 830 | DIR *dir; |
| 831 | struct dirent *ent; |
| 832 | |
| 833 | if ((dir = opendir(dir_path.c_str())) == NULL) { |
| 834 | die(blaze_exit_code::INTERNAL_ERROR, "opendir failed"); |
| 835 | } |
| 836 | |
| 837 | while ((ent = readdir(dir)) != NULL) { |
| 838 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) { |
| 839 | continue; |
| 840 | } |
| 841 | |
| 842 | string filename(blaze_util::JoinPath(dir_path, ent->d_name)); |
| 843 | bool is_directory; |
| 844 | if (ent->d_type == DT_UNKNOWN) { |
| 845 | struct stat buf; |
| 846 | if (lstat(filename.c_str(), &buf) == -1) { |
| 847 | die(blaze_exit_code::INTERNAL_ERROR, "stat failed"); |
| 848 | } |
| 849 | is_directory = S_ISDIR(buf.st_mode); |
| 850 | } else { |
| 851 | is_directory = (ent->d_type == DT_DIR); |
| 852 | } |
| 853 | |
| 854 | if (is_directory) { |
| 855 | CollectExtractedFiles(filename, files); |
| 856 | } else { |
| 857 | files.push_back(filename); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | closedir(dir); |
| 862 | } |
| 863 | |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 864 | // A devtools_ijar::ZipExtractorProcessor to extract the files from the blaze |
| 865 | // zip. |
| 866 | class ExtractBlazeZipProcessor : public devtools_ijar::ZipExtractorProcessor { |
| 867 | public: |
Thiago Farina | 9cb3275 | 2015-06-03 15:34:19 +0000 | [diff] [blame] | 868 | explicit ExtractBlazeZipProcessor(const string &embedded_binaries) |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 869 | : embedded_binaries_(embedded_binaries) {} |
| 870 | |
| 871 | virtual bool Accept(const char *filename, const devtools_ijar::u4 attr) { |
| 872 | return !devtools_ijar::zipattr_is_dir(attr); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 875 | virtual void Process(const char *filename, const devtools_ijar::u4 attr, |
| 876 | const devtools_ijar::u1 *data, const size_t size) { |
| 877 | string path = blaze_util::JoinPath(embedded_binaries_, filename); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 878 | if (MakeDirectories(blaze_util::Dirname(path), 0777) == -1) { |
| 879 | pdie(blaze_exit_code::INTERNAL_ERROR, |
| 880 | "couldn't create '%s'", path.c_str()); |
| 881 | } |
Damien Martin-Guillerez | e20e544 | 2015-03-26 09:04:23 +0000 | [diff] [blame] | 882 | int fd = open(path.c_str(), O_CREAT | O_WRONLY, 0755); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 883 | if (fd < 0) { |
| 884 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 885 | "\nFailed to open extraction file: %s", strerror(errno)); |
| 886 | } |
| 887 | |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 888 | if (write(fd, data, size) != size) { |
| 889 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 890 | "\nError writing zipped file to %s", path.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 891 | } |
| 892 | if (close(fd) != 0) { |
| 893 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 894 | "\nCould not close file %s", path.c_str()); |
| 895 | } |
| 896 | } |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 897 | |
| 898 | private: |
| 899 | const string embedded_binaries_; |
| 900 | }; |
| 901 | |
| 902 | // Actually extracts the embedded data files into the tree whose root |
| 903 | // is 'embedded_binaries'. |
| 904 | static void ActuallyExtractData(const string &argv0, |
| 905 | const string &embedded_binaries) { |
| 906 | ExtractBlazeZipProcessor processor(embedded_binaries); |
| 907 | if (MakeDirectories(embedded_binaries, 0777) == -1) { |
| 908 | pdie(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s'", |
| 909 | embedded_binaries.c_str()); |
| 910 | } |
| 911 | |
| 912 | fprintf(stderr, "Extracting %s installation...\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 913 | globals->options->product_name.c_str()); |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 914 | std::unique_ptr<devtools_ijar::ZipExtractor> extractor( |
| 915 | devtools_ijar::ZipExtractor::Create(argv0.c_str(), &processor)); |
| 916 | if (extractor.get() == NULL) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 917 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 918 | "\nFailed to open %s as a zip file: (%d) %s", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 919 | globals->options->product_name.c_str(), errno, strerror(errno)); |
Damien Martin-Guillerez | eb6e903 | 2015-06-01 14:45:21 +0000 | [diff] [blame] | 920 | } |
| 921 | if (extractor->ProcessAll() < 0) { |
| 922 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 923 | "\nFailed to extract %s as a zip file: %s", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 924 | globals->options->product_name.c_str(), extractor->GetError()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | const time_t TEN_YEARS_IN_SEC = 3600 * 24 * 365 * 10; |
| 928 | time_t future_time = time(NULL) + TEN_YEARS_IN_SEC; |
| 929 | |
| 930 | // Set the timestamps of the extracted files to the future and make sure (or |
| 931 | // at least as sure as we can...) that the files we have written are actually |
| 932 | // on the disk. |
| 933 | |
| 934 | vector<string> extracted_files; |
| 935 | CollectExtractedFiles(embedded_binaries, extracted_files); |
| 936 | |
| 937 | set<string> synced_directories; |
| 938 | for (vector<string>::iterator it = extracted_files.begin(); it != extracted_files.end(); it++) { |
| 939 | |
| 940 | const char *extracted_path = it->c_str(); |
| 941 | |
| 942 | // Set the time to a distantly futuristic value so we can observe tampering. |
| 943 | // Note that keeping the default timestamp set by unzip (1970-01-01) and using |
| 944 | // that to detect tampering is not enough, because we also need the timestamp |
| 945 | // to change between Blaze releases so that the metadata cache knows that |
| 946 | // the files may have changed. This is important for actions that use |
| 947 | // embedded binaries as artifacts. |
| 948 | struct utimbuf times = { future_time, future_time }; |
| 949 | if (utime(extracted_path, ×) == -1) { |
| 950 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 951 | "failed to set timestamp on '%s'", extracted_path); |
| 952 | } |
| 953 | |
| 954 | SyncFile(extracted_path); |
| 955 | |
| 956 | string directory = blaze_util::Dirname(extracted_path); |
| 957 | |
| 958 | // Now walk up until embedded_binaries and sync every directory in between. |
| 959 | // synced_directories is used to avoid syncing the same directory twice. |
| 960 | // The !directory.empty() and directory != "/" conditions are not strictly |
| 961 | // needed, but it makes this loop more robust, because otherwise, if due to |
| 962 | // some glitch, directory was not under embedded_binaries, it would get |
| 963 | // into an infinite loop. |
| 964 | while (directory != embedded_binaries && |
| 965 | synced_directories.count(directory) == 0 && |
| 966 | !directory.empty() && |
| 967 | directory != "/") { |
| 968 | SyncFile(directory.c_str()); |
| 969 | synced_directories.insert(directory); |
| 970 | directory = blaze_util::Dirname(directory); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | SyncFile(embedded_binaries.c_str()); |
| 975 | } |
| 976 | |
| 977 | // Installs Blaze by extracting the embedded data files, iff necessary. |
| 978 | // The MD5-named install_base directory on disk is trusted; we assume |
| 979 | // no-one has modified the extracted files beneath this directory once |
| 980 | // it is in place. Concurrency during extraction is handled by |
| 981 | // extracting in a tmp dir and then renaming it into place where it |
| 982 | // becomes visible automically at the new path. |
| 983 | // Populates globals->extracted_binaries with their extracted locations. |
| 984 | static void ExtractData(const string &self_path) { |
| 985 | // If the install dir doesn't exist, create it, if it does, we know it's good. |
| 986 | struct stat buf; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 987 | if (stat(globals->options->install_base.c_str(), &buf) == -1) { |
Thiago Farina | 8a67da4 | 2015-05-05 18:04:50 +0000 | [diff] [blame] | 988 | uint64_t st = MonotonicClock(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 989 | // Work in a temp dir to avoid races. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 990 | string tmp_install = globals->options->install_base + ".tmp." + |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 991 | ToString(getpid()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 992 | string tmp_binaries = tmp_install + "/_embedded_binaries"; |
| 993 | ActuallyExtractData(self_path, tmp_binaries); |
| 994 | |
Thiago Farina | 8a67da4 | 2015-05-05 18:04:50 +0000 | [diff] [blame] | 995 | uint64_t et = MonotonicClock(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 996 | globals->extract_data_time = (et - st) / 1000000LL; |
| 997 | |
| 998 | // Now rename the completed installation to its final name. If this |
| 999 | // fails due to an ENOTEMPTY then we assume another good |
| 1000 | // installation snuck in before us. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1001 | if (rename(tmp_install.c_str(), globals->options->install_base.c_str()) == -1 |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1002 | && errno != ENOTEMPTY) { |
| 1003 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1004 | "install base directory '%s' could not be renamed into place", |
| 1005 | tmp_install.c_str()); |
| 1006 | } |
| 1007 | } else { |
| 1008 | if (!S_ISDIR(buf.st_mode)) { |
| 1009 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1010 | "Error: Install base directory '%s' could not be created. " |
| 1011 | "It exists but is not a directory.", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1012 | globals->options->install_base.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | const time_t time_now = time(NULL); |
| 1016 | string real_install_dir = blaze_util::JoinPath( |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1017 | globals->options->install_base, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1018 | "_embedded_binaries"); |
| 1019 | for (const auto& it : globals->extracted_binaries) { |
| 1020 | string path = blaze_util::JoinPath(real_install_dir, it); |
| 1021 | // Check that the file exists and is readable. |
| 1022 | if (stat(path.c_str(), &buf) == -1) { |
| 1023 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1024 | "Error: corrupt installation: file '%s' missing." |
| 1025 | " Please remove '%s' and try again.", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1026 | path.c_str(), globals->options->install_base.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1027 | } |
| 1028 | // Check that the timestamp is in the future. A past timestamp would indicate |
| 1029 | // that the file has been tampered with. See ActuallyExtractData(). |
Damien Martin-Guillerez | 1299767 | 2015-09-03 21:54:08 +0000 | [diff] [blame] | 1030 | if (!S_ISDIR(buf.st_mode) && buf.st_mtime <= time_now) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1031 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1032 | "Error: corrupt installation: file '%s' " |
| 1033 | "modified. Please remove '%s' and try again.", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1034 | path.c_str(), globals->options->install_base.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // Returns true if the server needs to be restarted to accommodate changes |
| 1041 | // between the two argument lists. |
| 1042 | static bool ServerNeedsToBeKilled(const vector<string>& args1, |
| 1043 | const vector<string>& args2) { |
| 1044 | // We need not worry about one side missing an argument and the other side |
| 1045 | // having the default value, since this command line is already the |
| 1046 | // canonicalized one that always contains every switch (with default values |
| 1047 | // if it was not present on the real command line). Same applies for argument |
| 1048 | // ordering. |
| 1049 | if (args1.size() != args2.size()) { |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | for (int i = 0; i < args1.size(); i++) { |
| 1054 | string option_sources = "--option_sources="; |
| 1055 | if (args1[i].substr(0, option_sources.size()) == option_sources && |
| 1056 | args2[i].substr(0, option_sources.size()) == option_sources) { |
| 1057 | continue; |
| 1058 | } |
| 1059 | |
Lukacs Berki | 3d48683 | 2016-10-26 12:51:38 +0000 | [diff] [blame] | 1060 | string max_idle_secs = "--max_idle_secs="; |
| 1061 | if (args1[i].substr(0, max_idle_secs.size()) == max_idle_secs && |
| 1062 | args2[i].substr(0, max_idle_secs.size()) == max_idle_secs) { |
| 1063 | continue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Lukacs Berki | 3d48683 | 2016-10-26 12:51:38 +0000 | [diff] [blame] | 1066 | if (args1[i] != args2[i]) { |
| 1067 | return true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return false; |
| 1072 | } |
| 1073 | |
| 1074 | // Kills the running Blaze server, if any, if the startup options do not match. |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 1075 | static void KillRunningServerIfDifferentStartupOptions(BlazeServer* server) { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1076 | if (!server->Connected()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1077 | return; |
| 1078 | } |
| 1079 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1080 | string cmdline_path = globals->options->output_base + "/server/cmdline"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1081 | string joined_arguments; |
| 1082 | |
| 1083 | // No, /proc/$PID/cmdline does not work, because it is limited to 4K. Even |
| 1084 | // worse, its behavior differs slightly between kernels (in some, when longer |
| 1085 | // command lines are truncated, the last 4 bytes are replaced with |
| 1086 | // "..." + NUL. |
| 1087 | ReadFile(cmdline_path, &joined_arguments); |
| 1088 | vector<string> arguments = blaze_util::Split(joined_arguments, '\0'); |
| 1089 | |
| 1090 | // These strings contain null-separated command line arguments. If they are |
| 1091 | // the same, the server can stay alive, otherwise, it needs shuffle off this |
| 1092 | // mortal coil. |
| 1093 | if (ServerNeedsToBeKilled(arguments, GetArgumentArray())) { |
| 1094 | globals->restart_reason = NEW_OPTIONS; |
| 1095 | fprintf(stderr, |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 1096 | "WARNING: Running %s server needs to be killed, because the " |
| 1097 | "startup options are different.\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1098 | globals->options->product_name.c_str()); |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1099 | server->KillRunningServer(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1103 | // Kills the old running server if it is not the same version as us, |
| 1104 | // dealing with various combinations of installation scheme |
| 1105 | // (installation symlink and older MD5_MANIFEST contents). |
| 1106 | // This function requires that the installation be complete, and the |
| 1107 | // server lock acquired. |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 1108 | static void EnsureCorrectRunningVersion(BlazeServer* server) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1109 | // Read the previous installation's semaphore symlink in output_base. If the |
| 1110 | // target dirs don't match, or if the symlink was not present, then kill any |
| 1111 | // running servers. Lastly, symlink to our installation so others know which |
| 1112 | // installation is running. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1113 | string installation_path = globals->options->output_base + "/install"; |
Lukacs Berki | 497d824 | 2016-04-28 07:21:26 +0000 | [diff] [blame] | 1114 | string prev_installation; |
| 1115 | bool ok = ReadDirectorySymlink(installation_path.c_str(), &prev_installation); |
| 1116 | if (!ok || !CompareAbsolutePaths( |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1117 | prev_installation, globals->options->install_base)) { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1118 | if (server->Connected()) { |
| 1119 | server->KillRunningServer(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1120 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1121 | |
| 1122 | globals->restart_reason = NEW_VERSION; |
Thiago Farina | 4e4ffd2 | 2016-03-09 17:02:28 +0000 | [diff] [blame] | 1123 | UnlinkPath(installation_path.c_str()); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1124 | if (!SymlinkDirectories(globals->options->install_base.c_str(), |
Dmitry Lomov | 47afaab | 2016-02-19 08:21:13 +0000 | [diff] [blame] | 1125 | installation_path.c_str())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1126 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1127 | "failed to create installation symlink '%s'", |
| 1128 | installation_path.c_str()); |
| 1129 | } |
| 1130 | const time_t time_now = time(NULL); |
| 1131 | struct utimbuf times = { time_now, time_now }; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1132 | if (utime(globals->options->install_base.c_str(), ×) == -1) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1133 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1134 | "failed to set timestamp on '%s'", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1135 | globals->options->install_base.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 1140 | // A signal-safe version of fprintf(stderr, ...). |
| 1141 | // |
| 1142 | // WARNING: any output from the blaze client may be interleaved |
| 1143 | // with output from the blaze server. In --curses mode, |
| 1144 | // the Blaze server often erases the previous line of output. |
| 1145 | // So, be sure to end each such message with TWO newlines, |
| 1146 | // otherwise it may be erased by the next message from the |
| 1147 | // Blaze server. |
| 1148 | // Also, it's a good idea to start each message with a newline, |
| 1149 | // in case the Blaze server has written a partial line. |
| 1150 | static void sigprintf(const char *format, ...) { |
| 1151 | char buf[1024]; |
| 1152 | va_list ap; |
| 1153 | va_start(ap, format); |
| 1154 | int r = vsnprintf(buf, sizeof buf, format, ap); |
| 1155 | va_end(ap); |
| 1156 | if (write(STDERR_FILENO, buf, r) <= 0) { |
| 1157 | // We don't care, just placate the compiler. |
| 1158 | } |
| 1159 | } |
| 1160 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1161 | // Signal handler. |
| 1162 | static void handler(int signum) { |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 1163 | int saved_errno = errno; |
| 1164 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1165 | switch (signum) { |
| 1166 | case SIGINT: |
| 1167 | if (++globals->sigint_count >= 3) { |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 1168 | sigprintf("\n%s caught third interrupt signal; killed.\n\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1169 | globals->options->product_name.c_str()); |
Lukacs Berki | 793cd01 | 2016-06-20 09:48:47 +0000 | [diff] [blame] | 1170 | if (globals->server_pid != -1) { |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 1171 | KillServerProcess(globals->server_pid); |
Lukacs Berki | 793cd01 | 2016-06-20 09:48:47 +0000 | [diff] [blame] | 1172 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1173 | _exit(1); |
| 1174 | } |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 1175 | sigprintf("\n%s caught interrupt signal; shutting down.\n\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1176 | globals->options->product_name.c_str()); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1177 | blaze_server->Cancel(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1178 | break; |
| 1179 | case SIGTERM: |
Kristina Chodorow | 11d40d2 | 2015-03-17 18:26:59 +0000 | [diff] [blame] | 1180 | sigprintf("\n%s caught terminate signal; shutting down.\n\n", |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1181 | globals->options->product_name.c_str()); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1182 | blaze_server->Cancel(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1183 | break; |
| 1184 | case SIGPIPE: |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1185 | globals->received_signal = SIGPIPE; |
| 1186 | break; |
| 1187 | case SIGQUIT: |
| 1188 | sigprintf("\nSending SIGQUIT to JVM process %d (see %s).\n\n", |
| 1189 | globals->server_pid, |
| 1190 | globals->jvm_log_file.c_str()); |
| 1191 | kill(globals->server_pid, SIGQUIT); |
| 1192 | break; |
| 1193 | } |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 1194 | |
| 1195 | errno = saved_errno; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1198 | // Performs all I/O for a single client request to the server, and |
| 1199 | // shuts down the client (by exit or signal). |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 1200 | static ATTRIBUTE_NORETURN void SendServerRequest(BlazeServer* server) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1201 | while (true) { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1202 | if (!server->Connected()) { |
| 1203 | StartServerAndConnect(server); |
| 1204 | } |
| 1205 | |
Lukacs Berki | 4de9894 | 2016-09-09 09:23:36 +0000 | [diff] [blame] | 1206 | // Check for the case when the workspace directory deleted and then gets |
| 1207 | // recreated while the server is running |
| 1208 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1209 | string server_cwd = GetProcessCWD(globals->server_pid); |
Lukacs Berki | 4be230a | 2015-10-15 13:43:03 +0000 | [diff] [blame] | 1210 | // If server_cwd is empty, GetProcessCWD failed. This notably occurs when |
| 1211 | // running under Docker because then readlink(/proc/[pid]/cwd) returns |
| 1212 | // EPERM. |
| 1213 | // Docker issue #6687 (https://github.com/docker/docker/issues/6687) fixed |
| 1214 | // this, but one still needs the --cap-add SYS_PTRACE command line flag, at |
| 1215 | // least according to the discussion on Docker issue #6800 |
| 1216 | // (https://github.com/docker/docker/issues/6687), and even then, it's a |
| 1217 | // non-default Docker flag. Given that this occurs only in very weird |
| 1218 | // cases, it's better to assume that everything is alright if we can't get |
| 1219 | // the cwd. |
| 1220 | |
| 1221 | if (!server_cwd.empty() && |
| 1222 | (server_cwd != globals->workspace || // changed |
| 1223 | server_cwd.find(" (deleted)") != string::npos)) { // deleted. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1224 | // There's a distant possibility that the two paths look the same yet are |
| 1225 | // actually different because the two processes have different mount |
| 1226 | // tables. |
| 1227 | if (VerboseLogging()) { |
| 1228 | fprintf(stderr, "Server's cwd moved or deleted (%s).\n", |
| 1229 | server_cwd.c_str()); |
| 1230 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1231 | server->KillRunningServer(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1232 | } else { |
| 1233 | break; |
| 1234 | } |
| 1235 | } |
| 1236 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1237 | if (VerboseLogging()) { |
| 1238 | fprintf(stderr, "Connected (server pid=%d).\n", globals->server_pid); |
| 1239 | } |
| 1240 | |
| 1241 | // Wall clock time since process startup. |
| 1242 | globals->startup_time = ProcessClock() / 1000000LL; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1243 | |
| 1244 | // Unblock all signals. |
| 1245 | sigset_t sigset; |
| 1246 | sigemptyset(&sigset); |
| 1247 | sigprocmask(SIG_SETMASK, &sigset, NULL); |
| 1248 | |
| 1249 | signal(SIGINT, handler); |
| 1250 | signal(SIGTERM, handler); |
| 1251 | signal(SIGPIPE, handler); |
| 1252 | signal(SIGQUIT, handler); |
| 1253 | |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1254 | int exit_code = server->Communicate(); |
| 1255 | if (globals->received_signal) { |
| 1256 | // Kill ourselves with the same signal, so that callers see the |
| 1257 | // right WTERMSIG value. |
| 1258 | signal(globals->received_signal, SIG_DFL); |
| 1259 | raise(globals->received_signal); |
| 1260 | exit(1); // (in case raise didn't kill us for some reason) |
| 1261 | } else { |
| 1262 | exit(exit_code); |
| 1263 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | // Parse the options, storing parsed values in globals. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1267 | static void ParseOptions(int argc, const char *argv[]) { |
| 1268 | string error; |
| 1269 | blaze_exit_code::ExitCode parse_exit_code = |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1270 | globals->option_processor->ParseOptions(argc, argv, globals->workspace, |
| 1271 | globals->cwd, &error); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1272 | if (parse_exit_code != blaze_exit_code::SUCCESS) { |
| 1273 | die(parse_exit_code, "%s", error.c_str()); |
| 1274 | } |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1275 | globals->options = globals->option_processor->GetParsedStartupOptions(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | // Returns the canonical form of a path. |
| 1279 | static string MakeCanonical(const char *path) { |
| 1280 | char *resolved_path = realpath(path, NULL); |
| 1281 | if (resolved_path == NULL) { |
| 1282 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1283 | "realpath('%s') failed", path); |
| 1284 | } |
| 1285 | |
| 1286 | string ret = resolved_path; |
| 1287 | free(resolved_path); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | // Compute the globals globals->cwd and globals->workspace. |
| 1292 | static void ComputeWorkspace() { |
| 1293 | char cwdbuf[PATH_MAX]; |
| 1294 | if (getcwd(cwdbuf, sizeof cwdbuf) == NULL) { |
| 1295 | pdie(blaze_exit_code::INTERNAL_ERROR, "getcwd() failed"); |
| 1296 | } |
| 1297 | globals->cwd = MakeCanonical(cwdbuf); |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 1298 | globals->workspace = WorkspaceLayout::GetWorkspace(globals->cwd); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | // Figure out the base directories based on embedded data, username, cwd, etc. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1302 | // Sets globals->options->install_base, globals->options->output_base, |
Thiago Farina | 6fd9bf1 | 2016-04-26 09:02:18 +0000 | [diff] [blame] | 1303 | // globals->lockfile, globals->jvm_log_file. |
Thiago Farina | 2fd7890 | 2015-05-18 11:37:59 +0000 | [diff] [blame] | 1304 | static void ComputeBaseDirectories(const string &self_path) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1305 | // Only start a server when in a workspace because otherwise we won't do more |
| 1306 | // than emit a help message. |
Julio Merino | 211a95c | 2016-08-29 11:01:35 +0000 | [diff] [blame] | 1307 | if (!WorkspaceLayout::InWorkspace(globals->workspace)) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1308 | globals->options->batch = true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | // The default install_base is <output_user_root>/install/<md5(blaze)> |
| 1312 | // but if an install_base is specified on the command line, we use that as |
| 1313 | // the base instead. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1314 | if (globals->options->install_base.empty()) { |
| 1315 | string install_user_root = globals->options->output_user_root + "/install"; |
| 1316 | globals->options->install_base = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1317 | GetInstallBase(install_user_root, self_path); |
| 1318 | } else { |
Eric Fellheimer | 4c5eb0f | 2015-08-12 15:02:24 +0000 | [diff] [blame] | 1319 | // We call GetInstallBase anyway to populate extracted_binaries and |
| 1320 | // install_md5. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1321 | GetInstallBase("", self_path); |
| 1322 | } |
| 1323 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1324 | if (globals->options->output_base.empty()) { |
Dmitry Lomov | bc84cc8 | 2016-04-15 14:05:24 +0000 | [diff] [blame] | 1325 | #if !defined(__CYGWIN__) |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1326 | globals->options->output_base = GetHashedBaseDir( |
| 1327 | globals->options->output_user_root, globals->workspace); |
Dmitry Lomov | bc84cc8 | 2016-04-15 14:05:24 +0000 | [diff] [blame] | 1328 | #else |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1329 | globals->options->output_base = GetHashedBaseDirForWindows( |
| 1330 | blaze::GetOutputRoot(), globals->options->product_name, |
Dmitry Lomov | bc84cc8 | 2016-04-15 14:05:24 +0000 | [diff] [blame] | 1331 | blaze::GetUserName(), globals->workspace); |
| 1332 | #endif |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | struct stat buf; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1336 | const char *output_base = globals->options->output_base.c_str(); |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1337 | if (stat(output_base, &buf) == -1) { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1338 | if (MakeDirectories(globals->options->output_base, 0777) == -1) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1339 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1340 | "Output base directory '%s' could not be created", |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1341 | output_base); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1342 | } |
| 1343 | } else { |
| 1344 | if (!S_ISDIR(buf.st_mode)) { |
| 1345 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1346 | "Error: Output base directory '%s' could not be created. " |
| 1347 | "It exists but is not a directory.", |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1348 | output_base); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1349 | } |
| 1350 | } |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1351 | if (access(output_base, R_OK | W_OK | X_OK) != 0) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1352 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1353 | "Error: Output base directory '%s' must be readable and writable.", |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1354 | output_base); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1355 | } |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1356 | ExcludePathFromBackup(output_base); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1357 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1358 | globals->options->output_base = MakeCanonical(output_base); |
| 1359 | globals->lockfile = globals->options->output_base + "/lock"; |
| 1360 | globals->jvm_log_file = globals->options->output_base + "/server/jvm.out"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static void CheckEnvironment() { |
Lukacs Berki | 86a28b0 | 2016-10-25 10:34:45 +0000 | [diff] [blame] | 1364 | if (getenv("http_proxy") != NULL) { |
| 1365 | fprintf(stderr, "Warning: ignoring http_proxy in environment.\n"); |
| 1366 | unsetenv("http_proxy"); |
| 1367 | } |
| 1368 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1369 | if (getenv("LD_ASSUME_KERNEL") != NULL) { |
| 1370 | // Fix for bug: if ulimit -s and LD_ASSUME_KERNEL are both |
| 1371 | // specified, the JVM fails to create threads. See thread_stack_regtest. |
| 1372 | // This is also provoked by LD_LIBRARY_PATH=/usr/lib/debug, |
| 1373 | // or anything else that causes the JVM to use LinuxThreads. |
| 1374 | fprintf(stderr, "Warning: ignoring LD_ASSUME_KERNEL in environment.\n"); |
| 1375 | unsetenv("LD_ASSUME_KERNEL"); |
| 1376 | } |
| 1377 | |
| 1378 | if (getenv("LD_PRELOAD") != NULL) { |
| 1379 | fprintf(stderr, "Warning: ignoring LD_PRELOAD in environment.\n"); |
| 1380 | unsetenv("LD_PRELOAD"); |
| 1381 | } |
| 1382 | |
| 1383 | if (getenv("_JAVA_OPTIONS") != NULL) { |
| 1384 | // This would override --host_jvm_args |
| 1385 | fprintf(stderr, "Warning: ignoring _JAVA_OPTIONS in environment.\n"); |
| 1386 | unsetenv("_JAVA_OPTIONS"); |
| 1387 | } |
| 1388 | |
Thiago Farina | dfe43a2 | 2015-04-07 13:48:49 +0000 | [diff] [blame] | 1389 | if (getenv("TEST_TMPDIR") != NULL) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1390 | fprintf(stderr, "INFO: $TEST_TMPDIR defined: output root default is " |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1391 | "'%s'.\n", globals->options->output_root.c_str()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // TODO(bazel-team): We've also seen a failure during loading (creating |
| 1395 | // threads?) when ulimit -Hs 8192. Characterize that and check for it here. |
| 1396 | |
| 1397 | // Make the JVM use ISO-8859-1 for parsing its command line because "blaze |
| 1398 | // run" doesn't handle non-ASCII command line arguments. This is apparently |
| 1399 | // the most reliable way to select the platform default encoding. |
| 1400 | setenv("LANG", "en_US.ISO-8859-1", 1); |
| 1401 | setenv("LANGUAGE", "en_US.ISO-8859-1", 1); |
| 1402 | setenv("LC_ALL", "en_US.ISO-8859-1", 1); |
| 1403 | setenv("LC_CTYPE", "en_US.ISO-8859-1", 1); |
| 1404 | } |
| 1405 | |
Thiago Farina | 0b6963e | 2015-04-28 20:26:45 +0000 | [diff] [blame] | 1406 | static void SetupStreams() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1407 | // Line-buffer stderr, since we always flush at the end of a server |
| 1408 | // message. This saves lots of single-char calls to write(2). |
| 1409 | // This doesn't work if any writes to stderr have already occurred! |
| 1410 | setlinebuf(stderr); |
| 1411 | |
| 1412 | // Ensure we have three open fds. Otherwise we can end up with |
| 1413 | // bizarre things like stdout going to the lock file, etc. |
Lukacs Berki | cdd4227 | 2016-09-13 07:52:01 +0000 | [diff] [blame] | 1414 | if (fcntl(STDIN_FILENO, F_GETFL) == -1) open("/dev/null", O_RDONLY); |
| 1415 | if (fcntl(STDOUT_FILENO, F_GETFL) == -1) open("/dev/null", O_WRONLY); |
| 1416 | if (fcntl(STDERR_FILENO, F_GETFL) == -1) open("/dev/null", O_WRONLY); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1417 | } |
| 1418 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1419 | static void CheckBinaryPath(const string& argv0) { |
| 1420 | if (argv0[0] == '/') { |
| 1421 | globals->binary_path = argv0; |
| 1422 | } else { |
| 1423 | string abs_path = globals->cwd + '/' + argv0; |
| 1424 | char *resolved_path = realpath(abs_path.c_str(), NULL); |
| 1425 | if (resolved_path) { |
| 1426 | globals->binary_path = resolved_path; |
| 1427 | free(resolved_path); |
| 1428 | } else { |
| 1429 | // This happens during our integration tests, but thats okay, as we won't |
| 1430 | // log the invocation anyway. |
| 1431 | globals->binary_path = abs_path; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // Create the user's directory where we keep state, installations etc. |
| 1437 | // Typically, this happens inside a temp directory, so we have to be |
| 1438 | // careful about symlink attacks. |
| 1439 | static void CreateSecureOutputRoot() { |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1440 | const char* root = globals->options->output_user_root.c_str(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1441 | struct stat fileinfo = {}; |
| 1442 | |
Kristina Chodorow | 46af79d | 2015-03-20 22:35:37 +0000 | [diff] [blame] | 1443 | if (MakeDirectories(root, 0755) == -1) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1444 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "mkdir('%s')", root); |
| 1445 | } |
| 1446 | |
| 1447 | // The path already exists. |
| 1448 | // Check ownership and mode, and verify that it is a directory. |
| 1449 | |
| 1450 | if (lstat(root, &fileinfo) < 0) { |
| 1451 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "lstat('%s')", root); |
| 1452 | } |
| 1453 | |
| 1454 | if (fileinfo.st_uid != geteuid()) { |
| 1455 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "'%s' is not owned by me", |
| 1456 | root); |
| 1457 | } |
| 1458 | |
| 1459 | if ((fileinfo.st_mode & 022) != 0) { |
| 1460 | int new_mode = fileinfo.st_mode & (~022); |
| 1461 | if (chmod(root, new_mode) < 0) { |
| 1462 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1463 | "'%s' has mode %o, chmod to %o failed", root, |
| 1464 | fileinfo.st_mode & 07777, new_mode); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | if (stat(root, &fileinfo) < 0) { |
| 1469 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "stat('%s')", root); |
| 1470 | } |
| 1471 | |
| 1472 | if (!S_ISDIR(fileinfo.st_mode)) { |
| 1473 | die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "'%s' is not a directory", |
| 1474 | root); |
| 1475 | } |
Dave MacLachlan | 6b747ee | 2016-07-20 10:00:44 +0000 | [diff] [blame] | 1476 | |
| 1477 | ExcludePathFromBackup(root); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // TODO(bazel-team): Execute the server as a child process and write its exit |
| 1481 | // code to a file. In case the server becomes unresonsive or terminates |
| 1482 | // unexpectedly (in a way that isn't already handled), we can observe the file, |
| 1483 | // if it exists. (If it doesn't, then we know something went horribly wrong.) |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1484 | int Main(int argc, const char *argv[], OptionProcessor *option_processor) { |
Thiago Farina | 676cb9f | 2016-10-06 11:00:43 +0000 | [diff] [blame] | 1485 | globals = new GlobalVariables(option_processor); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1486 | SetupStreams(); |
| 1487 | |
| 1488 | // Must be done before command line parsing. |
| 1489 | ComputeWorkspace(); |
| 1490 | CheckBinaryPath(argv[0]); |
| 1491 | ParseOptions(argc, argv); |
Lukacs Berki | bb2230f | 2016-04-27 14:19:25 +0000 | [diff] [blame] | 1492 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1493 | string error; |
| 1494 | blaze_exit_code::ExitCode reexec_options_exit_code = |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1495 | globals->options->CheckForReExecuteOptions(argc, argv, &error); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1496 | if (reexec_options_exit_code != blaze_exit_code::SUCCESS) { |
| 1497 | die(reexec_options_exit_code, "%s", error.c_str()); |
| 1498 | } |
| 1499 | CheckEnvironment(); |
| 1500 | CreateSecureOutputRoot(); |
| 1501 | |
| 1502 | const string self_path = GetSelfPath(); |
| 1503 | ComputeBaseDirectories(self_path); |
| 1504 | |
Lukacs Berki | 3a3c483 | 2016-10-06 14:20:04 +0000 | [diff] [blame] | 1505 | blaze_server = static_cast<BlazeServer *>(new GrpcBlazeServer()); |
Lukacs Berki | 907dbbf | 2016-04-15 11:30:12 +0000 | [diff] [blame] | 1506 | |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 1507 | globals->command_wait_time = blaze_server->AcquireLock(); |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 1508 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1509 | WarnFilesystemType(globals->options->output_base); |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 1510 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1511 | ExtractData(self_path); |
Lukacs Berki | 949c876 | 2016-07-08 12:17:28 +0000 | [diff] [blame] | 1512 | VerifyJavaVersionAndSetJvm(); |
| 1513 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1514 | blaze_server->Connect(); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1515 | EnsureCorrectRunningVersion(blaze_server); |
| 1516 | KillRunningServerIfDifferentStartupOptions(blaze_server); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1517 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1518 | if (globals->options->batch) { |
| 1519 | SetScheduling(globals->options->batch_cpu_scheduling, |
| 1520 | globals->options->io_nice_level); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1521 | StartStandalone(blaze_server); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1522 | } else { |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1523 | SendServerRequest(blaze_server); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1524 | } |
| 1525 | return 0; |
| 1526 | } |
Thiago Farina | 0b6963e | 2015-04-28 20:26:45 +0000 | [diff] [blame] | 1527 | |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1528 | static void null_grpc_log_function(gpr_log_func_args *args) { |
| 1529 | } |
| 1530 | |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1531 | GrpcBlazeServer::GrpcBlazeServer() { |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1532 | gpr_set_log_function(null_grpc_log_function); |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1533 | connected_ = false; |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1534 | int fd[2]; |
| 1535 | if (pipe(fd) < 0) { |
| 1536 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1537 | "pipe()"); |
| 1538 | } |
| 1539 | recv_socket_ = fd[0]; |
| 1540 | send_socket_ = fd[1]; |
| 1541 | |
| 1542 | if (fcntl(recv_socket_, F_SETFD, FD_CLOEXEC) == -1) { |
| 1543 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1544 | "fcntl(F_SETFD, FD_CLOEXEC) failed"); |
| 1545 | } |
| 1546 | |
| 1547 | if (fcntl(send_socket_, F_SETFD, FD_CLOEXEC) == -1) { |
| 1548 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1549 | "fcntl(F_SETFD, FD_CLOEXEC) failed"); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | GrpcBlazeServer::~GrpcBlazeServer() { |
| 1554 | close(send_socket_); |
| 1555 | close(recv_socket_); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | bool GrpcBlazeServer::Connect() { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1559 | assert(!connected_); |
| 1560 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1561 | std::string server_dir = globals->options->output_base + "/server"; |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1562 | std::string port; |
Lukacs Berki | b7caf9d | 2016-04-25 09:44:14 +0000 | [diff] [blame] | 1563 | std::string ipv4_prefix = "127.0.0.1:"; |
Lukacs Berki | c8e7424 | 2016-04-28 08:32:04 +0000 | [diff] [blame] | 1564 | std::string ipv6_prefix_1 = "[0:0:0:0:0:0:0:1]:"; |
| 1565 | std::string ipv6_prefix_2 = "[::1]:"; |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1566 | |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 1567 | if (!ReadFile(server_dir + "/command_port", &port)) { |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1568 | return false; |
| 1569 | } |
| 1570 | |
Lukacs Berki | b7caf9d | 2016-04-25 09:44:14 +0000 | [diff] [blame] | 1571 | // Make sure that we are being directed to localhost |
| 1572 | if (port.compare(0, ipv4_prefix.size(), ipv4_prefix) |
Lukacs Berki | c8e7424 | 2016-04-28 08:32:04 +0000 | [diff] [blame] | 1573 | && port.compare(0, ipv6_prefix_1.size(), ipv6_prefix_1) |
| 1574 | && port.compare(0, ipv6_prefix_2.size(), ipv6_prefix_2)) { |
Lukacs Berki | b7caf9d | 2016-04-25 09:44:14 +0000 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
| 1577 | |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 1578 | if (!ReadFile(server_dir + "/request_cookie", &request_cookie_)) { |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
| 1581 | |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 1582 | if (!ReadFile(server_dir + "/response_cookie", &response_cookie_)) { |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1583 | return false; |
| 1584 | } |
| 1585 | |
| 1586 | std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel( |
Lukacs Berki | b7caf9d | 2016-04-25 09:44:14 +0000 | [diff] [blame] | 1587 | port, grpc::InsecureChannelCredentials())); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1588 | std::unique_ptr<command_server::CommandServer::Stub> client( |
| 1589 | command_server::CommandServer::NewStub(channel)); |
| 1590 | |
| 1591 | grpc::ClientContext context; |
| 1592 | context.set_deadline( |
Michajlo Matijkiw | 641c132 | 2016-08-12 18:05:29 +0000 | [diff] [blame] | 1593 | std::chrono::system_clock::now() + std::chrono::seconds(10)); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1594 | |
| 1595 | command_server::PingRequest request; |
| 1596 | command_server::PingResponse response; |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1597 | request.set_cookie(request_cookie_); |
Lukacs Berki | 7494c92 | 2016-04-27 11:17:51 +0000 | [diff] [blame] | 1598 | |
Lukacs Berki | 7e0249e | 2016-04-21 08:14:08 +0000 | [diff] [blame] | 1599 | grpc::Status status = client->Ping(&context, request, &response); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1600 | |
Lukacs Berki | c55e9c7 | 2016-04-25 13:43:40 +0000 | [diff] [blame] | 1601 | if (!status.ok() || response.cookie() != response_cookie_) { |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1602 | return false; |
| 1603 | } |
| 1604 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1605 | globals->server_pid = GetServerPid(server_dir); |
| 1606 | if (globals->server_pid <= 0) { |
| 1607 | pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| 1608 | "can't get PID of existing server (server dir=%s)", |
| 1609 | server_dir.c_str()); |
| 1610 | } |
| 1611 | |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1612 | this->client_ = std::move(client); |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1613 | connected_ = true; |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1614 | return true; |
| 1615 | } |
| 1616 | |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1617 | // Cancellation works as follows: |
| 1618 | // |
| 1619 | // When the user presses Ctrl-C, a SIGINT is delivered to the client, which is |
| 1620 | // translated into a BlazeServer::Cancel() call. Since it's not a good idea to |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1621 | // do significant work in signal handlers, all it does is write a byte to an |
| 1622 | // unnamed pipe. |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1623 | // |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1624 | // This unnamed pipe is used to communicate with the cancel thread. Whenever |
| 1625 | // something interesting happens, a byte is written into it, which is read by |
| 1626 | // the cancel thread. These commands are available: |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1627 | // |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1628 | // - NOP |
| 1629 | // - JOIN. The cancel thread needs to be terminated. |
| 1630 | // - CANCEL. If the command ID is already available, a cancel request is sent. |
| 1631 | // - COMMAND_ID_RECEIVED. The client learned the command ID from the server. |
| 1632 | // If there is a pending cancellation request, it is acted upon. |
| 1633 | // |
| 1634 | // The only data the cancellation thread shares with the main thread is the |
| 1635 | // file descriptor for receiving commands and command_id_, the latter of which |
| 1636 | // is protected by a mutex, which mainly serves as a memory fence. |
| 1637 | // |
| 1638 | // The cancellation thread is joined at the end of the execution of the command. |
| 1639 | // The main thread wakes it up just so that it can finish (using the JOIN |
| 1640 | // action) |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1641 | // |
| 1642 | // It's conceivable that the server is busy and thus it cannot service the |
| 1643 | // cancellation request. In that case, we simply ignore the failure and the both |
| 1644 | // the server and the client go on as if nothing had happened (except that this |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1645 | // Ctrl-C still counts as a SIGINT, three of which result in a SIGKILL being |
| 1646 | // delivered to the server) |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1647 | void GrpcBlazeServer::CancelThread() { |
| 1648 | bool running = true; |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1649 | bool cancel = false; |
| 1650 | bool command_id_received = false; |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1651 | while (running) { |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1652 | char buf; |
| 1653 | int bytes_read = read(recv_socket_, &buf, 1); |
| 1654 | if (bytes_read == -1 && errno == EINTR) { |
| 1655 | continue; |
| 1656 | } else if (bytes_read != 1) { |
| 1657 | pdie(blaze_exit_code::INTERNAL_ERROR, |
| 1658 | "Cannot communicate with cancel thread"); |
| 1659 | } |
| 1660 | |
| 1661 | switch (buf) { |
| 1662 | case CancelThreadAction::NOTHING: |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1665 | case CancelThreadAction::JOIN: |
| 1666 | running = false; |
| 1667 | break; |
| 1668 | |
| 1669 | case CancelThreadAction::COMMAND_ID_RECEIVED: |
| 1670 | command_id_received = true; |
| 1671 | if (cancel) { |
| 1672 | SendCancelMessage(); |
| 1673 | cancel = false; |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1674 | } |
| 1675 | break; |
| 1676 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1677 | case CancelThreadAction::CANCEL: |
| 1678 | if (command_id_received) { |
| 1679 | SendCancelMessage(); |
| 1680 | } else { |
| 1681 | cancel = true; |
| 1682 | } |
| 1683 | break; |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1688 | void GrpcBlazeServer::SendCancelMessage() { |
| 1689 | std::unique_lock<std::mutex> lock(cancel_thread_mutex_); |
| 1690 | |
| 1691 | command_server::CancelRequest request; |
| 1692 | request.set_cookie(request_cookie_); |
| 1693 | request.set_command_id(command_id_); |
| 1694 | grpc::ClientContext context; |
| 1695 | context.set_deadline(std::chrono::system_clock::now() + |
Lukacs Berki | 3ace300 | 2016-08-31 08:55:34 +0000 | [diff] [blame] | 1696 | std::chrono::seconds(10)); |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1697 | command_server::CancelResponse response; |
| 1698 | // There isn't a lot we can do if this request fails |
Lukacs Berki | 3ace300 | 2016-08-31 08:55:34 +0000 | [diff] [blame] | 1699 | grpc::Status status = client_->Cancel(&context, request, &response); |
| 1700 | if (!status.ok()) { |
| 1701 | fprintf(stderr, "\nCould not interrupt server (%s)\n\n", |
| 1702 | status.error_message().c_str()); |
| 1703 | } |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1706 | // This will wait indefinitely until the server shuts down |
| 1707 | void GrpcBlazeServer::KillRunningServer() { |
| 1708 | assert(connected_); |
| 1709 | assert(globals->server_pid > 0); |
| 1710 | |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1711 | grpc::ClientContext context; |
| 1712 | command_server::RunRequest request; |
| 1713 | command_server::RunResponse response; |
| 1714 | request.set_cookie(request_cookie_); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1715 | request.set_block_for_lock(globals->options->block_for_lock); |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1716 | request.set_client_description( |
| 1717 | "pid=" + ToString(getpid()) + " (for shutdown)"); |
| 1718 | request.add_arg("shutdown"); |
| 1719 | std::unique_ptr<grpc::ClientReader<command_server::RunResponse>> reader( |
| 1720 | client_->Run(&context, request)); |
| 1721 | |
| 1722 | while (reader->Read(&response)) {} |
| 1723 | |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1724 | // Kill the server process for good measure. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1725 | if (VerifyServerProcess(globals->server_pid, globals->options->output_base, |
| 1726 | globals->options->install_base)) { |
Lukacs Berki | ee44c38 | 2016-09-14 10:53:37 +0000 | [diff] [blame] | 1727 | KillServerProcess(globals->server_pid); |
| 1728 | } |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1729 | |
| 1730 | connected_ = false; |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | unsigned int GrpcBlazeServer::Communicate() { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1734 | assert(connected_); |
| 1735 | |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1736 | vector<string> arg_vector; |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1737 | string command = globals->option_processor->GetCommand(); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1738 | if (command != "") { |
| 1739 | arg_vector.push_back(command); |
| 1740 | AddLoggingArgs(&arg_vector); |
| 1741 | } |
| 1742 | |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1743 | globals->option_processor->GetCommandArguments(&arg_vector); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1744 | |
| 1745 | command_server::RunRequest request; |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1746 | request.set_cookie(request_cookie_); |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 1747 | request.set_block_for_lock(globals->options->block_for_lock); |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 1748 | request.set_client_description("pid=" + ToString(getpid())); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1749 | for (const string& arg : arg_vector) { |
| 1750 | request.add_arg(arg); |
| 1751 | } |
| 1752 | |
| 1753 | grpc::ClientContext context; |
| 1754 | command_server::RunResponse response; |
| 1755 | std::unique_ptr<grpc::ClientReader<command_server::RunResponse>> reader( |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1756 | client_->Run(&context, request)); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1757 | |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 1758 | // Release the server lock because the gRPC handles concurrent clients just |
| 1759 | // fine. Note that this may result in two "waiting for other client" messages |
| 1760 | // (one during server startup and one emitted by the server) |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1761 | blaze::ReleaseLock(&blaze_lock_); |
Lukacs Berki | 415d39a | 2016-04-28 13:18:54 +0000 | [diff] [blame] | 1762 | |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1763 | std::thread cancel_thread(&GrpcBlazeServer::CancelThread, this); |
| 1764 | bool command_id_set = false; |
Laurent Le Brun | 08849b2 | 2016-09-20 12:21:32 +0000 | [diff] [blame] | 1765 | bool pipe_broken = false; |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1766 | while (reader->Read(&response)) { |
Lukacs Berki | c55e9c7 | 2016-04-25 13:43:40 +0000 | [diff] [blame] | 1767 | if (response.cookie() != response_cookie_) { |
| 1768 | fprintf(stderr, "\nServer response cookie invalid, exiting\n"); |
| 1769 | return blaze_exit_code::INTERNAL_ERROR; |
| 1770 | } |
| 1771 | |
Laurent Le Brun | 08849b2 | 2016-09-20 12:21:32 +0000 | [diff] [blame] | 1772 | bool pipe_broken_now = false; |
Lukacs Berki | edeb753 | 2016-04-18 10:23:36 +0000 | [diff] [blame] | 1773 | if (response.standard_output().size() > 0) { |
Laurent Le Brun | 08849b2 | 2016-09-20 12:21:32 +0000 | [diff] [blame] | 1774 | int result = write(STDOUT_FILENO, response.standard_output().c_str(), |
| 1775 | response.standard_output().size()); |
| 1776 | if (result < 0 && errno == EPIPE) { |
| 1777 | pipe_broken_now = true; |
| 1778 | } |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Lukacs Berki | edeb753 | 2016-04-18 10:23:36 +0000 | [diff] [blame] | 1781 | if (response.standard_error().size() > 0) { |
Laurent Le Brun | 08849b2 | 2016-09-20 12:21:32 +0000 | [diff] [blame] | 1782 | int result = write(STDERR_FILENO, response.standard_error().c_str(), |
| 1783 | response.standard_error().size()); |
| 1784 | if (result < 0 && errno == EPIPE) { |
| 1785 | pipe_broken_now = true; |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | if (pipe_broken_now && !pipe_broken) { |
| 1790 | pipe_broken = true; |
| 1791 | Cancel(); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1792 | } |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1793 | |
| 1794 | if (!command_id_set && response.command_id().size() > 0) { |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1795 | std::unique_lock<std::mutex> lock(cancel_thread_mutex_); |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1796 | command_id_ = response.command_id(); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1797 | command_id_set = true; |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1798 | SendAction(CancelThreadAction::COMMAND_ID_RECEIVED); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1799 | } |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1802 | SendAction(CancelThreadAction::JOIN); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1803 | cancel_thread.join(); |
| 1804 | |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1805 | if (!response.finished()) { |
Lukacs Berki | 3ace300 | 2016-08-31 08:55:34 +0000 | [diff] [blame] | 1806 | fprintf(stderr, "\nServer finished RPC without an explicit exit code\n\n"); |
Lukacs Berki | 2896dc0 | 2016-07-07 07:55:04 +0000 | [diff] [blame] | 1807 | return GetExitCodeForAbruptExit(*globals); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Lukacs Berki | e6a34f6 | 2016-04-25 12:16:04 +0000 | [diff] [blame] | 1810 | return response.exit_code(); |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | void GrpcBlazeServer::Disconnect() { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1814 | assert(connected_); |
| 1815 | |
Lukacs Berki | 00cfb7d | 2016-04-20 09:01:52 +0000 | [diff] [blame] | 1816 | client_.reset(); |
| 1817 | request_cookie_ = ""; |
| 1818 | response_cookie_ = ""; |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1819 | connected_ = false; |
Lukacs Berki | 1b25ce2 | 2016-04-15 13:11:21 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1822 | void GrpcBlazeServer::SendAction(CancelThreadAction action) { |
| 1823 | char msg = action; |
Sasha Smundak | 1fdd31d | 2016-07-25 17:54:00 +0000 | [diff] [blame] | 1824 | if (write(send_socket_, &msg, 1) <= 0) { |
Lukacs Berki | 3ace300 | 2016-08-31 08:55:34 +0000 | [diff] [blame] | 1825 | sigprintf("\nCould not interrupt server (cannot write to client pipe)\n\n"); |
Sasha Smundak | 1fdd31d | 2016-07-25 17:54:00 +0000 | [diff] [blame] | 1826 | } |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1829 | void GrpcBlazeServer::Cancel() { |
Lukacs Berki | 1977d92 | 2016-05-02 09:31:37 +0000 | [diff] [blame] | 1830 | assert(connected_); |
Lukacs Berki | 6dd2909 | 2016-05-30 14:05:33 +0000 | [diff] [blame] | 1831 | SendAction(CancelThreadAction::CANCEL); |
Lukacs Berki | f1df38a | 2016-04-19 07:42:22 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1834 | } // namespace blaze |