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