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