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