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