blob: 22c7ecaff5c458873169c8feda28e73b24256be4 [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 Cherry2ec6a532020-01-27 08:35:13 -080017#include "logger_write.h"
18
Mark Salyzyn018a96d2016-03-01 13:45:42 -080019#include <errno.h>
Tom Cherry349b0c42020-01-08 14:47:42 -080020#include <inttypes.h>
Tom Cherry69ee5dd2020-01-22 07:48:42 -080021#include <libgen.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080022#include <stdlib.h>
23#include <string.h>
24#include <sys/time.h>
25
26#ifdef __BIONIC__
27#include <android/set_abort_message.h>
28#endif
29
Tom Cherrybbb16022020-03-09 12:43:18 -070030#include <atomic>
Tom Cherry349b0c42020-01-08 14:47:42 -080031
Tom Cherryff464b12020-01-27 13:49:26 -080032#include <android-base/errno_restorer.h>
Tom Cherry121292d2020-01-14 09:52:10 -080033#include <android-base/macros.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080034#include <private/android_filesystem_config.h>
35#include <private/android_logger.h>
36
Tom Cherry349b0c42020-01-08 14:47:42 -080037#include "android/log.h"
Tom Cherry69ee5dd2020-01-22 07:48:42 -080038#include "log/log_read.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080039#include "logger.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080040#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080041
Tom Cherrye2187bf2020-01-27 15:45:52 -080042#ifdef __ANDROID__
Tom Cherry21bb36c2020-01-08 15:18:26 -080043#include "logd_writer.h"
44#include "pmsg_writer.h"
Tom Cherry97ec4ee2019-10-02 10:52:55 -070045#endif
Tom Cherry2beabe52019-10-01 13:05:58 -070046
Tom Cherry349b0c42020-01-08 14:47:42 -080047#if defined(__APPLE__)
48#include <pthread.h>
49#elif defined(__linux__) && !defined(__ANDROID__)
50#include <syscall.h>
51#elif defined(_WIN32)
52#include <windows.h>
53#endif
54
Tom Cherryff464b12020-01-27 13:49:26 -080055using android::base::ErrnoRestorer;
56
Tom Cherry21bb36c2020-01-08 15:18:26 -080057#define LOG_BUF_SIZE 1024
58
Dan Willemsen0910d2d2016-11-29 13:39:55 -080059#if defined(__ANDROID__)
Tom Cherry06e0fce2019-12-11 14:26:37 -080060static int check_log_uid_permissions() {
Tom Cherryb47aa2a2020-01-08 15:34:14 -080061 uid_t uid = getuid();
Mark Salyzync33103c2016-03-28 16:20:29 -070062
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080063 /* Matches clientHasLogCredentials() in logd */
64 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
65 uid = geteuid();
Mark Salyzync33103c2016-03-28 16:20:29 -070066 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080067 gid_t gid = getgid();
68 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
69 gid = getegid();
70 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
71 int num_groups;
72 gid_t* groups;
Mark Salyzync33103c2016-03-28 16:20:29 -070073
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080074 num_groups = getgroups(0, NULL);
75 if (num_groups <= 0) {
76 return -EPERM;
77 }
Tom Cherry71ba1642019-01-10 10:37:36 -080078 groups = static_cast<gid_t*>(calloc(num_groups, sizeof(gid_t)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080079 if (!groups) {
80 return -ENOMEM;
81 }
82 num_groups = getgroups(num_groups, groups);
83 while (num_groups > 0) {
84 if (groups[num_groups - 1] == AID_LOG) {
85 break;
Mark Salyzync33103c2016-03-28 16:20:29 -070086 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080087 --num_groups;
88 }
89 free(groups);
90 if (num_groups <= 0) {
91 return -EPERM;
92 }
Mark Salyzync33103c2016-03-28 16:20:29 -070093 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080094 }
Mark Salyzync33103c2016-03-28 16:20:29 -070095 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080096 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080097 return 0;
Mark Salyzync33103c2016-03-28 16:20:29 -070098}
Tom Cherry06e0fce2019-12-11 14:26:37 -080099#endif
Mark Salyzync33103c2016-03-28 16:20:29 -0700100
Mark Salyzyndf7a4c62016-08-23 10:23:36 -0700101/*
102 * Release any logger resources. A new log write will immediately re-acquire.
103 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800104void __android_log_close() {
Tom Cherrye2187bf2020-01-27 15:45:52 -0800105#ifdef __ANDROID__
Tom Cherry21bb36c2020-01-08 15:18:26 -0800106 LogdClose();
107 PmsgClose();
Tom Cherry21bb36c2020-01-08 15:18:26 -0800108#endif
Mark Salyzyndf7a4c62016-08-23 10:23:36 -0700109}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800110
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800111#if defined(__GLIBC__) || defined(_WIN32)
112static const char* getprogname() {
113#if defined(__GLIBC__)
114 return program_invocation_short_name;
115#elif defined(_WIN32)
116 static bool first = true;
117 static char progname[MAX_PATH] = {};
118
119 if (first) {
120 char path[PATH_MAX + 1];
121 DWORD result = GetModuleFileName(nullptr, path, sizeof(path) - 1);
122 if (result == 0 || result == sizeof(path) - 1) return "";
123 path[PATH_MAX - 1] = 0;
124
125 char* path_basename = basename(path);
126
127 snprintf(progname, sizeof(progname), "%s", path_basename);
128 first = false;
129 }
130
131 return progname;
132#endif
133}
134#endif
135
136// It's possible for logging to happen during static initialization before our globals are
137// initialized, so we place this std::string in a function such that it is initialized on the first
138// call.
Tom Cherry2ec6a532020-01-27 08:35:13 -0800139std::string& GetDefaultTag() {
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800140 static std::string default_tag = getprogname();
141 return default_tag;
142}
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800143
144void __android_log_set_default_tag(const char* tag) {
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800145 GetDefaultTag().assign(tag, 0, LOGGER_ENTRY_MAX_PAYLOAD);
146}
147
Tom Cherryf1a975b2020-03-12 11:07:07 -0700148static std::atomic_int32_t minimum_log_priority = ANDROID_LOG_DEFAULT;
149int32_t __android_log_set_minimum_priority(int32_t priority) {
Tom Cherrybbb16022020-03-09 12:43:18 -0700150 return minimum_log_priority.exchange(priority, std::memory_order_relaxed);
Tom Cherry0391a872020-01-16 15:58:02 -0800151}
152
Tom Cherryf1a975b2020-03-12 11:07:07 -0700153int32_t __android_log_get_minimum_priority() {
Tom Cherry0391a872020-01-16 15:58:02 -0800154 return minimum_log_priority;
155}
156
Tom Cherry349b0c42020-01-08 14:47:42 -0800157#ifdef __ANDROID__
158static __android_logger_function logger_function = __android_log_logd_logger;
159#else
160static __android_logger_function logger_function = __android_log_stderr_logger;
161#endif
Tom Cherry349b0c42020-01-08 14:47:42 -0800162
163void __android_log_set_logger(__android_logger_function logger) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800164 logger_function = logger;
165}
166
167void __android_log_default_aborter(const char* abort_message) {
168#ifdef __ANDROID__
169 android_set_abort_message(abort_message);
170#else
171 UNUSED(abort_message);
172#endif
173 abort();
174}
175
176static __android_aborter_function aborter_function = __android_log_default_aborter;
Tom Cherry349b0c42020-01-08 14:47:42 -0800177
178void __android_log_set_aborter(__android_aborter_function aborter) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800179 aborter_function = aborter;
180}
181
182void __android_log_call_aborter(const char* abort_message) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800183 aborter_function(abort_message);
184}
185
186#ifdef __ANDROID__
Tom Cherry06e0fce2019-12-11 14:26:37 -0800187static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
Tom Cherryff464b12020-01-27 13:49:26 -0800188 int ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800189 struct timespec ts;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800190
Tom Cherry06e0fce2019-12-11 14:26:37 -0800191 if (log_id == LOG_ID_KERNEL) {
192 return -EINVAL;
193 }
194
Tom Cherryf2c27462020-04-08 14:36:05 -0700195 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn142b43d2016-12-28 10:30:57 -0800196
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800197 if (log_id == LOG_ID_SECURITY) {
198 if (vec[0].iov_len < 4) {
199 return -EINVAL;
200 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800201
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800202 ret = check_log_uid_permissions();
203 if (ret < 0) {
204 return ret;
205 }
206 if (!__android_log_security()) {
207 /* If only we could reset downstream logd counter */
208 return -EPERM;
209 }
Yao Chen025f05a2017-12-01 15:48:19 -0800210 } else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800211 if (vec[0].iov_len < 4) {
212 return -EINVAL;
213 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800214 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800215
Tom Cherry21bb36c2020-01-08 15:18:26 -0800216 ret = LogdWrite(log_id, &ts, vec, nr);
217 PmsgWrite(log_id, &ts, vec, nr);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800218
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800219 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800220}
Tom Cherry349b0c42020-01-08 14:47:42 -0800221#else
222static int write_to_log(log_id_t, struct iovec*, size_t) {
223 // Non-Android text logs should go to __android_log_stderr_logger, not here.
224 // Non-Android binary logs are always dropped.
225 return 1;
226}
227#endif
228
229// Copied from base/threads.cpp
230static uint64_t GetThreadId() {
231#if defined(__BIONIC__)
232 return gettid();
233#elif defined(__APPLE__)
234 uint64_t tid;
235 pthread_threadid_np(NULL, &tid);
236 return tid;
237#elif defined(__linux__)
238 return syscall(__NR_gettid);
239#elif defined(_WIN32)
240 return GetCurrentThreadId();
241#endif
242}
243
Tom Cherryebf43ad2020-03-11 11:07:13 -0700244void __android_log_stderr_logger(const struct __android_log_message* log_message) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800245 struct tm now;
246 time_t t = time(nullptr);
247
248#if defined(_WIN32)
249 localtime_s(&now, &t);
250#else
251 localtime_r(&t, &now);
252#endif
253
254 char timestamp[32];
255 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
256
257 static const char log_characters[] = "XXVDIWEF";
258 static_assert(arraysize(log_characters) - 1 == ANDROID_LOG_SILENT,
259 "Mismatch in size of log_characters and values in android_LogPriority");
Tom Cherryf1a975b2020-03-12 11:07:07 -0700260 int32_t priority =
Tom Cherryebf43ad2020-03-11 11:07:13 -0700261 log_message->priority > ANDROID_LOG_SILENT ? ANDROID_LOG_FATAL : log_message->priority;
Tom Cherry349b0c42020-01-08 14:47:42 -0800262 char priority_char = log_characters[priority];
263 uint64_t tid = GetThreadId();
264
Tom Cherryebf43ad2020-03-11 11:07:13 -0700265 if (log_message->file != nullptr) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800266 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n",
Tom Cherryebf43ad2020-03-11 11:07:13 -0700267 log_message->tag ? log_message->tag : "nullptr", priority_char, timestamp, getpid(),
268 tid, log_message->file, log_message->line, log_message->message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800269 } else {
270 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s\n",
Tom Cherryebf43ad2020-03-11 11:07:13 -0700271 log_message->tag ? log_message->tag : "nullptr", priority_char, timestamp, getpid(),
272 tid, log_message->message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800273 }
274}
275
Tom Cherryebf43ad2020-03-11 11:07:13 -0700276void __android_log_logd_logger(const struct __android_log_message* log_message) {
277 int buffer_id = log_message->buffer_id == LOG_ID_DEFAULT ? LOG_ID_MAIN : log_message->buffer_id;
Tom Cherry349b0c42020-01-08 14:47:42 -0800278
279 struct iovec vec[3];
280 vec[0].iov_base =
Tom Cherryebf43ad2020-03-11 11:07:13 -0700281 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(&log_message->priority));
Tom Cherry349b0c42020-01-08 14:47:42 -0800282 vec[0].iov_len = 1;
Tom Cherryebf43ad2020-03-11 11:07:13 -0700283 vec[1].iov_base = const_cast<void*>(static_cast<const void*>(log_message->tag));
284 vec[1].iov_len = strlen(log_message->tag) + 1;
285 vec[2].iov_base = const_cast<void*>(static_cast<const void*>(log_message->message));
286 vec[2].iov_len = strlen(log_message->message) + 1;
Tom Cherry349b0c42020-01-08 14:47:42 -0800287
288 write_to_log(static_cast<log_id_t>(buffer_id), vec, 3);
289}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800290
Tom Cherry2d9779e2019-02-08 11:46:19 -0800291int __android_log_write(int prio, const char* tag, const char* msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800292 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800293}
294
Tom Cherryebf43ad2020-03-11 11:07:13 -0700295void __android_log_write_log_message(__android_log_message* log_message) {
Tom Cherryff464b12020-01-27 13:49:26 -0800296 ErrnoRestorer errno_restorer;
297
Tom Cherryebf43ad2020-03-11 11:07:13 -0700298 if (log_message->buffer_id != LOG_ID_DEFAULT && log_message->buffer_id != LOG_ID_MAIN &&
299 log_message->buffer_id != LOG_ID_SYSTEM && log_message->buffer_id != LOG_ID_RADIO &&
300 log_message->buffer_id != LOG_ID_CRASH) {
Tom Cherryf48f6852020-01-28 13:06:29 -0800301 return;
302 }
303
Tom Cherryebf43ad2020-03-11 11:07:13 -0700304 if (log_message->tag == nullptr) {
Tom Cherryebf43ad2020-03-11 11:07:13 -0700305 log_message->tag = GetDefaultTag().c_str();
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800306 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800307
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800308#if __BIONIC__
Tom Cherryebf43ad2020-03-11 11:07:13 -0700309 if (log_message->priority == ANDROID_LOG_FATAL) {
310 android_set_abort_message(log_message->message);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800311 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800312#endif
313
Tom Cherryebf43ad2020-03-11 11:07:13 -0700314 logger_function(log_message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800315}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800316
Tom Cherry349b0c42020-01-08 14:47:42 -0800317int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
Tom Cherryff464b12020-01-27 13:49:26 -0800318 ErrnoRestorer errno_restorer;
319
Tom Cherry96e7ef52020-01-22 08:20:03 -0800320 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700321 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800322 }
323
Tom Cherryebf43ad2020-03-11 11:07:13 -0700324 __android_log_message log_message = {
325 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, msg};
326 __android_log_write_log_message(&log_message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800327 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800328}
329
Tom Cherry2d9779e2019-02-08 11:46:19 -0800330int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
Tom Cherryff464b12020-01-27 13:49:26 -0800331 ErrnoRestorer errno_restorer;
332
Tom Cherry96e7ef52020-01-22 08:20:03 -0800333 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700334 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800335 }
336
Tom Cherry3574c372020-02-21 15:06:46 -0800337 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800338
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800339 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800340
Tom Cherryebf43ad2020-03-11 11:07:13 -0700341 __android_log_message log_message = {
342 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
343 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800344 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800345}
346
Tom Cherry2d9779e2019-02-08 11:46:19 -0800347int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
Tom Cherryff464b12020-01-27 13:49:26 -0800348 ErrnoRestorer errno_restorer;
349
Tom Cherry96e7ef52020-01-22 08:20:03 -0800350 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700351 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800352 }
353
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800354 va_list ap;
Tom Cherry3574c372020-02-21 15:06:46 -0800355 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800356
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800357 va_start(ap, fmt);
358 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
359 va_end(ap);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800360
Tom Cherryebf43ad2020-03-11 11:07:13 -0700361 __android_log_message log_message = {
362 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
363 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800364 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800365}
366
Tom Cherry2d9779e2019-02-08 11:46:19 -0800367int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
Tom Cherryff464b12020-01-27 13:49:26 -0800368 ErrnoRestorer errno_restorer;
369
Tom Cherry96e7ef52020-01-22 08:20:03 -0800370 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700371 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800372 }
373
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800374 va_list ap;
Tom Cherry3574c372020-02-21 15:06:46 -0800375 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800376
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800377 va_start(ap, fmt);
378 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
379 va_end(ap);
380
Tom Cherryebf43ad2020-03-11 11:07:13 -0700381 __android_log_message log_message = {
382 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, buf};
383 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800384 return 1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800385}
386
Tom Cherry2d9779e2019-02-08 11:46:19 -0800387void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...) {
Tom Cherry3574c372020-02-21 15:06:46 -0800388 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800389
390 if (fmt) {
391 va_list ap;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800392 va_start(ap, fmt);
393 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
394 va_end(ap);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800395 } else {
396 /* Msg not provided, log condition. N.B. Do not use cond directly as
397 * format string as it could contain spurious '%' syntax (e.g.
398 * "%d" in "blocks%devs == 0").
399 */
400 if (cond)
401 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
402 else
403 strcpy(buf, "Unspecified assertion failed");
404 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800405
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800406 // Log assertion failures to stderr for the benefit of "adb shell" users
407 // and gtests (http://b/23675822).
Tom Cherry6f6ef392019-01-16 14:17:08 -0800408 TEMP_FAILURE_RETRY(write(2, buf, strlen(buf)));
409 TEMP_FAILURE_RETRY(write(2, "\n", 1));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800410
411 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Tom Cherry349b0c42020-01-08 14:47:42 -0800412 __android_log_call_aborter(buf);
413 abort();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800414}
415
Tom Cherry2d9779e2019-02-08 11:46:19 -0800416int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800417 ErrnoRestorer errno_restorer;
418
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800419 struct iovec vec[2];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800420
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800421 vec[0].iov_base = &tag;
422 vec[0].iov_len = sizeof(tag);
423 vec[1].iov_base = (void*)payload;
424 vec[1].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800425
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800426 return write_to_log(LOG_ID_EVENTS, vec, 2);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800427}
428
Tom Cherry2d9779e2019-02-08 11:46:19 -0800429int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800430 ErrnoRestorer errno_restorer;
431
Stefan Lafon701a0652017-08-24 20:14:06 -0700432 struct iovec vec[2];
433
434 vec[0].iov_base = &tag;
435 vec[0].iov_len = sizeof(tag);
436 vec[1].iov_base = (void*)payload;
437 vec[1].iov_len = len;
438
439 return write_to_log(LOG_ID_STATS, vec, 2);
440}
441
Tom Cherry2d9779e2019-02-08 11:46:19 -0800442int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800443 ErrnoRestorer errno_restorer;
444
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800445 struct iovec vec[2];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800446
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800447 vec[0].iov_base = &tag;
448 vec[0].iov_len = sizeof(tag);
449 vec[1].iov_base = (void*)payload;
450 vec[1].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800451
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800452 return write_to_log(LOG_ID_SECURITY, vec, 2);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800453}
454
455/*
456 * Like __android_log_bwrite, but takes the type as well. Doesn't work
457 * for the general case where we're generating lists of stuff, but very
458 * handy if we just want to dump an integer into the log.
459 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800460int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800461 ErrnoRestorer errno_restorer;
462
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800463 struct iovec vec[3];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800464
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800465 vec[0].iov_base = &tag;
466 vec[0].iov_len = sizeof(tag);
467 vec[1].iov_base = &type;
468 vec[1].iov_len = sizeof(type);
469 vec[2].iov_base = (void*)payload;
470 vec[2].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800471
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800472 return write_to_log(LOG_ID_EVENTS, vec, 3);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800473}
474
475/*
476 * Like __android_log_bwrite, but used for writing strings to the
477 * event log.
478 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800479int __android_log_bswrite(int32_t tag, const char* payload) {
Tom Cherryff464b12020-01-27 13:49:26 -0800480 ErrnoRestorer errno_restorer;
481
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800482 struct iovec vec[4];
483 char type = EVENT_TYPE_STRING;
484 uint32_t len = strlen(payload);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800485
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800486 vec[0].iov_base = &tag;
487 vec[0].iov_len = sizeof(tag);
488 vec[1].iov_base = &type;
489 vec[1].iov_len = sizeof(type);
490 vec[2].iov_base = &len;
491 vec[2].iov_len = sizeof(len);
492 vec[3].iov_base = (void*)payload;
493 vec[3].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800494
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800495 return write_to_log(LOG_ID_EVENTS, vec, 4);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800496}
497
498/*
499 * Like __android_log_security_bwrite, but used for writing strings to the
500 * security log.
501 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800502int __android_log_security_bswrite(int32_t tag, const char* payload) {
Tom Cherryff464b12020-01-27 13:49:26 -0800503 ErrnoRestorer errno_restorer;
504
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800505 struct iovec vec[4];
506 char type = EVENT_TYPE_STRING;
507 uint32_t len = strlen(payload);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800508
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800509 vec[0].iov_base = &tag;
510 vec[0].iov_len = sizeof(tag);
511 vec[1].iov_base = &type;
512 vec[1].iov_len = sizeof(type);
513 vec[2].iov_base = &len;
514 vec[2].iov_len = sizeof(len);
515 vec[3].iov_base = (void*)payload;
516 vec[3].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800517
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800518 return write_to_log(LOG_ID_SECURITY, vec, 4);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800519}