// 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 #include #include // strerror #include #include #include #include #include "blaze_exit_code.h" #include "blaze_util_platform.h" #include "blaze_util.h" #include "util/file.h" #include "util/strings.h" namespace blaze { 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); } 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 MonotonicClock() { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec * 1000000000LL + ts.tv_nsec; } uint64 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/" + std::to_string(pid) + "/cwd").c_str(), server_cwd, sizeof(server_cwd)) < 0) { return ""; } return string(server_cwd); } bool IsSharedLibrary(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