LogListener.cpp revision ae4d928d816e30dbe57c2c321b0f0759d0567b3f
1/* 2 * Copyright (C) 2012-2014 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 <limits.h> 18#include <sys/prctl.h> 19#include <sys/socket.h> 20#include <sys/types.h> 21#include <sys/un.h> 22#include <unistd.h> 23 24#include <cutils/sockets.h> 25#include <log/logger.h> 26#include <private/android_filesystem_config.h> 27#include <private/android_logger.h> 28 29#include "LogListener.h" 30 31LogListener::LogListener(LogBuffer *buf, LogReader *reader) 32 : SocketListener(getLogSocket(), false) 33 , logbuf(buf) 34 , reader(reader) 35{ } 36 37bool LogListener::onDataAvailable(SocketClient *cli) { 38 static bool name_set; 39 if (!name_set) { 40 prctl(PR_SET_NAME, "logd.writer"); 41 name_set = true; 42 } 43 44 char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time) 45 + LOGGER_ENTRY_MAX_PAYLOAD]; 46 struct iovec iov = { buffer, sizeof(buffer) }; 47 memset(buffer, 0, sizeof(buffer)); 48 49 char control[CMSG_SPACE(sizeof(struct ucred))]; 50 struct msghdr hdr = { 51 NULL, 52 0, 53 &iov, 54 1, 55 control, 56 sizeof(control), 57 0, 58 }; 59 60 int socket = cli->getSocket(); 61 62 ssize_t n = recvmsg(socket, &hdr, 0); 63 if (n <= (ssize_t)(sizeof(android_log_header_t))) { 64 return false; 65 } 66 67 struct ucred *cred = NULL; 68 69 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr); 70 while (cmsg != NULL) { 71 if (cmsg->cmsg_level == SOL_SOCKET 72 && cmsg->cmsg_type == SCM_CREDENTIALS) { 73 cred = (struct ucred *)CMSG_DATA(cmsg); 74 break; 75 } 76 cmsg = CMSG_NXTHDR(&hdr, cmsg); 77 } 78 79 if (cred == NULL) { 80 return false; 81 } 82 83 if (cred->uid == AID_LOGD) { 84 // ignore log messages we send to ourself. 85 // Such log messages are often generated by libraries we depend on 86 // which use standard Android logging. 87 return false; 88 } 89 90 android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer); 91 if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX || header->id == LOG_ID_KERNEL) { 92 return false; 93 } 94 95 char *msg = ((char *)buffer) + sizeof(android_log_header_t); 96 n -= sizeof(android_log_header_t); 97 98 // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a 99 // truncated message to the logs. 100 101 if (logbuf->log((log_id_t)header->id, header->realtime, 102 cred->uid, cred->pid, header->tid, msg, 103 ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX) >= 0) { 104 reader->notifyNewLog(); 105 } 106 107 return true; 108} 109 110int LogListener::getLogSocket() { 111 static const char socketName[] = "logdw"; 112 int sock = android_get_control_socket(socketName); 113 114 if (sock < 0) { 115 sock = socket_local_server(socketName, 116 ANDROID_SOCKET_NAMESPACE_RESERVED, 117 SOCK_DGRAM); 118 } 119 120 int on = 1; 121 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) { 122 return -1; 123 } 124 return sock; 125} 126