blob: 1cf8f96c644587985d4a2f12b4a62951115c0198 [file] [log] [blame]
Mark Salyzyndd9094a2016-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 Cherryd7df0982020-01-08 15:18:26 -080017#include "pmsg_writer.h"
Mark Salyzyndd9094a2016-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 Morelandcd077ee2017-04-13 23:48:57 -070026#include <log/log_properties.h>
Mark Salyzyndd9094a2016-03-01 13:45:42 -080027#include <private/android_logger.h>
28
Mark Salyzyndd9094a2016-03-01 13:45:42 -080029#include "logger.h"
Tom Cherryebebf692019-01-16 14:17:08 -080030#include "uio.h"
Mark Salyzyndd9094a2016-03-01 13:45:42 -080031
Tom Cherry32326ab2020-04-24 17:15:26 -070032static atomic_int pmsg_fd;
Mark Salyzynceee2f52016-10-10 07:27:42 -070033
Tom Cherry32326ab2020-04-24 17:15:26 -070034static void GetPmsgFd() {
Tom Cherryce8fb8f2020-11-13 11:05:56 -080035 // Note if open() fails and returns -1, that value is stored into pmsg_fd as an indication that
36 // pmsg is not available and open() should not be retried.
Tom Cherry32326ab2020-04-24 17:15:26 -070037 if (pmsg_fd != 0) {
Tom Cherrye9a284a2019-12-11 12:56:01 -080038 return;
Mark Salyzyn6e315682017-03-09 08:09:43 -080039 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -080040
Tom Cherry32326ab2020-04-24 17:15:26 -070041 int new_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
Tom Cherryce8fb8f2020-11-13 11:05:56 -080042
43 // Unlikely that new_fd is 0, but that is synonymous with our uninitialized value, and we'd prefer
44 // STDIN_FILENO != stdin, so we call open() to get a new fd value in this case.
45 if (new_fd == 0) {
46 new_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
47 close(0);
Tom Cherry32326ab2020-04-24 17:15:26 -070048 }
49
Tom Cherryce8fb8f2020-11-13 11:05:56 -080050 // pmsg_fd should only be opened once. If we see that pmsg_fd is uninitialized, we open
51 // "/dev/pmsg0" then attempt to compare/exchange it into pmsg_fd. If the compare/exchange was
52 // successful, then that will be the fd used for the duration of the program, otherwise a
53 // different thread has already opened and written the fd to the atomic, so close the new fd and
54 // return.
Tom Cherry32326ab2020-04-24 17:15:26 -070055 int uninitialized_value = 0;
56 if (!pmsg_fd.compare_exchange_strong(uninitialized_value, new_fd)) {
Tom Cherryce8fb8f2020-11-13 11:05:56 -080057 if (new_fd != -1) {
58 close(new_fd);
59 }
Tom Cherry32326ab2020-04-24 17:15:26 -070060 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -080061}
62
Tom Cherryd7df0982020-01-08 15:18:26 -080063void PmsgClose() {
Tom Cherrye9a284a2019-12-11 12:56:01 -080064 if (pmsg_fd > 0) {
65 close(pmsg_fd);
Mark Salyzyn6e315682017-03-09 08:09:43 -080066 }
Tom Cherrye9a284a2019-12-11 12:56:01 -080067 pmsg_fd = 0;
Mark Salyzyndd9094a2016-03-01 13:45:42 -080068}
69
Tom Cherryd7df0982020-01-08 15:18:26 -080070int PmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn6e315682017-03-09 08:09:43 -080071 static const unsigned headerLength = 2;
72 struct iovec newVec[nr + headerLength];
73 android_log_header_t header;
74 android_pmsg_log_header_t pmsgHeader;
75 size_t i, payloadSize;
76 ssize_t ret;
Mark Salyzyndd9094a2016-03-01 13:45:42 -080077
Alessandra Lorof2e9cce2022-09-02 15:40:36 +000078 if (!ANDROID_DEBUGGABLE) {
Tom Cherrye9a284a2019-12-11 12:56:01 -080079 if (logId != LOG_ID_EVENTS && logId != LOG_ID_SECURITY) {
80 return -1;
Mark Salyzyn509c1422016-03-25 15:50:46 -070081 }
82
Tom Cherrye9a284a2019-12-11 12:56:01 -080083 if (logId == LOG_ID_EVENTS) {
84 if (vec[0].iov_len < 4) {
85 return -EINVAL;
86 }
87
88 if (SNET_EVENT_LOG_TAG != *static_cast<uint32_t*>(vec[0].iov_base)) {
89 return -EPERM;
90 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -080091 }
Mark Salyzyn6e315682017-03-09 08:09:43 -080092 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -080093
Tom Cherry32326ab2020-04-24 17:15:26 -070094 GetPmsgFd();
Tom Cherrye9a284a2019-12-11 12:56:01 -080095
96 if (pmsg_fd <= 0) {
Mark Salyzyn6e315682017-03-09 08:09:43 -080097 return -EBADF;
98 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -080099
Mark Salyzyn6e315682017-03-09 08:09:43 -0800100 /*
101 * struct {
102 * // what we provide to pstore
103 * android_pmsg_log_header_t pmsgHeader;
104 * // what we provide to file
105 * android_log_header_t header;
106 * // caller provides
107 * union {
108 * struct {
109 * char prio;
110 * char payload[];
111 * } string;
112 * struct {
113 * uint32_t tag
114 * char payload[];
115 * } binary;
116 * };
117 * };
118 */
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800119
Mark Salyzyn6e315682017-03-09 08:09:43 -0800120 pmsgHeader.magic = LOGGER_MAGIC;
121 pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
Tom Cherry366afd82020-01-08 15:34:14 -0800122 pmsgHeader.uid = getuid();
Mark Salyzyn6e315682017-03-09 08:09:43 -0800123 pmsgHeader.pid = getpid();
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800124
Mark Salyzyn6e315682017-03-09 08:09:43 -0800125 header.id = logId;
126 header.tid = gettid();
127 header.realtime.tv_sec = ts->tv_sec;
128 header.realtime.tv_nsec = ts->tv_nsec;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800129
Mark Salyzyn6e315682017-03-09 08:09:43 -0800130 newVec[0].iov_base = (unsigned char*)&pmsgHeader;
131 newVec[0].iov_len = sizeof(pmsgHeader);
132 newVec[1].iov_base = (unsigned char*)&header;
133 newVec[1].iov_len = sizeof(header);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800134
Mark Salyzyn6e315682017-03-09 08:09:43 -0800135 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
136 newVec[i].iov_base = vec[i - headerLength].iov_base;
137 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
138
139 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
140 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
141 if (newVec[i].iov_len) {
142 ++i;
143 }
144 payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
145 break;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800146 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800147 }
148 pmsgHeader.len += payloadSize;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800149
Tom Cherrye9a284a2019-12-11 12:56:01 -0800150 ret = TEMP_FAILURE_RETRY(writev(pmsg_fd, newVec, i));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800151 if (ret < 0) {
152 ret = errno ? -errno : -ENOTCONN;
153 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800154
Mark Salyzyn6e315682017-03-09 08:09:43 -0800155 if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
156 ret -= sizeof(header) - sizeof(pmsgHeader);
157 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800158
Mark Salyzyn6e315682017-03-09 08:09:43 -0800159 return ret;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800160}
Mark Salyzynad655022016-03-10 09:50:08 -0800161
162/*
163 * Virtual pmsg filesystem
164 *
165 * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
166 * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
167 * file.
168 *
169 * Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
170 */
171
Mark Salyzyn6e315682017-03-09 08:09:43 -0800172static inline const char* strnrchr(const char* buf, size_t len, char c) {
173 const char* cp = buf + len;
174 while ((--cp > buf) && (*cp != c))
175 ;
176 if (cp <= buf) {
177 return buf + len;
178 }
179 return cp;
Mark Salyzynad655022016-03-10 09:50:08 -0800180}
181
182/* Write a buffer as filename references (tag = <basedir>:<basename>) */
Tom Cherry3d6a8782019-02-08 11:46:19 -0800183ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio, const char* filename,
184 const char* buf, size_t len) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800185 size_t length, packet_len;
186 const char* tag;
187 char *cp, *slash;
188 struct timespec ts;
189 struct iovec vec[3];
Mark Salyzynad655022016-03-10 09:50:08 -0800190
Mark Salyzyn6e315682017-03-09 08:09:43 -0800191 /* Make sure the logId value is not a bad idea */
192 if ((logId == LOG_ID_KERNEL) || /* Verbotten */
193 (logId == LOG_ID_EVENTS) || /* Do not support binary content */
194 (logId == LOG_ID_SECURITY) || /* Bad idea to allow */
195 ((unsigned)logId >= 32)) { /* fit within logMask on arch32 */
196 return -EINVAL;
197 }
Mark Salyzynad655022016-03-10 09:50:08 -0800198
Tom Cherrya5ff7ae2020-04-08 14:36:05 -0700199 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzynad655022016-03-10 09:50:08 -0800200
Mark Salyzyn6e315682017-03-09 08:09:43 -0800201 cp = strdup(filename);
202 if (!cp) {
203 return -ENOMEM;
204 }
Mark Salyzynad655022016-03-10 09:50:08 -0800205
Mark Salyzyn6e315682017-03-09 08:09:43 -0800206 tag = cp;
207 slash = strrchr(cp, '/');
208 if (slash) {
209 *slash = ':';
Mark Salyzynad655022016-03-10 09:50:08 -0800210 slash = strrchr(cp, '/');
211 if (slash) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800212 tag = slash + 1;
213 }
214 }
215
216 length = strlen(tag) + 1;
217 packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
218
219 vec[0].iov_base = &prio;
220 vec[0].iov_len = sizeof(char);
221 vec[1].iov_base = (unsigned char*)tag;
222 vec[1].iov_len = length;
223
Tom Cherryf623f022019-01-10 10:37:36 -0800224 for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800225 ssize_t ret;
226 size_t transfer;
227
Tom Cherryf623f022019-01-10 10:37:36 -0800228 if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800229 len -= length;
230 break;
Mark Salyzynad655022016-03-10 09:50:08 -0800231 }
232
Mark Salyzyn6e315682017-03-09 08:09:43 -0800233 transfer = length;
234 if (transfer > packet_len) {
235 transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
236 if ((transfer < length) && (buf[transfer] == '\n')) {
237 ++transfer;
238 }
Mark Salyzynad655022016-03-10 09:50:08 -0800239 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800240
241 vec[2].iov_base = (unsigned char*)buf;
242 vec[2].iov_len = transfer;
243
Tom Cherrye9a284a2019-12-11 12:56:01 -0800244 ret = PmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800245
246 if (ret <= 0) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800247 free(cp);
248 return ret ? ret : (len - length);
Mark Salyzynceee2f52016-10-10 07:27:42 -0700249 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800250 length -= transfer;
251 buf += transfer;
252 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800253 free(cp);
254 return len;
Mark Salyzynad655022016-03-10 09:50:08 -0800255}