| // Copyright 2014 Google Inc. All rights reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <errno.h> |
| #include <limits.h> |
| #include <string.h> // strerror |
| #include <sys/statfs.h> |
| #include <unistd.h> |
| |
| #include <cstdlib> |
| #include <cstdio> |
| |
| #include "src/main/cpp/blaze_util.h" |
| #include "src/main/cpp/blaze_util_platform.h" |
| #include "src/main/cpp/util/errors.h" |
| #include "src/main/cpp/util/exit_code.h" |
| #include "src/main/cpp/util/file.h" |
| #include "src/main/cpp/util/strings.h" |
| |
| namespace blaze { |
| |
| using blaze_util::die; |
| using blaze_util::pdie; |
| using std::string; |
| |
| void WarnFilesystemType(const string& output_base) { |
| } |
| |
| string GetSelfPath() { |
| char buffer[PATH_MAX] = {}; |
| ssize_t bytes = readlink("/proc/self/exe", buffer, sizeof(buffer)); |
| if (bytes == sizeof(buffer)) { |
| // symlink contents truncated |
| bytes = -1; |
| errno = ENAMETOOLONG; |
| } |
| if (bytes == -1) { |
| pdie(blaze_exit_code::INTERNAL_ERROR, "error reading /proc/self/exe"); |
| } |
| buffer[bytes] = '\0'; // readlink does not NUL-terminate |
| return string(buffer); |
| } |
| |
| string GetOutputRoot() { |
| return "/var/tmp"; |
| } |
| |
| pid_t GetPeerProcessId(int socket) { |
| struct ucred creds = {}; |
| socklen_t len = sizeof creds; |
| if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &creds, &len) == -1) { |
| pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| "can't get server pid from connection"); |
| } |
| return creds.pid; |
| } |
| |
| uint64_t MonotonicClock() { |
| struct timespec ts = {}; |
| clock_gettime(CLOCK_MONOTONIC, &ts); |
| return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| } |
| |
| uint64_t ProcessClock() { |
| struct timespec ts = {}; |
| clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
| return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| } |
| |
| void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) { |
| // TODO(bazel-team): There should be a similar function on Windows. |
| } |
| |
| string GetProcessCWD(int pid) { |
| char server_cwd[PATH_MAX] = {}; |
| if (readlink( |
| ("/proc/" + ToString(pid) + "/cwd").c_str(), |
| server_cwd, sizeof(server_cwd)) < 0) { |
| return ""; |
| } |
| |
| return string(server_cwd); |
| } |
| |
| bool IsSharedLibrary(const string &filename) { |
| return blaze_util::ends_with(filename, ".dll"); |
| } |
| |
| string GetDefaultHostJavabase() { |
| const char *javahome = getenv("JAVA_HOME"); |
| if (javahome == NULL) { |
| die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, |
| "Error: JAVA_HOME not set."); |
| } |
| return javahome; |
| } |
| |
| } // namespace blaze |