blob: 06e5e0447a9c15868c8b8ae7fc4746efbe7897a3 [file] [log] [blame]
Mark Salyzyn018a96d2016-03-01 13:45:42 -08001/*
2 * Copyright (C) 2007-2016 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
Tom Cherry21bb36c2020-01-08 15:18:26 -080017#include "pmsg_writer.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080018
19#include <errno.h>
20#include <fcntl.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/types.h>
24#include <time.h>
25
Tom Cherry2a6811b2019-12-11 12:56:01 -080026#include <shared_mutex>
27
Steven Moreland1f83aa42017-04-13 23:48:57 -070028#include <log/log_properties.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080029#include <private/android_logger.h>
30
Mark Salyzyn018a96d2016-03-01 13:45:42 -080031#include "logger.h"
Tom Cherry2a6811b2019-12-11 12:56:01 -080032#include "rwlock.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080033#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080034
Tom Cherry2a6811b2019-12-11 12:56:01 -080035static int pmsg_fd;
36static RwLock pmsg_fd_lock;
Mark Salyzyndb8a2662016-10-10 07:27:42 -070037
Tom Cherry2a6811b2019-12-11 12:56:01 -080038static void PmsgOpen() {
39 auto lock = std::unique_lock{pmsg_fd_lock};
40 if (pmsg_fd > 0) {
41 // Someone raced us and opened the socket already.
42 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080043 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080044
Tom Cherry2a6811b2019-12-11 12:56:01 -080045 pmsg_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080046}
47
Tom Cherry21bb36c2020-01-08 15:18:26 -080048void PmsgClose() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080049 auto lock = std::unique_lock{pmsg_fd_lock};
50 if (pmsg_fd > 0) {
51 close(pmsg_fd);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080052 }
Tom Cherry2a6811b2019-12-11 12:56:01 -080053 pmsg_fd = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080054}
55
Tom Cherry21bb36c2020-01-08 15:18:26 -080056int PmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057 static const unsigned headerLength = 2;
58 struct iovec newVec[nr + headerLength];
59 android_log_header_t header;
60 android_pmsg_log_header_t pmsgHeader;
61 size_t i, payloadSize;
62 ssize_t ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080063
Tom Cherry2a6811b2019-12-11 12:56:01 -080064 if (!__android_log_is_debuggable()) {
65 if (logId != LOG_ID_EVENTS && logId != LOG_ID_SECURITY) {
66 return -1;
Mark Salyzyn7ef52492016-03-25 15:50:46 -070067 }
68
Tom Cherry2a6811b2019-12-11 12:56:01 -080069 if (logId == LOG_ID_EVENTS) {
70 if (vec[0].iov_len < 4) {
71 return -EINVAL;
72 }
73
74 if (SNET_EVENT_LOG_TAG != *static_cast<uint32_t*>(vec[0].iov_base)) {
75 return -EPERM;
76 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080077 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080078 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080079
Tom Cherry2a6811b2019-12-11 12:56:01 -080080 auto lock = std::shared_lock{pmsg_fd_lock};
81
82 if (pmsg_fd <= 0) {
83 lock.unlock();
84 PmsgOpen();
85 lock.lock();
86 }
87
88 if (pmsg_fd <= 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089 return -EBADF;
90 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080091
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080092 /*
93 * struct {
94 * // what we provide to pstore
95 * android_pmsg_log_header_t pmsgHeader;
96 * // what we provide to file
97 * android_log_header_t header;
98 * // caller provides
99 * union {
100 * struct {
101 * char prio;
102 * char payload[];
103 * } string;
104 * struct {
105 * uint32_t tag
106 * char payload[];
107 * } binary;
108 * };
109 * };
110 */
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800111
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800112 pmsgHeader.magic = LOGGER_MAGIC;
113 pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
Tom Cherryb47aa2a2020-01-08 15:34:14 -0800114 pmsgHeader.uid = getuid();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800115 pmsgHeader.pid = getpid();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800116
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800117 header.id = logId;
118 header.tid = gettid();
119 header.realtime.tv_sec = ts->tv_sec;
120 header.realtime.tv_nsec = ts->tv_nsec;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800121
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800122 newVec[0].iov_base = (unsigned char*)&pmsgHeader;
123 newVec[0].iov_len = sizeof(pmsgHeader);
124 newVec[1].iov_base = (unsigned char*)&header;
125 newVec[1].iov_len = sizeof(header);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800126
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800127 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
128 newVec[i].iov_base = vec[i - headerLength].iov_base;
129 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
130
131 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
132 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
133 if (newVec[i].iov_len) {
134 ++i;
135 }
136 payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
137 break;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800138 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800139 }
140 pmsgHeader.len += payloadSize;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800141
Tom Cherry2a6811b2019-12-11 12:56:01 -0800142 ret = TEMP_FAILURE_RETRY(writev(pmsg_fd, newVec, i));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800143 if (ret < 0) {
144 ret = errno ? -errno : -ENOTCONN;
145 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800146
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800147 if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
148 ret -= sizeof(header) - sizeof(pmsgHeader);
149 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800150
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800151 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800152}
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800153
154/*
155 * Virtual pmsg filesystem
156 *
157 * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
158 * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
159 * file.
160 *
161 * Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
162 */
163
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800164static inline const char* strnrchr(const char* buf, size_t len, char c) {
165 const char* cp = buf + len;
166 while ((--cp > buf) && (*cp != c))
167 ;
168 if (cp <= buf) {
169 return buf + len;
170 }
171 return cp;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800172}
173
174/* Write a buffer as filename references (tag = <basedir>:<basename>) */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800175ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio, const char* filename,
176 const char* buf, size_t len) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800177 size_t length, packet_len;
178 const char* tag;
179 char *cp, *slash;
180 struct timespec ts;
181 struct iovec vec[3];
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800182
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800183 /* Make sure the logId value is not a bad idea */
184 if ((logId == LOG_ID_KERNEL) || /* Verbotten */
185 (logId == LOG_ID_EVENTS) || /* Do not support binary content */
186 (logId == LOG_ID_SECURITY) || /* Bad idea to allow */
187 ((unsigned)logId >= 32)) { /* fit within logMask on arch32 */
188 return -EINVAL;
189 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800190
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800191 clock_gettime(android_log_clockid(), &ts);
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800192
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800193 cp = strdup(filename);
194 if (!cp) {
195 return -ENOMEM;
196 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800197
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800198 tag = cp;
199 slash = strrchr(cp, '/');
200 if (slash) {
201 *slash = ':';
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800202 slash = strrchr(cp, '/');
203 if (slash) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 tag = slash + 1;
205 }
206 }
207
208 length = strlen(tag) + 1;
209 packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
210
211 vec[0].iov_base = &prio;
212 vec[0].iov_len = sizeof(char);
213 vec[1].iov_base = (unsigned char*)tag;
214 vec[1].iov_len = length;
215
Tom Cherry71ba1642019-01-10 10:37:36 -0800216 for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800217 ssize_t ret;
218 size_t transfer;
219
Tom Cherry71ba1642019-01-10 10:37:36 -0800220 if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800221 len -= length;
222 break;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800223 }
224
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800225 transfer = length;
226 if (transfer > packet_len) {
227 transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
228 if ((transfer < length) && (buf[transfer] == '\n')) {
229 ++transfer;
230 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800231 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800232
233 vec[2].iov_base = (unsigned char*)buf;
234 vec[2].iov_len = transfer;
235
Tom Cherry2a6811b2019-12-11 12:56:01 -0800236 ret = PmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800237
238 if (ret <= 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800239 free(cp);
240 return ret ? ret : (len - length);
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700241 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800242 length -= transfer;
243 buf += transfer;
244 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 free(cp);
246 return len;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800247}