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