blob: 96335651ef95092a9120194ff66d3ea732a94a67 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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 Merino28774852016-09-14 16:59:46 +000026#include "src/main/cpp/blaze.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027
28#include <assert.h>
29#include <ctype.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030#include <errno.h>
31#include <fcntl.h>
32#include <limits.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033#include <stdarg.h>
Thiago Farina8a67da42015-05-05 18:04:50 +000034#include <stdint.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038#include <time.h>
39#include <unistd.h>
Lukacs Berkie21e5922016-04-12 12:22:20 +000040
41#include <grpc/grpc.h>
Googler197547b2016-09-26 22:25:14 +000042#include <grpc/support/log.h>
Lukacs Berkie21e5922016-04-12 12:22:20 +000043#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 Nienhuysd08b27f2015-02-25 16:45:20 +010048#include <algorithm>
Lukacs Berki1b25ce22016-04-15 13:11:21 +000049#include <chrono> // NOLINT (gRPC requires this)
Lukacs Berkif1df38a2016-04-19 07:42:22 +000050#include <mutex> // NOLINT
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010051#include <set>
52#include <string>
Lukacs Berkif1df38a2016-04-19 07:42:22 +000053#include <thread> // NOLINT
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054#include <utility>
55#include <vector>
56
Lukacs Berkie21e5922016-04-12 12:22:20 +000057
Nathan Harmatabf98f392016-01-07 22:58:29 +000058#include "src/main/cpp/blaze_abrupt_exit.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000059#include "src/main/cpp/blaze_util.h"
60#include "src/main/cpp/blaze_util_platform.h"
Thiago Farina676cb9f2016-10-06 11:00:43 +000061#include "src/main/cpp/global_variables.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000062#include "src/main/cpp/option_processor.h"
Julio Merino28774852016-09-14 16:59:46 +000063#include "src/main/cpp/startup_options.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000064#include "src/main/cpp/util/errors.h"
Thiago Farina7f9357f2015-04-23 13:57:43 +000065#include "src/main/cpp/util/exit_code.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000066#include "src/main/cpp/util/file.h"
Laszlo Csomor9c951962016-11-10 13:31:27 +000067#include "src/main/cpp/util/file_platform.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000068#include "src/main/cpp/util/numbers.h"
69#include "src/main/cpp/util/port.h"
70#include "src/main/cpp/util/strings.h"
Julio Merino211a95c2016-08-29 11:01:35 +000071#include "src/main/cpp/workspace_layout.h"
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +000072#include "third_party/ijar/zip.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073
Lukacs Berkie21e5922016-04-12 12:22:20 +000074#include "src/main/protobuf/command_server.grpc.pb.h"
75
Thiago Farina241f46c2015-04-13 14:33:30 +000076using blaze_util::die;
77using blaze_util::pdie;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010079namespace blaze {
80
Thiago Farina80bb0f22016-10-17 15:57:13 +000081using std::set;
82using std::string;
83using std::vector;
84
Lukacs Berki1977d922016-05-02 09:31:37 +000085static int GetServerPid(const string &server_dir);
Eric Fellheimer3a695f32016-05-11 17:26:30 +000086static void VerifyJavaVersionAndSetJvm();
Lukacs Berki907dbbf2016-04-15 11:30:12 +000087
Lukacs Berki1977d922016-05-02 09:31:37 +000088// The following is a treatise on how the interaction between the client and the
89// server works.
90//
91// First, the client unconditionally acquires an flock() lock on
92// $OUTPUT_BASE/lock then verifies if it has already extracted itself by
93// checking if the directory it extracts itself to (install base + a checksum)
94// is present. If not, then it does the extraction. Care is taken that this
95// process is atomic so that Blazen in multiple output bases do not clash.
96//
97// Then the client tries to connect to the currently executing server and kills
98// it if at least one of the following conditions is true:
99//
100// - The server is of the wrong version (as determined by the
101// $OUTPUT_BASE/install symlink)
102// - The server has different startup options than the client wants
103// - The client wants to run the command in batch mode
104//
105// Then, if needed, the client adjusts the install link to indicate which
106// version of the server it is running.
107//
108// In batch mode, the client then simply executes the server while taking care
109// that the output base lock is kept until it finishes.
110//
111// If in server mode, the client starts up a server if needed then sends the
Thiago Farina69dac862016-11-02 09:48:27 +0000112// command to the client and streams back stdout and stderr. The output base
113// lock is released after the command is sent to the server (the server
114// implements its own locking mechanism).
Lukacs Berki1977d922016-05-02 09:31:37 +0000115
116// Synchronization between the client and the server is a little precarious
117// because the client needs to know the PID of the server and it is not
118// available using a Java API and we don't have JNI on Windows at the moment,
119// so the server can't just communicate this over the communication channel.
120// Thus, a PID file is used, but care needs to be taken that the contents of
121// this PID file are right.
122//
123// Upon server startup, the PID file is written before the client spawns the
124// server. Thus, when the client can connect, it can be certain that the PID
125// file is up to date.
126//
127// Upon server shutdown, the PID file is deleted using a server shutdown hook.
128// However, this happens *after* the server stopped listening, so it's possible
129// that a client has already started up a server and written a new PID file.
130// In order to avoid this, when the client starts up a new server, it reads the
131// contents of the PID file and kills the process indicated in it (it could do
132// with a bit more care, since PIDs can be reused, but for now, we just believe
133// the PID file)
134//
135// Some more interesting scenarios:
136//
137// - The server receives a kill signal and it does not have a chance to delete
138// the PID file: the client cannot connect, reads the PID file, kills the
139// process indicated in it and starts up a new server.
140//
141// - The server stopped accepting connections but hasn't quit yet and a new
142// client comes around: the new client will kill the server based on the
143// PID file before a new server is started up.
144//
145// Alternative implementations:
146//
147// - Don't deal with PIDs at all. This would make it impossible for the client
148// to deliver a SIGKILL to the server after three SIGINTs. It would only be
149// possible with gRPC anyway.
150//
151// - Have the server check that the PID file containts the correct things
152// before deleting them: there is a window of time between checking the file
153// and deleting it in which a new server can overwrite the PID file. The
154// output base lock cannot be acquired, either, because when starting up a
155// new server, the client already holds it.
156//
157// - Delete the PID file before stopping to accept connections: then a client
158// could come about after deleting the PID file but before stopping accepting
159// connections. It would also not be resilient against a dead server that
160// left a PID file around.
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000161class BlazeServer {
162 public:
163 virtual ~BlazeServer() {}
164
Lukacs Berki1977d922016-05-02 09:31:37 +0000165 // Acquire a lock for the server running in this output base. Returns the
166 // number of milliseconds spent waiting for the lock.
Lukacs Berki415d39a2016-04-28 13:18:54 +0000167 uint64_t AcquireLock();
168
Lukacs Berki1977d922016-05-02 09:31:37 +0000169 // Whether there is an active connection to a server.
170 bool Connected() const { return connected_; }
171
Lukacs Berkie6a34f62016-04-25 12:16:04 +0000172 // Connect to the server. Returns if the connection was successful. Only
173 // call this when this object is in disconnected state. If it returns true,
174 // this object will be in connected state.
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000175 virtual bool Connect() = 0;
Lukacs Berkie6a34f62016-04-25 12:16:04 +0000176
177 // Disconnects from an existing server. Only call this when this object is in
178 // connected state. After this call returns, the object will be in connected
179 // state.
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000180 virtual void Disconnect() = 0;
Lukacs Berkie6a34f62016-04-25 12:16:04 +0000181
182 // Send the command line to the server and forward whatever it says to stdout
183 // and stderr. Returns the desired exit code. Only call this when the server
184 // is in connected state.
185 virtual unsigned int Communicate() = 0;
186
187 // Disconnects and kills an existing server. Only call this when this object
188 // is in connected state.
Lukacs Berki1977d922016-05-02 09:31:37 +0000189 virtual void KillRunningServer() = 0;
Lukacs Berkie6a34f62016-04-25 12:16:04 +0000190
191 // Cancel the currently running command. If there is no command currently
Lukacs Berki1977d922016-05-02 09:31:37 +0000192 // running, the result is unspecified. When called, this object must be in
193 // connected state.
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000194 virtual void Cancel() = 0;
Thiago Farina69dac862016-11-02 09:48:27 +0000195
196 protected:
197 BlazeLock blaze_lock_;
198 bool connected_;
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000199};
200
Lukacs Berki415d39a2016-04-28 13:18:54 +0000201////////////////////////////////////////////////////////////////////////
202// Global Variables
203static GlobalVariables *globals;
204static BlazeServer *blaze_server;
205
Lukacs Berki415d39a2016-04-28 13:18:54 +0000206uint64_t BlazeServer::AcquireLock() {
207 return blaze::AcquireLock(
Julio Merino28774852016-09-14 16:59:46 +0000208 globals->options->output_base, globals->options->batch,
209 globals->options->block_for_lock, &blaze_lock_);
Lukacs Berki415d39a2016-04-28 13:18:54 +0000210}
211
Lukacs Berki1977d922016-05-02 09:31:37 +0000212// Communication method that uses gRPC on a socket bound to localhost. More
213// documentation is in command_server.proto .
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000214class GrpcBlazeServer : public BlazeServer {
215 public:
Lukacs Berki71675a52016-11-08 09:48:27 +0000216 GrpcBlazeServer(int connect_timeout_secs);
Lukacs Berki6dd29092016-05-30 14:05:33 +0000217 virtual ~GrpcBlazeServer();
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000218
Lukacs Berki9d52bc52016-06-07 11:11:04 +0000219 virtual bool Connect();
220 virtual void Disconnect();
221 virtual unsigned int Communicate();
222 virtual void KillRunningServer();
223 virtual void Cancel();
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000224
225 private:
Lukacs Berki6dd29092016-05-30 14:05:33 +0000226 enum CancelThreadAction { NOTHING, JOIN, CANCEL, COMMAND_ID_RECEIVED };
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000227
228 std::unique_ptr<command_server::CommandServer::Stub> client_;
229 std::string request_cookie_;
230 std::string response_cookie_;
231 std::string command_id_;
232
Lukacs Berki6dd29092016-05-30 14:05:33 +0000233 // protects command_id_ . Although we always set it before making the cancel
234 // thread do something with it, the mutex is still useful because it provides
235 // a memory fence.
236 std::mutex cancel_thread_mutex_;
Lukacs Berki8b999982016-04-26 15:40:38 +0000237
Lukacs Berki71675a52016-11-08 09:48:27 +0000238 int connect_timeout_secs_;
Laszlo Csomoref5ceef2016-11-18 11:19:02 +0000239
240 // Pipe that the main thread sends actions to and the cancel thread receieves
241 // actions from.
242 blaze_util::IPipe* _pipe;
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000243
244 void CancelThread();
Lukacs Berki6dd29092016-05-30 14:05:33 +0000245 void SendAction(CancelThreadAction action);
246 void SendCancelMessage();
Lukacs Berki00cfb7d2016-04-20 09:01:52 +0000247};
248
249
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100250////////////////////////////////////////////////////////////////////////
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100251// Logic
252
Lukacs Berki71675a52016-11-08 09:48:27 +0000253void debug_log(const char* format, ...) {
254 if (!globals->options->client_debug) {
255 return;
256 }
257
258 fprintf(stderr, "CLIENT: ");
259 va_list arglist;
260 va_start(arglist, format);
261 vfprintf(stderr, format, arglist);
262 va_end(arglist);
263 fprintf(stderr, "%s", "\n");
264 fflush(stderr);
265}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100266
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000267// A devtools_ijar::ZipExtractorProcessor to extract the InstallKeyFile
268class GetInstallKeyFileProcessor : public devtools_ijar::ZipExtractorProcessor {
269 public:
Thiago Farina9cb32752015-06-03 15:34:19 +0000270 explicit GetInstallKeyFileProcessor(string *install_base_key)
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000271 : install_base_key_(install_base_key) {}
272
273 virtual bool Accept(const char *filename, const devtools_ijar::u4 attr) {
274 globals->extracted_binaries.push_back(filename);
275 return strcmp(filename, "install_base_key") == 0;
276 }
277
278 virtual void Process(const char *filename, const devtools_ijar::u4 attr,
279 const devtools_ijar::u1 *data, const size_t size) {
280 string str(reinterpret_cast<const char *>(data), size);
281 blaze_util::StripWhitespace(&str);
Lukacs Berki58c29ae2015-10-16 14:48:33 +0000282 if (str.size() != 32) {
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000283 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
Lukacs Berki58c29ae2015-10-16 14:48:33 +0000284 "\nFailed to extract install_base_key: file size mismatch "
285 "(should be 32, is %zd)", str.size());
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000286 }
287 *install_base_key_ = str;
288 }
289
290 private:
291 string *install_base_key_;
292};
293
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100294// Returns the install base (the root concatenated with the contents of the file
295// 'install_base_key' contained as a ZIP entry in the Blaze binary); as a side
296// effect, it also populates the extracted_binaries global variable.
297static string GetInstallBase(const string &root, const string &self_path) {
Eric Fellheimer4c5eb0f2015-08-12 15:02:24 +0000298 GetInstallKeyFileProcessor processor(&globals->install_md5);
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000299 std::unique_ptr<devtools_ijar::ZipExtractor> extractor(
300 devtools_ijar::ZipExtractor::Create(self_path.c_str(), &processor));
301 if (extractor.get() == NULL) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100302 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000303 "\nFailed to open %s as a zip file: (%d) %s",
Julio Merino28774852016-09-14 16:59:46 +0000304 globals->options->product_name.c_str(), errno, strerror(errno));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100305 }
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000306 if (extractor->ProcessAll() < 0) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100307 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000308 "\nFailed to extract install_base_key: %s", extractor->GetError());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100309 }
310
Eric Fellheimer4c5eb0f2015-08-12 15:02:24 +0000311 if (globals->install_md5.empty()) {
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000312 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
313 "\nFailed to find install_base_key's in zip file");
314 }
Eric Fellheimer4c5eb0f2015-08-12 15:02:24 +0000315 return root + "/" + globals->install_md5;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100316}
317
318// Escapes colons by replacing them with '_C' and underscores by replacing them
319// with '_U'. E.g. "name:foo_bar" becomes "name_Cfoo_Ubar"
320static string EscapeForOptionSource(const string& input) {
321 string result = input;
322 blaze_util::Replace("_", "_U", &result);
323 blaze_util::Replace(":", "_C", &result);
324 return result;
325}
326
Thiago Farina6a2dc2b2016-10-28 13:05:22 +0000327// Returns the installed embedded binaries directory, under the shared
328// install_base location.
329string GetEmbeddedBinariesRoot(const string &install_base) {
330 return blaze_util::JoinPath(install_base, "_embedded_binaries");
331}
332
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100333// Returns the JVM command argument array.
334static vector<string> GetArgumentArray() {
335 vector<string> result;
336
337 // e.g. A Blaze server process running in ~/src/build_root (where there's a
338 // ~/src/build_root/WORKSPACE file) will appear in ps(1) as "blaze(src)".
339 string workspace =
340 blaze_util::Basename(blaze_util::Dirname(globals->workspace));
Julio Merino28774852016-09-14 16:59:46 +0000341 string product = globals->options->product_name;
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000342 blaze_util::ToLower(&product);
343 result.push_back(product + "(" + workspace + ")");
Julio Merino28774852016-09-14 16:59:46 +0000344 globals->options->AddJVMArgumentPrefix(
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000345 blaze_util::Dirname(blaze_util::Dirname(globals->jvm_path)),
346 &result);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100347
348 result.push_back("-XX:+HeapDumpOnOutOfMemoryError");
Julio Merino28774852016-09-14 16:59:46 +0000349 string heap_crash_path = globals->options->output_base;
Dmitry Lomov7608db52016-07-14 11:27:10 +0000350 result.push_back("-XX:HeapDumpPath=" + ConvertPath(heap_crash_path));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100351
352 result.push_back("-Xverify:none");
353
Janak Ramakrishnande735c02015-06-02 16:38:57 +0000354 vector<string> user_options;
355
Janak Ramakrishnan0acd1542016-01-06 18:42:30 +0000356 user_options.insert(user_options.begin(),
Julio Merino28774852016-09-14 16:59:46 +0000357 globals->options->host_jvm_args.begin(),
358 globals->options->host_jvm_args.end());
Janak Ramakrishnande735c02015-06-02 16:38:57 +0000359
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100360 // Add JVM arguments particular to building blaze64 and particular JVM
361 // versions.
362 string error;
363 blaze_exit_code::ExitCode jvm_args_exit_code =
Julio Merino28774852016-09-14 16:59:46 +0000364 globals->options->AddJVMArguments(globals->options->GetHostJavabase(),
Janak Ramakrishnande735c02015-06-02 16:38:57 +0000365 &result, user_options, &error);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100366 if (jvm_args_exit_code != blaze_exit_code::SUCCESS) {
367 die(jvm_args_exit_code, "%s", error.c_str());
368 }
369
Julio Merino28774852016-09-14 16:59:46 +0000370 if (globals->options->batch && globals->options->oom_more_eagerly) {
Janak Ramakrishnan70c57902016-03-10 00:58:59 +0000371 // Put this OOM trigger with kill after --host_jvm_args, in case
372 // --host_jvm_args contains user-specified OOM triggers since we want those
373 // to execute first.
374 result.push_back("-XX:OnOutOfMemoryError=kill -USR2 %p");
375 }
376
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100377 // We put all directories on the java.library.path that contain .so files.
378 string java_library_path = "-Djava.library.path=";
Thiago Farina6a2dc2b2016-10-28 13:05:22 +0000379 string real_install_dir =
380 GetEmbeddedBinariesRoot(globals->options->install_base);
381
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100382 bool first = true;
383 for (const auto& it : globals->extracted_binaries) {
Thiago Farina01f36002015-04-08 15:59:08 +0000384 if (IsSharedLibrary(it)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100385 if (!first) {
Dmitry Lomov78c0cc72015-08-11 16:44:21 +0000386 java_library_path += blaze::ListSeparator();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100387 }
388 first = false;
Dmitry Lomov78c0cc72015-08-11 16:44:21 +0000389 java_library_path += blaze::ConvertPath(
390 blaze_util::JoinPath(real_install_dir, blaze_util::Dirname(it)));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100391 }
392 }
393 result.push_back(java_library_path);
394
395 // Force use of latin1 for file names.
396 result.push_back("-Dfile.encoding=ISO-8859-1");
397
Julio Merino28774852016-09-14 16:59:46 +0000398 if (globals->options->host_jvm_debug) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100399 fprintf(stderr,
400 "Running host JVM under debugger (listening on TCP port 5005).\n");
401 // Start JVM so that it listens for a connection from a
402 // JDWP-compliant debugger:
403 result.push_back("-Xdebug");
404 result.push_back("-Xrunjdwp:transport=dt_socket,server=y,address=5005");
405 }
Janak Ramakrishnande735c02015-06-02 16:38:57 +0000406 result.insert(result.end(), user_options.begin(), user_options.end());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100407
Julio Merino28774852016-09-14 16:59:46 +0000408 globals->options->AddJVMArgumentSuffix(real_install_dir,
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000409 globals->extracted_binaries[0],
410 &result);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100411
Lukacs Berki3d486832016-10-26 12:51:38 +0000412 // JVM arguments are complete. Now pass in Blaze startup options.
413 // Note that we always use the --flag=ARG form (instead of the --flag ARG one)
414 // so that BlazeRuntime#splitStartupOptions has an easy job.
Lukacs Berki71675a52016-11-08 09:48:27 +0000415
416 // TODO(lberki): Test that whatever the list constructed after this line is
417 // actually a list of parseable startup options.
Julio Merino28774852016-09-14 16:59:46 +0000418 if (!globals->options->batch) {
Lukacs Berki3d486832016-10-26 12:51:38 +0000419 result.push_back("--max_idle_secs=" +
420 ToString(globals->options->max_idle_secs));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100421 } else {
Googlerc8c64e72015-03-23 23:22:18 +0000422 // --batch must come first in the arguments to Java main() because
423 // the code expects it to be at args[0] if it's been set.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100424 result.push_back("--batch");
425 }
Lukacs Berkice1445f2016-04-19 15:52:55 +0000426
Julio Merino28774852016-09-14 16:59:46 +0000427 if (globals->options->command_port != 0) {
Lukacs Berki7e0249e2016-04-21 08:14:08 +0000428 result.push_back(
Julio Merino28774852016-09-14 16:59:46 +0000429 "--command_port=" + ToString(globals->options->command_port));
Lukacs Berkice1445f2016-04-19 15:52:55 +0000430 }
431
Lukacs Berki71675a52016-11-08 09:48:27 +0000432 result.push_back(
433 "--connect_timeout_secs=" +
434 ToString(globals->options->connect_timeout_secs));
435
Dmitry Lomov78c0cc72015-08-11 16:44:21 +0000436 result.push_back("--install_base=" +
Julio Merino28774852016-09-14 16:59:46 +0000437 blaze::ConvertPath(globals->options->install_base));
Eric Fellheimer4c5eb0f2015-08-12 15:02:24 +0000438 result.push_back("--install_md5=" + globals->install_md5);
Dmitry Lomov78c0cc72015-08-11 16:44:21 +0000439 result.push_back("--output_base=" +
Julio Merino28774852016-09-14 16:59:46 +0000440 blaze::ConvertPath(globals->options->output_base));
Dmitry Lomov78c0cc72015-08-11 16:44:21 +0000441 result.push_back("--workspace_directory=" +
442 blaze::ConvertPath(globals->workspace));
Marian Lobur6dcdd602015-04-09 09:28:40 +0000443
Julio Merino28774852016-09-14 16:59:46 +0000444 if (globals->options->allow_configurable_attributes) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100445 result.push_back("--allow_configurable_attributes");
446 }
Julio Merino28774852016-09-14 16:59:46 +0000447 if (globals->options->deep_execroot) {
Lukacs Berki5fb98d12015-12-09 15:29:46 +0000448 result.push_back("--deep_execroot");
449 } else {
450 result.push_back("--nodeep_execroot");
451 }
Julio Merino28774852016-09-14 16:59:46 +0000452 if (globals->options->oom_more_eagerly) {
Janak Ramakrishnanadc706f2016-03-07 19:12:48 +0000453 result.push_back("--experimental_oom_more_eagerly");
454 }
Janak Ramakrishnan19fde1f2016-05-23 21:20:16 +0000455 result.push_back("--experimental_oom_more_eagerly_threshold=" +
Julio Merino28774852016-09-14 16:59:46 +0000456 ToString(globals->options->oom_more_eagerly_threshold));
Janak Ramakrishnan8cc772e2016-03-23 17:26:12 +0000457
Michajlo Matijkiwaf79a322016-09-16 15:44:35 +0000458 if (!globals->options->write_command_log) {
459 result.push_back("--nowrite_command_log");
460 }
461
Julio Merino28774852016-09-14 16:59:46 +0000462 if (globals->options->watchfs) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100463 result.push_back("--watchfs");
464 }
Julio Merino28774852016-09-14 16:59:46 +0000465 if (globals->options->fatal_event_bus_exceptions) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100466 result.push_back("--fatal_event_bus_exceptions");
467 } else {
468 result.push_back("--nofatal_event_bus_exceptions");
469 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100470
Lukacs Berki71675a52016-11-08 09:48:27 +0000471 // We use this syntax so that the logic in ServerNeedsToBeKilled() that
472 // decides whether the server needs killing is simpler. This is parsed by the
473 // Java code where --noclient_debug and --client_debug=false are equivalent.
474 // Note that --client_debug false (separated by space) won't work either,
475 // because the logic in ServerNeedsToBeKilled() assumes that every argument
476 // is in the --arg=value form.
477 if (globals->options->client_debug) {
478 result.push_back("--client_debug=true");
479 } else {
480 result.push_back("--client_debug=false");
481 }
482
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100483 // This is only for Blaze reporting purposes; the real interpretation of the
484 // jvm flags occurs when we set up the java command line.
Julio Merino28774852016-09-14 16:59:46 +0000485 if (globals->options->host_jvm_debug) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100486 result.push_back("--host_jvm_debug");
487 }
Julio Merino28774852016-09-14 16:59:46 +0000488 if (!globals->options->host_jvm_profile.empty()) {
489 result.push_back("--host_jvm_profile=" +
490 globals->options->host_jvm_profile);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100491 }
Julio Merino28774852016-09-14 16:59:46 +0000492 if (!globals->options->host_jvm_args.empty()) {
493 for (const auto &arg : globals->options->host_jvm_args) {
Janak Ramakrishnan533657e2015-11-13 23:34:14 +0000494 result.push_back("--host_jvm_args=" + arg);
495 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100496 }
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000497
Julio Merino28774852016-09-14 16:59:46 +0000498 if (globals->options->invocation_policy != NULL &&
499 strlen(globals->options->invocation_policy) > 0) {
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000500 result.push_back(string("--invocation_policy=") +
Julio Merino28774852016-09-14 16:59:46 +0000501 globals->options->invocation_policy);
Alex Humesky2f3f4cf2015-09-29 01:42:00 +0000502 }
503
Julio Merino28774852016-09-14 16:59:46 +0000504 result.push_back("--product_name=" + globals->options->product_name);
Luis Fernando Pino Duque623cdf82016-05-31 16:21:46 +0000505
Julio Merino28774852016-09-14 16:59:46 +0000506 globals->options->AddExtraOptions(&result);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100507
508 // The option sources are transmitted in the following format:
509 // --option_sources=option1:source1:option2:source2:...
510 string option_sources = "--option_sources=";
511 first = true;
Julio Merino28774852016-09-14 16:59:46 +0000512 for (const auto& it : globals->options->option_sources) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100513 if (!first) {
514 option_sources += ":";
515 }
516
517 first = false;
518 option_sources += EscapeForOptionSource(it.first) + ":" +
519 EscapeForOptionSource(it.second);
520 }
521
522 result.push_back(option_sources);
523 return result;
524}
525
Thiago Farina5735c252016-04-27 16:16:27 +0000526// Add common command options for logging to the given argument array.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100527static void AddLoggingArgs(vector<string>* args) {
Googler9588b812015-07-23 11:49:37 +0000528 args->push_back("--startup_time=" + ToString(globals->startup_time));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100529 if (globals->command_wait_time != 0) {
530 args->push_back("--command_wait_time=" +
Googler9588b812015-07-23 11:49:37 +0000531 ToString(globals->command_wait_time));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100532 }
533 if (globals->extract_data_time != 0) {
534 args->push_back("--extract_data_time=" +
Googler9588b812015-07-23 11:49:37 +0000535 ToString(globals->extract_data_time));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100536 }
537 if (globals->restart_reason != NO_RESTART) {
538 const char *reasons[] = {
539 "no_restart", "no_daemon", "new_version", "new_options"
540 };
541 args->push_back(
542 string("--restart_reason=") + reasons[globals->restart_reason]);
543 }
544 args->push_back(
545 string("--binary_path=") + globals->binary_path);
546}
547
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100548// Join the elements of the specified array with NUL's (\0's), akin to the
549// format of /proc/$PID/cmdline.
Thiago Farina0b6963e2015-04-28 20:26:45 +0000550static string GetArgumentString(const vector<string>& argument_array) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100551 string result;
552 blaze_util::JoinStrings(argument_array, '\0', &result);
553 return result;
554}
555
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100556// Do a chdir into the workspace, and die if it fails.
557static void GoToWorkspace() {
Julio Merino211a95c2016-08-29 11:01:35 +0000558 if (WorkspaceLayout::InWorkspace(globals->workspace) &&
Laszlo Csomor9c951962016-11-10 13:31:27 +0000559 !blaze_util::ChangeDirectory(globals->workspace)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100560 pdie(blaze_exit_code::INTERNAL_ERROR,
Laszlo Csomor9c951962016-11-10 13:31:27 +0000561 "changing directory into %s failed", globals->workspace.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100562 }
563}
564
565// Check the java version if a java version specification is bundled. On
Thiago Farina5735c252016-04-27 16:16:27 +0000566// success, returns the executable path of the java command.
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000567static void VerifyJavaVersionAndSetJvm() {
Julio Merino28774852016-09-14 16:59:46 +0000568 string exe = globals->options->GetJvm();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100569
570 string version_spec_file = blaze_util::JoinPath(
Thiago Farina6a2dc2b2016-10-28 13:05:22 +0000571 GetEmbeddedBinariesRoot(globals->options->install_base), "java.version");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100572 string version_spec = "";
573 if (ReadFile(version_spec_file, &version_spec)) {
574 blaze_util::StripWhitespace(&version_spec);
575 // A version specification is given, get version of java.
576 string jvm_version = GetJvmVersion(exe);
577
578 // Compare that jvm_version is found and at least the one specified.
579 if (jvm_version.size() == 0) {
580 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
581 "Java version not detected while at least %s is needed.\n"
582 "Please set JAVA_HOME.", version_spec.c_str());
583 } else if (!CheckJavaVersionIsAtLeast(jvm_version, version_spec)) {
584 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
585 "Java version is %s while at least %s is needed.\n"
586 "Please set JAVA_HOME.",
587 jvm_version.c_str(), version_spec.c_str());
588 }
589 }
590
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000591 globals->jvm_path = exe;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100592}
593
594// Starts the Blaze server. Returns a readable fd connected to the server.
595// This is currently used only to detect liveness.
Lukacs Berki1977d922016-05-02 09:31:37 +0000596static void StartServer(BlazeServerStartup** server_startup) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100597 vector<string> jvm_args_vector = GetArgumentArray();
598 string argument_string = GetArgumentString(jvm_args_vector);
Julio Merino28774852016-09-14 16:59:46 +0000599 string server_dir = globals->options->output_base + "/server";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100600 // Write the cmdline argument string to the server dir. If we get to this
601 // point, there is no server running, so we don't overwrite the cmdline file
602 // for the existing server. If might be that the server dies and the cmdline
603 // file stays there, but that is not a problem, since we always check the
604 // server, too.
Lukacs Berki5a781662016-04-25 11:17:31 +0000605 WriteFile(argument_string, server_dir + "/cmdline");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100606
607 // unless we restarted for a new-version, mark this as initial start
608 if (globals->restart_reason == NO_RESTART) {
609 globals->restart_reason = NO_DAEMON;
610 }
611
Julio Merino28774852016-09-14 16:59:46 +0000612 string exe = globals->options->GetExe(globals->jvm_path,
613 globals->extracted_binaries[0]);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100614 // Go to the workspace before we daemonize, so
615 // we can still print errors to the terminal.
616 GoToWorkspace();
617
Lukacs Berki1977d922016-05-02 09:31:37 +0000618 ExecuteDaemon(exe, jvm_args_vector, globals->jvm_log_file.c_str(),
619 server_dir, server_startup);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100620}
621
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100622// Replace this process with blaze in standalone/batch mode.
623// The batch mode blaze process handles the command and exits.
624//
625// This function passes the commands array to the blaze process.
626// This array should start with a command ("build", "info", etc.).
Lukacs Berki907dbbf2016-04-15 11:30:12 +0000627static void StartStandalone(BlazeServer* server) {
Lukacs Berki1977d922016-05-02 09:31:37 +0000628 if (server->Connected()) {
629 server->KillRunningServer();
630 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100631
632 // Wall clock time since process startup.
Laszlo Csomor943d3cf2016-11-07 14:27:21 +0000633 globals->startup_time = GetMillisecondsSinceProcessStart();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100634
635 if (VerboseLogging()) {
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000636 fprintf(stderr, "Starting %s in batch mode.\n",
Julio Merino28774852016-09-14 16:59:46 +0000637 globals->options->product_name.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100638 }
Julio Merino28774852016-09-14 16:59:46 +0000639 string command = globals->option_processor->GetCommand();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100640 vector<string> command_arguments;
Julio Merino28774852016-09-14 16:59:46 +0000641 globals->option_processor->GetCommandArguments(&command_arguments);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100642
643 if (!command_arguments.empty() && command == "shutdown") {
Julio Merino28774852016-09-14 16:59:46 +0000644 string product = globals->options->product_name;
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000645 blaze_util::ToLower(&product);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100646 fprintf(stderr,
647 "WARNING: Running command \"shutdown\" in batch mode. Batch mode "
Kristina Chodorow11d40d22015-03-17 18:26:59 +0000648 "is triggered\nwhen not running %s within a workspace. If you "
649 "intend to shutdown an\nexisting %s server, run \"%s "
650 "shutdown\" from the directory where\nit was started.\n",
Julio Merino28774852016-09-14 16:59:46 +0000651 globals->options->product_name.c_str(),
652 globals->options->product_name.c_str(), product.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100653 }
654 vector<string> jvm_args_vector = GetArgumentArray();
655 if (command != "") {
656 jvm_args_vector.push_back(command);
657 AddLoggingArgs(&jvm_args_vector);
658 }
659
660 jvm_args_vector.insert(jvm_args_vector.end(),
661 command_arguments.begin(),
662 command_arguments.end());
663
664 GoToWorkspace();
665
Julio Merino28774852016-09-14 16:59:46 +0000666 string exe = globals->options->GetExe(globals->jvm_path,
Eric Fellheimer3a695f32016-05-11 17:26:30 +0000667 globals->extracted_binaries[0]);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100668 ExecuteProgram(exe, jvm_args_vector);
669 pdie(blaze_exit_code::INTERNAL_ERROR, "execv of '%s' failed", exe.c_str());
670}
671
Laszlo Csomorae16e762016-11-18 10:16:08 +0000672static void WriteFileToStderrOrDie(const char *file_name) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100673 FILE *fp = fopen(file_name, "r");
674 if (fp == NULL) {
675 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
676 "opening %s failed", file_name);
677 }
678 char buffer[255];
679 int num_read;
680 while ((num_read = fread(buffer, 1, sizeof buffer, fp)) > 0) {
681 if (ferror(fp)) {
682 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
683 "failed to read from '%s'", file_name);
684 }
Laszlo Csomorae16e762016-11-18 10:16:08 +0000685 fwrite(buffer, 1, num_read, stderr);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100686 }
687 fclose(fp);
688}
689
Lukacs Berki4912f7f2016-06-17 16:12:22 +0000690// After connecting to the Blaze server, return its PID, or -1 if there was an
691// error.
Lukacs Berkid9da60f2016-04-26 11:40:24 +0000692static int GetServerPid(const string &server_dir) {
Lukacs Berki907dbbf2016-04-15 11:30:12 +0000693 // Note: there is no race here on startup since the server creates
694 // the pid file strictly before it binds the socket.
Lukacs Berkiea4c42e2016-04-25 07:22:11 +0000695 char buf[33];
696
697 // The server writes a file, but we need to handle old servers that still
698 // write a symlink.
Lukacs Berkiea4c42e2016-04-25 07:22:11 +0000699 int len;
Thiago Farina048bbfc2016-09-21 08:20:41 +0000700 string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
701 string pid_symlink = blaze_util::JoinPath(server_dir, kServerPidSymlink);
Lukacs Berkid9da60f2016-04-26 11:40:24 +0000702 len = readlink(pid_symlink.c_str(), buf, sizeof(buf) - 1);
Laszlo Csomorae16e762016-11-18 10:16:08 +0000703 string bufstr;
704 if (len > 0) {
705 bufstr = string(buf, len);
706 } else if (!blaze::ReadFile(pid_file, &bufstr, 32)) {
707 return -1;
Doug Rabsond655f2a2015-08-13 14:41:50 +0000708 }
Lukacs Berkiea4c42e2016-04-25 07:22:11 +0000709
710 int result;
Laszlo Csomorae16e762016-11-18 10:16:08 +0000711 if (!blaze_util::safe_strto32(bufstr, &result)) {
Lukacs Berkiea4c42e2016-04-25 07:22:11 +0000712 return -1;
713 }
714
715 return result;
Doug Rabsond655f2a2015-08-13 14:41:50 +0000716}
717
Lukacs Berki1977d922016-05-02 09:31:37 +0000718// Starts up a new server and connects to it. Exits if it didn't work not.
719static void StartServerAndConnect(BlazeServer *server) {
Julio Merino28774852016-09-14 16:59:46 +0000720 string server_dir = globals->options->output_base + "/server";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100721
722 // The server dir has the socket, so we don't allow access by other
723 // users.
724 if (MakeDirectories(server_dir, 0700) == -1) {
725 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
726 "server directory '%s' could not be created", server_dir.c_str());
727 }
728
Lukacs Berki1977d922016-05-02 09:31:37 +0000729 // If we couldn't connect to the server check if there is still a PID file
730 // and if so, kill the server that wrote it. This can happen e.g. if the
731 // server is in a GC pause and therefore cannot respond to ping requests and
732 // having two server instances running in the same output base is a
733 // disaster.
734 int server_pid = GetServerPid(server_dir);
735 if (server_pid > 0) {
Julio Merino28774852016-09-14 16:59:46 +0000736 if (VerifyServerProcess(server_pid, globals->options->output_base,
737 globals->options->install_base) &&
Lukacs Berkiee44c382016-09-14 10:53:37 +0000738 KillServerProcess(server_pid)) {
Lukacs Berki119dd4b2016-07-13 15:28:42 +0000739 fprintf(stderr, "Killed non-responsive server process (pid=%d)\n",
740 server_pid);
741 }
Lukacs Berki7e0249e2016-04-21 08:14:08 +0000742 }
743
Julio Merino28774852016-09-14 16:59:46 +0000744 SetScheduling(globals->options->batch_cpu_scheduling,
745 globals->options->io_nice_level);
Lukacs Berkif1df38a2016-04-19 07:42:22 +0000746
Lukacs Berki1977d922016-05-02 09:31:37 +0000747 BlazeServerStartup* server_startup;
748 StartServer(&server_startup);
Lukacs Berki5570bcc2016-11-15 15:45:58 +0000749
750 // Give the server two minutes to start up. That's enough to connect with a
751 // debugger.
752 auto try_until_time(
753 std::chrono::system_clock::now() + std::chrono::seconds(120));
754 bool had_to_wait = false;
755 while (std::chrono::system_clock::now() < try_until_time) {
756 auto next_attempt_time(
757 std::chrono::system_clock::now() + std::chrono::milliseconds(100));
Lukacs Berki1977d922016-05-02 09:31:37 +0000758 if (server->Connect()) {
Lukacs Berki5570bcc2016-11-15 15:45:58 +0000759 if (had_to_wait && !globals->options->client_debug) {
Lukacs Berki1977d922016-05-02 09:31:37 +0000760 fputc('\n', stderr);
761 fflush(stderr);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100762 }
Lukacs Berki1977d922016-05-02 09:31:37 +0000763 delete server_startup;
764 return;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100765 }
Lukacs Berki5570bcc2016-11-15 15:45:58 +0000766
767 had_to_wait = true;
Lukacs Berki71675a52016-11-08 09:48:27 +0000768 if (!globals->options->client_debug) {
769 fputc('.', stderr);
770 fflush(stderr);
771 }
772
Lukacs Berki5570bcc2016-11-15 15:45:58 +0000773 std::this_thread::sleep_until(next_attempt_time);
Lukacs Berki1977d922016-05-02 09:31:37 +0000774 if (!server_startup->IsStillAlive()) {
775 fprintf(stderr, "\nunexpected pipe read status: %s\n"
776 "Server presumed dead. Now printing '%s':\n",
777 strerror(errno), globals->jvm_log_file.c_str());
Laszlo Csomorae16e762016-11-18 10:16:08 +0000778 WriteFileToStderrOrDie(globals->jvm_log_file.c_str());
Lukacs Berki1977d922016-05-02 09:31:37 +0000779 exit(blaze_exit_code::INTERNAL_ERROR);
780 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100781 }
Lukacs Berki1977d922016-05-02 09:31:37 +0000782 die(blaze_exit_code::INTERNAL_ERROR,
Lukacs Berki5570bcc2016-11-15 15:45:58 +0000783 "\nError: couldn't connect to server after 120 seconds.");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100784}
785
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000786// A devtools_ijar::ZipExtractorProcessor to extract the files from the blaze
787// zip.
788class ExtractBlazeZipProcessor : public devtools_ijar::ZipExtractorProcessor {
789 public:
Thiago Farina9cb32752015-06-03 15:34:19 +0000790 explicit ExtractBlazeZipProcessor(const string &embedded_binaries)
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000791 : embedded_binaries_(embedded_binaries) {}
792
793 virtual bool Accept(const char *filename, const devtools_ijar::u4 attr) {
794 return !devtools_ijar::zipattr_is_dir(attr);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100795 }
796
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000797 virtual void Process(const char *filename, const devtools_ijar::u4 attr,
798 const devtools_ijar::u1 *data, const size_t size) {
799 string path = blaze_util::JoinPath(embedded_binaries_, filename);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100800 if (MakeDirectories(blaze_util::Dirname(path), 0777) == -1) {
801 pdie(blaze_exit_code::INTERNAL_ERROR,
802 "couldn't create '%s'", path.c_str());
803 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100804
Laszlo Csomorae16e762016-11-18 10:16:08 +0000805 if (!blaze::WriteFile(data, size, path)) {
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000806 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
Laszlo Csomorae16e762016-11-18 10:16:08 +0000807 "\nFailed to write zipped file \"%s\": %s", path.c_str(),
808 strerror(errno));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100809 }
810 }
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000811
812 private:
813 const string embedded_binaries_;
814};
815
816// Actually extracts the embedded data files into the tree whose root
817// is 'embedded_binaries'.
818static void ActuallyExtractData(const string &argv0,
819 const string &embedded_binaries) {
820 ExtractBlazeZipProcessor processor(embedded_binaries);
821 if (MakeDirectories(embedded_binaries, 0777) == -1) {
822 pdie(blaze_exit_code::INTERNAL_ERROR, "couldn't create '%s'",
823 embedded_binaries.c_str());
824 }
825
826 fprintf(stderr, "Extracting %s installation...\n",
Julio Merino28774852016-09-14 16:59:46 +0000827 globals->options->product_name.c_str());
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000828 std::unique_ptr<devtools_ijar::ZipExtractor> extractor(
829 devtools_ijar::ZipExtractor::Create(argv0.c_str(), &processor));
830 if (extractor.get() == NULL) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100831 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000832 "\nFailed to open %s as a zip file: (%d) %s",
Julio Merino28774852016-09-14 16:59:46 +0000833 globals->options->product_name.c_str(), errno, strerror(errno));
Damien Martin-Guillerezeb6e9032015-06-01 14:45:21 +0000834 }
835 if (extractor->ProcessAll() < 0) {
836 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
837 "\nFailed to extract %s as a zip file: %s",
Julio Merino28774852016-09-14 16:59:46 +0000838 globals->options->product_name.c_str(), extractor->GetError());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100839 }
840
841 const time_t TEN_YEARS_IN_SEC = 3600 * 24 * 365 * 10;
842 time_t future_time = time(NULL) + TEN_YEARS_IN_SEC;
843
844 // Set the timestamps of the extracted files to the future and make sure (or
845 // at least as sure as we can...) that the files we have written are actually
846 // on the disk.
847
848 vector<string> extracted_files;
Laszlo Csomor251bf032016-11-16 11:01:32 +0000849
850 // Walks the temporary directory recursively and collects full file paths.
851 blaze_util::GetAllFilesUnder(embedded_binaries, &extracted_files);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100852
853 set<string> synced_directories;
Laszlo Csomor251bf032016-11-16 11:01:32 +0000854 for (const auto &it : extracted_files) {
855 const char *extracted_path = it.c_str();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100856
857 // Set the time to a distantly futuristic value so we can observe tampering.
Laszlo Csomor251bf032016-11-16 11:01:32 +0000858 // Note that keeping the default timestamp set by unzip (1970-01-01) and
859 // using that to detect tampering is not enough, because we also need the
860 // timestamp to change between Blaze releases so that the metadata cache
861 // knows that the files may have changed. This is important for actions that
862 // use embedded binaries as artifacts.
Laszlo Csomor6c167652016-11-17 11:00:49 +0000863 if (!blaze_util::SetMtimeMillisec(it, future_time)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100864 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
865 "failed to set timestamp on '%s'", extracted_path);
866 }
867
Laszlo Csomorae16e762016-11-18 10:16:08 +0000868 blaze_util::SyncFile(it);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100869
870 string directory = blaze_util::Dirname(extracted_path);
871
872 // Now walk up until embedded_binaries and sync every directory in between.
873 // synced_directories is used to avoid syncing the same directory twice.
874 // The !directory.empty() and directory != "/" conditions are not strictly
875 // needed, but it makes this loop more robust, because otherwise, if due to
876 // some glitch, directory was not under embedded_binaries, it would get
877 // into an infinite loop.
878 while (directory != embedded_binaries &&
879 synced_directories.count(directory) == 0 &&
880 !directory.empty() &&
881 directory != "/") {
Laszlo Csomorae16e762016-11-18 10:16:08 +0000882 blaze_util::SyncFile(directory);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100883 synced_directories.insert(directory);
884 directory = blaze_util::Dirname(directory);
885 }
886 }
887
Laszlo Csomorae16e762016-11-18 10:16:08 +0000888 blaze_util::SyncFile(embedded_binaries);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100889}
890
891// Installs Blaze by extracting the embedded data files, iff necessary.
892// The MD5-named install_base directory on disk is trusted; we assume
893// no-one has modified the extracted files beneath this directory once
894// it is in place. Concurrency during extraction is handled by
895// extracting in a tmp dir and then renaming it into place where it
896// becomes visible automically at the new path.
897// Populates globals->extracted_binaries with their extracted locations.
898static void ExtractData(const string &self_path) {
899 // If the install dir doesn't exist, create it, if it does, we know it's good.
Laszlo Csomor8a48f612016-11-17 10:18:34 +0000900 if (!blaze_util::PathExists(globals->options->install_base)) {
Laszlo Csomor943d3cf2016-11-07 14:27:21 +0000901 uint64_t st = GetMillisecondsMonotonic();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100902 // Work in a temp dir to avoid races.
Julio Merino28774852016-09-14 16:59:46 +0000903 string tmp_install = globals->options->install_base + ".tmp." +
Laszlo Csomorae16e762016-11-18 10:16:08 +0000904 blaze::GetProcessIdAsString();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100905 string tmp_binaries = tmp_install + "/_embedded_binaries";
906 ActuallyExtractData(self_path, tmp_binaries);
907
Laszlo Csomor943d3cf2016-11-07 14:27:21 +0000908 uint64_t et = GetMillisecondsMonotonic();
909 globals->extract_data_time = et - st;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100910
911 // Now rename the completed installation to its final name. If this
912 // fails due to an ENOTEMPTY then we assume another good
913 // installation snuck in before us.
Julio Merino28774852016-09-14 16:59:46 +0000914 if (rename(tmp_install.c_str(), globals->options->install_base.c_str()) == -1
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100915 && errno != ENOTEMPTY) {
916 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
917 "install base directory '%s' could not be renamed into place",
918 tmp_install.c_str());
919 }
920 } else {
Laszlo Csomor8a48f612016-11-17 10:18:34 +0000921 if (!blaze_util::IsDirectory(globals->options->install_base)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100922 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
923 "Error: Install base directory '%s' could not be created. "
924 "It exists but is not a directory.",
Julio Merino28774852016-09-14 16:59:46 +0000925 globals->options->install_base.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100926 }
927
928 const time_t time_now = time(NULL);
929 string real_install_dir = blaze_util::JoinPath(
Julio Merino28774852016-09-14 16:59:46 +0000930 globals->options->install_base,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100931 "_embedded_binaries");
932 for (const auto& it : globals->extracted_binaries) {
933 string path = blaze_util::JoinPath(real_install_dir, it);
934 // Check that the file exists and is readable.
Laszlo Csomor8a48f612016-11-17 10:18:34 +0000935 if (!blaze_util::CanAccess(path, true, false, false)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100936 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
937 "Error: corrupt installation: file '%s' missing."
938 " Please remove '%s' and try again.",
Julio Merino28774852016-09-14 16:59:46 +0000939 path.c_str(), globals->options->install_base.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100940 }
Laszlo Csomor8a48f612016-11-17 10:18:34 +0000941 // Check that the timestamp is in the future. A past timestamp would
942 // indicate that the file has been tampered with.
943 // See ActuallyExtractData().
944 if (!blaze_util::IsDirectory(path)) {
945 time_t mtime = blaze_util::GetMtimeMillisec(path);
946 if (mtime == -1) {
947 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
948 "Error: could not retrieve mtime of file '%s'. "
949 "Please remove '%s' and try again.",
950 path.c_str(), globals->options->install_base.c_str());
951 } else if (mtime <= time_now) {
952 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
953 "Error: corrupt installation: file '%s' "
954 "modified. Please remove '%s' and try again.",
955 path.c_str(), globals->options->install_base.c_str());
956 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100957 }
958 }
959 }
960}
961
Lukacs Berki71675a52016-11-08 09:48:27 +0000962const char *volatile_startup_options[] = {
963 "--option_sources=",
964 "--max_idle_secs=",
965 "--connect_timeout_secs=",
966 "--client_debug=",
967 NULL,
968};
969
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100970// Returns true if the server needs to be restarted to accommodate changes
971// between the two argument lists.
972static bool ServerNeedsToBeKilled(const vector<string>& args1,
973 const vector<string>& args2) {
974 // We need not worry about one side missing an argument and the other side
975 // having the default value, since this command line is already the
976 // canonicalized one that always contains every switch (with default values
977 // if it was not present on the real command line). Same applies for argument
978 // ordering.
979 if (args1.size() != args2.size()) {
980 return true;
981 }
982
983 for (int i = 0; i < args1.size(); i++) {
Lukacs Berki71675a52016-11-08 09:48:27 +0000984 bool option_volatile = false;
985 for (const char** candidate = volatile_startup_options;
986 *candidate != NULL;
987 candidate++) {
988 string candidate_string(*candidate);
989 if (args1[i].substr(0, candidate_string.size()) == candidate_string &&
990 args2[i].substr(0, candidate_string.size()) == candidate_string) {
991 option_volatile = true;
992 break;
993 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100994 }
995
Lukacs Berki71675a52016-11-08 09:48:27 +0000996 if (!option_volatile && args1[i] != args2[i]) {
Lukacs Berki3d486832016-10-26 12:51:38 +0000997 return true;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100998 }
999 }
1000
1001 return false;
1002}
1003
1004// Kills the running Blaze server, if any, if the startup options do not match.
Lukacs Berki907dbbf2016-04-15 11:30:12 +00001005static void KillRunningServerIfDifferentStartupOptions(BlazeServer* server) {
Lukacs Berki1977d922016-05-02 09:31:37 +00001006 if (!server->Connected()) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001007 return;
1008 }
1009
Julio Merino28774852016-09-14 16:59:46 +00001010 string cmdline_path = globals->options->output_base + "/server/cmdline";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001011 string joined_arguments;
1012
1013 // No, /proc/$PID/cmdline does not work, because it is limited to 4K. Even
1014 // worse, its behavior differs slightly between kernels (in some, when longer
1015 // command lines are truncated, the last 4 bytes are replaced with
1016 // "..." + NUL.
1017 ReadFile(cmdline_path, &joined_arguments);
1018 vector<string> arguments = blaze_util::Split(joined_arguments, '\0');
1019
1020 // These strings contain null-separated command line arguments. If they are
1021 // the same, the server can stay alive, otherwise, it needs shuffle off this
1022 // mortal coil.
1023 if (ServerNeedsToBeKilled(arguments, GetArgumentArray())) {
1024 globals->restart_reason = NEW_OPTIONS;
1025 fprintf(stderr,
Kristina Chodorow11d40d22015-03-17 18:26:59 +00001026 "WARNING: Running %s server needs to be killed, because the "
1027 "startup options are different.\n",
Julio Merino28774852016-09-14 16:59:46 +00001028 globals->options->product_name.c_str());
Lukacs Berki1977d922016-05-02 09:31:37 +00001029 server->KillRunningServer();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001030 }
1031}
1032
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001033// Kills the old running server if it is not the same version as us,
1034// dealing with various combinations of installation scheme
1035// (installation symlink and older MD5_MANIFEST contents).
1036// This function requires that the installation be complete, and the
1037// server lock acquired.
Lukacs Berki907dbbf2016-04-15 11:30:12 +00001038static void EnsureCorrectRunningVersion(BlazeServer* server) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001039 // Read the previous installation's semaphore symlink in output_base. If the
1040 // target dirs don't match, or if the symlink was not present, then kill any
1041 // running servers. Lastly, symlink to our installation so others know which
1042 // installation is running.
Julio Merino28774852016-09-14 16:59:46 +00001043 string installation_path = globals->options->output_base + "/install";
Lukacs Berki497d8242016-04-28 07:21:26 +00001044 string prev_installation;
1045 bool ok = ReadDirectorySymlink(installation_path.c_str(), &prev_installation);
1046 if (!ok || !CompareAbsolutePaths(
Julio Merino28774852016-09-14 16:59:46 +00001047 prev_installation, globals->options->install_base)) {
Lukacs Berki1977d922016-05-02 09:31:37 +00001048 if (server->Connected()) {
1049 server->KillRunningServer();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001050 }
Lukacs Berki1977d922016-05-02 09:31:37 +00001051
1052 globals->restart_reason = NEW_VERSION;
Thiago Farina4e4ffd22016-03-09 17:02:28 +00001053 UnlinkPath(installation_path.c_str());
Julio Merino28774852016-09-14 16:59:46 +00001054 if (!SymlinkDirectories(globals->options->install_base.c_str(),
Dmitry Lomov47afaab2016-02-19 08:21:13 +00001055 installation_path.c_str())) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001056 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1057 "failed to create installation symlink '%s'",
1058 installation_path.c_str());
1059 }
1060 const time_t time_now = time(NULL);
Laszlo Csomor6c167652016-11-17 11:00:49 +00001061 if (!blaze_util::SetMtimeMillisec(globals->options->install_base,
1062 time_now)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001063 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1064 "failed to set timestamp on '%s'",
Julio Merino28774852016-09-14 16:59:46 +00001065 globals->options->install_base.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001066 }
1067 }
1068}
1069
Lukacs Berkiee44c382016-09-14 10:53:37 +00001070// A signal-safe version of fprintf(stderr, ...).
1071//
1072// WARNING: any output from the blaze client may be interleaved
1073// with output from the blaze server. In --curses mode,
1074// the Blaze server often erases the previous line of output.
1075// So, be sure to end each such message with TWO newlines,
1076// otherwise it may be erased by the next message from the
1077// Blaze server.
1078// Also, it's a good idea to start each message with a newline,
1079// in case the Blaze server has written a partial line.
1080static void sigprintf(const char *format, ...) {
1081 char buf[1024];
1082 va_list ap;
1083 va_start(ap, format);
1084 int r = vsnprintf(buf, sizeof buf, format, ap);
1085 va_end(ap);
1086 if (write(STDERR_FILENO, buf, r) <= 0) {
1087 // We don't care, just placate the compiler.
1088 }
1089}
1090
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001091// Signal handler.
1092static void handler(int signum) {
Lukacs Berkiee44c382016-09-14 10:53:37 +00001093 int saved_errno = errno;
1094
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001095 switch (signum) {
1096 case SIGINT:
1097 if (++globals->sigint_count >= 3) {
Kristina Chodorow11d40d22015-03-17 18:26:59 +00001098 sigprintf("\n%s caught third interrupt signal; killed.\n\n",
Julio Merino28774852016-09-14 16:59:46 +00001099 globals->options->product_name.c_str());
Lukacs Berki793cd012016-06-20 09:48:47 +00001100 if (globals->server_pid != -1) {
Lukacs Berkiee44c382016-09-14 10:53:37 +00001101 KillServerProcess(globals->server_pid);
Lukacs Berki793cd012016-06-20 09:48:47 +00001102 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001103 _exit(1);
1104 }
Kristina Chodorow11d40d22015-03-17 18:26:59 +00001105 sigprintf("\n%s caught interrupt signal; shutting down.\n\n",
Julio Merino28774852016-09-14 16:59:46 +00001106 globals->options->product_name.c_str());
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001107 blaze_server->Cancel();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001108 break;
1109 case SIGTERM:
Kristina Chodorow11d40d22015-03-17 18:26:59 +00001110 sigprintf("\n%s caught terminate signal; shutting down.\n\n",
Julio Merino28774852016-09-14 16:59:46 +00001111 globals->options->product_name.c_str());
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001112 blaze_server->Cancel();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001113 break;
1114 case SIGPIPE:
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001115 globals->received_signal = SIGPIPE;
1116 break;
1117 case SIGQUIT:
1118 sigprintf("\nSending SIGQUIT to JVM process %d (see %s).\n\n",
1119 globals->server_pid,
1120 globals->jvm_log_file.c_str());
1121 kill(globals->server_pid, SIGQUIT);
1122 break;
1123 }
Lukacs Berkiee44c382016-09-14 10:53:37 +00001124
1125 errno = saved_errno;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001126}
1127
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001128// Performs all I/O for a single client request to the server, and
1129// shuts down the client (by exit or signal).
Lukacs Berki907dbbf2016-04-15 11:30:12 +00001130static ATTRIBUTE_NORETURN void SendServerRequest(BlazeServer* server) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001131 while (true) {
Lukacs Berki1977d922016-05-02 09:31:37 +00001132 if (!server->Connected()) {
1133 StartServerAndConnect(server);
1134 }
1135
Lukacs Berki4de98942016-09-09 09:23:36 +00001136 // Check for the case when the workspace directory deleted and then gets
1137 // recreated while the server is running
1138
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001139 string server_cwd = GetProcessCWD(globals->server_pid);
Lukacs Berki4be230a2015-10-15 13:43:03 +00001140 // If server_cwd is empty, GetProcessCWD failed. This notably occurs when
1141 // running under Docker because then readlink(/proc/[pid]/cwd) returns
1142 // EPERM.
1143 // Docker issue #6687 (https://github.com/docker/docker/issues/6687) fixed
1144 // this, but one still needs the --cap-add SYS_PTRACE command line flag, at
1145 // least according to the discussion on Docker issue #6800
1146 // (https://github.com/docker/docker/issues/6687), and even then, it's a
1147 // non-default Docker flag. Given that this occurs only in very weird
1148 // cases, it's better to assume that everything is alright if we can't get
1149 // the cwd.
1150
1151 if (!server_cwd.empty() &&
1152 (server_cwd != globals->workspace || // changed
1153 server_cwd.find(" (deleted)") != string::npos)) { // deleted.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001154 // There's a distant possibility that the two paths look the same yet are
1155 // actually different because the two processes have different mount
1156 // tables.
1157 if (VerboseLogging()) {
1158 fprintf(stderr, "Server's cwd moved or deleted (%s).\n",
1159 server_cwd.c_str());
1160 }
Lukacs Berki1977d922016-05-02 09:31:37 +00001161 server->KillRunningServer();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001162 } else {
1163 break;
1164 }
1165 }
1166
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001167 if (VerboseLogging()) {
1168 fprintf(stderr, "Connected (server pid=%d).\n", globals->server_pid);
1169 }
1170
1171 // Wall clock time since process startup.
Laszlo Csomor943d3cf2016-11-07 14:27:21 +00001172 globals->startup_time = GetMillisecondsSinceProcessStart();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001173
1174 // Unblock all signals.
1175 sigset_t sigset;
1176 sigemptyset(&sigset);
1177 sigprocmask(SIG_SETMASK, &sigset, NULL);
1178
1179 signal(SIGINT, handler);
1180 signal(SIGTERM, handler);
1181 signal(SIGPIPE, handler);
1182 signal(SIGQUIT, handler);
1183
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001184 int exit_code = server->Communicate();
1185 if (globals->received_signal) {
1186 // Kill ourselves with the same signal, so that callers see the
1187 // right WTERMSIG value.
1188 signal(globals->received_signal, SIG_DFL);
1189 raise(globals->received_signal);
1190 exit(1); // (in case raise didn't kill us for some reason)
1191 } else {
1192 exit(exit_code);
1193 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001194}
1195
1196// Parse the options, storing parsed values in globals.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001197static void ParseOptions(int argc, const char *argv[]) {
1198 string error;
1199 blaze_exit_code::ExitCode parse_exit_code =
Julio Merino28774852016-09-14 16:59:46 +00001200 globals->option_processor->ParseOptions(argc, argv, globals->workspace,
1201 globals->cwd, &error);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001202 if (parse_exit_code != blaze_exit_code::SUCCESS) {
1203 die(parse_exit_code, "%s", error.c_str());
1204 }
Julio Merino28774852016-09-14 16:59:46 +00001205 globals->options = globals->option_processor->GetParsedStartupOptions();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001206}
1207
1208// Returns the canonical form of a path.
1209static string MakeCanonical(const char *path) {
1210 char *resolved_path = realpath(path, NULL);
1211 if (resolved_path == NULL) {
1212 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1213 "realpath('%s') failed", path);
1214 }
1215
1216 string ret = resolved_path;
1217 free(resolved_path);
1218 return ret;
1219}
1220
1221// Compute the globals globals->cwd and globals->workspace.
1222static void ComputeWorkspace() {
Laszlo Csomor9c951962016-11-10 13:31:27 +00001223 globals->cwd = MakeCanonical(blaze_util::GetCwd().c_str());
Julio Merino211a95c2016-08-29 11:01:35 +00001224 globals->workspace = WorkspaceLayout::GetWorkspace(globals->cwd);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001225}
1226
1227// Figure out the base directories based on embedded data, username, cwd, etc.
Julio Merino28774852016-09-14 16:59:46 +00001228// Sets globals->options->install_base, globals->options->output_base,
Thiago Farina6fd9bf12016-04-26 09:02:18 +00001229// globals->lockfile, globals->jvm_log_file.
Thiago Farina2fd78902015-05-18 11:37:59 +00001230static void ComputeBaseDirectories(const string &self_path) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001231 // Only start a server when in a workspace because otherwise we won't do more
1232 // than emit a help message.
Julio Merino211a95c2016-08-29 11:01:35 +00001233 if (!WorkspaceLayout::InWorkspace(globals->workspace)) {
Julio Merino28774852016-09-14 16:59:46 +00001234 globals->options->batch = true;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001235 }
1236
1237 // The default install_base is <output_user_root>/install/<md5(blaze)>
1238 // but if an install_base is specified on the command line, we use that as
1239 // the base instead.
Julio Merino28774852016-09-14 16:59:46 +00001240 if (globals->options->install_base.empty()) {
1241 string install_user_root = globals->options->output_user_root + "/install";
1242 globals->options->install_base =
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001243 GetInstallBase(install_user_root, self_path);
1244 } else {
Eric Fellheimer4c5eb0f2015-08-12 15:02:24 +00001245 // We call GetInstallBase anyway to populate extracted_binaries and
1246 // install_md5.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001247 GetInstallBase("", self_path);
1248 }
1249
Julio Merino28774852016-09-14 16:59:46 +00001250 if (globals->options->output_base.empty()) {
Laszlo Csomor6bf95762016-11-16 13:29:22 +00001251 globals->options->output_base = blaze::GetHashedBaseDir(
Julio Merino28774852016-09-14 16:59:46 +00001252 globals->options->output_user_root, globals->workspace);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001253 }
1254
Julio Merino28774852016-09-14 16:59:46 +00001255 const char *output_base = globals->options->output_base.c_str();
Laszlo Csomor8a48f612016-11-17 10:18:34 +00001256 if (!blaze_util::PathExists(globals->options->output_base)) {
Julio Merino28774852016-09-14 16:59:46 +00001257 if (MakeDirectories(globals->options->output_base, 0777) == -1) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001258 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1259 "Output base directory '%s' could not be created",
Dave MacLachlan6b747ee2016-07-20 10:00:44 +00001260 output_base);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001261 }
1262 } else {
Laszlo Csomor8a48f612016-11-17 10:18:34 +00001263 if (!blaze_util::IsDirectory(globals->options->output_base)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001264 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1265 "Error: Output base directory '%s' could not be created. "
1266 "It exists but is not a directory.",
Dave MacLachlan6b747ee2016-07-20 10:00:44 +00001267 output_base);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001268 }
1269 }
Laszlo Csomor9c951962016-11-10 13:31:27 +00001270 if (!blaze_util::CanAccess(globals->options->output_base, true, true, true)) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001271 die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1272 "Error: Output base directory '%s' must be readable and writable.",
Dave MacLachlan6b747ee2016-07-20 10:00:44 +00001273 output_base);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001274 }
Dave MacLachlan6b747ee2016-07-20 10:00:44 +00001275 ExcludePathFromBackup(output_base);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001276
Julio Merino28774852016-09-14 16:59:46 +00001277 globals->options->output_base = MakeCanonical(output_base);
1278 globals->lockfile = globals->options->output_base + "/lock";
1279 globals->jvm_log_file = globals->options->output_base + "/server/jvm.out";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001280}
1281
1282static void CheckEnvironment() {
Lukacs Berki86a28b02016-10-25 10:34:45 +00001283 if (getenv("http_proxy") != NULL) {
1284 fprintf(stderr, "Warning: ignoring http_proxy in environment.\n");
1285 unsetenv("http_proxy");
1286 }
1287
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001288 if (getenv("LD_ASSUME_KERNEL") != NULL) {
1289 // Fix for bug: if ulimit -s and LD_ASSUME_KERNEL are both
1290 // specified, the JVM fails to create threads. See thread_stack_regtest.
1291 // This is also provoked by LD_LIBRARY_PATH=/usr/lib/debug,
1292 // or anything else that causes the JVM to use LinuxThreads.
1293 fprintf(stderr, "Warning: ignoring LD_ASSUME_KERNEL in environment.\n");
1294 unsetenv("LD_ASSUME_KERNEL");
1295 }
1296
1297 if (getenv("LD_PRELOAD") != NULL) {
1298 fprintf(stderr, "Warning: ignoring LD_PRELOAD in environment.\n");
1299 unsetenv("LD_PRELOAD");
1300 }
1301
1302 if (getenv("_JAVA_OPTIONS") != NULL) {
1303 // This would override --host_jvm_args
1304 fprintf(stderr, "Warning: ignoring _JAVA_OPTIONS in environment.\n");
1305 unsetenv("_JAVA_OPTIONS");
1306 }
1307
Thiago Farinadfe43a22015-04-07 13:48:49 +00001308 if (getenv("TEST_TMPDIR") != NULL) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001309 fprintf(stderr, "INFO: $TEST_TMPDIR defined: output root default is "
Julio Merino28774852016-09-14 16:59:46 +00001310 "'%s'.\n", globals->options->output_root.c_str());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001311 }
1312
1313 // TODO(bazel-team): We've also seen a failure during loading (creating
1314 // threads?) when ulimit -Hs 8192. Characterize that and check for it here.
1315
1316 // Make the JVM use ISO-8859-1 for parsing its command line because "blaze
1317 // run" doesn't handle non-ASCII command line arguments. This is apparently
1318 // the most reliable way to select the platform default encoding.
1319 setenv("LANG", "en_US.ISO-8859-1", 1);
1320 setenv("LANGUAGE", "en_US.ISO-8859-1", 1);
1321 setenv("LC_ALL", "en_US.ISO-8859-1", 1);
1322 setenv("LC_CTYPE", "en_US.ISO-8859-1", 1);
1323}
1324
Thiago Farina0b6963e2015-04-28 20:26:45 +00001325static void SetupStreams() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001326 // Line-buffer stderr, since we always flush at the end of a server
1327 // message. This saves lots of single-char calls to write(2).
1328 // This doesn't work if any writes to stderr have already occurred!
1329 setlinebuf(stderr);
1330
1331 // Ensure we have three open fds. Otherwise we can end up with
1332 // bizarre things like stdout going to the lock file, etc.
Lukacs Berkicdd42272016-09-13 07:52:01 +00001333 if (fcntl(STDIN_FILENO, F_GETFL) == -1) open("/dev/null", O_RDONLY);
1334 if (fcntl(STDOUT_FILENO, F_GETFL) == -1) open("/dev/null", O_WRONLY);
1335 if (fcntl(STDERR_FILENO, F_GETFL) == -1) open("/dev/null", O_WRONLY);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001336}
1337
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001338static void CheckBinaryPath(const string& argv0) {
1339 if (argv0[0] == '/') {
1340 globals->binary_path = argv0;
1341 } else {
1342 string abs_path = globals->cwd + '/' + argv0;
1343 char *resolved_path = realpath(abs_path.c_str(), NULL);
1344 if (resolved_path) {
1345 globals->binary_path = resolved_path;
1346 free(resolved_path);
1347 } else {
1348 // This happens during our integration tests, but thats okay, as we won't
1349 // log the invocation anyway.
1350 globals->binary_path = abs_path;
1351 }
1352 }
1353}
1354
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001355// TODO(bazel-team): Execute the server as a child process and write its exit
1356// code to a file. In case the server becomes unresonsive or terminates
1357// unexpectedly (in a way that isn't already handled), we can observe the file,
1358// if it exists. (If it doesn't, then we know something went horribly wrong.)
Julio Merino28774852016-09-14 16:59:46 +00001359int Main(int argc, const char *argv[], OptionProcessor *option_processor) {
Thiago Farina676cb9f2016-10-06 11:00:43 +00001360 globals = new GlobalVariables(option_processor);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001361 SetupStreams();
1362
1363 // Must be done before command line parsing.
1364 ComputeWorkspace();
1365 CheckBinaryPath(argv[0]);
1366 ParseOptions(argc, argv);
Lukacs Berkibb2230f2016-04-27 14:19:25 +00001367
Lukacs Berki71675a52016-11-08 09:48:27 +00001368 debug_log("Debug logging active");
1369
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001370 CheckEnvironment();
Laszlo Csomor8a48f612016-11-17 10:18:34 +00001371 blaze::CreateSecureOutputRoot(globals->options->output_user_root);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001372
1373 const string self_path = GetSelfPath();
1374 ComputeBaseDirectories(self_path);
1375
Lukacs Berki71675a52016-11-08 09:48:27 +00001376 blaze_server = static_cast<BlazeServer *>(new GrpcBlazeServer(
1377 globals->options->connect_timeout_secs));
Lukacs Berki907dbbf2016-04-15 11:30:12 +00001378
Lukacs Berki415d39a2016-04-28 13:18:54 +00001379 globals->command_wait_time = blaze_server->AcquireLock();
Lukacs Berkice1445f2016-04-19 15:52:55 +00001380
Julio Merino28774852016-09-14 16:59:46 +00001381 WarnFilesystemType(globals->options->output_base);
Lukacs Berkice1445f2016-04-19 15:52:55 +00001382
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001383 ExtractData(self_path);
Lukacs Berki949c8762016-07-08 12:17:28 +00001384 VerifyJavaVersionAndSetJvm();
1385
Lukacs Berki1977d922016-05-02 09:31:37 +00001386 blaze_server->Connect();
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001387 EnsureCorrectRunningVersion(blaze_server);
1388 KillRunningServerIfDifferentStartupOptions(blaze_server);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001389
Julio Merino28774852016-09-14 16:59:46 +00001390 if (globals->options->batch) {
1391 SetScheduling(globals->options->batch_cpu_scheduling,
1392 globals->options->io_nice_level);
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001393 StartStandalone(blaze_server);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001394 } else {
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001395 SendServerRequest(blaze_server);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001396 }
1397 return 0;
1398}
Thiago Farina0b6963e2015-04-28 20:26:45 +00001399
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001400static void null_grpc_log_function(gpr_log_func_args *args) {
1401}
1402
Lukacs Berki71675a52016-11-08 09:48:27 +00001403GrpcBlazeServer::GrpcBlazeServer(int connect_timeout_secs) {
Lukacs Berki1977d922016-05-02 09:31:37 +00001404 connected_ = false;
Lukacs Berki71675a52016-11-08 09:48:27 +00001405 connect_timeout_secs_ = connect_timeout_secs;
1406
1407 gpr_set_log_function(null_grpc_log_function);
1408
Laszlo Csomoref5ceef2016-11-18 11:19:02 +00001409 _pipe = blaze_util::CreatePipe();
1410 if (_pipe == NULL) {
1411 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "Couldn't create pipe");
Lukacs Berki6dd29092016-05-30 14:05:33 +00001412 }
1413}
1414
1415GrpcBlazeServer::~GrpcBlazeServer() {
Laszlo Csomoref5ceef2016-11-18 11:19:02 +00001416 delete _pipe;
1417 _pipe = NULL;
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001418}
1419
1420bool GrpcBlazeServer::Connect() {
Lukacs Berki1977d922016-05-02 09:31:37 +00001421 assert(!connected_);
1422
Julio Merino28774852016-09-14 16:59:46 +00001423 std::string server_dir = globals->options->output_base + "/server";
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001424 std::string port;
Lukacs Berkib7caf9d2016-04-25 09:44:14 +00001425 std::string ipv4_prefix = "127.0.0.1:";
Lukacs Berkic8e74242016-04-28 08:32:04 +00001426 std::string ipv6_prefix_1 = "[0:0:0:0:0:0:0:1]:";
1427 std::string ipv6_prefix_2 = "[::1]:";
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001428
Lukacs Berki7e0249e2016-04-21 08:14:08 +00001429 if (!ReadFile(server_dir + "/command_port", &port)) {
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001430 return false;
1431 }
1432
Lukacs Berkib7caf9d2016-04-25 09:44:14 +00001433 // Make sure that we are being directed to localhost
1434 if (port.compare(0, ipv4_prefix.size(), ipv4_prefix)
Lukacs Berkic8e74242016-04-28 08:32:04 +00001435 && port.compare(0, ipv6_prefix_1.size(), ipv6_prefix_1)
1436 && port.compare(0, ipv6_prefix_2.size(), ipv6_prefix_2)) {
Lukacs Berkib7caf9d2016-04-25 09:44:14 +00001437 return false;
1438 }
1439
Lukacs Berki7e0249e2016-04-21 08:14:08 +00001440 if (!ReadFile(server_dir + "/request_cookie", &request_cookie_)) {
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001441 return false;
1442 }
1443
Lukacs Berki7e0249e2016-04-21 08:14:08 +00001444 if (!ReadFile(server_dir + "/response_cookie", &response_cookie_)) {
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001445 return false;
1446 }
1447
1448 std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel(
Lukacs Berkib7caf9d2016-04-25 09:44:14 +00001449 port, grpc::InsecureChannelCredentials()));
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001450 std::unique_ptr<command_server::CommandServer::Stub> client(
1451 command_server::CommandServer::NewStub(channel));
1452
1453 grpc::ClientContext context;
1454 context.set_deadline(
Lukacs Berki71675a52016-11-08 09:48:27 +00001455 std::chrono::system_clock::now() +
1456 std::chrono::seconds(connect_timeout_secs_));
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001457
1458 command_server::PingRequest request;
1459 command_server::PingResponse response;
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001460 request.set_cookie(request_cookie_);
Lukacs Berki7494c922016-04-27 11:17:51 +00001461
Lukacs Berki71675a52016-11-08 09:48:27 +00001462 debug_log("Trying to connect to server (timeout: %d secs)...",
1463 connect_timeout_secs_);
Lukacs Berki7e0249e2016-04-21 08:14:08 +00001464 grpc::Status status = client->Ping(&context, request, &response);
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001465
Lukacs Berkic55e9c72016-04-25 13:43:40 +00001466 if (!status.ok() || response.cookie() != response_cookie_) {
Lukacs Berki71675a52016-11-08 09:48:27 +00001467 debug_log("Connection to server failed: %s",
1468 status.error_message().c_str());
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001469 return false;
1470 }
1471
Lukacs Berki1977d922016-05-02 09:31:37 +00001472 globals->server_pid = GetServerPid(server_dir);
1473 if (globals->server_pid <= 0) {
1474 pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
1475 "can't get PID of existing server (server dir=%s)",
1476 server_dir.c_str());
1477 }
1478
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001479 this->client_ = std::move(client);
Lukacs Berki1977d922016-05-02 09:31:37 +00001480 connected_ = true;
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001481 return true;
1482}
1483
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001484// Cancellation works as follows:
1485//
1486// When the user presses Ctrl-C, a SIGINT is delivered to the client, which is
1487// translated into a BlazeServer::Cancel() call. Since it's not a good idea to
Lukacs Berki6dd29092016-05-30 14:05:33 +00001488// do significant work in signal handlers, all it does is write a byte to an
1489// unnamed pipe.
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001490//
Lukacs Berki6dd29092016-05-30 14:05:33 +00001491// This unnamed pipe is used to communicate with the cancel thread. Whenever
1492// something interesting happens, a byte is written into it, which is read by
1493// the cancel thread. These commands are available:
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001494//
Lukacs Berki6dd29092016-05-30 14:05:33 +00001495// - NOP
1496// - JOIN. The cancel thread needs to be terminated.
1497// - CANCEL. If the command ID is already available, a cancel request is sent.
1498// - COMMAND_ID_RECEIVED. The client learned the command ID from the server.
1499// If there is a pending cancellation request, it is acted upon.
1500//
1501// The only data the cancellation thread shares with the main thread is the
1502// file descriptor for receiving commands and command_id_, the latter of which
1503// is protected by a mutex, which mainly serves as a memory fence.
1504//
1505// The cancellation thread is joined at the end of the execution of the command.
1506// The main thread wakes it up just so that it can finish (using the JOIN
1507// action)
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001508//
1509// It's conceivable that the server is busy and thus it cannot service the
1510// cancellation request. In that case, we simply ignore the failure and the both
1511// the server and the client go on as if nothing had happened (except that this
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001512// Ctrl-C still counts as a SIGINT, three of which result in a SIGKILL being
1513// delivered to the server)
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001514void GrpcBlazeServer::CancelThread() {
1515 bool running = true;
Lukacs Berki6dd29092016-05-30 14:05:33 +00001516 bool cancel = false;
1517 bool command_id_received = false;
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001518 while (running) {
Lukacs Berki6dd29092016-05-30 14:05:33 +00001519 char buf;
Laszlo Csomoref5ceef2016-11-18 11:19:02 +00001520
1521 int bytes_read = _pipe->Receive(&buf, 1);
1522 if (bytes_read < 0 && errno == EINTR) {
Lukacs Berki6dd29092016-05-30 14:05:33 +00001523 continue;
1524 } else if (bytes_read != 1) {
1525 pdie(blaze_exit_code::INTERNAL_ERROR,
1526 "Cannot communicate with cancel thread");
1527 }
1528
1529 switch (buf) {
1530 case CancelThreadAction::NOTHING:
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001531 break;
1532
Lukacs Berki6dd29092016-05-30 14:05:33 +00001533 case CancelThreadAction::JOIN:
1534 running = false;
1535 break;
1536
1537 case CancelThreadAction::COMMAND_ID_RECEIVED:
1538 command_id_received = true;
1539 if (cancel) {
1540 SendCancelMessage();
1541 cancel = false;
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001542 }
1543 break;
1544
Lukacs Berki6dd29092016-05-30 14:05:33 +00001545 case CancelThreadAction::CANCEL:
1546 if (command_id_received) {
1547 SendCancelMessage();
1548 } else {
1549 cancel = true;
1550 }
1551 break;
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001552 }
1553 }
1554}
1555
Lukacs Berki6dd29092016-05-30 14:05:33 +00001556void GrpcBlazeServer::SendCancelMessage() {
1557 std::unique_lock<std::mutex> lock(cancel_thread_mutex_);
1558
1559 command_server::CancelRequest request;
1560 request.set_cookie(request_cookie_);
1561 request.set_command_id(command_id_);
1562 grpc::ClientContext context;
1563 context.set_deadline(std::chrono::system_clock::now() +
Lukacs Berki3ace3002016-08-31 08:55:34 +00001564 std::chrono::seconds(10));
Lukacs Berki6dd29092016-05-30 14:05:33 +00001565 command_server::CancelResponse response;
1566 // There isn't a lot we can do if this request fails
Lukacs Berki3ace3002016-08-31 08:55:34 +00001567 grpc::Status status = client_->Cancel(&context, request, &response);
1568 if (!status.ok()) {
1569 fprintf(stderr, "\nCould not interrupt server (%s)\n\n",
1570 status.error_message().c_str());
1571 }
Lukacs Berki6dd29092016-05-30 14:05:33 +00001572}
1573
Lukacs Berki1977d922016-05-02 09:31:37 +00001574// This will wait indefinitely until the server shuts down
1575void GrpcBlazeServer::KillRunningServer() {
1576 assert(connected_);
1577 assert(globals->server_pid > 0);
1578
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001579 grpc::ClientContext context;
1580 command_server::RunRequest request;
1581 command_server::RunResponse response;
1582 request.set_cookie(request_cookie_);
Julio Merino28774852016-09-14 16:59:46 +00001583 request.set_block_for_lock(globals->options->block_for_lock);
Laszlo Csomorae16e762016-11-18 10:16:08 +00001584 request.set_client_description("pid=" + blaze::GetProcessIdAsString() +
1585 " (for shutdown)");
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001586 request.add_arg("shutdown");
1587 std::unique_ptr<grpc::ClientReader<command_server::RunResponse>> reader(
1588 client_->Run(&context, request));
1589
1590 while (reader->Read(&response)) {}
1591
Lukacs Berki1977d922016-05-02 09:31:37 +00001592 // Kill the server process for good measure.
Julio Merino28774852016-09-14 16:59:46 +00001593 if (VerifyServerProcess(globals->server_pid, globals->options->output_base,
1594 globals->options->install_base)) {
Lukacs Berkiee44c382016-09-14 10:53:37 +00001595 KillServerProcess(globals->server_pid);
1596 }
Lukacs Berki1977d922016-05-02 09:31:37 +00001597
1598 connected_ = false;
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001599}
1600
1601unsigned int GrpcBlazeServer::Communicate() {
Lukacs Berki1977d922016-05-02 09:31:37 +00001602 assert(connected_);
1603
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001604 vector<string> arg_vector;
Julio Merino28774852016-09-14 16:59:46 +00001605 string command = globals->option_processor->GetCommand();
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001606 if (command != "") {
1607 arg_vector.push_back(command);
1608 AddLoggingArgs(&arg_vector);
1609 }
1610
Julio Merino28774852016-09-14 16:59:46 +00001611 globals->option_processor->GetCommandArguments(&arg_vector);
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001612
1613 command_server::RunRequest request;
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001614 request.set_cookie(request_cookie_);
Julio Merino28774852016-09-14 16:59:46 +00001615 request.set_block_for_lock(globals->options->block_for_lock);
Laszlo Csomorae16e762016-11-18 10:16:08 +00001616 request.set_client_description("pid=" + blaze::GetProcessIdAsString());
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001617 for (const string& arg : arg_vector) {
1618 request.add_arg(arg);
1619 }
1620
1621 grpc::ClientContext context;
1622 command_server::RunResponse response;
1623 std::unique_ptr<grpc::ClientReader<command_server::RunResponse>> reader(
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001624 client_->Run(&context, request));
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001625
Lukacs Berki415d39a2016-04-28 13:18:54 +00001626 // Release the server lock because the gRPC handles concurrent clients just
1627 // fine. Note that this may result in two "waiting for other client" messages
1628 // (one during server startup and one emitted by the server)
Lukacs Berki1977d922016-05-02 09:31:37 +00001629 blaze::ReleaseLock(&blaze_lock_);
Lukacs Berki415d39a2016-04-28 13:18:54 +00001630
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001631 std::thread cancel_thread(&GrpcBlazeServer::CancelThread, this);
1632 bool command_id_set = false;
Laurent Le Brun08849b22016-09-20 12:21:32 +00001633 bool pipe_broken = false;
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001634 while (reader->Read(&response)) {
Lukacs Berkic55e9c72016-04-25 13:43:40 +00001635 if (response.cookie() != response_cookie_) {
1636 fprintf(stderr, "\nServer response cookie invalid, exiting\n");
1637 return blaze_exit_code::INTERNAL_ERROR;
1638 }
1639
Laurent Le Brun08849b22016-09-20 12:21:32 +00001640 bool pipe_broken_now = false;
Lukacs Berkiedeb7532016-04-18 10:23:36 +00001641 if (response.standard_output().size() > 0) {
Laurent Le Brun08849b22016-09-20 12:21:32 +00001642 int result = write(STDOUT_FILENO, response.standard_output().c_str(),
1643 response.standard_output().size());
1644 if (result < 0 && errno == EPIPE) {
1645 pipe_broken_now = true;
1646 }
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001647 }
1648
Lukacs Berkiedeb7532016-04-18 10:23:36 +00001649 if (response.standard_error().size() > 0) {
Laurent Le Brun08849b22016-09-20 12:21:32 +00001650 int result = write(STDERR_FILENO, response.standard_error().c_str(),
1651 response.standard_error().size());
1652 if (result < 0 && errno == EPIPE) {
1653 pipe_broken_now = true;
1654 }
1655 }
1656
1657 if (pipe_broken_now && !pipe_broken) {
1658 pipe_broken = true;
1659 Cancel();
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001660 }
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001661
1662 if (!command_id_set && response.command_id().size() > 0) {
Lukacs Berki6dd29092016-05-30 14:05:33 +00001663 std::unique_lock<std::mutex> lock(cancel_thread_mutex_);
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001664 command_id_ = response.command_id();
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001665 command_id_set = true;
Lukacs Berki6dd29092016-05-30 14:05:33 +00001666 SendAction(CancelThreadAction::COMMAND_ID_RECEIVED);
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001667 }
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001668 }
1669
Lukacs Berki6dd29092016-05-30 14:05:33 +00001670 SendAction(CancelThreadAction::JOIN);
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001671 cancel_thread.join();
1672
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001673 if (!response.finished()) {
Lukacs Berki3ace3002016-08-31 08:55:34 +00001674 fprintf(stderr, "\nServer finished RPC without an explicit exit code\n\n");
Lukacs Berki2896dc02016-07-07 07:55:04 +00001675 return GetExitCodeForAbruptExit(*globals);
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001676 }
1677
Lukacs Berkie6a34f62016-04-25 12:16:04 +00001678 return response.exit_code();
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001679}
1680
1681void GrpcBlazeServer::Disconnect() {
Lukacs Berki1977d922016-05-02 09:31:37 +00001682 assert(connected_);
1683
Lukacs Berki00cfb7d2016-04-20 09:01:52 +00001684 client_.reset();
1685 request_cookie_ = "";
1686 response_cookie_ = "";
Lukacs Berki1977d922016-05-02 09:31:37 +00001687 connected_ = false;
Lukacs Berki1b25ce22016-04-15 13:11:21 +00001688}
1689
Lukacs Berki6dd29092016-05-30 14:05:33 +00001690void GrpcBlazeServer::SendAction(CancelThreadAction action) {
1691 char msg = action;
Laszlo Csomoref5ceef2016-11-18 11:19:02 +00001692 if (!_pipe->Send(&msg, 1)) {
Lukacs Berki3ace3002016-08-31 08:55:34 +00001693 sigprintf("\nCould not interrupt server (cannot write to client pipe)\n\n");
Sasha Smundak1fdd31d2016-07-25 17:54:00 +00001694 }
Lukacs Berki6dd29092016-05-30 14:05:33 +00001695}
1696
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001697void GrpcBlazeServer::Cancel() {
Lukacs Berki1977d922016-05-02 09:31:37 +00001698 assert(connected_);
Lukacs Berki6dd29092016-05-30 14:05:33 +00001699 SendAction(CancelThreadAction::CANCEL);
Lukacs Berkif1df38a2016-04-19 07:42:22 +00001700}
1701
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001702} // namespace blaze