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