1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "RecordingLogBuffer.h"
18
19 #include <android-base/file.h>
20
WriteLogMessage(int fd,const RecordedLogMessage & meta,const std::string & msg)21 static void WriteLogMessage(int fd, const RecordedLogMessage& meta, const std::string& msg) {
22 android::base::WriteFully(fd, &meta, sizeof(meta));
23 android::base::WriteFully(fd, msg.c_str(), meta.msg_len);
24 }
25
RecordLogMessage(log_id_t log_id,log_time realtime,uid_t uid,pid_t pid,pid_t tid,const char * msg,uint16_t len)26 void RecordingLogBuffer::RecordLogMessage(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid,
27 pid_t tid, const char* msg, uint16_t len) {
28 auto lock = std::lock_guard{lock_};
29 if (len > LOGGER_ENTRY_MAX_PAYLOAD) {
30 len = LOGGER_ENTRY_MAX_PAYLOAD;
31 }
32
33 RecordedLogMessage recorded_log_message = {
34 .uid = uid,
35 .pid = static_cast<uint32_t>(pid),
36 .tid = static_cast<uint32_t>(tid),
37 .realtime = realtime,
38 .msg_len = len,
39 .log_id = static_cast<uint8_t>(log_id),
40 };
41
42 if (!fd_.ok()) {
43 fd_.reset(open("/data/misc/logd/recorded-messages",
44 O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0666));
45 if (!fd_.ok()) {
46 since_boot_messages_.emplace_back(recorded_log_message, std::string(msg, len));
47 return;
48 } else {
49 for (const auto& [meta, msg] : since_boot_messages_) {
50 WriteLogMessage(fd_.get(), meta, msg);
51 }
52 }
53 }
54
55 WriteLogMessage(fd_.get(), recorded_log_message, std::string(msg, len));
56 }
57
Log(log_id_t log_id,log_time realtime,uid_t uid,pid_t pid,pid_t tid,const char * msg,uint16_t len)58 int RecordingLogBuffer::Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
59 const char* msg, uint16_t len) {
60 RecordLogMessage(log_id, realtime, uid, pid, tid, msg, len);
61 return SimpleLogBuffer::Log(log_id, realtime, uid, pid, tid, msg, len);
62 }