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