1 // Copyright 2019 Google LLC 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 // https://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 "sandboxed_api/sandbox2/logsink.h" 16 17 #include <unistd.h> 18 19 #include <csignal> 20 #include <string> 21 22 #include "absl/base/log_severity.h" 23 #include "absl/log/log_entry.h" 24 #include "absl/log/log_sink_registry.h" 25 #include "absl/strings/str_cat.h" 26 #include "absl/strings/str_format.h" 27 #include "absl/synchronization/mutex.h" 28 #include "sandboxed_api/sandbox2/logserver.pb.h" 29 30 namespace sandbox2 { 31 32 constexpr char LogSink::kLogFDName[]; 33 LogSink(int fd)34LogSink::LogSink(int fd) : comms_(fd) { absl::AddLogSink(this); } 35 ~LogSink()36LogSink::~LogSink() { absl::RemoveLogSink(this); } 37 Send(const absl::LogEntry & e)38void LogSink::Send(const absl::LogEntry& e) { 39 absl::MutexLock l(&lock_); 40 41 LogMessage msg; 42 msg.set_severity(static_cast<int>(e.log_severity())); 43 msg.set_path(std::string(e.source_basename())); 44 msg.set_line(e.source_line()); 45 msg.set_message(absl::StrCat(e.text_message(), "\n")); 46 msg.set_pid(getpid()); 47 48 if (!comms_.SendProtoBuf(msg)) { 49 absl::FPrintF(stderr, "sending log message to supervisor failed:\n%s\n", 50 e.text_message_with_prefix()); 51 } 52 53 if (e.log_severity() == absl::LogSeverity::kFatal) { 54 // Raise a SIGABRT to prevent the remaining code in logging to try to dump a 55 // symbolized stack trace which can lead to syscall violations. 56 kill(0, SIGABRT); 57 } 58 } 59 60 } // namespace sandbox2 61