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