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