blob: e24dff0b66fe79acc5d807d1ccb88693f3472ae1 [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 Ro34bc5672018-10-15 21:42:34 -070018#include <cutils/fs.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070019#include <cutils/sockets.h>
Howard Ro34bc5672018-10-15 21:42:34 -070020#include <cutils/threads.h>
Yao Chenb3be9ea2018-05-07 16:57:13 -070021#include <endian.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <inttypes.h>
25#include <poll.h>
26#include <private/android_filesystem_config.h>
27#include <private/android_logger.h>
28#include <stdarg.h>
29#include <stdatomic.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/stat.h>
34#include <sys/types.h>
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
42static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
Yao Chen63010542018-08-20 16:15:33 -070043static atomic_int dropped = 0;
Yao Chenb3be9ea2018-05-07 16:57:13 -070044
45void statsd_writer_init_lock() {
46 /*
47 * If we trigger a signal handler in the middle of locked activity and the
48 * signal handler logs a message, we could get into a deadlock state.
49 */
50 pthread_mutex_lock(&log_init_lock);
51}
52
53int statd_writer_trylock() {
54 return pthread_mutex_trylock(&log_init_lock);
55}
56
57void statsd_writer_init_unlock() {
58 pthread_mutex_unlock(&log_init_lock);
59}
60
61static int statsdAvailable();
62static int statsdOpen();
63static void statsdClose();
64static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr);
Yao Chen63010542018-08-20 16:15:33 -070065static void statsdNoteDrop();
Yao Chenb3be9ea2018-05-07 16:57:13 -070066
67struct android_log_transport_write statsdLoggerWrite = {
Yao Chen63010542018-08-20 16:15:33 -070068 .name = "statsd",
69 .sock = -EBADF,
70 .available = statsdAvailable,
71 .open = statsdOpen,
72 .close = statsdClose,
73 .write = statsdWrite,
74 .noteDrop = statsdNoteDrop,
Yao Chenb3be9ea2018-05-07 16:57:13 -070075};
76
77/* log_init_lock assumed */
78static int statsdOpen() {
79 int i, ret = 0;
80
81 i = atomic_load(&statsdLoggerWrite.sock);
82 if (i < 0) {
83 int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
84 if (sock < 0) {
85 ret = -errno;
86 } else {
87 struct sockaddr_un un;
88 memset(&un, 0, sizeof(struct sockaddr_un));
89 un.sun_family = AF_UNIX;
90 strcpy(un.sun_path, "/dev/socket/statsdw");
91
92 if (TEMP_FAILURE_RETRY(
93 connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) < 0) {
94 ret = -errno;
95 switch (ret) {
96 case -ENOTCONN:
97 case -ECONNREFUSED:
98 case -ENOENT:
99 i = atomic_exchange(&statsdLoggerWrite.sock, ret);
100 /* FALLTHRU */
101 default:
102 break;
103 }
104 close(sock);
105 } else {
106 ret = atomic_exchange(&statsdLoggerWrite.sock, sock);
107 if ((ret >= 0) && (ret != sock)) {
108 close(ret);
109 }
110 ret = 0;
111 }
112 }
113 }
114
115 return ret;
116}
117
118static void __statsdClose(int negative_errno) {
119 int sock = atomic_exchange(&statsdLoggerWrite.sock, negative_errno);
120 if (sock >= 0) {
121 close(sock);
122 }
123}
124
125static void statsdClose() {
126 __statsdClose(-EBADF);
127}
128
129static int statsdAvailable() {
130 if (atomic_load(&statsdLoggerWrite.sock) < 0) {
131 if (access("/dev/socket/statsdw", W_OK) == 0) {
132 return 0;
133 }
134 return -EBADF;
135 }
136 return 1;
137}
138
Yao Chen63010542018-08-20 16:15:33 -0700139static void statsdNoteDrop() {
140 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
141}
142
Yao Chenb3be9ea2018-05-07 16:57:13 -0700143static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) {
144 ssize_t ret;
145 int sock;
146 static const unsigned headerLength = 1;
147 struct iovec newVec[nr + headerLength];
148 android_log_header_t header;
149 size_t i, payloadSize;
Yao Chenb3be9ea2018-05-07 16:57:13 -0700150
151 sock = atomic_load(&statsdLoggerWrite.sock);
152 if (sock < 0) switch (sock) {
153 case -ENOTCONN:
154 case -ECONNREFUSED:
155 case -ENOENT:
156 break;
157 default:
158 return -EBADF;
159 }
160 /*
161 * struct {
162 * // what we provide to socket
163 * android_log_header_t header;
164 * // caller provides
165 * union {
166 * struct {
167 * char prio;
168 * char payload[];
169 * } string;
170 * struct {
171 * uint32_t tag
172 * char payload[];
173 * } binary;
174 * };
175 * };
176 */
177
178 header.tid = gettid();
179 header.realtime.tv_sec = ts->tv_sec;
180 header.realtime.tv_nsec = ts->tv_nsec;
181
182 newVec[0].iov_base = (unsigned char*)&header;
183 newVec[0].iov_len = sizeof(header);
184
185 // If we dropped events before, try to tell statsd.
186 if (sock >= 0) {
187 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
188 if (snapshot) {
189 android_log_event_int_t buffer;
190 header.id = LOG_ID_STATS;
191 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
192 buffer.payload.type = EVENT_TYPE_INT;
193 buffer.payload.data = htole32(snapshot);
194
195 newVec[headerLength].iov_base = &buffer;
196 newVec[headerLength].iov_len = sizeof(buffer);
197
198 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
199 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
200 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
201 }
202 }
203 }
204
205 header.id = LOG_ID_STATS;
206
207 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
208 newVec[i].iov_base = vec[i - headerLength].iov_base;
209 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
210
211 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
212 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
213 if (newVec[i].iov_len) {
214 ++i;
215 }
216 break;
217 }
218 }
219
220 /*
221 * The write below could be lost, but will never block.
222 *
223 * ENOTCONN occurs if statsd has died.
224 * ENOENT occurs if statsd is not running and socket is missing.
225 * ECONNREFUSED occurs if we can not reconnect to statsd.
226 * EAGAIN occurs if statsd is overloaded.
227 */
228 if (sock < 0) {
229 ret = sock;
230 } else {
231 ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
232 if (ret < 0) {
233 ret = -errno;
234 }
235 }
236 switch (ret) {
237 case -ENOTCONN:
238 case -ECONNREFUSED:
239 case -ENOENT:
240 if (statd_writer_trylock()) {
241 return ret; /* in a signal handler? try again when less stressed
242 */
243 }
244 __statsdClose(ret);
245 ret = statsdOpen();
246 statsd_writer_init_unlock();
247
248 if (ret < 0) {
249 return ret;
250 }
251
252 ret = TEMP_FAILURE_RETRY(writev(atomic_load(&statsdLoggerWrite.sock), newVec, i));
253 if (ret < 0) {
254 ret = -errno;
255 }
256 /* FALLTHRU */
257 default:
258 break;
259 }
260
261 if (ret > (ssize_t)sizeof(header)) {
262 ret -= sizeof(header);
Yao Chenb3be9ea2018-05-07 16:57:13 -0700263 }
264
265 return ret;
266}