blob: e1ec52bc29873641a6931f9b0de4e2c7db0b6442 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
2 * Copyright (C) 2012-2013 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 <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Tom Cherry0b2a0112019-06-06 13:41:20 -070020#include <linux/capability.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070021#include <poll.h>
Mark Salyzyn882f8562013-12-26 15:13:36 -080022#include <sched.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070023#include <semaphore.h>
24#include <signal.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/capability.h>
Mark Salyzyneb06de72014-10-13 09:59:37 -070029#include <sys/klog.h>
Elliott Hughese5a0f202014-07-18 17:39:41 -070030#include <sys/prctl.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070031#include <sys/resource.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080032#include <sys/stat.h>
33#include <sys/types.h>
Mark Salyzynccbadc62015-03-12 12:25:35 -070034#include <syslog.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080035#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080036
Mark Salyzynd5600fd2015-06-12 14:59:42 -070037#include <memory>
38
Tom Cherryf93b4002020-06-03 09:23:49 -070039#include <android-base/logging.h>
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040040#include <android-base/macros.h>
Tom Cherryf93b4002020-06-03 09:23:49 -070041#include <android-base/stringprintf.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080042#include <cutils/android_get_control_file.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070043#include <cutils/sockets.h>
Mark Salyzynff32f3c2015-04-13 14:24:45 -070044#include <log/event_tag_map.h>
William Robertsaeca97b2015-07-31 13:10:36 -070045#include <packagelistparser/packagelistparser.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070046#include <private/android_filesystem_config.h>
Mark Salyzyn5740a462016-03-28 15:42:08 -070047#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080048#include <processgroup/sched_policy.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070049#include <utils/threads.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080050
Tom Cherryd5b38382020-05-12 13:16:41 -070051#include "ChattyLogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080052#include "CommandListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090053#include "LogAudit.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080054#include "LogBuffer.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070055#include "LogKlog.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080056#include "LogListener.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070057#include "LogReader.h"
Tom Cherry64e90162020-05-07 14:44:43 -070058#include "LogStatistics.h"
Tom Cherry1a12ae32020-05-01 16:13:18 -070059#include "LogTags.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070060#include "LogUtils.h"
Tom Cherry1a796bc2020-05-13 09:28:37 -070061#include "SerializedLogBuffer.h"
Tom Cherry8f613462020-05-12 12:46:43 -070062#include "SimpleLogBuffer.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080063
Mark Salyzyn501c3732017-03-10 14:31:54 -080064#define KMSG_PRIORITY(PRI) \
65 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
66 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070067
Tom Cherry0b2a0112019-06-06 13:41:20 -070068// The service is designed to be run by init, it does not respond well to starting up manually. Init
69// has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This
70// allows debuggers, etc to be attached to logd at the very beginning, while still having init
71// handle the user, groups, capabilities, files, etc setup.
Tom Cherry54b00a92020-06-22 10:15:04 -070072static void DropPrivs(bool klogd, bool auditd) {
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080073 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Tom Cherry54b00a92020-06-22 10:15:04 -070074 PLOG(FATAL) << "failed to set background scheduling policy";
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080075 }
76
Tom Cherry54b00a92020-06-22 10:15:04 -070077 sched_param param = {};
Mark Salyzyn501c3732017-03-10 14:31:54 -080078 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Tom Cherry54b00a92020-06-22 10:15:04 -070079 PLOG(FATAL) << "failed to set batch scheduler";
Mark Salyzyn882f8562013-12-26 15:13:36 -080080 }
81
Tom Cherryf93b4002020-06-03 09:23:49 -070082 if (!__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -070083 prctl(PR_SET_DUMPABLE, 0) == -1) {
Tom Cherry54b00a92020-06-22 10:15:04 -070084 PLOG(FATAL) << "failed to clear PR_SET_DUMPABLE";
Mark Salyzyn6a70ded2016-10-28 14:49:53 -070085 }
86
Tom Cherry0b2a0112019-06-06 13:41:20 -070087 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
88 if (cap_clear(caps.get()) < 0) {
Tom Cherry54b00a92020-06-22 10:15:04 -070089 PLOG(FATAL) << "cap_clear() failed";
Mark Salyzyn501c3732017-03-10 14:31:54 -080090 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070091 if (klogd) {
Tom Cherry54b00a92020-06-22 10:15:04 -070092 cap_value_t cap_syslog = CAP_SYSLOG;
93 if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, &cap_syslog, CAP_SET) < 0 ||
94 cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, &cap_syslog, CAP_SET) < 0) {
95 PLOG(FATAL) << "Failed to set CAP_SYSLOG";
96 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070097 }
98 if (auditd) {
Tom Cherry54b00a92020-06-22 10:15:04 -070099 cap_value_t cap_audit_control = CAP_AUDIT_CONTROL;
100 if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, &cap_audit_control, CAP_SET) < 0 ||
101 cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, &cap_audit_control, CAP_SET) < 0) {
102 PLOG(FATAL) << "Failed to set CAP_AUDIT_CONTROL";
103 }
Mark Salyzyn501c3732017-03-10 14:31:54 -0800104 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700105 if (cap_set_proc(caps.get()) < 0) {
Tom Cherry54b00a92020-06-22 10:15:04 -0700106 PLOG(FATAL) << "cap_set_proc() failed";
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700107 }
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108}
109
Mark Salyzyn501c3732017-03-10 14:31:54 -0800110char* android::uidToName(uid_t u) {
Tom Cherry36f53992017-09-06 10:07:37 -0700111 struct Userdata {
112 uid_t uid;
113 char* name;
114 } userdata = {
115 .uid = u,
116 .name = nullptr,
117 };
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700118
Tom Cherry36f53992017-09-06 10:07:37 -0700119 packagelist_parse(
120 [](pkg_info* info, void* callback_parameter) {
121 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
122 bool result = true;
123 if (info->uid == userdata->uid) {
124 userdata->name = strdup(info->name);
125 // false to stop processing
126 result = false;
127 }
128 packagelist_free(info);
129 return result;
130 },
131 &userdata);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700132
Tom Cherry36f53992017-09-06 10:07:37 -0700133 return userdata.name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700134}
135
Mark Salyzyn501c3732017-03-10 14:31:54 -0800136static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700137 if (!al && !kl) {
138 return;
139 }
140
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700141 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700142 if (rc <= 0) {
143 return;
144 }
145
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700146 // Margin for additional input race or trailing nul
147 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800148 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700149
150 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
151 if (rc <= 0) {
152 return;
153 }
154
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700155 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700156 len = rc + 1;
157 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700158 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700159
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700160 ssize_t sublen;
161 for (char *ptr = nullptr, *tok = buf.get();
162 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
163 tok = nullptr) {
164 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700165 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700166 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700167 }
168 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700169 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700170 }
171 }
172}
173
Mark Salyzynd8f01802016-10-31 13:49:44 -0700174static int issueReinit() {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800175 int sock = TEMP_FAILURE_RETRY(socket_local_client(
176 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700177 if (sock < 0) return -errno;
178
179 static const char reinitStr[] = "reinit";
180 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
181 if (ret < 0) return -errno;
182
183 struct pollfd p;
184 memset(&p, 0, sizeof(p));
185 p.fd = sock;
186 p.events = POLLIN;
187 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
188 if (ret < 0) return -errno;
189 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
190
191 static const char success[] = "success";
192 char buffer[sizeof(success) - 1];
193 memset(buffer, 0, sizeof(buffer));
194 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
195 if (ret < 0) return -errno;
196
197 return strncmp(buffer, success, sizeof(success) - 1) != 0;
198}
199
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700200// Foreground waits for exit of the main persistent threads
201// that are started here. The threads are created to manage
202// UNIX domain client sockets for writing, reading and
203// controlling the user space logger, and for any additional
204// logging plugins like auditd and restart control. Additional
205// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800206int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900207 // logd is written under the assumption that the timezone is UTC.
208 // If TZ is not set, persist.sys.timezone is looked up in some time utility
209 // libc functions, including mktime. It confuses the logd time handling,
210 // so here explicitly set TZ to UTC, which overrides the property.
211 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700212 // issue reinit command. KISS argument parsing.
213 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700214 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700215 }
216
Tom Cherryf93b4002020-06-03 09:23:49 -0700217 android::base::InitLogging(
218 argv, [](android::base::LogId log_id, android::base::LogSeverity severity,
219 const char* tag, const char* file, unsigned int line, const char* message) {
220 if (tag && strcmp(tag, "logd") != 0) {
221 auto prefixed_message = android::base::StringPrintf("%s: %s", tag, message);
222 android::base::KernelLogger(log_id, severity, "logd", file, line,
223 prefixed_message.c_str());
224 } else {
225 android::base::KernelLogger(log_id, severity, "logd", file, line, message);
226 }
227 });
228
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700229 static const char dev_kmsg[] = "/dev/kmsg";
Tom Cherryf93b4002020-06-03 09:23:49 -0700230 int fdDmesg = android_get_control_file(dev_kmsg);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700231 if (fdDmesg < 0) {
232 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
233 }
234
235 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800236 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800237 "ro.logd.kernel",
238 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700239 if (klogd) {
240 static const char proc_kmsg[] = "/proc/kmsg";
241 fdPmesg = android_get_control_file(proc_kmsg);
242 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800243 fdPmesg = TEMP_FAILURE_RETRY(
244 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700245 }
Tom Cherryf93b4002020-06-03 09:23:49 -0700246 if (fdPmesg < 0) PLOG(ERROR) << "Failed to open " << proc_kmsg;
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700247 }
248
Tom Cherry0b2a0112019-06-06 13:41:20 -0700249 bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
Tom Cherry54b00a92020-06-22 10:15:04 -0700250 DropPrivs(klogd, auditd);
Tom Cherry0b2a0112019-06-06 13:41:20 -0700251
Tom Cherry1a12ae32020-05-01 16:13:18 -0700252 // A cache of event log tags
253 LogTags log_tags;
Tom Cherry68630a02020-05-11 16:29:29 -0700254
Tom Cherry5a3db392020-05-01 17:03:20 -0700255 // Pruning configuration.
256 PruneList prune_list;
Tom Cherry68630a02020-05-11 16:29:29 -0700257
Tom Cherry64e90162020-05-07 14:44:43 -0700258 // Partial (required for chatty) or full logging statistics.
259 bool enable_full_log_statistics = __android_logger_property_get_bool(
260 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
261 BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
262 LogStatistics log_statistics(enable_full_log_statistics);
Tom Cherry1a12ae32020-05-01 16:13:18 -0700263
Mark Salyzyn0175b072014-02-26 09:50:16 -0800264 // Serves the purpose of managing the last logs times read on a
265 // socket connection, and as a reader lock on a range of log
266 // entries.
Tom Cherry68630a02020-05-11 16:29:29 -0700267 LogReaderList reader_list;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800268
Tom Cherry8f613462020-05-12 12:46:43 -0700269 // LogBuffer is the object which is responsible for holding all log entries.
270 LogBuffer* logBuf;
271 if (true) {
272 logBuf = new ChattyLogBuffer(&reader_list, &log_tags, &prune_list, &log_statistics);
273 } else {
274 logBuf = new SimpleLogBuffer(&reader_list, &log_tags, &log_statistics);
275 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800276
Mark Salyzyn0175b072014-02-26 09:50:16 -0800277 // LogReader listens on /dev/socket/logdr. When a client
278 // connects, log entries in the LogBuffer are written to the client.
Tom Cherry68630a02020-05-11 16:29:29 -0700279 LogReader* reader = new LogReader(logBuf, &reader_list);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800280 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700281 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800282 }
283
284 // LogListener listens on /dev/socket/logdw for client
285 // initiated log messages. New log entries are added to LogBuffer
286 // and LogReader is notified to send updates to connected clients.
Tom Cherry68630a02020-05-11 16:29:29 -0700287 LogListener* swl = new LogListener(logBuf);
Tom Cherryc64dd8e2020-05-06 12:04:09 -0700288 if (!swl->StartListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700289 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800290 }
291
292 // Command listener listens on /dev/socket/logd for incoming logd
293 // administrative commands.
Tom Cherry64e90162020-05-07 14:44:43 -0700294 CommandListener* cl = new CommandListener(logBuf, &log_tags, &prune_list, &log_statistics);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800295 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700296 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800297 }
298
William Roberts29d238d2013-02-08 09:45:26 +0900299 // LogAudit listens on NETLINK_AUDIT socket for selinux
300 // initiated log messages. New log entries are added to LogBuffer
301 // and LogReader is notified to send updates to connected clients.
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700302 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000303 if (auditd) {
Tom Cherry68630a02020-05-11 16:29:29 -0700304 int dmesg_fd = __android_logger_property_get_bool("ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
305 ? fdDmesg
306 : -1;
307 al = new LogAudit(logBuf, dmesg_fd, &log_statistics);
Sami Tolvanena742d102016-06-14 18:04:43 +0000308 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700309
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700310 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700311 if (klogd) {
Tom Cherry68630a02020-05-11 16:29:29 -0700312 kl = new LogKlog(logBuf, fdDmesg, fdPmesg, al != nullptr, &log_statistics);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700313 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700314
Sami Tolvanena742d102016-06-14 18:04:43 +0000315 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700316
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700317 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700318 if (kl && kl->startListener()) {
319 delete kl;
320 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700321
Sami Tolvanena742d102016-06-14 18:04:43 +0000322 if (al && al->startListener()) {
323 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900324 }
325
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700326 TEMP_FAILURE_RETRY(pause());
327
Elliott Hughescef62b42018-06-13 10:33:45 -0700328 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800329}