Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 15 | #include "src/main/cpp/blaze_util.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | #include <fcntl.h> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
lberki | d3d37f0 | 2018-04-18 11:44:14 -0700 | [diff] [blame] | 22 | #include <cassert> |
ccalvarin | 9eea0f9 | 2018-03-21 15:32:30 -0700 | [diff] [blame] | 23 | #include <iostream> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | |
Dmitry Lomov | 78c0cc7 | 2015-08-11 16:44:21 +0000 | [diff] [blame] | 25 | #include "src/main/cpp/blaze_util_platform.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 26 | #include "src/main/cpp/util/errors.h" |
Thiago Farina | 7f9357f | 2015-04-23 13:57:43 +0000 | [diff] [blame] | 27 | #include "src/main/cpp/util/exit_code.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 28 | #include "src/main/cpp/util/file.h" |
ccalvarin | 9eea0f9 | 2018-03-21 15:32:30 -0700 | [diff] [blame] | 29 | #include "src/main/cpp/util/logging.h" |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 30 | #include "src/main/cpp/util/numbers.h" |
ccalvarin | 755278d | 2018-06-07 11:05:54 -0700 | [diff] [blame] | 31 | #include "src/main/cpp/util/path_platform.h" |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame] | 32 | #include "src/main/cpp/util/port.h" |
ccalvarin | 9eea0f9 | 2018-03-21 15:32:30 -0700 | [diff] [blame] | 33 | #include "src/main/cpp/util/strings.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | namespace blaze { |
| 36 | |
lberki | d3d37f0 | 2018-04-18 11:44:14 -0700 | [diff] [blame] | 37 | using std::map; |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::vector; |
| 40 | |
Thiago Farina | 048bbfc | 2016-09-21 08:20:41 +0000 | [diff] [blame] | 41 | const char kServerPidFile[] = "server.pid.txt"; |
Lukacs Berki | d9da60f | 2016-04-26 11:40:24 +0000 | [diff] [blame] | 42 | |
mschaller | fd37b51 | 2017-07-11 18:21:36 +0200 | [diff] [blame] | 43 | const unsigned int kPostShutdownGracePeriodSeconds = 60; |
| 44 | |
| 45 | const unsigned int kPostKillGracePeriodSeconds = 10; |
| 46 | |
Thiago Farina | 2fd7890 | 2015-05-18 11:37:59 +0000 | [diff] [blame] | 47 | const char* GetUnaryOption(const char *arg, |
| 48 | const char *next_arg, |
| 49 | const char *key) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | const char *value = blaze_util::var_strprefix(arg, key); |
| 51 | if (value == NULL) { |
| 52 | return NULL; |
| 53 | } else if (value[0] == '=') { |
| 54 | return value + 1; |
| 55 | } else if (value[0]) { |
| 56 | return NULL; // trailing garbage in key name |
| 57 | } |
| 58 | |
| 59 | return next_arg; |
| 60 | } |
| 61 | |
| 62 | bool GetNullaryOption(const char *arg, const char *key) { |
| 63 | const char *value = blaze_util::var_strprefix(arg, key); |
| 64 | if (value == NULL) { |
| 65 | return false; |
| 66 | } else if (value[0] == '=') { |
ccalvarin | 8448f57 | 2018-04-06 12:42:09 -0700 | [diff] [blame] | 67 | BAZEL_DIE(blaze_exit_code::BAD_ARGV) |
| 68 | << "In argument '" << arg << "': option '" << key |
| 69 | << "' does not take a value."; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | } else if (value[0]) { |
| 71 | return false; // trailing garbage in key name |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 77 | const char* SearchUnaryOption(const vector<string>& args, |
| 78 | const char *key) { |
| 79 | if (args.empty()) { |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | vector<string>::size_type i = 0; |
| 84 | for (; i < args.size() - 1; ++i) { |
Luis Fernando Pino Duque | d56e1c6 | 2017-03-24 18:04:00 +0000 | [diff] [blame] | 85 | if (args[i] == "--") { |
| 86 | return NULL; |
| 87 | } |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 88 | const char* result = GetUnaryOption(args[i].c_str(), |
| 89 | args[i + 1].c_str(), |
| 90 | key); |
| 91 | if (result != NULL) { |
| 92 | return result; |
| 93 | } |
| 94 | } |
| 95 | return GetUnaryOption(args[i].c_str(), NULL, key); |
| 96 | } |
| 97 | |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 98 | bool SearchNullaryOption(const vector<string>& args, |
| 99 | const string& flag_name, |
| 100 | const bool default_value) { |
| 101 | const string positive_flag = "--" + flag_name; |
| 102 | const string negative_flag = "--no" + flag_name; |
| 103 | bool result = default_value; |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 104 | for (vector<string>::size_type i = 0; i < args.size(); i++) { |
Luis Fernando Pino Duque | d56e1c6 | 2017-03-24 18:04:00 +0000 | [diff] [blame] | 105 | if (args[i] == "--") { |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 106 | break; |
Luis Fernando Pino Duque | d56e1c6 | 2017-03-24 18:04:00 +0000 | [diff] [blame] | 107 | } |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 108 | if (GetNullaryOption(args[i].c_str(), positive_flag.c_str())) { |
| 109 | result = true; |
| 110 | } else if (GetNullaryOption(args[i].c_str(), negative_flag.c_str())) { |
| 111 | result = false; |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 114 | return result; |
lpino | 4ed3db6 | 2017-08-10 16:25:54 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 117 | bool IsArg(const string& arg) { |
| 118 | return blaze_util::starts_with(arg, "-") && (arg != "--help") |
| 119 | && (arg != "-help") && (arg != "-h"); |
| 120 | } |
| 121 | |
ccalvarin | 755278d | 2018-06-07 11:05:54 -0700 | [diff] [blame] | 122 | std::string AbsolutePathFromFlag(const std::string& value) { |
| 123 | if (value.empty()) { |
| 124 | return blaze_util::GetCwd(); |
| 125 | } else { |
| 126 | return blaze_util::MakeAbsolute(value); |
| 127 | } |
| 128 | } |
| 129 | |
mschaller | fd37b51 | 2017-07-11 18:21:36 +0200 | [diff] [blame] | 130 | void LogWait(unsigned int elapsed_seconds, unsigned int wait_seconds) { |
| 131 | SigPrintf("WARNING: Waiting for server process to terminate " |
| 132 | "(waited %d seconds, waiting at most %d)\n", |
| 133 | elapsed_seconds, wait_seconds); |
| 134 | } |
| 135 | |
| 136 | bool AwaitServerProcessTermination(int pid, const string& output_base, |
| 137 | unsigned int wait_seconds) { |
| 138 | uint64_t st = GetMillisecondsMonotonic(); |
| 139 | const unsigned int first_seconds = 5; |
| 140 | bool logged_first = false; |
| 141 | const unsigned int second_seconds = 10; |
| 142 | bool logged_second = false; |
| 143 | const unsigned int third_seconds = 30; |
| 144 | bool logged_third = false; |
| 145 | |
| 146 | while (VerifyServerProcess(pid, output_base)) { |
| 147 | TrySleep(100); |
| 148 | uint64_t elapsed_millis = GetMillisecondsMonotonic() - st; |
| 149 | if (!logged_first && elapsed_millis > first_seconds * 1000) { |
| 150 | LogWait(first_seconds, wait_seconds); |
| 151 | logged_first = true; |
| 152 | } |
| 153 | if (!logged_second && elapsed_millis > second_seconds * 1000) { |
| 154 | LogWait(second_seconds, wait_seconds); |
| 155 | logged_second = true; |
| 156 | } |
| 157 | if (!logged_third && elapsed_millis > third_seconds * 1000) { |
| 158 | LogWait(third_seconds, wait_seconds); |
| 159 | logged_third = true; |
| 160 | } |
| 161 | if (elapsed_millis > wait_seconds * 1000) { |
| 162 | SigPrintf("INFO: Waited %d seconds for server process (pid=%d) to" |
| 163 | " terminate.\n", |
| 164 | wait_seconds, pid); |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
ccalvarin | 9eea0f9 | 2018-03-21 15:32:30 -0700 | [diff] [blame] | 171 | // For now, we don't have the client set up to log to a file. If --client_debug |
| 172 | // is passed, however, all BAZEL_LOG statements will be output to stderr. |
| 173 | // If/when we switch to logging these to a file, care will have to be taken to |
| 174 | // either log to both stderr and the file in the case of --client_debug, or be |
| 175 | // ok that these log lines will only go to one stream. |
| 176 | void SetDebugLog(bool enabled) { |
| 177 | if (enabled) { |
| 178 | blaze_util::SetLoggingOutputStreamToStderr(); |
| 179 | } else { |
| 180 | blaze_util::SetLoggingOutputStream(nullptr); |
Dmitry Lomov | 891f540 | 2017-07-19 12:26:39 +0200 | [diff] [blame] | 181 | } |
Dmitry Lomov | 891f540 | 2017-07-19 12:26:39 +0200 | [diff] [blame] | 182 | } |
| 183 | |
lberki | d3d37f0 | 2018-04-18 11:44:14 -0700 | [diff] [blame] | 184 | void WithEnvVars::SetEnvVars(const map<string, EnvVarValue>& vars) { |
| 185 | for (const auto& var : vars) { |
| 186 | switch (var.second.action) { |
| 187 | case EnvVarAction::UNSET: |
| 188 | UnsetEnv(var.first); |
| 189 | break; |
| 190 | |
| 191 | case EnvVarAction::SET: |
| 192 | SetEnv(var.first, var.second.value); |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | assert(false); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | WithEnvVars::WithEnvVars(const map<string, EnvVarValue>& vars) { |
| 202 | for (const auto& v : vars) { |
| 203 | if (ExistsEnv(v.first)) { |
| 204 | _old_values[v.first] = EnvVarValue(EnvVarAction::SET, GetEnv(v.first)); |
| 205 | } else { |
| 206 | _old_values[v.first] = EnvVarValue(EnvVarAction::UNSET, ""); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | SetEnvVars(vars); |
| 211 | } |
| 212 | |
| 213 | WithEnvVars::~WithEnvVars() { |
| 214 | SetEnvVars(_old_values); |
| 215 | } |
| 216 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | } // namespace blaze |