The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | e9c4196 | 2014-01-02 13:52:29 -0800 | [diff] [blame] | 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | */ |
Mark Salyzyn | 31dd00f | 2015-03-04 17:01:30 -0800 | [diff] [blame] | 16 | #if (FAKE_LOG_DEVICE == 0) |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 17 | #include <endian.h> |
Mark Salyzyn | 31dd00f | 2015-03-04 17:01:30 -0800 | [diff] [blame] | 18 | #endif |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 21 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <stdarg.h> |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 25 | #include <stdatomic.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Nick Kralevich | a170322 | 2013-03-15 09:45:12 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 30 | #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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | |
Dan Albert | c68fedc | 2014-08-18 17:29:34 -0700 | [diff] [blame] | 38 | #ifdef __BIONIC__ |
| 39 | #include <android/set_abort_message.h> |
| 40 | #endif |
| 41 | |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 42 | #include <log/logd.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 43 | #include <log/logger.h> |
| 44 | #include <log/log_read.h> |
| 45 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 46 | #include <private/android_logger.h> |
Mark Salyzyn | e9c4196 | 2014-01-02 13:52:29 -0800 | [diff] [blame] | 47 | |
Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 48 | #define LOG_BUF_SIZE 1024 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
| 50 | #if FAKE_LOG_DEVICE |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 51 | /* This will be defined when building for the host. */ |
Kristian Monsen | b5a9890 | 2014-01-28 11:26:56 -0800 | [diff] [blame] | 52 | #include "fake_log_device.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | #endif |
| 54 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 56 | static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init; |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 57 | #if !defined(_WIN32) |
| 58 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; |
| 59 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 61 | #ifndef __unused |
| 62 | #define __unused __attribute__((__unused__)) |
| 63 | #endif |
Kristian Monsen | b5a9890 | 2014-01-28 11:26:56 -0800 | [diff] [blame] | 64 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 65 | #if FAKE_LOG_DEVICE |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 66 | static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 }; |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 67 | #else |
| 68 | static int logd_fd = -1; |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 69 | static int pstore_fd = -1; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 70 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * This is used by the C++ code to decide if it should write logs through |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 74 | * the C code. Basically, if /dev/socket/logd is available, we're running in |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | * the simulator rather than a desktop tool and want to use the device. |
| 76 | */ |
| 77 | static enum { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 78 | kLogUninitialized, kLogNotAvailable, kLogAvailable |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | } g_log_status = kLogUninitialized; |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | int __android_log_dev_available(void) |
| 82 | { |
| 83 | if (g_log_status == kLogUninitialized) { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 84 | if (access("/dev/socket/logdw", W_OK) == 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | g_log_status = kLogAvailable; |
| 86 | else |
| 87 | g_log_status = kLogNotAvailable; |
| 88 | } |
| 89 | |
| 90 | return (g_log_status == kLogAvailable); |
| 91 | } |
| 92 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 93 | /* log_init_lock assumed */ |
| 94 | static int __write_to_log_initialize() |
| 95 | { |
| 96 | int i, ret = 0; |
| 97 | |
| 98 | #if FAKE_LOG_DEVICE |
| 99 | for (i = 0; i < LOG_ID_MAX; i++) { |
| 100 | char buf[sizeof("/dev/log_system")]; |
| 101 | snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i)); |
| 102 | log_fds[i] = fakeLogOpen(buf, O_WRONLY); |
| 103 | } |
| 104 | #else |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 105 | if (pstore_fd < 0) { |
| 106 | pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 109 | if (logd_fd < 0) { |
| 110 | i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 111 | if (i < 0) { |
| 112 | ret = -errno; |
| 113 | } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 114 | ret = -errno; |
| 115 | close(i); |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 116 | } else { |
| 117 | struct sockaddr_un un; |
| 118 | memset(&un, 0, sizeof(struct sockaddr_un)); |
| 119 | un.sun_family = AF_UNIX; |
| 120 | strcpy(un.sun_path, "/dev/socket/logdw"); |
| 121 | |
| 122 | if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un, |
| 123 | sizeof(struct sockaddr_un))) < 0) { |
| 124 | ret = -errno; |
| 125 | close(i); |
| 126 | } else { |
| 127 | logd_fd = i; |
| 128 | } |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 131 | #endif |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 136 | static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | { |
| 138 | ssize_t ret; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 139 | #if FAKE_LOG_DEVICE |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | int log_fd; |
| 141 | |
| 142 | if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) { |
| 143 | log_fd = log_fds[(int)log_id]; |
| 144 | } else { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 145 | return -EBADF; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | do { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 148 | ret = fakeLogWritev(log_fd, vec, nr); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 149 | if (ret < 0) { |
| 150 | ret = -errno; |
| 151 | } |
| 152 | } while (ret == -EINTR); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 153 | #else |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 154 | static const unsigned header_length = 2; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 155 | struct iovec newVec[nr + header_length]; |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 156 | android_log_header_t header; |
| 157 | android_pmsg_log_header_t pmsg_header; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 158 | struct timespec ts; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 159 | size_t i, payload_size; |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 160 | static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */ |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 161 | static pid_t last_pid = (pid_t) -1; |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 162 | static atomic_int_fast32_t dropped; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 163 | |
Mark Salyzyn | 31f7df5 | 2015-03-13 15:57:11 -0700 | [diff] [blame] | 164 | if (!nr) { |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 168 | if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */ |
| 169 | last_uid = getuid(); |
| 170 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 171 | if (last_pid == (pid_t) -1) { |
| 172 | last_pid = getpid(); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 173 | } |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 174 | /* |
| 175 | * struct { |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 176 | * // what we provide to pstore |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 177 | * android_pmsg_log_header_t pmsg_header; |
| 178 | * // what we provide to socket |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 179 | * android_log_header_t header; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 180 | * // caller provides |
| 181 | * union { |
| 182 | * struct { |
| 183 | * char prio; |
| 184 | * char payload[]; |
| 185 | * } string; |
| 186 | * struct { |
| 187 | * uint32_t tag |
| 188 | * char payload[]; |
| 189 | * } binary; |
| 190 | * }; |
| 191 | * }; |
| 192 | */ |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 193 | |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 194 | if (android_log_timestamp() == 'm') { |
| 195 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 196 | } else { |
| 197 | clock_gettime(CLOCK_REALTIME, &ts); |
| 198 | } |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 199 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 200 | pmsg_header.magic = LOGGER_MAGIC; |
| 201 | pmsg_header.len = sizeof(pmsg_header) + sizeof(header); |
| 202 | pmsg_header.uid = last_uid; |
| 203 | pmsg_header.pid = last_pid; |
| 204 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 205 | header.tid = gettid(); |
| 206 | header.realtime.tv_sec = ts.tv_sec; |
| 207 | header.realtime.tv_nsec = ts.tv_nsec; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 208 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 209 | newVec[0].iov_base = (unsigned char *) &pmsg_header; |
| 210 | newVec[0].iov_len = sizeof(pmsg_header); |
| 211 | newVec[1].iov_base = (unsigned char *) &header; |
| 212 | newVec[1].iov_len = sizeof(header); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 213 | |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 214 | if (logd_fd > 0) { |
| 215 | int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed); |
| 216 | if (snapshot) { |
| 217 | android_log_event_int_t buffer; |
| 218 | |
| 219 | header.id = LOG_ID_EVENTS; |
| 220 | buffer.header.tag = htole32(LIBLOG_LOG_TAG); |
| 221 | buffer.payload.type = EVENT_TYPE_INT; |
| 222 | buffer.payload.data = htole32(snapshot); |
| 223 | |
| 224 | newVec[2].iov_base = &buffer; |
| 225 | newVec[2].iov_len = sizeof(buffer); |
| 226 | |
| 227 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2)); |
| 228 | if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) { |
| 229 | atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | header.id = log_id; |
| 235 | |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 236 | for (payload_size = 0, i = header_length; i < nr + header_length; i++) { |
| 237 | newVec[i].iov_base = vec[i - header_length].iov_base; |
| 238 | payload_size += newVec[i].iov_len = vec[i - header_length].iov_len; |
| 239 | |
| 240 | if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 241 | newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD; |
| 242 | if (newVec[i].iov_len) { |
| 243 | ++i; |
| 244 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 245 | payload_size = LOGGER_ENTRY_MAX_PAYLOAD; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 246 | break; |
| 247 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 248 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 249 | pmsg_header.len += payload_size; |
| 250 | |
| 251 | if (pstore_fd >= 0) { |
| 252 | TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i)); |
| 253 | } |
| 254 | |
| 255 | if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */ |
| 256 | /* |
| 257 | * ignore log messages we send to ourself (logd). |
| 258 | * Such log messages are often generated by libraries we depend on |
| 259 | * which use standard Android logging. |
| 260 | */ |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | if (logd_fd < 0) { |
| 265 | return -EBADF; |
| 266 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 267 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 268 | /* |
| 269 | * The write below could be lost, but will never block. |
| 270 | * |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 271 | * To logd, we drop the pmsg_header |
| 272 | * |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 273 | * ENOTCONN occurs if logd dies. |
| 274 | * EAGAIN occurs if logd is overloaded. |
| 275 | */ |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 276 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 277 | if (ret < 0) { |
| 278 | ret = -errno; |
| 279 | if (ret == -ENOTCONN) { |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 280 | #if !defined(_WIN32) |
| 281 | pthread_mutex_lock(&log_init_lock); |
| 282 | #endif |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 283 | close(logd_fd); |
| 284 | logd_fd = -1; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 285 | ret = __write_to_log_initialize(); |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 286 | #if !defined(_WIN32) |
| 287 | pthread_mutex_unlock(&log_init_lock); |
| 288 | #endif |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 289 | |
| 290 | if (ret < 0) { |
| 291 | return ret; |
| 292 | } |
| 293 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 294 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 295 | if (ret < 0) { |
| 296 | ret = -errno; |
| 297 | } |
| 298 | } |
| 299 | } |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 300 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 301 | if (ret > (ssize_t)sizeof(header)) { |
| 302 | ret -= sizeof(header); |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 303 | } else if (ret == -EAGAIN) { |
| 304 | atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed); |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 305 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 306 | #endif |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 307 | |
| 308 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 311 | #if FAKE_LOG_DEVICE |
| 312 | static const char *LOG_NAME[LOG_ID_MAX] = { |
| 313 | [LOG_ID_MAIN] = "main", |
| 314 | [LOG_ID_RADIO] = "radio", |
| 315 | [LOG_ID_EVENTS] = "events", |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 316 | [LOG_ID_SYSTEM] = "system", |
Mark Salyzyn | 440e109 | 2014-10-10 15:13:15 -0700 | [diff] [blame] | 317 | [LOG_ID_CRASH] = "crash", |
| 318 | [LOG_ID_KERNEL] = "kernel", |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
Adam Lesinski | a1ac84c | 2014-10-20 12:31:30 -0700 | [diff] [blame] | 321 | const char *android_log_id_to_name(log_id_t log_id) |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 322 | { |
| 323 | if (log_id >= LOG_ID_MAX) { |
| 324 | log_id = LOG_ID_MAIN; |
| 325 | } |
| 326 | return LOG_NAME[log_id]; |
| 327 | } |
| 328 | #endif |
| 329 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
| 331 | { |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 332 | #if !defined(_WIN32) |
| 333 | pthread_mutex_lock(&log_init_lock); |
| 334 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 335 | |
| 336 | if (write_to_log == __write_to_log_init) { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 337 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 339 | ret = __write_to_log_initialize(); |
| 340 | if (ret < 0) { |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 341 | #if !defined(_WIN32) |
| 342 | pthread_mutex_unlock(&log_init_lock); |
| 343 | #endif |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 344 | #if (FAKE_LOG_DEVICE == 0) |
| 345 | if (pstore_fd >= 0) { |
| 346 | __write_to_log_daemon(log_id, vec, nr); |
| 347 | } |
| 348 | #endif |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 349 | return ret; |
| 350 | } |
| 351 | |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 352 | write_to_log = __write_to_log_daemon; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Mark Salyzyn | c2e7d49 | 2015-11-16 19:00:41 +0000 | [diff] [blame^] | 355 | #if !defined(_WIN32) |
| 356 | pthread_mutex_unlock(&log_init_lock); |
| 357 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | |
| 359 | return write_to_log(log_id, vec, nr); |
| 360 | } |
| 361 | |
| 362 | int __android_log_write(int prio, const char *tag, const char *msg) |
| 363 | { |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 364 | return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 367 | int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg) |
| 368 | { |
| 369 | struct iovec vec[3]; |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 370 | char tmp_tag[32]; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 371 | |
| 372 | if (!tag) |
| 373 | tag = ""; |
| 374 | |
| 375 | /* XXX: This needs to go! */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 376 | if ((bufID != LOG_ID_RADIO) && |
| 377 | (!strcmp(tag, "HTC_RIL") || |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 378 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
Jeff Sharkey | 84dcf09 | 2012-08-13 11:27:30 -0700 | [diff] [blame] | 379 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 380 | !strcmp(tag, "AT") || |
| 381 | !strcmp(tag, "GSM") || |
| 382 | !strcmp(tag, "STK") || |
| 383 | !strcmp(tag, "CDMA") || |
| 384 | !strcmp(tag, "PHONE") || |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 385 | !strcmp(tag, "SMS"))) { |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 386 | bufID = LOG_ID_RADIO; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 387 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 388 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 389 | tag = tmp_tag; |
| 390 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 391 | |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 392 | #if __BIONIC__ |
| 393 | if (prio == ANDROID_LOG_FATAL) { |
| 394 | android_set_abort_message(msg); |
| 395 | } |
| 396 | #endif |
| 397 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 398 | vec[0].iov_base = (unsigned char *) &prio; |
| 399 | vec[0].iov_len = 1; |
| 400 | vec[1].iov_base = (void *) tag; |
| 401 | vec[1].iov_len = strlen(tag) + 1; |
| 402 | vec[2].iov_base = (void *) msg; |
| 403 | vec[2].iov_len = strlen(msg) + 1; |
| 404 | |
| 405 | return write_to_log(bufID, vec, 3); |
| 406 | } |
| 407 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) |
| 409 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 410 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | |
| 412 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 413 | |
| 414 | return __android_log_write(prio, tag, buf); |
| 415 | } |
| 416 | |
| 417 | int __android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 418 | { |
| 419 | va_list ap; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 420 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | |
| 422 | va_start(ap, fmt); |
| 423 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 424 | va_end(ap); |
| 425 | |
| 426 | return __android_log_write(prio, tag, buf); |
| 427 | } |
| 428 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 429 | int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...) |
| 430 | { |
| 431 | va_list ap; |
| 432 | char buf[LOG_BUF_SIZE]; |
| 433 | |
| 434 | va_start(ap, fmt); |
| 435 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 436 | va_end(ap); |
| 437 | |
| 438 | return __android_log_buf_write(bufID, prio, tag, buf); |
| 439 | } |
| 440 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | void __android_log_assert(const char *cond, const char *tag, |
Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 442 | const char *fmt, ...) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 444 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 446 | if (fmt) { |
| 447 | va_list ap; |
| 448 | va_start(ap, fmt); |
| 449 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 450 | va_end(ap); |
| 451 | } else { |
| 452 | /* Msg not provided, log condition. N.B. Do not use cond directly as |
| 453 | * format string as it could contain spurious '%' syntax (e.g. |
| 454 | * "%d" in "blocks%devs == 0"). |
| 455 | */ |
| 456 | if (cond) |
| 457 | snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond); |
| 458 | else |
| 459 | strcpy(buf, "Unspecified assertion failed"); |
| 460 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 461 | |
| 462 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
Elliott Hughes | 02ff4b8 | 2015-03-07 11:21:37 -0800 | [diff] [blame] | 463 | abort(); /* abort so we have a chance to debug the situation */ |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 464 | /* NOTREACHED */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len) |
| 468 | { |
| 469 | struct iovec vec[2]; |
| 470 | |
| 471 | vec[0].iov_base = &tag; |
| 472 | vec[0].iov_len = sizeof(tag); |
| 473 | vec[1].iov_base = (void*)payload; |
| 474 | vec[1].iov_len = len; |
| 475 | |
| 476 | return write_to_log(LOG_ID_EVENTS, vec, 2); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Like __android_log_bwrite, but takes the type as well. Doesn't work |
| 481 | * for the general case where we're generating lists of stuff, but very |
| 482 | * handy if we just want to dump an integer into the log. |
| 483 | */ |
| 484 | int __android_log_btwrite(int32_t tag, char type, const void *payload, |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 485 | size_t len) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 486 | { |
| 487 | struct iovec vec[3]; |
| 488 | |
| 489 | vec[0].iov_base = &tag; |
| 490 | vec[0].iov_len = sizeof(tag); |
| 491 | vec[1].iov_base = &type; |
| 492 | vec[1].iov_len = sizeof(type); |
| 493 | vec[2].iov_base = (void*)payload; |
| 494 | vec[2].iov_len = len; |
| 495 | |
| 496 | return write_to_log(LOG_ID_EVENTS, vec, 3); |
| 497 | } |
Nick Kralevich | 2a4d05a | 2014-07-01 10:57:16 -0700 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * Like __android_log_bwrite, but used for writing strings to the |
| 501 | * event log. |
| 502 | */ |
| 503 | int __android_log_bswrite(int32_t tag, const char *payload) |
| 504 | { |
| 505 | struct iovec vec[4]; |
| 506 | char type = EVENT_TYPE_STRING; |
| 507 | uint32_t len = strlen(payload); |
| 508 | |
| 509 | 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; |
| 517 | |
| 518 | return write_to_log(LOG_ID_EVENTS, vec, 4); |
| 519 | } |