| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018, The Android Open Source Project | 
|  | 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 | */ | 
|  | 16 | #include "statsd_writer.h" | 
|  | 17 |  | 
| Howard Ro | d173009 | 2018-10-22 17:37:42 -0700 | [diff] [blame] | 18 | #include <cutils/fs.h> | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 19 | #include <cutils/sockets.h> | 
| Howard Ro | d173009 | 2018-10-22 17:37:42 -0700 | [diff] [blame] | 20 | #include <cutils/threads.h> | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
|  | 22 | #include <fcntl.h> | 
|  | 23 | #include <inttypes.h> | 
|  | 24 | #include <poll.h> | 
|  | 25 | #include <private/android_filesystem_config.h> | 
|  | 26 | #include <private/android_logger.h> | 
|  | 27 | #include <stdarg.h> | 
|  | 28 | #include <stdatomic.h> | 
|  | 29 | #include <stdio.h> | 
|  | 30 | #include <stdlib.h> | 
|  | 31 | #include <string.h> | 
|  | 32 | #include <sys/stat.h> | 
|  | 33 | #include <sys/types.h> | 
| Tom Cherry | 6f6ef39 | 2019-01-16 14:17:08 -0800 | [diff] [blame] | 34 | #include <sys/uio.h> | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 35 | #include <sys/un.h> | 
|  | 36 | #include <time.h> | 
|  | 37 | #include <unistd.h> | 
|  | 38 |  | 
|  | 39 | /* branchless on many architectures. */ | 
|  | 40 | #define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y)))) | 
|  | 41 |  | 
| Howard Ro | d173009 | 2018-10-22 17:37:42 -0700 | [diff] [blame] | 42 | #ifndef htole32 | 
|  | 43 | #if __BYTE_ORDER == __LITTLE_ENDIAN | 
|  | 44 | #define htole32(x) (x) | 
|  | 45 | #else | 
|  | 46 | #define htole32(x) __bswap_32(x) | 
|  | 47 | #endif | 
|  | 48 | #endif | 
|  | 49 |  | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 50 | #ifndef htole64 | 
|  | 51 | #if __BYTE_ORDER == __LITTLE_ENDIAN | 
|  | 52 | #define htole64(x) (x) | 
|  | 53 | #else | 
|  | 54 | #define htole64(x) __bswap_64(x) | 
|  | 55 | #endif | 
|  | 56 | #endif | 
|  | 57 |  | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 58 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; | 
| Yao Chen | 6301054 | 2018-08-20 16:15:33 -0700 | [diff] [blame] | 59 | static atomic_int dropped = 0; | 
| Yao Chen | 1d35761 | 2018-11-08 15:43:08 -0800 | [diff] [blame] | 60 | static atomic_int log_error = 0; | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 61 | static atomic_int atom_tag = 0; | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 62 |  | 
|  | 63 | void statsd_writer_init_lock() { | 
|  | 64 | /* | 
|  | 65 | * If we trigger a signal handler in the middle of locked activity and the | 
|  | 66 | * signal handler logs a message, we could get into a deadlock state. | 
|  | 67 | */ | 
|  | 68 | pthread_mutex_lock(&log_init_lock); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | int statd_writer_trylock() { | 
|  | 72 | return pthread_mutex_trylock(&log_init_lock); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | void statsd_writer_init_unlock() { | 
|  | 76 | pthread_mutex_unlock(&log_init_lock); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | static int statsdAvailable(); | 
|  | 80 | static int statsdOpen(); | 
|  | 81 | static void statsdClose(); | 
|  | 82 | static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr); | 
| Yao Chen | 6301054 | 2018-08-20 16:15:33 -0700 | [diff] [blame] | 83 | static void statsdNoteDrop(); | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | struct android_log_transport_write statsdLoggerWrite = { | 
| Yao Chen | 6301054 | 2018-08-20 16:15:33 -0700 | [diff] [blame] | 86 | .name = "statsd", | 
|  | 87 | .sock = -EBADF, | 
|  | 88 | .available = statsdAvailable, | 
|  | 89 | .open = statsdOpen, | 
|  | 90 | .close = statsdClose, | 
|  | 91 | .write = statsdWrite, | 
|  | 92 | .noteDrop = statsdNoteDrop, | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 93 | }; | 
|  | 94 |  | 
|  | 95 | /* log_init_lock assumed */ | 
|  | 96 | static int statsdOpen() { | 
|  | 97 | int i, ret = 0; | 
|  | 98 |  | 
|  | 99 | i = atomic_load(&statsdLoggerWrite.sock); | 
|  | 100 | if (i < 0) { | 
| Howard Ro | d173009 | 2018-10-22 17:37:42 -0700 | [diff] [blame] | 101 | int flags = SOCK_DGRAM; | 
|  | 102 | #ifdef SOCK_CLOEXEC | 
|  | 103 | flags |= SOCK_CLOEXEC; | 
|  | 104 | #endif | 
|  | 105 | #ifdef SOCK_NONBLOCK | 
|  | 106 | flags |= SOCK_NONBLOCK; | 
|  | 107 | #endif | 
|  | 108 | int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, flags, 0)); | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 109 | if (sock < 0) { | 
|  | 110 | ret = -errno; | 
|  | 111 | } else { | 
|  | 112 | struct sockaddr_un un; | 
|  | 113 | memset(&un, 0, sizeof(struct sockaddr_un)); | 
|  | 114 | un.sun_family = AF_UNIX; | 
|  | 115 | strcpy(un.sun_path, "/dev/socket/statsdw"); | 
|  | 116 |  | 
|  | 117 | if (TEMP_FAILURE_RETRY( | 
|  | 118 | connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) { | 
|  | 119 | ret = -errno; | 
|  | 120 | switch (ret) { | 
|  | 121 | case -ENOTCONN: | 
|  | 122 | case -ECONNREFUSED: | 
|  | 123 | case -ENOENT: | 
|  | 124 | i = atomic_exchange(&statsdLoggerWrite.sock, ret); | 
|  | 125 | /* FALLTHRU */ | 
|  | 126 | default: | 
|  | 127 | break; | 
|  | 128 | } | 
|  | 129 | close(sock); | 
|  | 130 | } else { | 
|  | 131 | ret = atomic_exchange(&statsdLoggerWrite.sock, sock); | 
|  | 132 | if ((ret >= 0) && (ret != sock)) { | 
|  | 133 | close(ret); | 
|  | 134 | } | 
|  | 135 | ret = 0; | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | return ret; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | static void __statsdClose(int negative_errno) { | 
|  | 144 | int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno); | 
|  | 145 | if (sock >= 0) { | 
|  | 146 | close(sock); | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | static void statsdClose() { | 
|  | 151 | __statsdClose(-EBADF); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static int statsdAvailable() { | 
|  | 155 | if (atomic_load(&statsdLoggerWrite.sock) < 0) { | 
|  | 156 | if (access("/dev/socket/statsdw", W_OK) == 0) { | 
|  | 157 | return 0; | 
|  | 158 | } | 
|  | 159 | return -EBADF; | 
|  | 160 | } | 
|  | 161 | return 1; | 
|  | 162 | } | 
|  | 163 |  | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 164 | static void statsdNoteDrop(int error, int tag) { | 
| Yao Chen | 6301054 | 2018-08-20 16:15:33 -0700 | [diff] [blame] | 165 | atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed); | 
| Yao Chen | 1d35761 | 2018-11-08 15:43:08 -0800 | [diff] [blame] | 166 | atomic_exchange_explicit(&log_error, error, memory_order_relaxed); | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 167 | atomic_exchange_explicit(&atom_tag, tag, memory_order_relaxed); | 
| Yao Chen | 6301054 | 2018-08-20 16:15:33 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 170 | static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) { | 
|  | 171 | ssize_t ret; | 
|  | 172 | int sock; | 
|  | 173 | static const unsigned headerLength = 1; | 
|  | 174 | struct iovec newVec[nr + headerLength]; | 
|  | 175 | android_log_header_t header; | 
|  | 176 | size_t i, payloadSize; | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | sock = atomic_load(&statsdLoggerWrite.sock); | 
|  | 179 | if (sock < 0) switch (sock) { | 
|  | 180 | case -ENOTCONN: | 
|  | 181 | case -ECONNREFUSED: | 
|  | 182 | case -ENOENT: | 
|  | 183 | break; | 
|  | 184 | default: | 
|  | 185 | return -EBADF; | 
|  | 186 | } | 
|  | 187 | /* | 
|  | 188 | *  struct { | 
|  | 189 | *      // what we provide to socket | 
|  | 190 | *      android_log_header_t header; | 
|  | 191 | *      // caller provides | 
|  | 192 | *      union { | 
|  | 193 | *          struct { | 
|  | 194 | *              char     prio; | 
|  | 195 | *              char     payload[]; | 
|  | 196 | *          } string; | 
|  | 197 | *          struct { | 
|  | 198 | *              uint32_t tag | 
|  | 199 | *              char     payload[]; | 
|  | 200 | *          } binary; | 
|  | 201 | *      }; | 
|  | 202 | *  }; | 
|  | 203 | */ | 
|  | 204 |  | 
|  | 205 | header.tid = gettid(); | 
|  | 206 | header.realtime.tv_sec = ts->tv_sec; | 
|  | 207 | header.realtime.tv_nsec = ts->tv_nsec; | 
|  | 208 |  | 
|  | 209 | newVec[0].iov_base = (unsigned char*)&header; | 
|  | 210 | newVec[0].iov_len = sizeof(header); | 
|  | 211 |  | 
|  | 212 | // If we dropped events before, try to tell statsd. | 
|  | 213 | if (sock >= 0) { | 
|  | 214 | int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed); | 
|  | 215 | if (snapshot) { | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 216 | android_log_event_long_t buffer; | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 217 | header.id = LOG_ID_STATS; | 
| Yao Chen | 1d35761 | 2018-11-08 15:43:08 -0800 | [diff] [blame] | 218 | // store the last log error in the tag field. This tag field is not used by statsd. | 
|  | 219 | buffer.header.tag = htole32(atomic_load(&log_error)); | 
| Yao Chen | 14c6db6 | 2019-03-26 13:51:53 -0700 | [diff] [blame] | 220 | buffer.payload.type = EVENT_TYPE_LONG; | 
|  | 221 | // format: | 
|  | 222 | // |atom_tag|dropped_count| | 
|  | 223 | int64_t composed_long = atomic_load(&atom_tag); | 
|  | 224 | // Send 2 int32's via an int64. | 
|  | 225 | composed_long = ((composed_long << 32) | ((int64_t)snapshot)); | 
|  | 226 | buffer.payload.data = htole64(composed_long); | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 227 |  | 
|  | 228 | newVec[headerLength].iov_base = &buffer; | 
|  | 229 | newVec[headerLength].iov_len = sizeof(buffer); | 
|  | 230 |  | 
|  | 231 | ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2)); | 
|  | 232 | if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) { | 
|  | 233 | atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed); | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | header.id = LOG_ID_STATS; | 
|  | 239 |  | 
|  | 240 | for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) { | 
|  | 241 | newVec[i].iov_base = vec[i - headerLength].iov_base; | 
|  | 242 | payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len; | 
|  | 243 |  | 
|  | 244 | if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) { | 
|  | 245 | newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD; | 
|  | 246 | if (newVec[i].iov_len) { | 
|  | 247 | ++i; | 
|  | 248 | } | 
|  | 249 | break; | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | /* | 
|  | 254 | * The write below could be lost, but will never block. | 
|  | 255 | * | 
|  | 256 | * ENOTCONN occurs if statsd has died. | 
|  | 257 | * ENOENT occurs if statsd is not running and socket is missing. | 
|  | 258 | * ECONNREFUSED occurs if we can not reconnect to statsd. | 
|  | 259 | * EAGAIN occurs if statsd is overloaded. | 
|  | 260 | */ | 
|  | 261 | if (sock < 0) { | 
|  | 262 | ret = sock; | 
|  | 263 | } else { | 
|  | 264 | ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i)); | 
|  | 265 | if (ret < 0) { | 
|  | 266 | ret = -errno; | 
|  | 267 | } | 
|  | 268 | } | 
|  | 269 | switch (ret) { | 
|  | 270 | case -ENOTCONN: | 
|  | 271 | case -ECONNREFUSED: | 
|  | 272 | case -ENOENT: | 
|  | 273 | if (statd_writer_trylock()) { | 
|  | 274 | return ret; /* in a signal handler? try again when less stressed | 
|  | 275 | */ | 
|  | 276 | } | 
|  | 277 | __statsdClose(ret); | 
|  | 278 | ret = statsdOpen(); | 
|  | 279 | statsd_writer_init_unlock(); | 
|  | 280 |  | 
|  | 281 | if (ret < 0) { | 
|  | 282 | return ret; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i)); | 
|  | 286 | if (ret < 0) { | 
|  | 287 | ret = -errno; | 
|  | 288 | } | 
|  | 289 | /* FALLTHRU */ | 
|  | 290 | default: | 
|  | 291 | break; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | if (ret > (ssize_t)sizeof(header)) { | 
|  | 295 | ret -= sizeof(header); | 
| Yao Chen | b3be9ea | 2018-05-07 16:57:13 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
|  | 298 | return ret; | 
|  | 299 | } |