blob: 5e4ff98b39e206f7c9dc93aa57910072ab359951 [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 Salyzyn018a96d2016-03-01 13:45:42 -080029#include <private/android_filesystem_config.h>
30#include <private/android_logger.h>
31
32#include "config_write.h"
33#include "log_portability.h"
34#include "logger.h"
35
36static int pmsgOpen();
37static void pmsgClose();
38static int pmsgAvailable(log_id_t logId);
39static int pmsgWrite(log_id_t logId, struct timespec *ts,
40 struct iovec *vec, size_t nr);
41
42LIBLOG_HIDDEN struct android_log_transport_write pmsgLoggerWrite = {
43 .node = { &pmsgLoggerWrite.node, &pmsgLoggerWrite.node },
44 .context.fd = -1,
45 .name = "pmsg",
46 .available = pmsgAvailable,
47 .open = pmsgOpen,
48 .close = pmsgClose,
49 .write = pmsgWrite,
50};
51
52static int pmsgOpen()
53{
Mark Salyzyndb8a2662016-10-10 07:27:42 -070054 int fd = atomic_load(&pmsgLoggerWrite.context.fd);
55 if (fd < 0) {
56 int i;
57
58 fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
59 i = atomic_exchange(&pmsgLoggerWrite.context.fd, fd);
60 if ((i >= 0) && (i != fd)) {
61 close(i);
62 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -080063 }
64
Mark Salyzyndb8a2662016-10-10 07:27:42 -070065 return fd;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080066}
67
68static void pmsgClose()
69{
Mark Salyzyndb8a2662016-10-10 07:27:42 -070070 int fd = atomic_exchange(&pmsgLoggerWrite.context.fd, -1);
71 if (fd >= 0) {
72 close(fd);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080073 }
74}
75
76static int pmsgAvailable(log_id_t logId)
77{
78 if (logId > LOG_ID_SECURITY) {
79 return -EINVAL;
80 }
Mark Salyzyn7ef52492016-03-25 15:50:46 -070081 if ((logId != LOG_ID_SECURITY) &&
82 (logId != LOG_ID_EVENTS) &&
83 !__android_log_is_debuggable()) {
84 return -EINVAL;
85 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -070086 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -080087 if (access("/dev/pmsg0", W_OK) == 0) {
88 return 0;
89 }
90 return -EBADF;
91 }
92 return 1;
93}
94
Mark Salyzyn7ef52492016-03-25 15:50:46 -070095/*
96 * Extract a 4-byte value from a byte stream.
97 */
98static inline uint32_t get4LE(const uint8_t* src)
99{
100 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
101}
102
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800103static int pmsgWrite(log_id_t logId, struct timespec *ts,
104 struct iovec *vec, size_t nr)
105{
106 static const unsigned headerLength = 2;
107 struct iovec newVec[nr + headerLength];
108 android_log_header_t header;
109 android_pmsg_log_header_t pmsgHeader;
110 size_t i, payloadSize;
111 ssize_t ret;
112
Mark Salyzyn7ef52492016-03-25 15:50:46 -0700113 if ((logId == LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
114 if (vec[0].iov_len < 4) {
115 return -EINVAL;
116 }
117
118 if (SNET_EVENT_LOG_TAG != get4LE(vec[0].iov_base)) {
119 return -EPERM;
120 }
121 }
122
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700123 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800124 return -EBADF;
125 }
126
127 /*
128 * struct {
129 * // what we provide to pstore
130 * android_pmsg_log_header_t pmsgHeader;
131 * // what we provide to file
132 * android_log_header_t header;
133 * // caller provides
134 * union {
135 * struct {
136 * char prio;
137 * char payload[];
138 * } string;
139 * struct {
140 * uint32_t tag
141 * char payload[];
142 * } binary;
143 * };
144 * };
145 */
146
147 pmsgHeader.magic = LOGGER_MAGIC;
148 pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
149 pmsgHeader.uid = __android_log_uid();
Mark Salyzynec4f5c72016-07-13 07:38:39 -0700150 pmsgHeader.pid = getpid();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800151
152 header.id = logId;
153 header.tid = gettid();
154 header.realtime.tv_sec = ts->tv_sec;
155 header.realtime.tv_nsec = ts->tv_nsec;
156
157 newVec[0].iov_base = (unsigned char *)&pmsgHeader;
158 newVec[0].iov_len = sizeof(pmsgHeader);
159 newVec[1].iov_base = (unsigned char *)&header;
160 newVec[1].iov_len = sizeof(header);
161
162 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
163 newVec[i].iov_base = vec[i - headerLength].iov_base;
164 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
165
166 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
167 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
168 if (newVec[i].iov_len) {
169 ++i;
170 }
171 payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
172 break;
173 }
174 }
175 pmsgHeader.len += payloadSize;
176
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700177 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&pmsgLoggerWrite.context.fd),
178 newVec, i));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800179 if (ret < 0) {
180 ret = errno ? -errno : -ENOTCONN;
181 }
182
183 if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
184 ret -= sizeof(header) - sizeof(pmsgHeader);
185 }
186
187 return ret;
188}
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800189
190/*
191 * Virtual pmsg filesystem
192 *
193 * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
194 * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
195 * file.
196 *
197 * Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
198 */
199
200static inline const char *strnrchr(const char *buf, size_t len, char c) {
201 const char *cp = buf + len;
202 while ((--cp > buf) && (*cp != c));
203 if (cp <= buf) {
204 return buf + len;
205 }
206 return cp;
207}
208
209/* Write a buffer as filename references (tag = <basedir>:<basename>) */
210LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_write(
211 log_id_t logId,
212 char prio,
213 const char *filename,
214 const char *buf, size_t len) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700215 bool weOpened;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800216 size_t length, packet_len;
217 const char *tag;
218 char *cp, *slash;
219 struct timespec ts;
220 struct iovec vec[3];
221
222 /* Make sure the logId value is not a bad idea */
223 if ((logId == LOG_ID_KERNEL) || /* Verbotten */
224 (logId == LOG_ID_EVENTS) || /* Do not support binary content */
225 (logId == LOG_ID_SECURITY) || /* Bad idea to allow */
226 ((unsigned)logId >= 32)) { /* fit within logMask on arch32 */
227 return -EINVAL;
228 }
229
230 clock_gettime(android_log_clockid(), &ts);
231
232 cp = strdup(filename);
233 if (!cp) {
234 return -ENOMEM;
235 }
236
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800237 tag = cp;
238 slash = strrchr(cp, '/');
239 if (slash) {
240 *slash = ':';
241 slash = strrchr(cp, '/');
242 if (slash) {
243 tag = slash + 1;
244 }
245 }
246
247 length = strlen(tag) + 1;
248 packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
249
250 vec[0].iov_base = &prio;
251 vec[0].iov_len = sizeof(char);
252 vec[1].iov_base = (unsigned char *)tag;
253 vec[1].iov_len = length;
254
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700255 weOpened = false;
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800256 for (ts.tv_nsec = 0, length = len;
257 length;
258 ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
259 ssize_t ret;
260 size_t transfer;
261
262 if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
263 ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
264 len -= length;
265 break;
266 }
267
268 transfer = length;
269 if (transfer > packet_len) {
270 transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
271 if ((transfer < length) && (buf[transfer] == '\n')) {
272 ++transfer;
273 }
274 }
275
276 vec[2].iov_base = (unsigned char *)buf;
277 vec[2].iov_len = transfer;
278
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700279 if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
280 if (!weOpened) { /* Impossible for weOpened = true here */
281 __android_log_lock();
282 }
283 weOpened = atomic_load(&pmsgLoggerWrite.context.fd) < 0;
284 if (!weOpened) {
285 __android_log_unlock();
286 } else if (pmsgOpen() < 0) {
287 __android_log_unlock();
Ting-Yuan Huang106c3e62017-02-07 15:53:32 -0800288 free(cp);
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700289 return -EBADF;
290 }
291 }
292
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800293 ret = pmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
294
295 if (ret <= 0) {
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700296 if (weOpened) {
297 pmsgClose();
298 __android_log_unlock();
299 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800300 free(cp);
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700301 return ret ? ret : (len - length);
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800302 }
303 length -= transfer;
304 buf += transfer;
305 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700306 if (weOpened) {
307 pmsgClose();
308 __android_log_unlock();
309 }
Mark Salyzynd4b061b2016-03-10 09:50:08 -0800310 free(cp);
311 return len;
312}