blob: 39dd2272a736a0c1c042ff3f15f8029eba05c3f5 [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
Mark Salyzyn2ad0bd02016-02-23 08:55:43 -080030#include "LogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080031#include "LogListener.h"
Mark Salyzyn083b0372015-12-04 10:59:45 -080032#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080033
Mark Salyzyn77187782015-05-12 15:21:31 -070034LogListener::LogListener(LogBuffer *buf, LogReader *reader) :
35 SocketListener(getLogSocket(), false),
36 logbuf(buf),
37 reader(reader) {
38}
Mark Salyzyn0175b072014-02-26 09:50:16 -080039
40bool LogListener::onDataAvailable(SocketClient *cli) {
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070041 static bool name_set;
42 if (!name_set) {
43 prctl(PR_SET_NAME, "logd.writer");
44 name_set = true;
45 }
Mark Salyzyn8daa9af2014-04-28 14:07:23 -070046
Mark Salyzyn8444eb82014-04-24 09:43:23 -070047 char buffer[sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time)
Mark Salyzynb059cf52014-03-18 15:30:17 -070048 + LOGGER_ENTRY_MAX_PAYLOAD];
Mark Salyzyn0175b072014-02-26 09:50:16 -080049 struct iovec iov = { buffer, sizeof(buffer) };
Mark Salyzyn0175b072014-02-26 09:50:16 -080050
Mark Salyzyn46906402016-01-12 10:08:03 -080051 char control[CMSG_SPACE(sizeof(struct ucred))] __aligned(4);
Mark Salyzyn0175b072014-02-26 09:50:16 -080052 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
Mark Salyzyn1d9c7e72015-12-15 12:30:46 -080064 // 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));
Mark Salyzyn0175b072014-02-26 09:50:16 -080067 ssize_t n = recvmsg(socket, &hdr, 0);
Mark Salyzynb5f6e452015-01-16 13:38:07 -080068 if (n <= (ssize_t)(sizeof(android_log_header_t))) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080069 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
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070088 if (cred->uid == AID_LOGD) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080089 // 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
Mark Salyzynb5f6e452015-01-16 13:38:07 -080095 android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
Mark Salyzynae4d9282014-10-15 08:49:39 -070096 if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX || header->id == LOG_ID_KERNEL) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080097 return false;
98 }
Mark Salyzyn0175b072014-02-26 09:50:16 -080099
Mark Salyzyn083b0372015-12-04 10:59:45 -0800100 if ((header->id == LOG_ID_SECURITY) &&
101 (!__android_log_security() ||
Mark Salyzyn674ce6e2016-01-26 21:47:35 +0000102 !clientHasLogCredentials(cred->uid, cred->gid, cred->pid))) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800103 return false;
104 }
105
Mark Salyzynb5f6e452015-01-16 13:38:07 -0800106 char *msg = ((char *)buffer) + sizeof(android_log_header_t);
107 n -= sizeof(android_log_header_t);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700109 // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
110 // truncated message to the logs.
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700111
Mark Salyzyn202e1532015-02-09 08:21:05 -0800112 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 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800117
118 return true;
119}
120
121int LogListener::getLogSocket() {
Mark Salyzyndfc47e82014-03-24 10:26:47 -0700122 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
Mark Salyzyn0175b072014-02-26 09:50:16 -0800131 int on = 1;
132 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
133 return -1;
134 }
135 return sock;
136}