Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <assert.h> |
| 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
Josh Gao | d371133 | 2020-08-20 16:27:01 -0700 | [diff] [blame] | 33 | #include <linux/net.h> |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 34 | #include <pthread.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <stddef.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/socket.h> |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 41 | #include <sys/syscall.h> |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/uio.h> |
| 44 | #include <sys/un.h> |
| 45 | #include <time.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | #include <android/set_abort_message.h> |
| 49 | #include <async_safe/log.h> |
| 50 | |
| 51 | #include "private/CachedProperty.h" |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 52 | #include "private/ErrnoRestorer.h" |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 53 | #include "private/ScopedPthreadMutexLocker.h" |
| 54 | |
Josh Gao | d371133 | 2020-08-20 16:27:01 -0700 | [diff] [blame] | 55 | // Don't call libc's close or socket, since it might call back into us as a result of fdsan/fdtrack. |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 56 | #pragma GCC poison close |
| 57 | static int __close(int fd) { |
| 58 | return syscall(__NR_close, fd); |
| 59 | } |
| 60 | |
Josh Gao | d371133 | 2020-08-20 16:27:01 -0700 | [diff] [blame] | 61 | static int __socket(int domain, int type, int protocol) { |
| 62 | #if defined(__i386__) |
| 63 | unsigned long args[3] = {static_cast<unsigned long>(domain), static_cast<unsigned long>(type), |
| 64 | static_cast<unsigned long>(protocol)}; |
| 65 | return syscall(__NR_socketcall, SYS_SOCKET, &args); |
| 66 | #else |
| 67 | return syscall(__NR_socket, domain, type, protocol); |
| 68 | #endif |
| 69 | } |
| 70 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 71 | // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java. |
| 72 | enum AndroidEventLogType { |
| 73 | EVENT_TYPE_INT = 0, |
| 74 | EVENT_TYPE_LONG = 1, |
| 75 | EVENT_TYPE_STRING = 2, |
| 76 | EVENT_TYPE_LIST = 3, |
| 77 | EVENT_TYPE_FLOAT = 4, |
| 78 | }; |
| 79 | |
| 80 | struct BufferOutputStream { |
| 81 | public: |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 82 | BufferOutputStream(char* buffer, size_t size) : total(0), pos_(buffer), avail_(size) { |
| 83 | if (avail_ > 0) pos_[0] = '\0'; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 84 | } |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 85 | ~BufferOutputStream() = default; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 86 | |
| 87 | void Send(const char* data, int len) { |
| 88 | if (len < 0) { |
| 89 | len = strlen(data); |
| 90 | } |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 91 | total += len; |
| 92 | |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 93 | if (avail_ <= 1) { |
| 94 | // No space to put anything else. |
| 95 | return; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 96 | } |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 97 | |
| 98 | if (static_cast<size_t>(len) >= avail_) { |
| 99 | len = avail_ - 1; |
| 100 | } |
| 101 | memcpy(pos_, data, len); |
| 102 | pos_ += len; |
| 103 | pos_[0] = '\0'; |
| 104 | avail_ -= len; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | size_t total; |
| 108 | |
| 109 | private: |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 110 | char* pos_; |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 111 | size_t avail_; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | struct FdOutputStream { |
| 115 | public: |
| 116 | explicit FdOutputStream(int fd) : total(0), fd_(fd) {} |
| 117 | |
| 118 | void Send(const char* data, int len) { |
| 119 | if (len < 0) { |
| 120 | len = strlen(data); |
| 121 | } |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 122 | total += len; |
| 123 | |
| 124 | while (len > 0) { |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 125 | ssize_t bytes = TEMP_FAILURE_RETRY(write(fd_, data, len)); |
| 126 | if (bytes == -1) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 129 | data += bytes; |
| 130 | len -= bytes; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | size_t total; |
| 135 | |
| 136 | private: |
| 137 | int fd_; |
| 138 | }; |
| 139 | |
| 140 | /*** formatted output implementation |
| 141 | ***/ |
| 142 | |
| 143 | /* Parse a decimal string from 'format + *ppos', |
| 144 | * return the value, and writes the new position past |
| 145 | * the decimal string in '*ppos' on exit. |
| 146 | * |
| 147 | * NOTE: Does *not* handle a sign prefix. |
| 148 | */ |
| 149 | static unsigned parse_decimal(const char* format, int* ppos) { |
| 150 | const char* p = format + *ppos; |
| 151 | unsigned result = 0; |
| 152 | |
| 153 | for (;;) { |
| 154 | int ch = *p; |
| 155 | unsigned d = static_cast<unsigned>(ch - '0'); |
| 156 | |
| 157 | if (d >= 10U) { |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | result = result * 10 + d; |
| 162 | p++; |
| 163 | } |
| 164 | *ppos = p - format; |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | // Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes. |
| 169 | // Assumes that buf_size > 0. |
| 170 | static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) { |
| 171 | char* p = buf; |
| 172 | char* end = buf + buf_size - 1; |
| 173 | |
| 174 | // Generate digit string in reverse order. |
| 175 | while (value) { |
| 176 | unsigned d = value % base; |
| 177 | value /= base; |
| 178 | if (p != end) { |
| 179 | char ch; |
| 180 | if (d < 10) { |
| 181 | ch = '0' + d; |
| 182 | } else { |
| 183 | ch = (caps ? 'A' : 'a') + (d - 10); |
| 184 | } |
| 185 | *p++ = ch; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Special case for 0. |
| 190 | if (p == buf) { |
| 191 | if (p != end) { |
| 192 | *p++ = '0'; |
| 193 | } |
| 194 | } |
| 195 | *p = '\0'; |
| 196 | |
| 197 | // Reverse digit string in-place. |
| 198 | size_t length = p - buf; |
| 199 | for (size_t i = 0, j = length - 1; i < j; ++i, --j) { |
| 200 | char ch = buf[i]; |
| 201 | buf[i] = buf[j]; |
| 202 | buf[j] = ch; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) { |
| 207 | // Decode the conversion specifier. |
| 208 | int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o'); |
| 209 | int base = 10; |
Elliott Hughes | f5b4e3c | 2023-08-22 13:50:39 -0700 | [diff] [blame] | 210 | if (tolower(conversion) == 'x') { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 211 | base = 16; |
| 212 | } else if (conversion == 'o') { |
| 213 | base = 8; |
Elliott Hughes | f5b4e3c | 2023-08-22 13:50:39 -0700 | [diff] [blame] | 214 | } else if (tolower(conversion) == 'b') { |
| 215 | base = 2; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 216 | } |
| 217 | bool caps = (conversion == 'X'); |
| 218 | |
| 219 | if (is_signed && static_cast<int64_t>(value) < 0) { |
| 220 | buf[0] = '-'; |
| 221 | buf += 1; |
| 222 | buf_size -= 1; |
| 223 | value = static_cast<uint64_t>(-static_cast<int64_t>(value)); |
| 224 | } |
| 225 | format_unsigned(buf, buf_size, value, base, caps); |
| 226 | } |
| 227 | |
| 228 | template <typename Out> |
| 229 | static void SendRepeat(Out& o, char ch, int count) { |
| 230 | char pad[8]; |
| 231 | memset(pad, ch, sizeof(pad)); |
| 232 | |
| 233 | const int pad_size = static_cast<int>(sizeof(pad)); |
| 234 | while (count > 0) { |
| 235 | int avail = count; |
| 236 | if (avail > pad_size) { |
| 237 | avail = pad_size; |
| 238 | } |
| 239 | o.Send(pad, avail); |
| 240 | count -= avail; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Perform formatted output to an output target 'o' */ |
| 245 | template <typename Out> |
| 246 | static void out_vformat(Out& o, const char* format, va_list args) { |
| 247 | int nn = 0; |
| 248 | |
| 249 | for (;;) { |
| 250 | int mm; |
| 251 | int padZero = 0; |
| 252 | int padLeft = 0; |
| 253 | char sign = '\0'; |
| 254 | int width = -1; |
| 255 | int prec = -1; |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 256 | bool alternate = false; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 257 | size_t bytelen = sizeof(int); |
| 258 | int slen; |
Christopher Ferris | 35759fa | 2023-03-20 16:31:18 -0700 | [diff] [blame] | 259 | char buffer[64]; // temporary buffer used to format numbers/format errno string |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 260 | |
| 261 | char c; |
| 262 | |
| 263 | /* first, find all characters that are not 0 or '%' */ |
| 264 | /* then send them to the output directly */ |
| 265 | mm = nn; |
| 266 | do { |
| 267 | c = format[mm]; |
| 268 | if (c == '\0' || c == '%') break; |
| 269 | mm++; |
| 270 | } while (1); |
| 271 | |
| 272 | if (mm > nn) { |
| 273 | o.Send(format + nn, mm - nn); |
| 274 | nn = mm; |
| 275 | } |
| 276 | |
| 277 | /* is this it ? then exit */ |
| 278 | if (c == '\0') break; |
| 279 | |
| 280 | /* nope, we are at a '%' modifier */ |
| 281 | nn++; // skip it |
| 282 | |
| 283 | /* parse flags */ |
| 284 | for (;;) { |
| 285 | c = format[nn++]; |
| 286 | if (c == '\0') { /* single trailing '%' ? */ |
| 287 | c = '%'; |
| 288 | o.Send(&c, 1); |
| 289 | return; |
| 290 | } else if (c == '0') { |
| 291 | padZero = 1; |
| 292 | continue; |
| 293 | } else if (c == '-') { |
| 294 | padLeft = 1; |
| 295 | continue; |
| 296 | } else if (c == ' ' || c == '+') { |
| 297 | sign = c; |
| 298 | continue; |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 299 | } else if (c == '#') { |
| 300 | alternate = true; |
| 301 | continue; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 302 | } |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | /* parse field width */ |
| 307 | if ((c >= '0' && c <= '9')) { |
| 308 | nn--; |
| 309 | width = static_cast<int>(parse_decimal(format, &nn)); |
| 310 | c = format[nn++]; |
| 311 | } |
| 312 | |
| 313 | /* parse precision */ |
| 314 | if (c == '.') { |
| 315 | prec = static_cast<int>(parse_decimal(format, &nn)); |
| 316 | c = format[nn++]; |
| 317 | } |
| 318 | |
| 319 | /* length modifier */ |
| 320 | switch (c) { |
| 321 | case 'h': |
| 322 | bytelen = sizeof(short); |
| 323 | if (format[nn] == 'h') { |
| 324 | bytelen = sizeof(char); |
| 325 | nn += 1; |
| 326 | } |
| 327 | c = format[nn++]; |
| 328 | break; |
| 329 | case 'l': |
| 330 | bytelen = sizeof(long); |
| 331 | if (format[nn] == 'l') { |
| 332 | bytelen = sizeof(long long); |
| 333 | nn += 1; |
| 334 | } |
| 335 | c = format[nn++]; |
| 336 | break; |
| 337 | case 'z': |
| 338 | bytelen = sizeof(size_t); |
| 339 | c = format[nn++]; |
| 340 | break; |
| 341 | case 't': |
| 342 | bytelen = sizeof(ptrdiff_t); |
| 343 | c = format[nn++]; |
| 344 | break; |
| 345 | default:; |
| 346 | } |
| 347 | |
| 348 | /* conversion specifier */ |
| 349 | const char* str = buffer; |
| 350 | if (c == 's') { |
| 351 | /* string */ |
| 352 | str = va_arg(args, const char*); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 353 | } else if (c == 'c') { |
| 354 | /* character */ |
| 355 | /* NOTE: char is promoted to int when passed through the stack */ |
| 356 | buffer[0] = static_cast<char>(va_arg(args, int)); |
| 357 | buffer[1] = '\0'; |
| 358 | } else if (c == 'p') { |
| 359 | uint64_t value = reinterpret_cast<uintptr_t>(va_arg(args, void*)); |
| 360 | buffer[0] = '0'; |
| 361 | buffer[1] = 'x'; |
| 362 | format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x'); |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 363 | } else if (c == 'm') { |
Christopher Ferris | 35759fa | 2023-03-20 16:31:18 -0700 | [diff] [blame] | 364 | strerror_r(errno, buffer, sizeof(buffer)); |
Elliott Hughes | f5b4e3c | 2023-08-22 13:50:39 -0700 | [diff] [blame] | 365 | } else if (tolower(c) == 'b' || c == 'd' || c == 'i' || c == 'o' || c == 'u' || |
| 366 | tolower(c) == 'x') { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 367 | /* integers - first read value from stack */ |
| 368 | uint64_t value; |
| 369 | int is_signed = (c == 'd' || c == 'i' || c == 'o'); |
| 370 | |
| 371 | /* NOTE: int8_t and int16_t are promoted to int when passed |
| 372 | * through the stack |
| 373 | */ |
| 374 | switch (bytelen) { |
| 375 | case 1: |
| 376 | value = static_cast<uint8_t>(va_arg(args, int)); |
| 377 | break; |
| 378 | case 2: |
| 379 | value = static_cast<uint16_t>(va_arg(args, int)); |
| 380 | break; |
| 381 | case 4: |
| 382 | value = va_arg(args, uint32_t); |
| 383 | break; |
| 384 | case 8: |
| 385 | value = va_arg(args, uint64_t); |
| 386 | break; |
| 387 | default: |
| 388 | return; /* should not happen */ |
| 389 | } |
| 390 | |
| 391 | /* sign extension, if needed */ |
| 392 | if (is_signed) { |
| 393 | int shift = 64 - 8 * bytelen; |
| 394 | value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift); |
| 395 | } |
| 396 | |
Elliott Hughes | f5b4e3c | 2023-08-22 13:50:39 -0700 | [diff] [blame] | 397 | if (alternate && value != 0 && (tolower(c) == 'x' || c == 'o' || tolower(c) == 'b')) { |
| 398 | if (tolower(c) == 'x' || tolower(c) == 'b') { |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 399 | buffer[0] = '0'; |
Elliott Hughes | f5b4e3c | 2023-08-22 13:50:39 -0700 | [diff] [blame] | 400 | buffer[1] = c; |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 401 | format_integer(buffer + 2, sizeof(buffer) - 2, value, c); |
| 402 | } else { |
| 403 | buffer[0] = '0'; |
| 404 | format_integer(buffer + 1, sizeof(buffer) - 1, value, c); |
| 405 | } |
| 406 | } else { |
| 407 | /* format the number properly into our buffer */ |
| 408 | format_integer(buffer, sizeof(buffer), value, c); |
| 409 | } |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 410 | } else if (c == '%') { |
| 411 | buffer[0] = '%'; |
| 412 | buffer[1] = '\0'; |
| 413 | } else { |
| 414 | __assert(__FILE__, __LINE__, "conversion specifier unsupported"); |
| 415 | } |
| 416 | |
zijunzhao | 75c36fe | 2022-01-28 19:22:24 +0000 | [diff] [blame] | 417 | if (str == nullptr) { |
| 418 | str = "(null)"; |
| 419 | } |
| 420 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 421 | /* if we are here, 'str' points to the content that must be |
| 422 | * outputted. handle padding and alignment now */ |
| 423 | |
| 424 | slen = strlen(str); |
| 425 | |
| 426 | if (sign != '\0' || prec != -1) { |
| 427 | __assert(__FILE__, __LINE__, "sign/precision unsupported"); |
| 428 | } |
| 429 | |
| 430 | if (slen < width && !padLeft) { |
| 431 | char padChar = padZero ? '0' : ' '; |
| 432 | SendRepeat(o, padChar, width - slen); |
| 433 | } |
| 434 | |
| 435 | o.Send(str, slen); |
| 436 | |
| 437 | if (slen < width && padLeft) { |
| 438 | char padChar = padZero ? '0' : ' '; |
| 439 | SendRepeat(o, padChar, width - slen); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 444 | int async_safe_format_buffer_va_list(char* buffer, size_t buffer_size, const char* format, |
| 445 | va_list args) { |
| 446 | BufferOutputStream os(buffer, buffer_size); |
| 447 | out_vformat(os, format, args); |
| 448 | return os.total; |
| 449 | } |
| 450 | |
Christopher Ferris | 9247640 | 2017-08-22 11:24:09 -0700 | [diff] [blame] | 451 | int async_safe_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) { |
| 452 | va_list args; |
| 453 | va_start(args, format); |
| 454 | int buffer_len = async_safe_format_buffer_va_list(buffer, buffer_size, format, args); |
| 455 | va_end(args); |
| 456 | return buffer_len; |
| 457 | } |
| 458 | |
Ryan Prichard | 5de9a31c | 2018-10-02 18:13:28 -0700 | [diff] [blame] | 459 | int async_safe_format_fd_va_list(int fd, const char* format, va_list args) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 460 | FdOutputStream os(fd); |
Ryan Prichard | 5de9a31c | 2018-10-02 18:13:28 -0700 | [diff] [blame] | 461 | out_vformat(os, format, args); |
| 462 | return os.total; |
| 463 | } |
| 464 | |
| 465 | int async_safe_format_fd(int fd, const char* format, ...) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 466 | va_list args; |
| 467 | va_start(args, format); |
Ryan Prichard | 5de9a31c | 2018-10-02 18:13:28 -0700 | [diff] [blame] | 468 | int result = async_safe_format_fd_va_list(fd, format, args); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 469 | va_end(args); |
Ryan Prichard | 5de9a31c | 2018-10-02 18:13:28 -0700 | [diff] [blame] | 470 | return result; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int write_stderr(const char* tag, const char* msg) { |
| 474 | iovec vec[4]; |
| 475 | vec[0].iov_base = const_cast<char*>(tag); |
| 476 | vec[0].iov_len = strlen(tag); |
| 477 | vec[1].iov_base = const_cast<char*>(": "); |
| 478 | vec[1].iov_len = 2; |
| 479 | vec[2].iov_base = const_cast<char*>(msg); |
| 480 | vec[2].iov_len = strlen(msg); |
| 481 | vec[3].iov_base = const_cast<char*>("\n"); |
| 482 | vec[3].iov_len = 1; |
| 483 | |
| 484 | int result = TEMP_FAILURE_RETRY(writev(STDERR_FILENO, vec, 4)); |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | static int open_log_socket() { |
| 489 | // ToDo: Ideally we want this to fail if the gid of the current |
| 490 | // process is AID_LOGD, but will have to wait until we have |
| 491 | // registered this in private/android_filesystem_config.h. We have |
| 492 | // found that all logd crashes thus far have had no problem stuffing |
| 493 | // the UNIX domain socket and moving on so not critical *today*. |
| 494 | |
Josh Gao | d371133 | 2020-08-20 16:27:01 -0700 | [diff] [blame] | 495 | int log_fd = TEMP_FAILURE_RETRY(__socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 496 | if (log_fd == -1) { |
| 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | union { |
| 501 | struct sockaddr addr; |
| 502 | struct sockaddr_un addrUn; |
| 503 | } u; |
| 504 | memset(&u, 0, sizeof(u)); |
| 505 | u.addrUn.sun_family = AF_UNIX; |
| 506 | strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path)); |
| 507 | |
| 508 | if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) { |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 509 | __close(log_fd); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | return log_fd; |
| 514 | } |
| 515 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 516 | struct log_time { // Wire format |
| 517 | uint32_t tv_sec; |
| 518 | uint32_t tv_nsec; |
| 519 | }; |
| 520 | |
| 521 | int async_safe_write_log(int priority, const char* tag, const char* msg) { |
| 522 | int main_log_fd = open_log_socket(); |
| 523 | if (main_log_fd == -1) { |
| 524 | // Try stderr instead. |
| 525 | return write_stderr(tag, msg); |
| 526 | } |
| 527 | |
| 528 | iovec vec[6]; |
| 529 | char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN; |
| 530 | vec[0].iov_base = &log_id; |
| 531 | vec[0].iov_len = sizeof(log_id); |
| 532 | uint16_t tid = gettid(); |
| 533 | vec[1].iov_base = &tid; |
| 534 | vec[1].iov_len = sizeof(tid); |
| 535 | timespec ts; |
Elliott Hughes | 53dc9dd | 2017-09-19 14:02:50 -0700 | [diff] [blame] | 536 | clock_gettime(CLOCK_REALTIME, &ts); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 537 | log_time realtime_ts; |
| 538 | realtime_ts.tv_sec = ts.tv_sec; |
| 539 | realtime_ts.tv_nsec = ts.tv_nsec; |
| 540 | vec[2].iov_base = &realtime_ts; |
| 541 | vec[2].iov_len = sizeof(realtime_ts); |
| 542 | |
| 543 | vec[3].iov_base = &priority; |
| 544 | vec[3].iov_len = 1; |
| 545 | vec[4].iov_base = const_cast<char*>(tag); |
| 546 | vec[4].iov_len = strlen(tag) + 1; |
| 547 | vec[5].iov_base = const_cast<char*>(msg); |
| 548 | vec[5].iov_len = strlen(msg) + 1; |
| 549 | |
| 550 | int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0]))); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 551 | __close(main_log_fd); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 552 | return result; |
| 553 | } |
| 554 | |
| 555 | int async_safe_format_log_va_list(int priority, const char* tag, const char* format, va_list args) { |
Elliott Hughes | 8aecba7 | 2017-10-17 15:34:41 -0700 | [diff] [blame] | 556 | ErrnoRestorer errno_restorer; |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 557 | char buffer[1024]; |
| 558 | BufferOutputStream os(buffer, sizeof(buffer)); |
| 559 | out_vformat(os, format, args); |
| 560 | return async_safe_write_log(priority, tag, buffer); |
| 561 | } |
| 562 | |
| 563 | int async_safe_format_log(int priority, const char* tag, const char* format, ...) { |
| 564 | va_list args; |
| 565 | va_start(args, format); |
| 566 | int result = async_safe_format_log_va_list(priority, tag, format, args); |
| 567 | va_end(args); |
| 568 | return result; |
| 569 | } |
| 570 | |
| 571 | void async_safe_fatal_va_list(const char* prefix, const char* format, va_list args) { |
| 572 | char msg[1024]; |
| 573 | BufferOutputStream os(msg, sizeof(msg)); |
| 574 | |
| 575 | if (prefix) { |
| 576 | os.Send(prefix, strlen(prefix)); |
| 577 | os.Send(": ", 2); |
| 578 | } |
| 579 | |
| 580 | out_vformat(os, format, args); |
| 581 | |
| 582 | // Log to stderr for the benefit of "adb shell" users and gtests. |
| 583 | struct iovec iov[2] = { |
Ryan Prichard | 5258c25 | 2018-05-01 17:59:59 -0700 | [diff] [blame] | 584 | {msg, strlen(msg)}, {const_cast<char*>("\n"), 1}, |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 585 | }; |
| 586 | TEMP_FAILURE_RETRY(writev(2, iov, 2)); |
| 587 | |
| 588 | // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed). |
| 589 | async_safe_write_log(ANDROID_LOG_FATAL, "libc", msg); |
| 590 | |
| 591 | android_set_abort_message(msg); |
| 592 | } |
| 593 | |
Elliott Hughes | 695713e | 2017-06-20 17:28:42 -0700 | [diff] [blame] | 594 | void async_safe_fatal_no_abort(const char* fmt, ...) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 595 | va_list args; |
| 596 | va_start(args, fmt); |
| 597 | async_safe_fatal_va_list(nullptr, fmt, args); |
| 598 | va_end(args); |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 599 | } |