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