/* * Copyright (C) 2007-2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "logd_writer.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "logger.h" #include "uio.h" static atomic_int logd_socket; // Note that it is safe to call connect() multiple times on DGRAM Unix domain sockets, so this // function is used to reconnect to logd without requiring a new socket. static void LogdConnect() { sockaddr_un un = {}; un.sun_family = AF_UNIX; strcpy(un.sun_path, "/dev/socket/logdw"); TEMP_FAILURE_RETRY(connect(logd_socket, reinterpret_cast(&un), sizeof(sockaddr_un))); } // logd_socket should only be opened once. If we see that logd_socket is uninitialized, we create a // new socket and attempt to exchange it into the atomic logd_socket. If the compare/exchange was // successful, then that will be the socket used for the duration of the program, otherwise a // different thread has already opened and written the socket to the atomic, so close the new socket // and return. static void GetSocket() { if (logd_socket != 0) { return; } int new_socket = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)); if (new_socket <= 0) { return; } int uninitialized_value = 0; if (!logd_socket.compare_exchange_strong(uninitialized_value, new_socket)) { close(new_socket); return; } LogdConnect(); } // This is the one exception to the above. Zygote uses this to clean up open FD's after fork() and // before specialization. It is single threaded at this point and therefore this function is // explicitly not thread safe. It sets logd_socket to 0, so future logs will be safely initialized // whenever they happen. void LogdClose() { if (logd_socket > 0) { close(logd_socket); } logd_socket = 0; } int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) { ssize_t ret; static const unsigned headerLength = 1; struct iovec newVec[nr + headerLength]; android_log_header_t header; size_t i, payloadSize; GetSocket(); if (logd_socket <= 0) { return -EBADF; } /* logd, after initialization and priv drop */ if (getuid() == AID_LOGD) { /* * ignore log messages we send to ourself (logd). * Such log messages are often generated by libraries we depend on * which use standard Android logging. */ return 0; } header.id = logId; header.tid = gettid(); header.realtime.tv_sec = ts->tv_sec; header.realtime.tv_nsec = ts->tv_nsec; newVec[0].iov_base = (unsigned char*)&header; newVec[0].iov_len = sizeof(header); for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) { newVec[i].iov_base = vec[i - headerLength].iov_base; payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len; if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) { newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD; if (newVec[i].iov_len) { ++i; } break; } } ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i)); if (ret < 0) { LogdConnect(); ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i)); } if (ret < 0) { ret = -errno; } return ret; }