blob: 846dd7c41a399c802c6cc04ce82720b4cf157521 [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 Salyzyn46906402016-01-12 10:08:03 -080018#include <sys/cdefs.h>
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070019#include <sys/prctl.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080020#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>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070027#include <private/android_filesystem_config.h>
Mark Salyzynb5f6e452015-01-16 13:38:07 -080028#include <private/android_logger.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080029
30#include "LogListener.h"
Mark Salyzyn083b0372015-12-04 10:59:45 -080031#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080032
Mark Salyzyn77187782015-05-12 15:21:31 -070033LogListener::LogListener(LogBuffer *buf, LogReader *reader) :
34 SocketListener(getLogSocket(), false),
35 logbuf(buf),
36 reader(reader) {
37}
Mark Salyzyn0175b072014-02-26 09:50:16 -080038
39bool LogListener::onDataAvailable(SocketClient *cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070040 static bool name_set;
41 if (!name_set) {
42 prctl(PR_SET_NAME, "logd.writer");
43 name_set = true;
44 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070045
Mark Salyzyn8444eb82014-04-24 09:43:23 -070046 char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
Mark Salyzynb059cf52014-03-18 15:30:17 -070047 + LOGGER_ENTRY_MAX_PAYLOAD];
Mark Salyzyn0175b072014-02-26 09:50:16 -080048 struct iovec iov = { buffer, sizeof(buffer) };
Mark Salyzyn0175b072014-02-26 09:50:16 -080049
Mark Salyzyn46906402016-01-12 10:08:03 -080050 char control[CMSG_SPACE(sizeof(struct ucred))] __aligned(4);
Mark Salyzyn0175b072014-02-26 09:50:16 -080051 struct msghdr hdr = {
52 NULL,
53 0,
54 &iov,
55 1,
56 control,
57 sizeof(control),
58 0,
59 };
60
61 int socket = cli->getSocket();
62
Mark Salyzyn1d9c7e72015-12-15 12:30:46 -080063 // To clear the entire buffer is secure/safe, but this contributes to 1.68%
64 // overhead under logging load. We are safe because we check counts.
65 // memset(buffer, 0, sizeof(buffer));
Mark Salyzyn0175b072014-02-26 09:50:16 -080066 ssize_t n = recvmsg(socket, &hdr, 0);
Mark Salyzynb5f6e452015-01-16 13:38:07 -080067 if (n <= (ssize_t)(sizeof(android_log_header_t))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080068 return false;
69 }
70
71 struct ucred *cred = NULL;
72
73 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
74 while (cmsg != NULL) {
75 if (cmsg->cmsg_level == SOL_SOCKET
76 && cmsg->cmsg_type == SCM_CREDENTIALS) {
77 cred = (struct ucred *)CMSG_DATA(cmsg);
78 break;
79 }
80 cmsg = CMSG_NXTHDR(&hdr, cmsg);
81 }
82
83 if (cred == NULL) {
84 return false;
85 }
86
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070087 if (cred->uid == AID_LOGD) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080088 // ignore log messages we send to ourself.
89 // Such log messages are often generated by libraries we depend on
90 // which use standard Android logging.
91 return false;
92 }
93
Mark Salyzynb5f6e452015-01-16 13:38:07 -080094 android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
Mark Salyzynae4d9282014-10-15 08:49:39 -070095 if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX || header->id == LOG_ID_KERNEL) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080096 return false;
97 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080098
Mark Salyzyn083b0372015-12-04 10:59:45 -080099 if ((header->id == LOG_ID_SECURITY) &&
100 (!__android_log_security() ||
Mark Salyzyn674ce6e2016-01-26 21:47:35 +0000101 !clientHasLogCredentials(cred->uid, cred->gid, cred->pid))) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800102 return false;
103 }
104
Mark Salyzynb5f6e452015-01-16 13:38:07 -0800105 char *msg = ((char *)buffer) + sizeof(android_log_header_t);
106 n -= sizeof(android_log_header_t);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800107
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700108 // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
109 // truncated message to the logs.
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700110
Mark Salyzyn202e1532015-02-09 08:21:05 -0800111 if (logbuf->log((log_id_t)header->id, header->realtime,
112 cred->uid, cred->pid, header->tid, msg,
113 ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX) >= 0) {
114 reader->notifyNewLog();
115 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800116
117 return true;
118}
119
120int LogListener::getLogSocket() {
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700121 static const char socketName[] = "logdw";
122 int sock = android_get_control_socket(socketName);
123
124 if (sock < 0) {
125 sock = socket_local_server(socketName,
126 ANDROID_SOCKET_NAMESPACE_RESERVED,
127 SOCK_DGRAM);
128 }
129
Mark Salyzyn0175b072014-02-26 09:50:16 -0800130 int on = 1;
131 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
132 return -1;
133 }
134 return sock;
135}