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
27#include "LogListener.h"
28
29LogListener::LogListener(LogBuffer *buf, LogReader *reader)
30        : SocketListener(getLogSocket(), false)
31        , logbuf(buf)
32        , reader(reader)
33{  }
34
35bool LogListener::onDataAvailable(SocketClient *cli) {
36    prctl(PR_SET_NAME, "logd.writer");
37
38    char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
39        + LOGGER_ENTRY_MAX_PAYLOAD];
40    struct iovec iov = { buffer, sizeof(buffer) };
41    memset(buffer, 0, sizeof(buffer));
42
43    char control[CMSG_SPACE(sizeof(struct ucred))];
44    struct msghdr hdr = {
45        NULL,
46        0,
47        &iov,
48        1,
49        control,
50        sizeof(control),
51        0,
52    };
53
54    int socket = cli->getSocket();
55
56    ssize_t n = recvmsg(socket, &hdr, 0);
57    if (n <= (ssize_t)(sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time))) {
58        return false;
59    }
60
61    struct ucred *cred = NULL;
62
63    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
64    while (cmsg != NULL) {
65        if (cmsg->cmsg_level == SOL_SOCKET
66                && cmsg->cmsg_type  == SCM_CREDENTIALS) {
67            cred = (struct ucred *)CMSG_DATA(cmsg);
68            break;
69        }
70        cmsg = CMSG_NXTHDR(&hdr, cmsg);
71    }
72
73    if (cred == NULL) {
74        return false;
75    }
76
77    if (cred->uid == getuid()) {
78        // ignore log messages we send to ourself.
79        // Such log messages are often generated by libraries we depend on
80        // which use standard Android logging.
81        return false;
82    }
83
84    // First log element is always log_id.
85    log_id_t log_id = (log_id_t) *((typeof_log_id_t *) buffer);
86    if (log_id < 0 || log_id >= LOG_ID_MAX) {
87        return false;
88    }
89    char *msg = ((char *)buffer) + sizeof_log_id_t;
90    n -= sizeof_log_id_t;
91
92    // second element is the thread id of the caller
93    pid_t tid = (pid_t) *((uint16_t *) msg);
94    msg += sizeof(uint16_t);
95    n -= sizeof(uint16_t);
96
97    // third element is the realtime at point of caller
98    log_time realtime(msg);
99    msg += sizeof(log_time);
100    n -= sizeof(log_time);
101
102    // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
103    // truncated message to the logs.
104
105    logbuf->log(log_id, realtime, cred->uid, cred->pid, tid, msg,
106        ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
107    reader->notifyNewLog();
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