Skip to content

Commit d3435b0

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 bazelbuild@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 bazelbuild#15076 Closes bazelbuild#14391 RELNOTES: None PiperOrigin-RevId: 441198110
1 parent 5c73d86 commit d3435b0

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/main/cpp/BUILD

+1
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

-7
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);

src/main/cpp/get_self_path_linux.cc

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 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

0 commit comments

Comments
 (0)