blob: 8186cea97090524f594eb9d4790ebcdbfd523d5f [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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
Mark Salyzyne0fa2912014-04-28 16:39:04 -070017#include <limits.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070018#include <sys/prctl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080019#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) {
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070036 prctl(PR_SET_NAME, "logd.writer");
37
Mark Salyzyn8444eb82014-04-24 09:43:23 -070038 char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
Mark Salyzynb059cf52014-03-18 15:30:17 -070039 + LOGGER_ENTRY_MAX_PAYLOAD];
Mark Salyzyn0175b072014-02-26 09:50:16 -080040 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);
Mark Salyzyndbb708a2014-03-21 14:37:06 -070057 if (n <= (ssize_t)(sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080058 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 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080089 char *msg = ((char *)buffer) + sizeof_log_id_t;
90 n -= sizeof_log_id_t;
91
Mark Salyzynb992d0d2014-03-20 16:09:38 -070092 // 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
Mark Salyzyn0175b072014-02-26 09:50:16 -080098 log_time realtime(msg);
99 msg += sizeof(log_time);
100 n -= sizeof(log_time);
101
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700102 // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
103 // truncated message to the logs.
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700104
105 logbuf->log(log_id, realtime, cred->uid, cred->pid, tid, msg,
Mark Salyzyn6e2c0f72014-05-02 15:47:24 -0700106 ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700107 reader->notifyNewLog();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108
109 return true;
110}
111
112int LogListener::getLogSocket() {
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700113 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
Mark Salyzyn0175b072014-02-26 09:50:16 -0800122 int on = 1;
123 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
124 return -1;
125 }
126 return sock;
127}