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