blob: f5be95c19dedb77bbb5bc8b0f416dcc417865176 [file] [log] [blame]
Yao Chenb3be9ea2018-05-07 16:57:13 -07001/*
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 Rod1730092018-10-22 17:37:42 -070018#include <cutils/fs.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070019#include <cutils/sockets.h>
Howard Rod1730092018-10-22 17:37:42 -070020#include <cutils/threads.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070021#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 Cherry6f6ef392019-01-16 14:17:08 -080034#include <sys/uio.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070035#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 Rod1730092018-10-22 17:37:42 -070042#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 Chenb3be9ea2018-05-07 16:57:13 -070050static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Yao Chen63010542018-08-20 16:15:33 -070051static atomic_int dropped = 0;
Yao Chen1d357612018-11-08 15:43:08 -080052static atomic_int log_error = 0;
Yao Chenb3be9ea2018-05-07 16:57:13 -070053
54void 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
62int statd_writer_trylock() {
63 return pthread_mutex_trylock(&log_init_lock);
64}
65
66void statsd_writer_init_unlock() {
67 pthread_mutex_unlock(&log_init_lock);
68}
69
70static int statsdAvailable();
71static int statsdOpen();
72static void statsdClose();
73static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
Yao Chen63010542018-08-20 16:15:33 -070074static void statsdNoteDrop();
Yao Chenb3be9ea2018-05-07 16:57:13 -070075
76struct android_log_transport_write statsdLoggerWrite = {
Yao Chen63010542018-08-20 16:15:33 -070077 .name = "statsd",
78 .sock = -EBADF,
79 .available = statsdAvailable,
80 .open = statsdOpen,
81 .close = statsdClose,
82 .write = statsdWrite,
83 .noteDrop = statsdNoteDrop,
Yao Chenb3be9ea2018-05-07 16:57:13 -070084};
85
86/* log_init_lock assumed */
87static int statsdOpen() {
88 int i, ret = 0;
89
90 i = atomic_load(&statsdLoggerWrite.sock);
91 if (i < 0) {
Howard Rod1730092018-10-22 17:37:42 -070092 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 Chenb3be9ea2018-05-07 16:57:13 -0700100 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
134static void __statsdClose(int negative_errno) {
135 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
136 if (sock >= 0) {
137 close(sock);
138 }
139}
140
141static void statsdClose() {
142 __statsdClose(-EBADF);
143}
144
145static 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 Chen1d357612018-11-08 15:43:08 -0800155static void statsdNoteDrop(int error) {
Yao Chen63010542018-08-20 16:15:33 -0700156 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Yao Chen1d357612018-11-08 15:43:08 -0800157 atomic_exchange_explicit(&log_error, error, memory_order_relaxed);
Yao Chen63010542018-08-20 16:15:33 -0700158}
159
Yao Chenb3be9ea2018-05-07 16:57:13 -0700160static 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 Chenb3be9ea2018-05-07 16:57:13 -0700167
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 Chen1d357612018-11-08 15:43:08 -0800208 // 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 Chenb3be9ea2018-05-07 16:57:13 -0700210 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 Chenb3be9ea2018-05-07 16:57:13 -0700281 }
282
283 return ret;
284}