Mark Salyzyn | 018a96d | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2016 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 | |
| 17 | #include <endian.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <poll.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdatomic.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
| 33 | |
Mark Salyzyn | 6584d0a | 2016-09-28 13:26:55 -0700 | [diff] [blame^] | 34 | #include <android/log.h> |
Mark Salyzyn | 018a96d | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 35 | #include <cutils/sockets.h> |
Mark Salyzyn | 018a96d | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 36 | #include <log/logger.h> |
Mark Salyzyn | 018a96d | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
| 38 | #include <private/android_logger.h> |
| 39 | |
| 40 | #include "config_read.h" |
| 41 | #include "log_portability.h" |
| 42 | #include "logger.h" |
| 43 | |
| 44 | /* branchless on many architectures. */ |
| 45 | #define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y)))) |
| 46 | |
| 47 | static int logdAvailable(log_id_t LogId); |
| 48 | static int logdVersion(struct android_log_logger *logger, |
| 49 | struct android_log_transport_context *transp); |
| 50 | static int logdRead(struct android_log_logger_list *logger_list, |
| 51 | struct android_log_transport_context *transp, |
| 52 | struct log_msg *log_msg); |
| 53 | static int logdPoll(struct android_log_logger_list *logger_list, |
| 54 | struct android_log_transport_context *transp); |
| 55 | static void logdClose(struct android_log_logger_list *logger_list, |
| 56 | struct android_log_transport_context *transp); |
| 57 | static int logdClear(struct android_log_logger *logger, |
| 58 | struct android_log_transport_context *transp); |
| 59 | static ssize_t logdSetSize(struct android_log_logger *logger, |
| 60 | struct android_log_transport_context *transp, |
| 61 | size_t size); |
| 62 | static ssize_t logdGetSize(struct android_log_logger *logger, |
| 63 | struct android_log_transport_context *transp); |
| 64 | static ssize_t logdGetReadableSize(struct android_log_logger *logger, |
| 65 | struct android_log_transport_context *transp); |
| 66 | static ssize_t logdGetPrune(struct android_log_logger_list *logger, |
| 67 | struct android_log_transport_context *transp, |
| 68 | char *buf, size_t len); |
| 69 | static ssize_t logdSetPrune(struct android_log_logger_list *logger, |
| 70 | struct android_log_transport_context *transp, |
| 71 | char *buf, size_t len); |
| 72 | static ssize_t logdGetStats(struct android_log_logger_list *logger, |
| 73 | struct android_log_transport_context *transp, |
| 74 | char *buf, size_t len); |
| 75 | |
| 76 | LIBLOG_HIDDEN struct android_log_transport_read logdLoggerRead = { |
| 77 | .node = { &logdLoggerRead.node, &logdLoggerRead.node }, |
| 78 | .name = "logd", |
| 79 | .available = logdAvailable, |
| 80 | .version = logdVersion, |
| 81 | .read = logdRead, |
| 82 | .poll = logdPoll, |
| 83 | .close = logdClose, |
| 84 | .clear = logdClear, |
| 85 | .getSize = logdGetSize, |
| 86 | .setSize = logdSetSize, |
Mark Salyzyn | ccfb244 | 2016-04-29 07:38:13 -0700 | [diff] [blame] | 87 | .getReadableSize = logdGetReadableSize, |
Mark Salyzyn | 018a96d | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 88 | .getPrune = logdGetPrune, |
| 89 | .setPrune = logdSetPrune, |
| 90 | .getStats = logdGetStats, |
| 91 | }; |
| 92 | |
| 93 | static int logdAvailable(log_id_t logId) |
| 94 | { |
| 95 | if (logId > LOG_ID_KERNEL) { |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | if (logId == LOG_ID_SECURITY) { |
| 99 | uid_t uid = __android_log_uid(); |
| 100 | if (uid != AID_SYSTEM) { |
| 101 | return -EPERM; |
| 102 | } |
| 103 | } |
| 104 | if (access("/dev/socket/logdw", W_OK) == 0) { |
| 105 | return 0; |
| 106 | } |
| 107 | return -EBADF; |
| 108 | } |
| 109 | |
| 110 | /* Private copy of ../libcutils/socket_local_client.c prevent library loops */ |
| 111 | |
| 112 | #if defined(_WIN32) |
| 113 | |
| 114 | LIBLOG_WEAK int socket_local_client(const char *name, int namespaceId, int type) |
| 115 | { |
| 116 | errno = ENOSYS; |
| 117 | return -ENOSYS; |
| 118 | } |
| 119 | |
| 120 | #else /* !_WIN32 */ |
| 121 | |
| 122 | #include <sys/socket.h> |
| 123 | #include <sys/un.h> |
| 124 | #include <sys/select.h> |
| 125 | #include <sys/types.h> |
| 126 | |
| 127 | /* Private copy of ../libcutils/socket_local.h prevent library loops */ |
| 128 | #define FILESYSTEM_SOCKET_PREFIX "/tmp/" |
| 129 | #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/" |
| 130 | /* End of ../libcutils/socket_local.h */ |
| 131 | |
| 132 | #define LISTEN_BACKLOG 4 |
| 133 | |
| 134 | /* Documented in header file. */ |
| 135 | LIBLOG_WEAK int socket_make_sockaddr_un(const char *name, int namespaceId, |
| 136 | struct sockaddr_un *p_addr, |
| 137 | socklen_t *alen) |
| 138 | { |
| 139 | memset (p_addr, 0, sizeof (*p_addr)); |
| 140 | size_t namelen; |
| 141 | |
| 142 | switch (namespaceId) { |
| 143 | case ANDROID_SOCKET_NAMESPACE_ABSTRACT: |
| 144 | #if defined(__linux__) |
| 145 | namelen = strlen(name); |
| 146 | |
| 147 | /* Test with length +1 for the *initial* '\0'. */ |
| 148 | if ((namelen + 1) > sizeof(p_addr->sun_path)) { |
| 149 | goto error; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Note: The path in this case is *not* supposed to be |
| 154 | * '\0'-terminated. ("man 7 unix" for the gory details.) |
| 155 | */ |
| 156 | |
| 157 | p_addr->sun_path[0] = 0; |
| 158 | memcpy(p_addr->sun_path + 1, name, namelen); |
| 159 | #else |
| 160 | /* this OS doesn't have the Linux abstract namespace */ |
| 161 | |
| 162 | namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX); |
| 163 | /* unix_path_max appears to be missing on linux */ |
| 164 | if (namelen > sizeof(*p_addr) |
| 165 | - offsetof(struct sockaddr_un, sun_path) - 1) { |
| 166 | goto error; |
| 167 | } |
| 168 | |
| 169 | strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX); |
| 170 | strcat(p_addr->sun_path, name); |
| 171 | #endif |
| 172 | break; |
| 173 | |
| 174 | case ANDROID_SOCKET_NAMESPACE_RESERVED: |
| 175 | namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX); |
| 176 | /* unix_path_max appears to be missing on linux */ |
| 177 | if (namelen > sizeof(*p_addr) |
| 178 | - offsetof(struct sockaddr_un, sun_path) - 1) { |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX); |
| 183 | strcat(p_addr->sun_path, name); |
| 184 | break; |
| 185 | |
| 186 | case ANDROID_SOCKET_NAMESPACE_FILESYSTEM: |
| 187 | namelen = strlen(name); |
| 188 | /* unix_path_max appears to be missing on linux */ |
| 189 | if (namelen > sizeof(*p_addr) |
| 190 | - offsetof(struct sockaddr_un, sun_path) - 1) { |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | strcpy(p_addr->sun_path, name); |
| 195 | break; |
| 196 | |
| 197 | default: |
| 198 | /* invalid namespace id */ |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | p_addr->sun_family = AF_LOCAL; |
| 203 | *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; |
| 204 | return 0; |
| 205 | error: |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * connect to peer named "name" on fd |
| 211 | * returns same fd or -1 on error. |
| 212 | * fd is not closed on error. that's your job. |
| 213 | * |
| 214 | * Used by AndroidSocketImpl |
| 215 | */ |
| 216 | LIBLOG_WEAK int socket_local_client_connect(int fd, const char *name, |
| 217 | int namespaceId, int type __unused) |
| 218 | { |
| 219 | struct sockaddr_un addr; |
| 220 | socklen_t alen; |
| 221 | int err; |
| 222 | |
| 223 | err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen); |
| 224 | |
| 225 | if (err < 0) { |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | if(connect(fd, (struct sockaddr *) &addr, alen) < 0) { |
| 230 | goto error; |
| 231 | } |
| 232 | |
| 233 | return fd; |
| 234 | |
| 235 | error: |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * connect to peer named "name" |
| 241 | * returns fd or -1 on error |
| 242 | */ |
| 243 | LIBLOG_WEAK int socket_local_client(const char *name, int namespaceId, int type) |
| 244 | { |
| 245 | int s; |
| 246 | |
| 247 | s = socket(AF_LOCAL, type, 0); |
| 248 | if(s < 0) return -1; |
| 249 | |
| 250 | if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) { |
| 251 | close(s); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | return s; |
| 256 | } |
| 257 | |
| 258 | #endif /* !_WIN32 */ |
| 259 | /* End of ../libcutils/socket_local_client.c */ |
| 260 | |
| 261 | /* worker for sending the command to the logger */ |
| 262 | static ssize_t send_log_msg(struct android_log_logger *logger, |
| 263 | const char *msg, char *buf, size_t buf_size) |
| 264 | { |
| 265 | ssize_t ret; |
| 266 | size_t len; |
| 267 | char *cp; |
| 268 | int errno_save = 0; |
| 269 | int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 270 | SOCK_STREAM); |
| 271 | if (sock < 0) { |
| 272 | return sock; |
| 273 | } |
| 274 | |
| 275 | if (msg) { |
| 276 | snprintf(buf, buf_size, msg, logger ? logger->logId : (unsigned) -1); |
| 277 | } |
| 278 | |
| 279 | len = strlen(buf) + 1; |
| 280 | ret = TEMP_FAILURE_RETRY(write(sock, buf, len)); |
| 281 | if (ret <= 0) { |
| 282 | goto done; |
| 283 | } |
| 284 | |
| 285 | len = buf_size; |
| 286 | cp = buf; |
| 287 | while ((ret = TEMP_FAILURE_RETRY(read(sock, cp, len))) > 0) { |
| 288 | struct pollfd p; |
| 289 | |
| 290 | if (((size_t)ret == len) || (buf_size < PAGE_SIZE)) { |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | len -= ret; |
| 295 | cp += ret; |
| 296 | |
| 297 | memset(&p, 0, sizeof(p)); |
| 298 | p.fd = sock; |
| 299 | p.events = POLLIN; |
| 300 | |
| 301 | /* Give other side 20ms to refill pipe */ |
| 302 | ret = TEMP_FAILURE_RETRY(poll(&p, 1, 20)); |
| 303 | |
| 304 | if (ret <= 0) { |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | if (!(p.revents & POLLIN)) { |
| 309 | ret = 0; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (ret >= 0) { |
| 315 | ret += buf_size - len; |
| 316 | } |
| 317 | |
| 318 | done: |
| 319 | if ((ret == -1) && errno) { |
| 320 | errno_save = errno; |
| 321 | } |
| 322 | close(sock); |
| 323 | if (errno_save) { |
| 324 | errno = errno_save; |
| 325 | } |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | static int check_log_success(char *buf, ssize_t ret) |
| 330 | { |
| 331 | if (ret < 0) { |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | if (strncmp(buf, "success", 7)) { |
| 336 | errno = EINVAL; |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int logdClear(struct android_log_logger *logger, |
| 344 | struct android_log_transport_context *transp __unused) |
| 345 | { |
| 346 | char buf[512]; |
| 347 | |
| 348 | return check_log_success(buf, |
| 349 | send_log_msg(logger, "clear %d", buf, sizeof(buf))); |
| 350 | } |
| 351 | |
| 352 | /* returns the total size of the log's ring buffer */ |
| 353 | static ssize_t logdGetSize(struct android_log_logger *logger, |
| 354 | struct android_log_transport_context *transp __unused) |
| 355 | { |
| 356 | char buf[512]; |
| 357 | |
| 358 | ssize_t ret = send_log_msg(logger, "getLogSize %d", buf, sizeof(buf)); |
| 359 | if (ret < 0) { |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | if ((buf[0] < '0') || ('9' < buf[0])) { |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | return atol(buf); |
| 368 | } |
| 369 | |
| 370 | static ssize_t logdSetSize( |
| 371 | struct android_log_logger *logger, |
| 372 | struct android_log_transport_context *transp __unused, |
| 373 | size_t size) |
| 374 | { |
| 375 | char buf[512]; |
| 376 | |
| 377 | snprintf(buf, sizeof(buf), "setLogSize %d %zu", logger->logId, size); |
| 378 | |
| 379 | return check_log_success(buf, send_log_msg(NULL, NULL, buf, sizeof(buf))); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * returns the readable size of the log's ring buffer (that is, amount of the |
| 384 | * log consumed) |
| 385 | */ |
| 386 | static ssize_t logdGetReadableSize( |
| 387 | struct android_log_logger *logger, |
| 388 | struct android_log_transport_context *transp __unused) |
| 389 | { |
| 390 | char buf[512]; |
| 391 | |
| 392 | ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf)); |
| 393 | if (ret < 0) { |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | if ((buf[0] < '0') || ('9' < buf[0])) { |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | return atol(buf); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * returns the logger version |
| 406 | */ |
| 407 | static int logdVersion( |
| 408 | struct android_log_logger *logger __unused, |
| 409 | struct android_log_transport_context *transp __unused) |
| 410 | { |
| 411 | uid_t uid = __android_log_uid(); |
| 412 | return ((uid != AID_ROOT) && (uid != AID_LOG) && (uid != AID_SYSTEM)) ? 3 : 4; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * returns statistics |
| 417 | */ |
| 418 | static ssize_t logdGetStats(struct android_log_logger_list *logger_list, |
| 419 | struct android_log_transport_context *transp __unused, |
| 420 | char *buf, size_t len) |
| 421 | { |
| 422 | struct android_log_logger *logger; |
| 423 | char *cp = buf; |
| 424 | size_t remaining = len; |
| 425 | size_t n; |
| 426 | |
| 427 | n = snprintf(cp, remaining, "getStatistics"); |
| 428 | n = min(n, remaining); |
| 429 | remaining -= n; |
| 430 | cp += n; |
| 431 | |
| 432 | logger_for_each(logger, logger_list) { |
| 433 | n = snprintf(cp, remaining, " %d", logger->logId); |
| 434 | n = min(n, remaining); |
| 435 | remaining -= n; |
| 436 | cp += n; |
| 437 | } |
| 438 | |
| 439 | if (logger_list->pid) { |
| 440 | snprintf(cp, remaining, " pid=%u", logger_list->pid); |
| 441 | } |
| 442 | |
| 443 | return send_log_msg(NULL, NULL, buf, len); |
| 444 | } |
| 445 | |
| 446 | static ssize_t logdGetPrune( |
| 447 | struct android_log_logger_list *logger_list __unused, |
| 448 | struct android_log_transport_context *transp __unused, |
| 449 | char *buf, size_t len) |
| 450 | { |
| 451 | return send_log_msg(NULL, "getPruneList", buf, len); |
| 452 | } |
| 453 | |
| 454 | static ssize_t logdSetPrune( |
| 455 | struct android_log_logger_list *logger_list __unused, |
| 456 | struct android_log_transport_context *transp __unused, |
| 457 | char *buf, size_t len) |
| 458 | { |
| 459 | const char cmd[] = "setPruneList "; |
| 460 | const size_t cmdlen = sizeof(cmd) - 1; |
| 461 | |
| 462 | if (strlen(buf) > (len - cmdlen)) { |
| 463 | return -ENOMEM; /* KISS */ |
| 464 | } |
| 465 | memmove(buf + cmdlen, buf, len - cmdlen); |
| 466 | buf[len - 1] = '\0'; |
| 467 | memcpy(buf, cmd, cmdlen); |
| 468 | |
| 469 | return check_log_success(buf, send_log_msg(NULL, NULL, buf, len)); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | static void caught_signal(int signum __unused) |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | static int logdOpen(struct android_log_logger_list *logger_list, |
| 478 | struct android_log_transport_context *transp) |
| 479 | { |
| 480 | struct android_log_logger *logger; |
| 481 | struct sigaction ignore; |
| 482 | struct sigaction old_sigaction; |
| 483 | unsigned int old_alarm = 0; |
| 484 | char buffer[256], *cp, c; |
| 485 | int e, ret, remaining; |
| 486 | |
| 487 | int sock = transp->context.sock; |
| 488 | if (sock > 0) { |
| 489 | return sock; |
| 490 | } |
| 491 | |
| 492 | if (!logger_list) { |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | |
| 496 | sock = socket_local_client("logdr", |
| 497 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 498 | SOCK_SEQPACKET); |
| 499 | if (sock == 0) { |
| 500 | /* Guarantee not file descriptor zero */ |
| 501 | int newsock = socket_local_client("logdr", |
| 502 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 503 | SOCK_SEQPACKET); |
| 504 | close(sock); |
| 505 | sock = newsock; |
| 506 | } |
| 507 | if (sock <= 0) { |
| 508 | if ((sock == -1) && errno) { |
| 509 | return -errno; |
| 510 | } |
| 511 | return sock; |
| 512 | } |
| 513 | |
| 514 | strcpy(buffer, (logger_list->mode & ANDROID_LOG_NONBLOCK) ? |
| 515 | "dumpAndClose" : "stream"); |
| 516 | cp = buffer + strlen(buffer); |
| 517 | |
| 518 | strcpy(cp, " lids"); |
| 519 | cp += 5; |
| 520 | c = '='; |
| 521 | remaining = sizeof(buffer) - (cp - buffer); |
| 522 | logger_for_each(logger, logger_list) { |
| 523 | ret = snprintf(cp, remaining, "%c%u", c, logger->logId); |
| 524 | ret = min(ret, remaining); |
| 525 | remaining -= ret; |
| 526 | cp += ret; |
| 527 | c = ','; |
| 528 | } |
| 529 | |
| 530 | if (logger_list->tail) { |
| 531 | ret = snprintf(cp, remaining, " tail=%u", logger_list->tail); |
| 532 | ret = min(ret, remaining); |
| 533 | remaining -= ret; |
| 534 | cp += ret; |
| 535 | } |
| 536 | |
| 537 | if (logger_list->start.tv_sec || logger_list->start.tv_nsec) { |
| 538 | if (logger_list->mode & ANDROID_LOG_WRAP) { |
| 539 | // ToDo: alternate API to allow timeout to be adjusted. |
| 540 | ret = snprintf(cp, remaining, " timeout=%u", |
| 541 | ANDROID_LOG_WRAP_DEFAULT_TIMEOUT); |
| 542 | ret = min(ret, remaining); |
| 543 | remaining -= ret; |
| 544 | cp += ret; |
| 545 | } |
| 546 | ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32, |
| 547 | logger_list->start.tv_sec, |
| 548 | logger_list->start.tv_nsec); |
| 549 | ret = min(ret, remaining); |
| 550 | remaining -= ret; |
| 551 | cp += ret; |
| 552 | } |
| 553 | |
| 554 | if (logger_list->pid) { |
| 555 | ret = snprintf(cp, remaining, " pid=%u", logger_list->pid); |
| 556 | ret = min(ret, remaining); |
| 557 | cp += ret; |
| 558 | } |
| 559 | |
| 560 | if (logger_list->mode & ANDROID_LOG_NONBLOCK) { |
| 561 | /* Deal with an unresponsive logd */ |
| 562 | memset(&ignore, 0, sizeof(ignore)); |
| 563 | ignore.sa_handler = caught_signal; |
| 564 | sigemptyset(&ignore.sa_mask); |
| 565 | /* particularily useful if tombstone is reporting for logd */ |
| 566 | sigaction(SIGALRM, &ignore, &old_sigaction); |
| 567 | old_alarm = alarm(30); |
| 568 | } |
| 569 | ret = write(sock, buffer, cp - buffer); |
| 570 | e = errno; |
| 571 | if (logger_list->mode & ANDROID_LOG_NONBLOCK) { |
| 572 | if (e == EINTR) { |
| 573 | e = ETIMEDOUT; |
| 574 | } |
| 575 | alarm(old_alarm); |
| 576 | sigaction(SIGALRM, &old_sigaction, NULL); |
| 577 | } |
| 578 | |
| 579 | if (ret <= 0) { |
| 580 | close(sock); |
| 581 | if ((ret == -1) && e) { |
| 582 | return -e; |
| 583 | } |
| 584 | if (ret == 0) { |
| 585 | return -EIO; |
| 586 | } |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | return transp->context.sock = sock; |
| 591 | } |
| 592 | |
| 593 | /* Read from the selected logs */ |
| 594 | static int logdRead(struct android_log_logger_list *logger_list, |
| 595 | struct android_log_transport_context *transp, |
| 596 | struct log_msg *log_msg) |
| 597 | { |
| 598 | int ret, e; |
| 599 | struct sigaction ignore; |
| 600 | struct sigaction old_sigaction; |
| 601 | unsigned int old_alarm = 0; |
| 602 | |
| 603 | ret = logdOpen(logger_list, transp); |
| 604 | if (ret < 0) { |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | memset(log_msg, 0, sizeof(*log_msg)); |
| 609 | |
| 610 | if (logger_list->mode & ANDROID_LOG_NONBLOCK) { |
| 611 | memset(&ignore, 0, sizeof(ignore)); |
| 612 | ignore.sa_handler = caught_signal; |
| 613 | sigemptyset(&ignore.sa_mask); |
| 614 | /* particularily useful if tombstone is reporting for logd */ |
| 615 | sigaction(SIGALRM, &ignore, &old_sigaction); |
| 616 | old_alarm = alarm(30); |
| 617 | } |
| 618 | |
| 619 | /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */ |
| 620 | ret = recv(ret, log_msg, LOGGER_ENTRY_MAX_LEN, 0); |
| 621 | e = errno; |
| 622 | |
| 623 | if (logger_list->mode & ANDROID_LOG_NONBLOCK) { |
| 624 | if ((ret == 0) || (e == EINTR)) { |
| 625 | e = EAGAIN; |
| 626 | ret = -1; |
| 627 | } |
| 628 | alarm(old_alarm); |
| 629 | sigaction(SIGALRM, &old_sigaction, NULL); |
| 630 | } |
| 631 | |
| 632 | if ((ret == -1) && e) { |
| 633 | return -e; |
| 634 | } |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | static int logdPoll(struct android_log_logger_list *logger_list, |
| 639 | struct android_log_transport_context *transp) |
| 640 | { |
| 641 | struct pollfd p; |
| 642 | |
| 643 | int ret = logdOpen(logger_list, transp); |
| 644 | if (ret < 0) { |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | memset(&p, 0, sizeof(p)); |
| 649 | p.fd = ret; |
| 650 | p.events = POLLIN; |
| 651 | ret = poll(&p, 1, 20); |
| 652 | if ((ret > 0) && !(p.revents & POLLIN)) { |
| 653 | ret = 0; |
| 654 | } |
| 655 | if ((ret == -1) && errno) { |
| 656 | return -errno; |
| 657 | } |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | /* Close all the logs */ |
| 662 | static void logdClose(struct android_log_logger_list *logger_list __unused, |
| 663 | struct android_log_transport_context *transp) |
| 664 | { |
| 665 | if (transp->context.sock > 0) { |
| 666 | close (transp->context.sock); |
| 667 | transp->context.sock = -1; |
| 668 | } |
| 669 | } |