blob: 8e676bdbf6ce2c044baf3bb34d88e6f0c24c3a59 [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
Steven Moreland1f83aa42017-04-13 23:48:57 -070026#include <log/log_properties.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080027#include <private/android_logger.h>
28
Mark Salyzyn018a96d2016-03-01 13:45:42 -080029#include "logger.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080030#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080031
Tom Cherry7acfba22020-04-24 17:15:26 -070032static atomic_int pmsg_fd;
Mark Salyzyndb8a2662016-10-10 07:27:42 -070033
Tom Cherry7acfba22020-04-24 17:15:26 -070034// pmsg_fd should only beopened once. If we see that pmsg_fd is uninitialized, we open "/dev/pmsg0"
35// then attempt to compare/exchange it into pmsg_fd. If the compare/exchange was successful, then
36// that will be the fd used for the duration of the program, otherwise a different thread has
37// already opened and written the fd to the atomic, so close the new fd and return.
38static void GetPmsgFd() {
39 if (pmsg_fd != 0) {
Tom Cherry2a6811b2019-12-11 12:56:01 -080040 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080041 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080042
Tom Cherry7acfba22020-04-24 17:15:26 -070043 int new_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
44 if (new_fd <= 0) {
45 return;
46 }
47
48 int uninitialized_value = 0;
49 if (!pmsg_fd.compare_exchange_strong(uninitialized_value, new_fd)) {
50 close(new_fd);
51 return;
52 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080053}
54
Tom Cherry21bb36c2020-01-08 15:18:26 -080055void PmsgClose() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080056 if (pmsg_fd > 0) {
57 close(pmsg_fd);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080058 }
Tom Cherry2a6811b2019-12-11 12:56:01 -080059 pmsg_fd = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080060}
61
Tom Cherry21bb36c2020-01-08 15:18:26 -080062int PmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080063 static const unsigned headerLength = 2;
64 struct iovec newVec[nr + headerLength];
65 android_log_header_t header;
66 android_pmsg_log_header_t pmsgHeader;
67 size_t i, payloadSize;
68 ssize_t ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080069
Tom Cherry2a6811b2019-12-11 12:56:01 -080070 if (!__android_log_is_debuggable()) {
71 if (logId != LOG_ID_EVENTS && logId != LOG_ID_SECURITY) {
72 return -1;
Mark Salyzyn7ef52492016-03-25 15:50:46 -070073 }
74
Tom Cherry2a6811b2019-12-11 12:56:01 -080075 if (logId == LOG_ID_EVENTS) {
76 if (vec[0].iov_len < 4) {
77 return -EINVAL;
78 }
79
80 if (SNET_EVENT_LOG_TAG != *static_cast<uint32_t*>(vec[0].iov_base)) {
81 return -EPERM;
82 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080083 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080084 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080085
Tom Cherry7acfba22020-04-24 17:15:26 -070086 GetPmsgFd();
Tom Cherry2a6811b2019-12-11 12:56:01 -080087
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
Tom Cherryf2c27462020-04-08 14:36:05 -0700191 clock_gettime(CLOCK_REALTIME, &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}