blob: b3c4a1a04261da497e3b6133b4dc4e0c9e0f01a0 [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
17/*
18 * pmsg write handler
19 */
20
21#include <errno.h>
22#include <fcntl.h>
Mark Salyzyndb8a2662016-10-10 07:27:42 -070023#include <stdbool.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080024#include <stdlib.h>
25#include <string.h>
26#include <sys/types.h>
27#include <time.h>
28
Mark Salyzyn6584d0a2016-09-28 13:26:55 -070029#include <android/log.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080030#include <log/logger.h>
31
32#include <private/android_filesystem_config.h>
33#include <private/android_logger.h>
34
35#include "config_write.h"
36#include "log_portability.h"
37#include "logger.h"
38
39static int pmsgOpen();
40static void pmsgClose();
41static int pmsgAvailable(log_id_t logId);
42static int pmsgWrite(log_id_t logId, struct timespec *ts,
43 struct iovec *vec, size_t nr);
44
45LIBLOG_HIDDEN struct android_log_transport_write pmsgLoggerWrite = {
46 .node = { &pmsgLoggerWrite.node, &pmsgLoggerWrite.node },
47 .context.fd = -1,
48 .name = "pmsg",
49 .available = pmsgAvailable,
50 .open = pmsgOpen,
51 .close = pmsgClose,
52 .write = pmsgWrite,
53};
54
55static int pmsgOpen()
56{
Mark Salyzyndb8a2662016-10-10 07:27:42 -070057 int fd = atomic_load(&pmsgLoggerWrite.context.fd);
58 if (fd < 0) {
59 int i;
60
61 fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
62 i = atomic_exchange(&pmsgLoggerWrite.context.fd, fd);
63 if ((i >= 0) && (i != fd)) {
64 close(i);
65 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080066 }
67
Mark Salyzyndb8a2662016-10-10 07:27:42 -070068 return fd;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080069}
70
71static void pmsgClose()
72{
Mark Salyzyndb8a2662016-10-10 07:27:42 -070073 int fd = atomic_exchange(&pmsgLoggerWrite.context.fd, -1);
74 if (fd >= 0) {
75 close(fd);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080076 }
77}
78
79static int pmsgAvailable(log_id_t logId)
80{
81 if (logId > LOG_ID_SECURITY) {
82 return -EINVAL;
83 }
Mark Salyzyn7ef52492016-03-25 15:50:46 -070084 if ((logId != LOG_ID_SECURITY) &&
85 (logId != LOG_ID_EVENTS) &&
86 !__android_log_is_debuggable()) {
87 return -EINVAL;
88 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -070089 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -080090 if (access("/dev/pmsg0", W_OK) == 0) {
91 return 0;
92 }
93 return -EBADF;
94 }
95 return 1;
96}
97
Mark Salyzyn7ef52492016-03-25 15:50:46 -070098/*
99 * Extract a 4-byte value from a byte stream.
100 */
101static inline uint32_t get4LE(const uint8_t* src)
102{
103 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
104}
105
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800106static int pmsgWrite(log_id_t logId, struct timespec *ts,
107 struct iovec *vec, size_t nr)
108{
109 static const unsigned headerLength = 2;
110 struct iovec newVec[nr + headerLength];
111 android_log_header_t header;
112 android_pmsg_log_header_t pmsgHeader;
113 size_t i, payloadSize;
114 ssize_t ret;
115
Mark Salyzyn7ef52492016-03-25 15:50:46 -0700116 if ((logId == LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
117 if (vec[0].iov_len < 4) {
118 return -EINVAL;
119 }
120
121 if (SNET_EVENT_LOG_TAG != get4LE(vec[0].iov_base)) {
122 return -EPERM;
123 }
124 }
125
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700126 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800127 return -EBADF;
128 }
129
130 /*
131 * struct {
132 * // what we provide to pstore
133 * android_pmsg_log_header_t pmsgHeader;
134 * // what we provide to file
135 * android_log_header_t header;
136 * // caller provides
137 * union {
138 * struct {
139 * char prio;
140 * char payload[];
141 * } string;
142 * struct {
143 * uint32_t tag
144 * char payload[];
145 * } binary;
146 * };
147 * };
148 */
149
150 pmsgHeader.magic = LOGGER_MAGIC;
151 pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
152 pmsgHeader.uid = __android_log_uid();
Mark Salyzynec4f5c72016-07-13 07:38:39 -0700153 pmsgHeader.pid = getpid();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800154
155 header.id = logId;
156 header.tid = gettid();
157 header.realtime.tv_sec = ts->tv_sec;
158 header.realtime.tv_nsec = ts->tv_nsec;
159
160 newVec[0].iov_base = (unsigned char *)&pmsgHeader;
161 newVec[0].iov_len = sizeof(pmsgHeader);
162 newVec[1].iov_base = (unsigned char *)&header;
163 newVec[1].iov_len = sizeof(header);
164
165 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
166 newVec[i].iov_base = vec[i - headerLength].iov_base;
167 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
168
169 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
170 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
171 if (newVec[i].iov_len) {
172 ++i;
173 }
174 payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
175 break;
176 }
177 }
178 pmsgHeader.len += payloadSize;
179
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700180 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&pmsgLoggerWrite.context.fd),
181 newVec, i));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800182 if (ret < 0) {
183 ret = errno ? -errno : -ENOTCONN;
184 }
185
186 if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
187 ret -= sizeof(header) - sizeof(pmsgHeader);
188 }
189
190 return ret;
191}
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800192
193/*
194 * Virtual pmsg filesystem
195 *
196 * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
197 * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
198 * file.
199 *
200 * Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
201 */
202
203static inline const char *strnrchr(const char *buf, size_t len, char c) {
204 const char *cp = buf + len;
205 while ((--cp > buf) && (*cp != c));
206 if (cp <= buf) {
207 return buf + len;
208 }
209 return cp;
210}
211
212/* Write a buffer as filename references (tag = <basedir>:<basename>) */
213LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_write(
214 log_id_t logId,
215 char prio,
216 const char *filename,
217 const char *buf, size_t len) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700218 bool weOpened;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800219 size_t length, packet_len;
220 const char *tag;
221 char *cp, *slash;
222 struct timespec ts;
223 struct iovec vec[3];
224
225 /* Make sure the logId value is not a bad idea */
226 if ((logId == LOG_ID_KERNEL) || /* Verbotten */
227 (logId == LOG_ID_EVENTS) || /* Do not support binary content */
228 (logId == LOG_ID_SECURITY) || /* Bad idea to allow */
229 ((unsigned)logId >= 32)) { /* fit within logMask on arch32 */
230 return -EINVAL;
231 }
232
233 clock_gettime(android_log_clockid(), &ts);
234
235 cp = strdup(filename);
236 if (!cp) {
237 return -ENOMEM;
238 }
239
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800240 tag = cp;
241 slash = strrchr(cp, '/');
242 if (slash) {
243 *slash = ':';
244 slash = strrchr(cp, '/');
245 if (slash) {
246 tag = slash + 1;
247 }
248 }
249
250 length = strlen(tag) + 1;
251 packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
252
253 vec[0].iov_base = &prio;
254 vec[0].iov_len = sizeof(char);
255 vec[1].iov_base = (unsigned char *)tag;
256 vec[1].iov_len = length;
257
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700258 weOpened = false;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800259 for (ts.tv_nsec = 0, length = len;
260 length;
261 ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
262 ssize_t ret;
263 size_t transfer;
264
265 if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
266 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
267 len -= length;
268 break;
269 }
270
271 transfer = length;
272 if (transfer > packet_len) {
273 transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
274 if ((transfer < length) && (buf[transfer] == '\n')) {
275 ++transfer;
276 }
277 }
278
279 vec[2].iov_base = (unsigned char *)buf;
280 vec[2].iov_len = transfer;
281
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700282 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
283 if (!weOpened) { /* Impossible for weOpened = true here */
284 __android_log_lock();
285 }
286 weOpened = atomic_load(&pmsgLoggerWrite.context.fd) < 0;
287 if (!weOpened) {
288 __android_log_unlock();
289 } else if (pmsgOpen() < 0) {
290 __android_log_unlock();
291 return -EBADF;
292 }
293 }
294
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800295 ret = pmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
296
297 if (ret <= 0) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700298 if (weOpened) {
299 pmsgClose();
300 __android_log_unlock();
301 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800302 free(cp);
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700303 return ret ? ret : (len - length);
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800304 }
305 length -= transfer;
306 buf += transfer;
307 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700308 if (weOpened) {
309 pmsgClose();
310 __android_log_unlock();
311 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800312 free(cp);
313 return len;
314}