Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 20 | #include <linux/capability.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 21 | #include <poll.h> |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 22 | #include <sched.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 23 | #include <semaphore.h> |
| 24 | #include <signal.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/capability.h> |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 29 | #include <sys/klog.h> |
Elliott Hughes | e5a0f20 | 2014-07-18 17:39:41 -0700 | [diff] [blame] | 30 | #include <sys/prctl.h> |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 34 | #include <syslog.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 35 | #include <unistd.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 37 | #include <memory> |
| 38 | |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Jorge Lucangeli Obes | 2bbdbe8 | 2016-07-15 13:57:08 -0400 | [diff] [blame] | 40 | #include <android-base/macros.h> |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 43 | #include <cutils/android_get_control_file.h> |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 44 | #include <cutils/sockets.h> |
Mark Salyzyn | ff32f3c | 2015-04-13 14:24:45 -0700 | [diff] [blame] | 45 | #include <log/event_tag_map.h> |
William Roberts | aeca97b | 2015-07-31 13:10:36 -0700 | [diff] [blame] | 46 | #include <packagelistparser/packagelistparser.h> |
Mark Salyzyn | e3aeeee | 2015-03-17 07:56:32 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 5740a46 | 2016-03-28 15:42:08 -0700 | [diff] [blame] | 48 | #include <private/android_logger.h> |
Suren Baghdasaryan | 0284333 | 2018-12-21 12:30:16 -0800 | [diff] [blame] | 49 | #include <processgroup/sched_policy.h> |
Riley Andrews | d98f4e8 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 50 | #include <utils/threads.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 51 | |
Tom Cherry | d5b3838 | 2020-05-12 13:16:41 -0700 | [diff] [blame] | 52 | #include "ChattyLogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 53 | #include "CommandListener.h" |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 54 | #include "LogAudit.h" |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 55 | #include "LogBuffer.h" |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 56 | #include "LogKlog.h" |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 57 | #include "LogListener.h" |
Tom Cherry | 283c9a1 | 2020-05-14 19:25:05 -0700 | [diff] [blame] | 58 | #include "LogReader.h" |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 59 | #include "LogStatistics.h" |
Tom Cherry | 1a12ae3 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 60 | #include "LogTags.h" |
Mark Salyzyn | 5ac5c6b | 2015-08-28 08:02:59 -0700 | [diff] [blame] | 61 | #include "LogUtils.h" |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 62 | #include "SerializedLogBuffer.h" |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 63 | #include "SimpleLogBuffer.h" |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 64 | |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 65 | using android::base::GetProperty; |
| 66 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 67 | #define KMSG_PRIORITY(PRI) \ |
| 68 | '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \ |
| 69 | '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>' |
Mark Salyzyn | ccbadc6 | 2015-03-12 12:25:35 -0700 | [diff] [blame] | 70 | |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 71 | // The service is designed to be run by init, it does not respond well to starting up manually. Init |
| 72 | // has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This |
| 73 | // allows debuggers, etc to be attached to logd at the very beginning, while still having init |
| 74 | // handle the user, groups, capabilities, files, etc setup. |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 75 | static void DropPrivs(bool klogd, bool auditd) { |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 76 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 77 | PLOG(FATAL) << "failed to set background scheduling policy"; |
Mark Salyzyn | 56ba4b5 | 2015-01-30 15:19:48 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 80 | sched_param param = {}; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 81 | if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 82 | PLOG(FATAL) << "failed to set batch scheduler"; |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 85 | if (!__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) && |
Elliott Hughes | cef62b4 | 2018-06-13 10:33:45 -0700 | [diff] [blame] | 86 | prctl(PR_SET_DUMPABLE, 0) == -1) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 87 | PLOG(FATAL) << "failed to clear PR_SET_DUMPABLE"; |
Mark Salyzyn | 6a70ded | 2016-10-28 14:49:53 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 90 | std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free); |
| 91 | if (cap_clear(caps.get()) < 0) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 92 | PLOG(FATAL) << "cap_clear() failed"; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 93 | } |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 94 | if (klogd) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 95 | cap_value_t cap_syslog = CAP_SYSLOG; |
| 96 | if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, &cap_syslog, CAP_SET) < 0 || |
| 97 | cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, &cap_syslog, CAP_SET) < 0) { |
| 98 | PLOG(FATAL) << "Failed to set CAP_SYSLOG"; |
| 99 | } |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 100 | } |
| 101 | if (auditd) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 102 | cap_value_t cap_audit_control = CAP_AUDIT_CONTROL; |
| 103 | if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, &cap_audit_control, CAP_SET) < 0 || |
| 104 | cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, &cap_audit_control, CAP_SET) < 0) { |
| 105 | PLOG(FATAL) << "Failed to set CAP_AUDIT_CONTROL"; |
| 106 | } |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 107 | } |
Mark Salyzyn | f0b8e1b | 2016-10-28 14:49:53 -0700 | [diff] [blame] | 108 | if (cap_set_proc(caps.get()) < 0) { |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 109 | PLOG(FATAL) << "cap_set_proc() failed"; |
Mark Salyzyn | f0b8e1b | 2016-10-28 14:49:53 -0700 | [diff] [blame] | 110 | } |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 113 | char* android::uidToName(uid_t u) { |
Tom Cherry | 36f5399 | 2017-09-06 10:07:37 -0700 | [diff] [blame] | 114 | struct Userdata { |
| 115 | uid_t uid; |
| 116 | char* name; |
| 117 | } userdata = { |
| 118 | .uid = u, |
| 119 | .name = nullptr, |
| 120 | }; |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 121 | |
Tom Cherry | 36f5399 | 2017-09-06 10:07:37 -0700 | [diff] [blame] | 122 | packagelist_parse( |
| 123 | [](pkg_info* info, void* callback_parameter) { |
| 124 | auto userdata = reinterpret_cast<Userdata*>(callback_parameter); |
| 125 | bool result = true; |
| 126 | if (info->uid == userdata->uid) { |
| 127 | userdata->name = strdup(info->name); |
| 128 | // false to stop processing |
| 129 | result = false; |
| 130 | } |
| 131 | packagelist_free(info); |
| 132 | return result; |
| 133 | }, |
| 134 | &userdata); |
Mark Salyzyn | 95108f1 | 2015-04-20 07:26:27 -0700 | [diff] [blame] | 135 | |
Tom Cherry | 36f5399 | 2017-09-06 10:07:37 -0700 | [diff] [blame] | 136 | return userdata.name; |
Mark Salyzyn | 08739ba | 2015-03-16 08:26:05 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 139 | static void readDmesg(LogAudit* al, LogKlog* kl) { |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 140 | if (!al && !kl) { |
| 141 | return; |
| 142 | } |
| 143 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 144 | int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 145 | if (rc <= 0) { |
| 146 | return; |
| 147 | } |
| 148 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 149 | // Margin for additional input race or trailing nul |
| 150 | ssize_t len = rc + 1024; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 151 | std::unique_ptr<char[]> buf(new char[len]); |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 152 | |
| 153 | rc = klogctl(KLOG_READ_ALL, buf.get(), len); |
| 154 | if (rc <= 0) { |
| 155 | return; |
| 156 | } |
| 157 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 158 | if (rc < len) { |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 159 | len = rc + 1; |
| 160 | } |
Mark Salyzyn | ea1a241 | 2015-09-02 07:39:53 -0700 | [diff] [blame] | 161 | buf[--len] = '\0'; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 162 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 163 | ssize_t sublen; |
| 164 | for (char *ptr = nullptr, *tok = buf.get(); |
| 165 | (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen)); |
| 166 | tok = nullptr) { |
| 167 | if ((sublen <= 0) || !*tok) continue; |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 168 | if (al) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 169 | rc = al->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 170 | } |
| 171 | if (kl) { |
Mark Salyzyn | 151beac | 2015-09-04 11:37:42 -0700 | [diff] [blame] | 172 | rc = kl->log(tok, sublen); |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 177 | static int issueReinit() { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 178 | int sock = TEMP_FAILURE_RETRY(socket_local_client( |
| 179 | "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM)); |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 180 | if (sock < 0) return -errno; |
| 181 | |
| 182 | static const char reinitStr[] = "reinit"; |
| 183 | ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr))); |
| 184 | if (ret < 0) return -errno; |
| 185 | |
| 186 | struct pollfd p; |
| 187 | memset(&p, 0, sizeof(p)); |
| 188 | p.fd = sock; |
| 189 | p.events = POLLIN; |
| 190 | ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000)); |
| 191 | if (ret < 0) return -errno; |
| 192 | if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME; |
| 193 | |
| 194 | static const char success[] = "success"; |
| 195 | char buffer[sizeof(success) - 1]; |
| 196 | memset(buffer, 0, sizeof(buffer)); |
| 197 | ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer))); |
| 198 | if (ret < 0) return -errno; |
| 199 | |
| 200 | return strncmp(buffer, success, sizeof(success) - 1) != 0; |
| 201 | } |
| 202 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 203 | // Foreground waits for exit of the main persistent threads |
| 204 | // that are started here. The threads are created to manage |
| 205 | // UNIX domain client sockets for writing, reading and |
| 206 | // controlling the user space logger, and for any additional |
| 207 | // logging plugins like auditd and restart control. Additional |
| 208 | // transitory per-client threads are created for each reader. |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 209 | int main(int argc, char* argv[]) { |
Hidehiko Abe | 352476e | 2017-03-29 17:41:17 +0900 | [diff] [blame] | 210 | // logd is written under the assumption that the timezone is UTC. |
| 211 | // If TZ is not set, persist.sys.timezone is looked up in some time utility |
| 212 | // libc functions, including mktime. It confuses the logd time handling, |
| 213 | // so here explicitly set TZ to UTC, which overrides the property. |
| 214 | setenv("TZ", "UTC", 1); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 215 | // issue reinit command. KISS argument parsing. |
| 216 | if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { |
Mark Salyzyn | d8f0180 | 2016-10-31 13:49:44 -0700 | [diff] [blame] | 217 | return issueReinit(); |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 220 | android::base::InitLogging( |
| 221 | argv, [](android::base::LogId log_id, android::base::LogSeverity severity, |
| 222 | const char* tag, const char* file, unsigned int line, const char* message) { |
| 223 | if (tag && strcmp(tag, "logd") != 0) { |
| 224 | auto prefixed_message = android::base::StringPrintf("%s: %s", tag, message); |
| 225 | android::base::KernelLogger(log_id, severity, "logd", file, line, |
| 226 | prefixed_message.c_str()); |
| 227 | } else { |
| 228 | android::base::KernelLogger(log_id, severity, "logd", file, line, message); |
| 229 | } |
| 230 | }); |
| 231 | |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 232 | static const char dev_kmsg[] = "/dev/kmsg"; |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 233 | int fdDmesg = android_get_control_file(dev_kmsg); |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 234 | if (fdDmesg < 0) { |
| 235 | fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC)); |
| 236 | } |
| 237 | |
| 238 | int fdPmesg = -1; |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 239 | bool klogd = __android_logger_property_get_bool( |
Siarhei Vishniakou | e8ed36b | 2017-12-28 14:13:22 -0800 | [diff] [blame] | 240 | "ro.logd.kernel", |
| 241 | BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE); |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 242 | if (klogd) { |
| 243 | static const char proc_kmsg[] = "/proc/kmsg"; |
| 244 | fdPmesg = android_get_control_file(proc_kmsg); |
| 245 | if (fdPmesg < 0) { |
Mark Salyzyn | 501c373 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 246 | fdPmesg = TEMP_FAILURE_RETRY( |
| 247 | open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC)); |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 248 | } |
Tom Cherry | f93b400 | 2020-06-03 09:23:49 -0700 | [diff] [blame] | 249 | if (fdPmesg < 0) PLOG(ERROR) << "Failed to open " << proc_kmsg; |
Mark Salyzyn | e0b8ccd | 2016-10-27 08:21:35 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 252 | bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE); |
Tom Cherry | 54b00a9 | 2020-06-22 10:15:04 -0700 | [diff] [blame] | 253 | DropPrivs(klogd, auditd); |
Tom Cherry | 0b2a011 | 2019-06-06 13:41:20 -0700 | [diff] [blame] | 254 | |
Tom Cherry | 1a12ae3 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 255 | // A cache of event log tags |
| 256 | LogTags log_tags; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 257 | |
Tom Cherry | 5a3db39 | 2020-05-01 17:03:20 -0700 | [diff] [blame] | 258 | // Pruning configuration. |
| 259 | PruneList prune_list; |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 260 | |
Tom Cherry | bc72197 | 2020-07-22 15:30:02 -0700 | [diff] [blame^] | 261 | std::string buffer_type = GetProperty("logd.buffer_type", "serialized"); |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 262 | |
Tom Cherry | 64e9016 | 2020-05-07 14:44:43 -0700 | [diff] [blame] | 263 | // Partial (required for chatty) or full logging statistics. |
| 264 | bool enable_full_log_statistics = __android_logger_property_get_bool( |
| 265 | "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST | |
| 266 | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE); |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 267 | LogStatistics log_statistics(enable_full_log_statistics, buffer_type == "serialized"); |
Tom Cherry | 1a12ae3 | 2020-05-01 16:13:18 -0700 | [diff] [blame] | 268 | |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 269 | // Serves the purpose of managing the last logs times read on a socket connection, and as a |
| 270 | // reader lock on a range of log entries. |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 271 | LogReaderList reader_list; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 272 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 273 | // LogBuffer is the object which is responsible for holding all log entries. |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 274 | LogBuffer* log_buffer = nullptr; |
| 275 | if (buffer_type == "chatty") { |
| 276 | log_buffer = new ChattyLogBuffer(&reader_list, &log_tags, &prune_list, &log_statistics); |
| 277 | } else if (buffer_type == "serialized") { |
| 278 | log_buffer = new SerializedLogBuffer(&reader_list, &log_tags, &log_statistics); |
| 279 | } else if (buffer_type == "simple") { |
| 280 | log_buffer = new SimpleLogBuffer(&reader_list, &log_tags, &log_statistics); |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 281 | } else { |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 282 | LOG(FATAL) << "buffer_type must be one of 'chatty', 'serialized', or 'simple'"; |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 283 | } |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 284 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 285 | // LogReader listens on /dev/socket/logdr. When a client |
| 286 | // connects, log entries in the LogBuffer are written to the client. |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 287 | LogReader* reader = new LogReader(log_buffer, &reader_list); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 288 | if (reader->startListener()) { |
Elliott Hughes | cef62b4 | 2018-06-13 10:33:45 -0700 | [diff] [blame] | 289 | return EXIT_FAILURE; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // LogListener listens on /dev/socket/logdw for client |
| 293 | // initiated log messages. New log entries are added to LogBuffer |
| 294 | // and LogReader is notified to send updates to connected clients. |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 295 | LogListener* swl = new LogListener(log_buffer); |
Tom Cherry | c64dd8e | 2020-05-06 12:04:09 -0700 | [diff] [blame] | 296 | if (!swl->StartListener()) { |
Elliott Hughes | cef62b4 | 2018-06-13 10:33:45 -0700 | [diff] [blame] | 297 | return EXIT_FAILURE; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Command listener listens on /dev/socket/logd for incoming logd |
| 301 | // administrative commands. |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 302 | CommandListener* cl = new CommandListener(log_buffer, &log_tags, &prune_list, &log_statistics); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 303 | if (cl->startListener()) { |
Elliott Hughes | cef62b4 | 2018-06-13 10:33:45 -0700 | [diff] [blame] | 304 | return EXIT_FAILURE; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 305 | } |
| 306 | |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 307 | // LogAudit listens on NETLINK_AUDIT socket for selinux |
| 308 | // initiated log messages. New log entries are added to LogBuffer |
| 309 | // and LogReader is notified to send updates to connected clients. |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 310 | LogAudit* al = nullptr; |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 311 | if (auditd) { |
Tom Cherry | 68630a0 | 2020-05-11 16:29:29 -0700 | [diff] [blame] | 312 | int dmesg_fd = __android_logger_property_get_bool("ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE) |
| 313 | ? fdDmesg |
| 314 | : -1; |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 315 | al = new LogAudit(log_buffer, dmesg_fd, &log_statistics); |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 316 | } |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 317 | |
Mark Salyzyn | 0484b3b | 2016-08-11 08:02:06 -0700 | [diff] [blame] | 318 | LogKlog* kl = nullptr; |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 319 | if (klogd) { |
Tom Cherry | c1edd66 | 2020-07-14 09:52:24 -0700 | [diff] [blame] | 320 | kl = new LogKlog(log_buffer, fdDmesg, fdPmesg, al != nullptr, &log_statistics); |
Mark Salyzyn | a1aacb7 | 2014-10-15 08:49:39 -0700 | [diff] [blame] | 321 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 322 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 323 | readDmesg(al, kl); |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 324 | |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 325 | // failure is an option ... messages are in dmesg (required by standard) |
Mark Salyzyn | d5600fd | 2015-06-12 14:59:42 -0700 | [diff] [blame] | 326 | if (kl && kl->startListener()) { |
| 327 | delete kl; |
| 328 | } |
Mark Salyzyn | eb06de7 | 2014-10-13 09:59:37 -0700 | [diff] [blame] | 329 | |
Sami Tolvanen | a742d10 | 2016-06-14 18:04:43 +0000 | [diff] [blame] | 330 | if (al && al->startListener()) { |
| 331 | delete al; |
William Roberts | 29d238d | 2013-02-08 09:45:26 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Mark Salyzyn | 11e55cb | 2015-03-10 16:45:17 -0700 | [diff] [blame] | 334 | TEMP_FAILURE_RETRY(pause()); |
| 335 | |
Elliott Hughes | cef62b4 | 2018-06-13 10:33:45 -0700 | [diff] [blame] | 336 | return EXIT_SUCCESS; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 337 | } |