1 /*
2 * Copyright (C) 2017 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 * stderr write handler. Output is logcat-like, and responds to
19 * logcat's environment variables ANDROID_PRINTF_LOG and
20 * ANDROID_LOG_TAGS to filter output.
21 *
22 * This transport only provides a writer, that means that it does not
23 * provide an End-To-End capability as the logs are effectively _lost_
24 * to the stderr file stream. The purpose of this transport is to
25 * supply a means for command line tools to report their logging
26 * to the stderr stream, in line with all other activities.
27 */
28
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include <log/event_tag_map.h>
38 #include <log/log.h>
39 #include <log/logprint.h>
40 #include <log/uio.h>
41
42 #include "log_portability.h"
43 #include "logger.h"
44
45 static int stderrOpen();
46 static void stderrClose();
47 static int stderrAvailable(log_id_t logId);
48 static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
49 size_t nr);
50
51 struct stderrContext {
52 AndroidLogFormat* logformat;
53 #if defined(__ANDROID__)
54 EventTagMap* eventTagMap;
55 #endif
56 };
57
58 LIBLOG_HIDDEN struct android_log_transport_write stderrLoggerWrite = {
59 .node = { &stderrLoggerWrite.node, &stderrLoggerWrite.node },
60 .context.priv = NULL,
61 .name = "stderr",
62 .available = stderrAvailable,
63 .open = stderrOpen,
64 .close = stderrClose,
65 .write = stderrWrite,
66 };
67
stderrOpen()68 static int stderrOpen() {
69 struct stderrContext* ctx;
70 const char* envStr;
71 bool setFormat;
72
73 if (!stderr || (fileno(stderr) < 0)) {
74 return -EBADF;
75 }
76
77 if (stderrLoggerWrite.context.priv) {
78 return fileno(stderr);
79 }
80
81 ctx = calloc(1, sizeof(struct stderrContext));
82 if (!ctx) {
83 return -ENOMEM;
84 }
85
86 ctx->logformat = android_log_format_new();
87 if (!ctx->logformat) {
88 free(ctx);
89 return -ENOMEM;
90 }
91
92 envStr = getenv("ANDROID_PRINTF_LOG");
93 setFormat = false;
94
95 if (envStr) {
96 char* formats = strdup(envStr);
97 char* sv = NULL;
98 char* arg = formats;
99 while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) {
100 AndroidLogPrintFormat format = android_log_formatFromString(arg);
101 arg = NULL;
102 if (format == FORMAT_OFF) {
103 continue;
104 }
105 if (android_log_setPrintFormat(ctx->logformat, format) <= 0) {
106 continue;
107 }
108 setFormat = true;
109 }
110 free(formats);
111 }
112 if (!setFormat) {
113 AndroidLogPrintFormat format = android_log_formatFromString("threadtime");
114 android_log_setPrintFormat(ctx->logformat, format);
115 }
116 envStr = getenv("ANDROID_LOG_TAGS");
117 if (envStr) {
118 android_log_addFilterString(ctx->logformat, envStr);
119 }
120 stderrLoggerWrite.context.priv = ctx;
121
122 return fileno(stderr);
123 }
124
stderrClose()125 static void stderrClose() {
126 struct stderrContext* ctx = stderrLoggerWrite.context.priv;
127
128 if (ctx) {
129 stderrLoggerWrite.context.priv = NULL;
130 if (ctx->logformat) {
131 android_log_format_free(ctx->logformat);
132 ctx->logformat = NULL;
133 }
134 #if defined(__ANDROID__)
135 if (ctx->eventTagMap) {
136 android_closeEventTagMap(ctx->eventTagMap);
137 ctx->eventTagMap = NULL;
138 }
139 #endif
140 }
141 }
142
stderrAvailable(log_id_t logId)143 static int stderrAvailable(log_id_t logId) {
144 if ((logId >= LOG_ID_MAX) || (logId == LOG_ID_KERNEL)) {
145 return -EINVAL;
146 }
147 return 1;
148 }
149
stderrWrite(log_id_t logId,struct timespec * ts,struct iovec * vec,size_t nr)150 static int stderrWrite(log_id_t logId, struct timespec* ts, struct iovec* vec,
151 size_t nr) {
152 struct log_msg log_msg;
153 AndroidLogEntry entry;
154 char binaryMsgBuf[1024];
155 int err;
156 size_t i;
157 struct stderrContext* ctx = stderrLoggerWrite.context.priv;
158
159 if (!ctx) return -EBADF;
160 if (!vec || !nr) return -EINVAL;
161
162 log_msg.entry.len = 0;
163 log_msg.entry.hdr_size = sizeof(log_msg.entry);
164 log_msg.entry.pid = getpid();
165 #ifdef __BIONIC__
166 log_msg.entry.tid = gettid();
167 #else
168 log_msg.entry.tid = getpid();
169 #endif
170 log_msg.entry.sec = ts->tv_sec;
171 log_msg.entry.nsec = ts->tv_nsec;
172 log_msg.entry.lid = logId;
173 log_msg.entry.uid = __android_log_uid();
174
175 for (i = 0; i < nr; ++i) {
176 size_t len = vec[i].iov_len;
177 if ((log_msg.entry.len + len) > LOGGER_ENTRY_MAX_PAYLOAD) {
178 len = LOGGER_ENTRY_MAX_PAYLOAD - log_msg.entry.len;
179 }
180 if (!len) continue;
181 memcpy(log_msg.entry.msg + log_msg.entry.len, vec[i].iov_base, len);
182 log_msg.entry.len += len;
183 }
184
185 if ((logId == LOG_ID_EVENTS) || (logId == LOG_ID_SECURITY)) {
186 #if defined(__ANDROID__)
187 if (!ctx->eventTagMap) {
188 ctx->eventTagMap = android_openEventTagMap(NULL);
189 }
190 #endif
191 err = android_log_processBinaryLogBuffer(&log_msg.entry_v1, &entry,
192 #if defined(__ANDROID__)
193 ctx->eventTagMap,
194 #else
195 NULL,
196 #endif
197 binaryMsgBuf, sizeof(binaryMsgBuf));
198 } else {
199 err = android_log_processLogBuffer(&log_msg.entry_v1, &entry);
200 }
201
202 /* print known truncated data, in essence logcat --debug */
203 if ((err < 0) && !entry.message) return -EINVAL;
204
205 if (!android_log_shouldPrintLine(ctx->logformat, entry.tag, entry.priority)) {
206 return log_msg.entry.len;
207 }
208
209 err = android_log_printLogLine(ctx->logformat, fileno(stderr), &entry);
210 if (err < 0) return errno ? -errno : -EINVAL;
211 return log_msg.entry.len;
212 }
213