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 | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 87 | static int log_fds[(int)LOG_ID_MAX] = { -1, -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 | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 184 | static atomic_int_fast32_t dropped_security; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 185 | |
Mark Salyzyn | 31f7df5 | 2015-03-13 15:57:11 -0700 | [diff] [blame] | 186 | if (!nr) { |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 190 | if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */ |
| 191 | last_uid = getuid(); |
| 192 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 193 | if (last_pid == (pid_t) -1) { |
| 194 | last_pid = getpid(); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 195 | } |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 196 | if (log_id == LOG_ID_SECURITY) { |
| 197 | if ((last_uid != AID_SYSTEM) && (last_uid != AID_ROOT)) { |
| 198 | uid_t uid = geteuid(); |
| 199 | if ((uid != AID_SYSTEM) && (uid != AID_ROOT)) { |
| 200 | gid_t gid = getgid(); |
| 201 | if ((gid != AID_SYSTEM) && (gid != AID_ROOT)) { |
| 202 | gid = getegid(); |
| 203 | if ((gid != AID_SYSTEM) && (gid != AID_ROOT)) { |
| 204 | return -EPERM; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | if (!__android_log_security()) { |
| 210 | return -EPERM; |
| 211 | } |
| 212 | } |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 213 | /* |
| 214 | * struct { |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 215 | * // what we provide to pstore |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 216 | * android_pmsg_log_header_t pmsg_header; |
| 217 | * // what we provide to socket |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 218 | * android_log_header_t header; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 219 | * // caller provides |
| 220 | * union { |
| 221 | * struct { |
| 222 | * char prio; |
| 223 | * char payload[]; |
| 224 | * } string; |
| 225 | * struct { |
| 226 | * uint32_t tag |
| 227 | * char payload[]; |
| 228 | * } binary; |
| 229 | * }; |
| 230 | * }; |
| 231 | */ |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 232 | |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 233 | clock_gettime(android_log_clockid(), &ts); |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 234 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 235 | pmsg_header.magic = LOGGER_MAGIC; |
| 236 | pmsg_header.len = sizeof(pmsg_header) + sizeof(header); |
| 237 | pmsg_header.uid = last_uid; |
| 238 | pmsg_header.pid = last_pid; |
| 239 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 240 | header.tid = gettid(); |
| 241 | header.realtime.tv_sec = ts.tv_sec; |
| 242 | header.realtime.tv_nsec = ts.tv_nsec; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 243 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 244 | newVec[0].iov_base = (unsigned char *) &pmsg_header; |
| 245 | newVec[0].iov_len = sizeof(pmsg_header); |
| 246 | newVec[1].iov_base = (unsigned char *) &header; |
| 247 | newVec[1].iov_len = sizeof(header); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 248 | |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 249 | if (logd_fd > 0) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 250 | int32_t snapshot = atomic_exchange_explicit(&dropped_security, 0, |
| 251 | memory_order_relaxed); |
| 252 | if (snapshot) { |
| 253 | android_log_event_int_t buffer; |
| 254 | |
| 255 | header.id = LOG_ID_SECURITY; |
| 256 | buffer.header.tag = htole32(LIBLOG_LOG_TAG); |
| 257 | buffer.payload.type = EVENT_TYPE_INT; |
| 258 | buffer.payload.data = htole32(snapshot); |
| 259 | |
| 260 | newVec[2].iov_base = &buffer; |
| 261 | newVec[2].iov_len = sizeof(buffer); |
| 262 | |
| 263 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2)); |
| 264 | if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) { |
| 265 | atomic_fetch_add_explicit(&dropped_security, snapshot, |
| 266 | memory_order_relaxed); |
| 267 | } |
| 268 | } |
| 269 | snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed); |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 270 | if (snapshot) { |
| 271 | android_log_event_int_t buffer; |
| 272 | |
| 273 | header.id = LOG_ID_EVENTS; |
| 274 | buffer.header.tag = htole32(LIBLOG_LOG_TAG); |
| 275 | buffer.payload.type = EVENT_TYPE_INT; |
| 276 | buffer.payload.data = htole32(snapshot); |
| 277 | |
| 278 | newVec[2].iov_base = &buffer; |
| 279 | newVec[2].iov_len = sizeof(buffer); |
| 280 | |
| 281 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2)); |
| 282 | if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 283 | atomic_fetch_add_explicit(&dropped, snapshot, |
| 284 | memory_order_relaxed); |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | header.id = log_id; |
| 290 | |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 291 | for (payload_size = 0, i = header_length; i < nr + header_length; i++) { |
| 292 | newVec[i].iov_base = vec[i - header_length].iov_base; |
| 293 | payload_size += newVec[i].iov_len = vec[i - header_length].iov_len; |
| 294 | |
| 295 | if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 296 | newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD; |
| 297 | if (newVec[i].iov_len) { |
| 298 | ++i; |
| 299 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 300 | payload_size = LOGGER_ENTRY_MAX_PAYLOAD; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 301 | break; |
| 302 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 303 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 304 | pmsg_header.len += payload_size; |
| 305 | |
| 306 | if (pstore_fd >= 0) { |
| 307 | TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i)); |
| 308 | } |
| 309 | |
| 310 | if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */ |
| 311 | /* |
| 312 | * ignore log messages we send to ourself (logd). |
| 313 | * Such log messages are often generated by libraries we depend on |
| 314 | * which use standard Android logging. |
| 315 | */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if (logd_fd < 0) { |
| 320 | return -EBADF; |
| 321 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 322 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 323 | /* |
| 324 | * The write below could be lost, but will never block. |
| 325 | * |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 326 | * To logd, we drop the pmsg_header |
| 327 | * |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 328 | * ENOTCONN occurs if logd dies. |
| 329 | * EAGAIN occurs if logd is overloaded. |
| 330 | */ |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 331 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 332 | if (ret < 0) { |
| 333 | ret = -errno; |
| 334 | if (ret == -ENOTCONN) { |
Mark Salyzyn | 2d2e0a5 | 2015-11-06 12:26:52 -0800 | [diff] [blame] | 335 | lock(); |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 336 | close(logd_fd); |
| 337 | logd_fd = -1; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 338 | ret = __write_to_log_initialize(); |
Mark Salyzyn | 2d2e0a5 | 2015-11-06 12:26:52 -0800 | [diff] [blame] | 339 | unlock(); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 340 | |
| 341 | if (ret < 0) { |
| 342 | return ret; |
| 343 | } |
| 344 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 345 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 346 | if (ret < 0) { |
| 347 | ret = -errno; |
| 348 | } |
| 349 | } |
| 350 | } |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 351 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 352 | if (ret > (ssize_t)sizeof(header)) { |
| 353 | ret -= sizeof(header); |
Mark Salyzyn | d45d36e | 2015-02-12 15:14:26 -0800 | [diff] [blame] | 354 | } else if (ret == -EAGAIN) { |
| 355 | atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 356 | if (log_id == LOG_ID_SECURITY) { |
| 357 | atomic_fetch_add_explicit(&dropped_security, 1, |
| 358 | memory_order_relaxed); |
| 359 | } |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 360 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 361 | #endif |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 362 | |
| 363 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 366 | #if FAKE_LOG_DEVICE |
| 367 | static const char *LOG_NAME[LOG_ID_MAX] = { |
| 368 | [LOG_ID_MAIN] = "main", |
| 369 | [LOG_ID_RADIO] = "radio", |
| 370 | [LOG_ID_EVENTS] = "events", |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 371 | [LOG_ID_SYSTEM] = "system", |
Mark Salyzyn | 440e109 | 2014-10-10 15:13:15 -0700 | [diff] [blame] | 372 | [LOG_ID_CRASH] = "crash", |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 373 | [LOG_ID_SECURITY] = "security", |
Mark Salyzyn | 440e109 | 2014-10-10 15:13:15 -0700 | [diff] [blame] | 374 | [LOG_ID_KERNEL] = "kernel", |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 375 | }; |
| 376 | |
Adam Lesinski | a1ac84c | 2014-10-20 12:31:30 -0700 | [diff] [blame] | 377 | const char *android_log_id_to_name(log_id_t log_id) |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 378 | { |
| 379 | if (log_id >= LOG_ID_MAX) { |
| 380 | log_id = LOG_ID_MAIN; |
| 381 | } |
| 382 | return LOG_NAME[log_id]; |
| 383 | } |
| 384 | #endif |
| 385 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
| 387 | { |
Mark Salyzyn | 2d2e0a5 | 2015-11-06 12:26:52 -0800 | [diff] [blame] | 388 | lock(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 389 | |
| 390 | if (write_to_log == __write_to_log_init) { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 391 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 393 | ret = __write_to_log_initialize(); |
| 394 | if (ret < 0) { |
Mark Salyzyn | 2d2e0a5 | 2015-11-06 12:26:52 -0800 | [diff] [blame] | 395 | unlock(); |
Mark Salyzyn | 0d00a44 | 2015-03-13 12:15:57 -0700 | [diff] [blame] | 396 | #if (FAKE_LOG_DEVICE == 0) |
| 397 | if (pstore_fd >= 0) { |
| 398 | __write_to_log_daemon(log_id, vec, nr); |
| 399 | } |
| 400 | #endif |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 404 | write_to_log = __write_to_log_daemon; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Mark Salyzyn | 2d2e0a5 | 2015-11-06 12:26:52 -0800 | [diff] [blame] | 407 | unlock(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | |
| 409 | return write_to_log(log_id, vec, nr); |
| 410 | } |
| 411 | |
| 412 | int __android_log_write(int prio, const char *tag, const char *msg) |
| 413 | { |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 414 | 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] | 415 | } |
| 416 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 417 | int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg) |
| 418 | { |
| 419 | struct iovec vec[3]; |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 420 | char tmp_tag[32]; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 421 | |
| 422 | if (!tag) |
| 423 | tag = ""; |
| 424 | |
| 425 | /* XXX: This needs to go! */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 426 | if ((bufID != LOG_ID_RADIO) && |
| 427 | (!strcmp(tag, "HTC_RIL") || |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 428 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
Jeff Sharkey | 84dcf09 | 2012-08-13 11:27:30 -0700 | [diff] [blame] | 429 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 430 | !strcmp(tag, "AT") || |
| 431 | !strcmp(tag, "GSM") || |
| 432 | !strcmp(tag, "STK") || |
| 433 | !strcmp(tag, "CDMA") || |
| 434 | !strcmp(tag, "PHONE") || |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 435 | !strcmp(tag, "SMS"))) { |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 436 | bufID = LOG_ID_RADIO; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 437 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 438 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 439 | tag = tmp_tag; |
| 440 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 441 | |
Dan Albert | c7aadc4 | 2015-04-02 10:32:44 -0700 | [diff] [blame] | 442 | #if __BIONIC__ |
| 443 | if (prio == ANDROID_LOG_FATAL) { |
| 444 | android_set_abort_message(msg); |
| 445 | } |
| 446 | #endif |
| 447 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 448 | vec[0].iov_base = (unsigned char *) &prio; |
| 449 | vec[0].iov_len = 1; |
| 450 | vec[1].iov_base = (void *) tag; |
| 451 | vec[1].iov_len = strlen(tag) + 1; |
| 452 | vec[2].iov_base = (void *) msg; |
| 453 | vec[2].iov_len = strlen(msg) + 1; |
| 454 | |
| 455 | return write_to_log(bufID, vec, 3); |
| 456 | } |
| 457 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) |
| 459 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 460 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 461 | |
| 462 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 463 | |
| 464 | return __android_log_write(prio, tag, buf); |
| 465 | } |
| 466 | |
| 467 | int __android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 468 | { |
| 469 | va_list ap; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 470 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 471 | |
| 472 | va_start(ap, fmt); |
| 473 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 474 | va_end(ap); |
| 475 | |
| 476 | return __android_log_write(prio, tag, buf); |
| 477 | } |
| 478 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 479 | int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...) |
| 480 | { |
| 481 | va_list ap; |
| 482 | char buf[LOG_BUF_SIZE]; |
| 483 | |
| 484 | va_start(ap, fmt); |
| 485 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 486 | va_end(ap); |
| 487 | |
| 488 | return __android_log_buf_write(bufID, prio, tag, buf); |
| 489 | } |
| 490 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | void __android_log_assert(const char *cond, const char *tag, |
Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 492 | const char *fmt, ...) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 494 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 496 | if (fmt) { |
| 497 | va_list ap; |
| 498 | va_start(ap, fmt); |
| 499 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 500 | va_end(ap); |
| 501 | } else { |
| 502 | /* Msg not provided, log condition. N.B. Do not use cond directly as |
| 503 | * format string as it could contain spurious '%' syntax (e.g. |
| 504 | * "%d" in "blocks%devs == 0"). |
| 505 | */ |
| 506 | if (cond) |
| 507 | snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond); |
| 508 | else |
| 509 | strcpy(buf, "Unspecified assertion failed"); |
| 510 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 511 | |
| 512 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
Elliott Hughes | 02ff4b8 | 2015-03-07 11:21:37 -0800 | [diff] [blame] | 513 | abort(); /* abort so we have a chance to debug the situation */ |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 514 | /* NOTREACHED */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len) |
| 518 | { |
| 519 | struct iovec vec[2]; |
| 520 | |
| 521 | vec[0].iov_base = &tag; |
| 522 | vec[0].iov_len = sizeof(tag); |
| 523 | vec[1].iov_base = (void*)payload; |
| 524 | vec[1].iov_len = len; |
| 525 | |
| 526 | return write_to_log(LOG_ID_EVENTS, vec, 2); |
| 527 | } |
| 528 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 529 | int __android_log_security_bwrite(int32_t tag, const void *payload, size_t len) |
| 530 | { |
| 531 | struct iovec vec[2]; |
| 532 | |
| 533 | vec[0].iov_base = &tag; |
| 534 | vec[0].iov_len = sizeof(tag); |
| 535 | vec[1].iov_base = (void*)payload; |
| 536 | vec[1].iov_len = len; |
| 537 | |
| 538 | return write_to_log(LOG_ID_SECURITY, vec, 2); |
| 539 | } |
| 540 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 | /* |
| 542 | * Like __android_log_bwrite, but takes the type as well. Doesn't work |
| 543 | * for the general case where we're generating lists of stuff, but very |
| 544 | * handy if we just want to dump an integer into the log. |
| 545 | */ |
| 546 | int __android_log_btwrite(int32_t tag, char type, const void *payload, |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 547 | size_t len) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | { |
| 549 | struct iovec vec[3]; |
| 550 | |
| 551 | vec[0].iov_base = &tag; |
| 552 | vec[0].iov_len = sizeof(tag); |
| 553 | vec[1].iov_base = &type; |
| 554 | vec[1].iov_len = sizeof(type); |
| 555 | vec[2].iov_base = (void*)payload; |
| 556 | vec[2].iov_len = len; |
| 557 | |
| 558 | return write_to_log(LOG_ID_EVENTS, vec, 3); |
| 559 | } |
Nick Kralevich | 2a4d05a | 2014-07-01 10:57:16 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Like __android_log_bwrite, but used for writing strings to the |
| 563 | * event log. |
| 564 | */ |
| 565 | int __android_log_bswrite(int32_t tag, const char *payload) |
| 566 | { |
| 567 | struct iovec vec[4]; |
| 568 | char type = EVENT_TYPE_STRING; |
| 569 | uint32_t len = strlen(payload); |
| 570 | |
| 571 | vec[0].iov_base = &tag; |
| 572 | vec[0].iov_len = sizeof(tag); |
| 573 | vec[1].iov_base = &type; |
| 574 | vec[1].iov_len = sizeof(type); |
| 575 | vec[2].iov_base = &len; |
| 576 | vec[2].iov_len = sizeof(len); |
| 577 | vec[3].iov_base = (void*)payload; |
| 578 | vec[3].iov_len = len; |
| 579 | |
| 580 | return write_to_log(LOG_ID_EVENTS, vec, 4); |
| 581 | } |