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