Skip to content

Commit 417e116

Browse files
meteorcloudycopybara-github
authored andcommitted
Seperate GetSelfPath implementation for Blaze and Bazel
In Blaze, we keep using "/proc/self/exe", but in Bazel we resolve this symlink to the actual Bazel binary. This is essentially a rollback of d79ec03 only for Bazel, because it solved an issue that rare in Bazel, but it's preventing users from running Bazel inside a Linux docker container on Apple Silicon machines. Fixes #15076 Closes #14391 RELNOTES: None PiperOrigin-RevId: 441177762 Change-Id: I48d7283cf7f42f4220e2261f02809c8b5270ef70
1 parent 4710ef8 commit 417e116

4 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/main/cpp/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cc_library(
3636
"//conditions:default": [
3737
"blaze_util_linux.cc",
3838
"blaze_util_posix.cc",
39+
"get_self_path_linux.cc",
3940
],
4041
}),
4142
hdrs = [

src/main/cpp/blaze_util_linux.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ void WarnFilesystemType(const blaze_util::Path &output_base) {
8282
}
8383
}
8484

85-
string GetSelfPath(const char* argv0) {
86-
// The file to which this symlink points could change contents or go missing
87-
// concurrent with execution of the Bazel client, so we don't eagerly resolve
88-
// it.
89-
return "/proc/self/exe";
90-
}
91-
9285
uint64_t GetMillisecondsMonotonic() {
9386
struct timespec ts = {};
9487
clock_gettime(CLOCK_MONOTONIC, &ts);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2014 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <unistd.h>
16+
#include <limits.h>
17+
18+
#include "src/main/cpp/blaze_util_platform.h"
19+
#include "src/main/cpp/util/errors.h"
20+
#include "src/main/cpp/util/exit_code.h"
21+
#include "src/main/cpp/util/logging.h"
22+
23+
namespace blaze {
24+
25+
using blaze_util::GetLastErrorString;
26+
using std::string;
27+
28+
string GetSelfPath(const char* argv0) {
29+
char buffer[PATH_MAX] = {};
30+
ssize_t bytes = readlink("/proc/self/exe", buffer, sizeof(buffer));
31+
if (bytes == sizeof(buffer)) {
32+
// symlink contents truncated
33+
bytes = -1;
34+
errno = ENAMETOOLONG;
35+
}
36+
if (bytes == -1) {
37+
BAZEL_DIE(blaze_exit_code::INTERNAL_ERROR)
38+
<< "error reading /proc/self/exe: " << GetLastErrorString();
39+
}
40+
buffer[bytes] = '\0'; // readlink does not NUL-terminate
41+
return string(buffer);
42+
}
43+
44+
} // namespace blaze
45+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2014 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "src/main/cpp/blaze_util_platform.h"
16+
namespace blaze {
17+
18+
std::string GetSelfPath(const char* argv0) {
19+
// The file to which this symlink points could change contents or go missing
20+
// concurrent with execution of the Bazel client, so we don't eagerly resolve
21+
// it.
22+
return "/proc/self/exe";
23+
}
24+
25+
} // namespace blaze
26+

0 commit comments

Comments
 (0)