blob: 012a1a59efbc6ade9d3b6fbbd9307243455e0449 [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
Mostyn Bramley-Moore44e8d102015-11-10 11:27:39 +000015#include <errno.h> // errno, ENAMETOOLONG
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016#include <limits.h>
Lukacs Berki4de98942016-09-09 09:23:36 +000017#include <linux/magic.h>
Thiago Farinafdc7f982015-04-27 23:01:34 +000018#include <pwd.h>
Lukacs Berki43e620b2016-05-02 11:47:40 +000019#include <signal.h>
jmmv3cb1aef2019-03-14 07:38:00 -070020#include <spawn.h>
Douglas Dawson5ca52f72016-07-13 16:38:40 +000021#include <stdio.h>
22#include <stdlib.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023#include <string.h> // strerror
Thiago Farina8a67da42015-05-05 18:04:50 +000024#include <sys/socket.h>
Laszlo Csomor48513d72016-12-21 12:47:40 +000025#include <sys/stat.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026#include <sys/statfs.h>
Damien Martin-Guillerez27b94752015-03-23 13:00:28 +000027#include <sys/types.h>
Thiago Farinafdc7f982015-04-27 23:01:34 +000028#include <unistd.h>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000030#include "src/main/cpp/blaze_util.h"
Thiago Farina7f9357f2015-04-23 13:57:43 +000031#include "src/main/cpp/blaze_util_platform.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000032#include "src/main/cpp/util/errors.h"
Thiago Farina7f9357f2015-04-23 13:57:43 +000033#include "src/main/cpp/util/exit_code.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000034#include "src/main/cpp/util/file.h"
ccalvarin73839762018-03-23 15:35:00 -070035#include "src/main/cpp/util/logging.h"
ccalvarinac69da02018-06-05 15:27:26 -070036#include "src/main/cpp/util/path.h"
Thiago Farina38320332015-05-18 09:12:21 +000037#include "src/main/cpp/util/port.h"
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000038#include "src/main/cpp/util/strings.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039
40namespace blaze {
41
ccalvarinc5a58802018-03-29 11:13:24 -070042using blaze_util::GetLastErrorString;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043using std::string;
Lukacs Berki43e620b2016-05-02 11:47:40 +000044using std::vector;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045
Kristina Chodorow93963352015-03-20 19:11:19 +000046string GetOutputRoot() {
Lukacs Berki30bed8f2015-10-26 12:18:24 +000047 string base;
Thiago Farina2a2b3522017-07-04 04:14:32 -040048 string home = GetHomeDir();
49 if (!home.empty()) {
Lukacs Berki30bed8f2015-10-26 12:18:24 +000050 base = home;
Damien Martin-Guillerez27b94752015-03-23 13:00:28 +000051 } else {
Thiago Farina2a2b3522017-07-04 04:14:32 -040052 char buf[2048];
Lukacs Berki30bed8f2015-10-26 12:18:24 +000053 struct passwd pwbuf;
54 struct passwd *pw = NULL;
55 int uid = getuid();
56 int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
57 if (r != -1 && pw != NULL) {
58 base = pw->pw_dir;
59 }
Damien Martin-Guillerez27b94752015-03-23 13:00:28 +000060 }
Lukacs Berki30bed8f2015-10-26 12:18:24 +000061
Thiago Farina2a2b3522017-07-04 04:14:32 -040062 if (!base.empty()) {
Lukacs Berki30bed8f2015-10-26 12:18:24 +000063 return blaze_util::JoinPath(base, ".cache/bazel");
64 }
65
66 return "/tmp";
Kristina Chodorow93963352015-03-20 19:11:19 +000067}
68
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069void WarnFilesystemType(const string& output_base) {
70 struct statfs buf = {};
71 if (statfs(output_base.c_str(), &buf) < 0) {
ccalvarin73839762018-03-23 15:35:00 -070072 BAZEL_LOG(WARNING) << "couldn't get file system type information for '"
73 << output_base << "': " << strerror(errno);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 return;
75 }
76
Lukacs Berki4de98942016-09-09 09:23:36 +000077 if (buf.f_type == NFS_SUPER_MAGIC) {
ccalvarin73839762018-03-23 15:35:00 -070078 BAZEL_LOG(WARNING) << "Output base '" << output_base
79 << "' is on NFS. This may lead to surprising failures "
80 "and undetermined behavior.";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081 }
82}
83
84string GetSelfPath() {
nharmatad79ec032019-03-06 14:54:15 -080085 // The file to which this symlink points could change contents or go missing
86 // concurrent with execution of the Bazel client, so we don't eagerly resolve
87 // it.
88 return "/proc/self/exe";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089}
90
Laszlo Csomor943d3cf2016-11-07 14:27:21 +000091uint64_t GetMillisecondsMonotonic() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010092 struct timespec ts = {};
93 clock_gettime(CLOCK_MONOTONIC, &ts);
Laszlo Csomor943d3cf2016-11-07 14:27:21 +000094 return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010095}
96
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010097void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010098 if (batch_cpu_scheduling) {
99 sched_param param = {};
100 param.sched_priority = 0;
101 if (sched_setscheduler(0, SCHED_BATCH, &param)) {
ccalvarin8448f572018-04-06 12:42:09 -0700102 BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR)
103 << "sched_setscheduler(SCHED_BATCH) failed: " << GetLastErrorString();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100104 }
105 }
106
107 if (io_nice_level >= 0) {
108 if (blaze_util::sys_ioprio_set(
109 IOPRIO_WHO_PROCESS, getpid(),
110 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, io_nice_level)) < 0) {
ccalvarin8448f572018-04-06 12:42:09 -0700111 BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR)
112 << "ioprio_set() with class " << IOPRIO_CLASS_BE << " and level "
113 << io_nice_level << " failed: " << GetLastErrorString();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100114 }
115 }
116}
117
118string GetProcessCWD(int pid) {
119 char server_cwd[PATH_MAX] = {};
120 if (readlink(
Googler9588b812015-07-23 11:49:37 +0000121 ("/proc/" + ToString(pid) + "/cwd").c_str(),
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100122 server_cwd, sizeof(server_cwd)) < 0) {
123 return "";
124 }
125
126 return string(server_cwd);
127}
128
Thiago Farina01f36002015-04-08 15:59:08 +0000129bool IsSharedLibrary(const string &filename) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100130 return blaze_util::ends_with(filename, ".so");
131}
132
Laszlo Csomor48513d72016-12-21 12:47:40 +0000133static string Which(const string &executable) {
Laszlo Csomor0d107492019-03-13 09:04:27 -0700134 string path(GetPathEnv("PATH"));
Laszlo Csomor48513d72016-12-21 12:47:40 +0000135 if (path.empty()) {
cushon849df362018-05-14 01:51:45 -0700136 return "";
Laszlo Csomor48513d72016-12-21 12:47:40 +0000137 }
138
Thiago Farina2a2b3522017-07-04 04:14:32 -0400139 vector<string> pieces = blaze_util::Split(path, ':');
Laszlo Csomor48513d72016-12-21 12:47:40 +0000140 for (auto piece : pieces) {
141 if (piece.empty()) {
142 piece = ".";
143 }
144
145 struct stat file_stat;
146 string candidate = blaze_util::JoinPath(piece, executable);
147 if (access(candidate.c_str(), X_OK) == 0 &&
148 stat(candidate.c_str(), &file_stat) == 0 &&
149 S_ISREG(file_stat.st_mode)) {
150 return candidate;
151 }
152 }
153 return "";
154}
155
cushon849df362018-05-14 01:51:45 -0700156string GetSystemJavabase() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100157 // if JAVA_HOME is defined, then use it as default.
Laszlo Csomor0d107492019-03-13 09:04:27 -0700158 string javahome = GetPathEnv("JAVA_HOME");
Thiago Farina2a2b3522017-07-04 04:14:32 -0400159 if (!javahome.empty()) {
Philipp Wollermann8db450f2019-02-03 09:26:07 -0800160 string javac = blaze_util::JoinPath(javahome, "bin/javac");
161 if (access(javac.c_str(), X_OK) == 0) {
162 return javahome;
163 }
164 BAZEL_LOG(WARNING)
165 << "Ignoring JAVA_HOME, because it must point to a JDK, not a JRE.";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100166 }
167
168 // which javac
Laszlo Csomor48513d72016-12-21 12:47:40 +0000169 string javac_dir = Which("javac");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100170 if (javac_dir.empty()) {
cushon849df362018-05-14 01:51:45 -0700171 return "";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100172 }
173
174 // Resolve all symlinks.
175 char resolved_path[PATH_MAX];
176 if (realpath(javac_dir.c_str(), resolved_path) == NULL) {
cushon849df362018-05-14 01:51:45 -0700177 return "";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100178 }
179 javac_dir = resolved_path;
180
181 // dirname dirname
182 return blaze_util::Dirname(blaze_util::Dirname(javac_dir));
183}
184
Lukacs Berkiee44c382016-09-14 10:53:37 +0000185// Called from a signal handler!
186static bool GetStartTime(const string& pid, string* start_time) {
187 string statfile = "/proc/" + pid + "/stat";
188 string statline;
189
Laszlo Csomor49970e02016-11-28 08:55:47 +0000190 if (!blaze_util::ReadFile(statfile, &statline)) {
Lukacs Berki43e620b2016-05-02 11:47:40 +0000191 return false;
192 }
193
Lukacs Berkiee44c382016-09-14 10:53:37 +0000194 vector<string> stat_entries = blaze_util::Split(statline, ' ');
195 if (stat_entries.size() < 22) {
ccalvarin8448f572018-04-06 12:42:09 -0700196 BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
197 << "Format of stat file at " << statfile
198 << " is unknown: " << GetLastErrorString();
Lukacs Berkia34c4fe2016-09-05 11:14:43 +0000199 }
200
Lukacs Berkiee44c382016-09-14 10:53:37 +0000201 // Start time since startup in jiffies. This combined with the PID should be
202 // unique.
203 *start_time = stat_entries[21];
Lukacs Berki43e620b2016-05-02 11:47:40 +0000204 return true;
205}
206
jmmv3cb1aef2019-03-14 07:38:00 -0700207int ConfigureDaemonProcess(posix_spawnattr_t* attrp,
208 const StartupOptions* options) {
209 // No interesting platform-specific details to configure on this platform.
210 return 0;
211}
212
lberkif2014a12017-05-10 04:30:39 -0400213void WriteSystemSpecificProcessIdentifier(
214 const string& server_dir, pid_t server_pid) {
215 string pid_string = ToString(server_pid);
Lukacs Berkiee44c382016-09-14 10:53:37 +0000216
217 string start_time;
lberkif2014a12017-05-10 04:30:39 -0400218 if (!GetStartTime(pid_string, &start_time)) {
ccalvarin8448f572018-04-06 12:42:09 -0700219 BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
220 << "Cannot get start time of process " << pid_string << ": "
221 << GetLastErrorString();
Lukacs Berki43e620b2016-05-02 11:47:40 +0000222 }
223
224 string start_time_file = blaze_util::JoinPath(server_dir, "server.starttime");
Laszlo Csomor49970e02016-11-28 08:55:47 +0000225 if (!blaze_util::WriteFile(start_time, start_time_file)) {
ccalvarin8448f572018-04-06 12:42:09 -0700226 BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
227 << "Cannot write start time in server dir " << server_dir << ": "
228 << GetLastErrorString();
Lukacs Berki43e620b2016-05-02 11:47:40 +0000229 }
230}
231
232// On Linux we use a combination of PID and start time to identify the server
233// process. That is supposed to be unique unless one can start more processes
234// than there are PIDs available within a single jiffy.
mschallerfd37b512017-07-11 18:21:36 +0200235bool VerifyServerProcess(int pid, const string& output_base) {
Lukacs Berkiee44c382016-09-14 10:53:37 +0000236 string start_time;
237 if (!GetStartTime(ToString(pid), &start_time)) {
Lukacs Berki0bb79c62016-09-08 11:45:51 +0000238 // Cannot read PID file from /proc . Process died meantime, all is good. No
239 // stale server is present.
Lukacs Berki119dd4b2016-07-13 15:28:42 +0000240 return false;
Lukacs Berki43e620b2016-05-02 11:47:40 +0000241 }
242
243 string recorded_start_time;
Laszlo Csomor49970e02016-11-28 08:55:47 +0000244 bool file_present = blaze_util::ReadFile(
Lukacs Berkib29db482016-05-04 09:39:48 +0000245 blaze_util::JoinPath(output_base, "server/server.starttime"),
246 &recorded_start_time);
Lukacs Berki43e620b2016-05-02 11:47:40 +0000247
lberkif2014a12017-05-10 04:30:39 -0400248 // If start time file got deleted, but PID file didn't, assume that this is an
Lukacs Berkiee44c382016-09-14 10:53:37 +0000249 // old Blaze process that doesn't know how to write start time files yet.
250 return !file_present || recorded_start_time == start_time;
251}
Lukacs Berki43e620b2016-05-02 11:47:40 +0000252
Dave MacLachlan6b747ee2016-07-20 10:00:44 +0000253// Not supported.
254void ExcludePathFromBackup(const string &path) {
255}
256
jmmva96369c2017-07-10 18:14:36 +0200257int32_t GetExplicitSystemLimit(const int resource) {
258 return -1;
259}
260
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100261} // namespace blaze