blob: 85a4aab52165e17d7c26867d493bfb122dd551fb [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
Mark Salyzyne9c41962014-01-02 13:52:29 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08003 *
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 */
Mark Salyzyn31dd00f2015-03-04 17:01:30 -080016#if (FAKE_LOG_DEVICE == 0)
Mark Salyzynd45d36e2015-02-12 15:14:26 -080017#include <endian.h>
Mark Salyzyn31dd00f2015-03-04 17:01:30 -080018#endif
Mark Salyzyn154f4602014-02-20 14:59:07 -080019#include <errno.h>
20#include <fcntl.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080021#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <pthread.h>
23#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdarg.h>
Mark Salyzynd45d36e2015-02-12 15:14:26 -080025#include <stdatomic.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070029#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080030#include <sys/types.h>
31#if (FAKE_LOG_DEVICE == 0)
32#include <sys/socket.h>
33#include <sys/un.h>
34#endif
35#include <time.h>
36#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Dan Albertc68fedc2014-08-18 17:29:34 -070038#ifdef __BIONIC__
39#include <android/set_abort_message.h>
40#endif
41
Mark Salyzyna0140042015-03-13 06:50:32 -070042#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070043#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080044#include <log/logger.h>
45#include <log/log_read.h>
46#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080047#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080048
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080049#include "log_cdefs.h"
50
Mark Salyzyncf4aa032013-11-22 07:54:30 -080051#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
53#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080054/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080055#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#endif
57
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080059static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -080061#if !defined(_WIN32)
62static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
63
64static void lock()
65{
66 /*
67 * If we trigger a signal handler in the middle of locked activity and the
68 * signal handler logs a message, we could get into a deadlock state.
69 */
70 pthread_mutex_lock(&log_init_lock);
71}
72
Mark Salyzyna0140042015-03-13 06:50:32 -070073static int trylock()
74{
75 return pthread_mutex_trylock(&log_init_lock);
76}
77
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -080078static void unlock()
79{
80 pthread_mutex_unlock(&log_init_lock);
81}
82
83#else /* !defined(_WIN32) */
84
85#define lock() ((void)0)
Mark Salyzyna0140042015-03-13 06:50:32 -070086#define trylock() (0) /* success */
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -080087#define unlock() ((void)0)
88
89#endif /* !defined(_WIN32) */
90
Mark Salyzyn154f4602014-02-20 14:59:07 -080091#if FAKE_LOG_DEVICE
Mark Salyzyn083b0372015-12-04 10:59:45 -080092static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070093#else
94static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080095static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080096#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
98/*
99 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -0800100 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 * the simulator rather than a desktop tool and want to use the device.
102 */
103static enum {
Chris Pearson19299902010-06-02 16:25:35 -0700104 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -0800106
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800107LIBLOG_ABI_PUBLIC int __android_log_dev_available()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108{
109 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800110 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 g_log_status = kLogAvailable;
112 else
113 g_log_status = kLogNotAvailable;
114 }
115
116 return (g_log_status == kLogAvailable);
117}
118
Mark Salyzyn8245af12014-03-25 13:45:33 -0700119/* log_init_lock assumed */
120static int __write_to_log_initialize()
121{
122 int i, ret = 0;
123
124#if FAKE_LOG_DEVICE
125 for (i = 0; i < LOG_ID_MAX; i++) {
Mark Salyzyn0085a132016-02-17 09:54:47 -0800126 char buf[sizeof("/dev/log_security")];
Mark Salyzyn8245af12014-03-25 13:45:33 -0700127 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
128 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
129 }
130#else
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700131 if (pstore_fd < 0) {
132 pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700133 }
134
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700135 if (logd_fd < 0) {
136 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
137 if (i < 0) {
138 ret = -errno;
139 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700140 ret = -errno;
141 close(i);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700142 } else {
143 struct sockaddr_un un;
144 memset(&un, 0, sizeof(struct sockaddr_un));
145 un.sun_family = AF_UNIX;
146 strcpy(un.sun_path, "/dev/socket/logdw");
147
148 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
149 sizeof(struct sockaddr_un))) < 0) {
150 ret = -errno;
151 close(i);
152 } else {
153 logd_fd = i;
154 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700155 }
156 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700157#endif
158
159 return ret;
160}
161
Mark Salyzyn53016d82015-02-04 12:28:47 -0800162static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163{
164 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700165#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 int log_fd;
167
168 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
169 log_fd = log_fds[(int)log_id];
170 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700171 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800174 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700175 if (ret < 0) {
176 ret = -errno;
177 }
178 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800179#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800180 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700181 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800182 android_log_header_t header;
183 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700184 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700185 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700186 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800187 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800188 static atomic_int_fast32_t dropped;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800189 static atomic_int_fast32_t dropped_security;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700190
Mark Salyzyn31f7df52015-03-13 15:57:11 -0700191 if (!nr) {
192 return -EINVAL;
193 }
194
Mark Salyzyn076ba812014-05-29 17:53:41 -0700195 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
196 last_uid = getuid();
197 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800198 if (last_pid == (pid_t) -1) {
199 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800200 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800201 if (log_id == LOG_ID_SECURITY) {
Mark Salyzyna0140042015-03-13 06:50:32 -0700202 if (vec[0].iov_len < 4) {
203 return -EINVAL;
204 }
Mark Salyzyn9107c462016-01-26 13:02:20 -0800205 /* Matches clientHasLogCredentials() in logd */
206 if ((last_uid != AID_SYSTEM) && (last_uid != AID_ROOT) && (last_uid != AID_LOG)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800207 uid_t uid = geteuid();
Mark Salyzyn9107c462016-01-26 13:02:20 -0800208 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800209 gid_t gid = getgid();
Mark Salyzyn9107c462016-01-26 13:02:20 -0800210 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800211 gid = getegid();
Mark Salyzyn9107c462016-01-26 13:02:20 -0800212 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
213 int num_groups;
214 gid_t *groups;
215
216 num_groups = getgroups(0, NULL);
217 if (num_groups <= 0) {
218 return -EPERM;
219 }
220 groups = calloc(num_groups, sizeof(gid_t));
221 if (!groups) {
222 return -ENOMEM;
223 }
224 num_groups = getgroups(num_groups, groups);
225 while (num_groups > 0) {
226 if (groups[num_groups - 1] == AID_LOG) {
227 break;
228 }
229 --num_groups;
230 }
231 free(groups);
232 if (num_groups <= 0) {
233 return -EPERM;
234 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800235 }
236 }
237 }
238 }
239 if (!__android_log_security()) {
Mark Salyzyna0140042015-03-13 06:50:32 -0700240 atomic_store(&dropped_security, 0);
241 return -EPERM;
242 }
243 } else if (log_id == LOG_ID_EVENTS) {
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000244 static atomic_uintptr_t map;
245 int ret;
246 const char *tag;
247 EventTagMap *m, *f;
248
Mark Salyzyna0140042015-03-13 06:50:32 -0700249 if (vec[0].iov_len < 4) {
250 return -EINVAL;
251 }
252
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000253 tag = NULL;
254 f = NULL;
255 m = (EventTagMap *)atomic_load(&map);
256
257 if (!m) {
258 ret = trylock();
259 m = (EventTagMap *)atomic_load(&map); /* trylock flush cache */
Mark Salyzyna0140042015-03-13 06:50:32 -0700260 if (!m) {
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000261 m = android_openEventTagMap(EVENT_TAG_MAP_FILE);
262 if (ret) { /* trylock failed, use local copy, mark for close */
263 f = m;
264 } else {
265 if (!m) { /* One chance to open map file */
266 m = (EventTagMap *)(uintptr_t)-1LL;
Mark Salyzyna0140042015-03-13 06:50:32 -0700267 }
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000268 atomic_store(&map, (uintptr_t)m);
Mark Salyzyna0140042015-03-13 06:50:32 -0700269 }
270 }
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000271 if (!ret) { /* trylock succeeded, unlock */
272 unlock();
273 }
274 }
275 if (m && (m != (EventTagMap *)(uintptr_t)-1LL)) {
276 tag = android_lookupEventTag(
Mark Salyzyna0140042015-03-13 06:50:32 -0700277 m,
278 htole32(((uint32_t *)vec[0].iov_base)[0]));
Mark Salyzyn0ee8de32016-01-06 21:17:43 +0000279 }
280 ret = __android_log_is_loggable(ANDROID_LOG_INFO,
281 tag,
282 ANDROID_LOG_VERBOSE);
283 if (f) { /* local copy marked for close */
284 android_closeEventTagMap(f);
285 }
286 if (!ret) {
287 return -EPERM;
Mark Salyzyna0140042015-03-13 06:50:32 -0700288 }
289 } else {
290 /* Validate the incoming tag, tag content can not split across iovec */
291 char prio = ANDROID_LOG_VERBOSE;
292 const char *tag = vec[0].iov_base;
293 size_t len = vec[0].iov_len;
294 if (!tag) {
295 len = 0;
296 }
297 if (len > 0) {
298 prio = *tag;
299 if (len > 1) {
300 --len;
301 ++tag;
302 } else {
303 len = vec[1].iov_len;
304 tag = ((const char *)vec[1].iov_base);
305 if (!tag) {
306 len = 0;
307 }
308 }
309 }
310 /* tag must be nul terminated */
311 if (strnlen(tag, len) >= len) {
312 tag = NULL;
313 }
314
315 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800316 return -EPERM;
317 }
318 }
Mark Salyzyna0140042015-03-13 06:50:32 -0700319
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700320 /*
321 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800322 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800323 * android_pmsg_log_header_t pmsg_header;
324 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800325 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700326 * // caller provides
327 * union {
328 * struct {
329 * char prio;
330 * char payload[];
331 * } string;
332 * struct {
333 * uint32_t tag
334 * char payload[];
335 * } binary;
336 * };
337 * };
338 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700339
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800340 clock_gettime(android_log_clockid(), &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700341
Mark Salyzynd91ab582014-12-15 10:52:12 -0800342 pmsg_header.magic = LOGGER_MAGIC;
343 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
344 pmsg_header.uid = last_uid;
345 pmsg_header.pid = last_pid;
346
Mark Salyzyn7a809402015-01-16 13:38:07 -0800347 header.tid = gettid();
348 header.realtime.tv_sec = ts.tv_sec;
349 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800350
Mark Salyzynd91ab582014-12-15 10:52:12 -0800351 newVec[0].iov_base = (unsigned char *) &pmsg_header;
352 newVec[0].iov_len = sizeof(pmsg_header);
353 newVec[1].iov_base = (unsigned char *) &header;
354 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800355
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800356 if (logd_fd > 0) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800357 int32_t snapshot = atomic_exchange_explicit(&dropped_security, 0,
358 memory_order_relaxed);
359 if (snapshot) {
360 android_log_event_int_t buffer;
361
362 header.id = LOG_ID_SECURITY;
363 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
364 buffer.payload.type = EVENT_TYPE_INT;
365 buffer.payload.data = htole32(snapshot);
366
367 newVec[2].iov_base = &buffer;
368 newVec[2].iov_len = sizeof(buffer);
369
370 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
371 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
372 atomic_fetch_add_explicit(&dropped_security, snapshot,
373 memory_order_relaxed);
374 }
375 }
376 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
Mark Salyzyna0140042015-03-13 06:50:32 -0700377 if (snapshot && __android_log_is_loggable(ANDROID_LOG_INFO,
378 "liblog",
379 ANDROID_LOG_VERBOSE)) {
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800380 android_log_event_int_t buffer;
381
382 header.id = LOG_ID_EVENTS;
383 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
384 buffer.payload.type = EVENT_TYPE_INT;
385 buffer.payload.data = htole32(snapshot);
386
387 newVec[2].iov_base = &buffer;
388 newVec[2].iov_len = sizeof(buffer);
389
390 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
391 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800392 atomic_fetch_add_explicit(&dropped, snapshot,
393 memory_order_relaxed);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800394 }
395 }
396 }
397
398 header.id = log_id;
399
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700400 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
401 newVec[i].iov_base = vec[i - header_length].iov_base;
402 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
403
404 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
405 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
406 if (newVec[i].iov_len) {
407 ++i;
408 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800409 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700410 break;
411 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800412 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800413 pmsg_header.len += payload_size;
414
415 if (pstore_fd >= 0) {
416 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
417 }
418
419 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
420 /*
421 * ignore log messages we send to ourself (logd).
422 * Such log messages are often generated by libraries we depend on
423 * which use standard Android logging.
424 */
425 return 0;
426 }
427
428 if (logd_fd < 0) {
429 return -EBADF;
430 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800431
Mark Salyzyn8245af12014-03-25 13:45:33 -0700432 /*
433 * The write below could be lost, but will never block.
434 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800435 * To logd, we drop the pmsg_header
436 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700437 * ENOTCONN occurs if logd dies.
438 * EAGAIN occurs if logd is overloaded.
439 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800440 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700441 if (ret < 0) {
442 ret = -errno;
443 if (ret == -ENOTCONN) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800444 lock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700445 close(logd_fd);
446 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700447 ret = __write_to_log_initialize();
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800448 unlock();
Mark Salyzyn8245af12014-03-25 13:45:33 -0700449
450 if (ret < 0) {
451 return ret;
452 }
453
Mark Salyzynd91ab582014-12-15 10:52:12 -0800454 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700455 if (ret < 0) {
456 ret = -errno;
457 }
458 }
459 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700460
Mark Salyzyn7a809402015-01-16 13:38:07 -0800461 if (ret > (ssize_t)sizeof(header)) {
462 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800463 } else if (ret == -EAGAIN) {
464 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800465 if (log_id == LOG_ID_SECURITY) {
466 atomic_fetch_add_explicit(&dropped_security, 1,
467 memory_order_relaxed);
468 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700469 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800470#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700471
472 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473}
474
Mark Salyzyn154f4602014-02-20 14:59:07 -0800475#if FAKE_LOG_DEVICE
476static const char *LOG_NAME[LOG_ID_MAX] = {
477 [LOG_ID_MAIN] = "main",
478 [LOG_ID_RADIO] = "radio",
479 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700480 [LOG_ID_SYSTEM] = "system",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700481 [LOG_ID_CRASH] = "crash",
Mark Salyzyn083b0372015-12-04 10:59:45 -0800482 [LOG_ID_SECURITY] = "security",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700483 [LOG_ID_KERNEL] = "kernel",
Mark Salyzyn154f4602014-02-20 14:59:07 -0800484};
485
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800486LIBLOG_ABI_PUBLIC const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800487{
488 if (log_id >= LOG_ID_MAX) {
489 log_id = LOG_ID_MAIN;
490 }
491 return LOG_NAME[log_id];
492}
493#endif
494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
496{
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800497 lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498
499 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700500 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501
Mark Salyzyn8245af12014-03-25 13:45:33 -0700502 ret = __write_to_log_initialize();
503 if (ret < 0) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800504 unlock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700505#if (FAKE_LOG_DEVICE == 0)
506 if (pstore_fd >= 0) {
507 __write_to_log_daemon(log_id, vec, nr);
508 }
509#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700510 return ret;
511 }
512
Mark Salyzyn53016d82015-02-04 12:28:47 -0800513 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 }
515
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800516 unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517
518 return write_to_log(log_id, vec, nr);
519}
520
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800521LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char *tag,
522 const char *msg)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523{
Dan Albertc7aadc42015-04-02 10:32:44 -0700524 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525}
526
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800527LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio,
528 const char *tag, const char *msg)
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800529{
530 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800531 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800532
533 if (!tag)
534 tag = "";
535
536 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800537 if ((bufID != LOG_ID_RADIO) &&
538 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800539 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700540 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800541 !strcmp(tag, "AT") ||
542 !strcmp(tag, "GSM") ||
543 !strcmp(tag, "STK") ||
544 !strcmp(tag, "CDMA") ||
545 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800546 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800547 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700548 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800549 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
550 tag = tmp_tag;
551 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800552
Dan Albertc7aadc42015-04-02 10:32:44 -0700553#if __BIONIC__
554 if (prio == ANDROID_LOG_FATAL) {
555 android_set_abort_message(msg);
556 }
557#endif
558
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800559 vec[0].iov_base = (unsigned char *) &prio;
560 vec[0].iov_len = 1;
561 vec[1].iov_base = (void *) tag;
562 vec[1].iov_len = strlen(tag) + 1;
563 vec[2].iov_base = (void *) msg;
564 vec[2].iov_len = strlen(msg) + 1;
565
566 return write_to_log(bufID, vec, 3);
567}
568
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800569LIBLOG_ABI_PUBLIC int __android_log_vprint(int prio, const char *tag,
570 const char *fmt, va_list ap)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571{
Chris Pearson19299902010-06-02 16:25:35 -0700572 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573
574 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
575
576 return __android_log_write(prio, tag, buf);
577}
578
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800579LIBLOG_ABI_PUBLIC int __android_log_print(int prio, const char *tag,
580 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581{
582 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800583 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584
585 va_start(ap, fmt);
586 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
587 va_end(ap);
588
589 return __android_log_write(prio, tag, buf);
590}
591
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800592LIBLOG_ABI_PUBLIC int __android_log_buf_print(int bufID, int prio,
593 const char *tag,
594 const char *fmt, ...)
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800595{
596 va_list ap;
597 char buf[LOG_BUF_SIZE];
598
599 va_start(ap, fmt);
600 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
601 va_end(ap);
602
603 return __android_log_buf_write(bufID, prio, tag, buf);
604}
605
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800606LIBLOG_ABI_PUBLIC void __android_log_assert(
607 const char *cond,
608 const char *tag,
609 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610{
Chris Pearson19299902010-06-02 16:25:35 -0700611 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612
Chris Pearson19299902010-06-02 16:25:35 -0700613 if (fmt) {
614 va_list ap;
615 va_start(ap, fmt);
616 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
617 va_end(ap);
618 } else {
619 /* Msg not provided, log condition. N.B. Do not use cond directly as
620 * format string as it could contain spurious '%' syntax (e.g.
621 * "%d" in "blocks%devs == 0").
622 */
623 if (cond)
624 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
625 else
626 strcpy(buf, "Unspecified assertion failed");
627 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628
629 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800630 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700631 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632}
633
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800634LIBLOG_ABI_PUBLIC int __android_log_bwrite(int32_t tag,
635 const void *payload, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636{
637 struct iovec vec[2];
638
639 vec[0].iov_base = &tag;
640 vec[0].iov_len = sizeof(tag);
641 vec[1].iov_base = (void*)payload;
642 vec[1].iov_len = len;
643
644 return write_to_log(LOG_ID_EVENTS, vec, 2);
645}
646
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800647LIBLOG_ABI_PUBLIC int __android_log_security_bwrite(int32_t tag,
648 const void *payload,
649 size_t len)
Mark Salyzyn083b0372015-12-04 10:59:45 -0800650{
651 struct iovec vec[2];
652
653 vec[0].iov_base = &tag;
654 vec[0].iov_len = sizeof(tag);
655 vec[1].iov_base = (void*)payload;
656 vec[1].iov_len = len;
657
658 return write_to_log(LOG_ID_SECURITY, vec, 2);
659}
660
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661/*
662 * Like __android_log_bwrite, but takes the type as well. Doesn't work
663 * for the general case where we're generating lists of stuff, but very
664 * handy if we just want to dump an integer into the log.
665 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800666LIBLOG_ABI_PUBLIC int __android_log_btwrite(int32_t tag, char type,
667 const void *payload, size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668{
669 struct iovec vec[3];
670
671 vec[0].iov_base = &tag;
672 vec[0].iov_len = sizeof(tag);
673 vec[1].iov_base = &type;
674 vec[1].iov_len = sizeof(type);
675 vec[2].iov_base = (void*)payload;
676 vec[2].iov_len = len;
677
678 return write_to_log(LOG_ID_EVENTS, vec, 3);
679}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700680
681/*
682 * Like __android_log_bwrite, but used for writing strings to the
683 * event log.
684 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800685LIBLOG_ABI_PUBLIC int __android_log_bswrite(int32_t tag, const char *payload)
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700686{
687 struct iovec vec[4];
688 char type = EVENT_TYPE_STRING;
689 uint32_t len = strlen(payload);
690
691 vec[0].iov_base = &tag;
692 vec[0].iov_len = sizeof(tag);
693 vec[1].iov_base = &type;
694 vec[1].iov_len = sizeof(type);
695 vec[2].iov_base = &len;
696 vec[2].iov_len = sizeof(len);
697 vec[3].iov_base = (void*)payload;
698 vec[3].iov_len = len;
699
700 return write_to_log(LOG_ID_EVENTS, vec, 4);
701}
Rubin Xud545d292016-01-10 22:31:19 +0000702
703/*
704 * Like __android_log_security_bwrite, but used for writing strings to the
705 * security log.
706 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800707LIBLOG_ABI_PUBLIC int __android_log_security_bswrite(int32_t tag,
708 const char *payload)
Rubin Xud545d292016-01-10 22:31:19 +0000709{
710 struct iovec vec[4];
711 char type = EVENT_TYPE_STRING;
712 uint32_t len = strlen(payload);
713
714 vec[0].iov_base = &tag;
715 vec[0].iov_len = sizeof(tag);
716 vec[1].iov_base = &type;
717 vec[1].iov_len = sizeof(type);
718 vec[2].iov_base = &len;
719 vec[2].iov_len = sizeof(len);
720 vec[3].iov_base = (void*)payload;
721 vec[3].iov_len = len;
722
723 return write_to_log(LOG_ID_SECURITY, vec, 4);
724}