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