blob: 87131472bf37b2bf57e9b1db5788e73de0ecff3c [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
Jorge Lucangeli Obes2bbdbe82016-07-15 13:57:08 -040039#include <android-base/macros.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080040#include <cutils/android_get_control_file.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080041#include <cutils/properties.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070042#include <cutils/sockets.h>
Mark Salyzynff32f3c2015-04-13 14:24:45 -070043#include <log/event_tag_map.h>
William Robertsaeca97b2015-07-31 13:10:36 -070044#include <packagelistparser/packagelistparser.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070045#include <private/android_filesystem_config.h>
Mark Salyzyn5740a462016-03-28 15:42:08 -070046#include <private/android_logger.h>
Suren Baghdasaryan02843332018-12-21 12:30:16 -080047#include <processgroup/sched_policy.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070048#include <utils/threads.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080049
Mark Salyzyn0175b072014-02-26 09:50:16 -080050#include "CommandListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090051#include "LogAudit.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080052#include "LogBuffer.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070053#include "LogKlog.h"
Mark Salyzyn501c3732017-03-10 14:31:54 -080054#include "LogListener.h"
Tom Cherry1a12ae32020-05-01 16:13:18 -070055#include "LogTags.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070056#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080057
Mark Salyzyn501c3732017-03-10 14:31:54 -080058#define KMSG_PRIORITY(PRI) \
59 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
60 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070061
Tom Cherry0b2a0112019-06-06 13:41:20 -070062// The service is designed to be run by init, it does not respond well to starting up manually. Init
63// has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This
64// allows debuggers, etc to be attached to logd at the very beginning, while still having init
65// handle the user, groups, capabilities, files, etc setup.
Mark Salyzynd2b32912016-10-28 15:11:46 -070066static int drop_privs(bool klogd, bool auditd) {
Elliott Hughescef62b42018-06-13 10:33:45 -070067 sched_param param = {};
Mark Salyzyn882f8562013-12-26 15:13:36 -080068
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080069 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070070 android::prdebug("failed to set background scheduling policy");
Elliott Hughescef62b42018-06-13 10:33:45 -070071 return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080072 }
73
Mark Salyzyn501c3732017-03-10 14:31:54 -080074 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070075 android::prdebug("failed to set batch scheduler");
Elliott Hughescef62b42018-06-13 10:33:45 -070076 return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -080077 }
78
Mark Salyzyn4b42ea52018-08-15 12:17:18 -070079 if (!__android_logger_property_get_bool("ro.debuggable",
80 BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -070081 prctl(PR_SET_DUMPABLE, 0) == -1) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -070082 android::prdebug("failed to clear PR_SET_DUMPABLE");
83 return -1;
84 }
85
Tom Cherry0b2a0112019-06-06 13:41:20 -070086 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
87 if (cap_clear(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080088 return -1;
89 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070090 std::vector<cap_value_t> cap_value;
91 if (klogd) {
92 cap_value.emplace_back(CAP_SYSLOG);
93 }
94 if (auditd) {
95 cap_value.emplace_back(CAP_AUDIT_CONTROL);
96 }
97
98 if (cap_set_flag(caps.get(), CAP_PERMITTED, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
99 return -1;
100 }
101 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800102 return -1;
103 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700104 if (cap_set_proc(caps.get()) < 0) {
Tom Cherry0b2a0112019-06-06 13:41:20 -0700105 android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700106 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700107 }
108
Mark Salyzyn0175b072014-02-26 09:50:16 -0800109 return 0;
110}
111
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700112// Property helper
Mark Salyzyn501c3732017-03-10 14:31:54 -0800113static bool check_flag(const char* prop, const char* flag) {
114 const char* cp = strcasestr(prop, flag);
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800115 if (!cp) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700116 return false;
117 }
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800118 // We only will document comma (,)
119 static const char sep[] = ",:;|+ \t\f";
120 if ((cp != prop) && !strchr(sep, cp[-1])) {
121 return false;
122 }
123 cp += strlen(flag);
124 return !*cp || !!strchr(sep, *cp);
125}
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700126
Mark Salyzynccbadc62015-03-12 12:25:35 -0700127static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800128void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800129 if (fdDmesg < 0) {
130 return;
131 }
132
133 static const char message[] = {
134 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
135 };
136 char buffer[256];
137 memcpy(buffer, message, sizeof(message));
138
139 va_list ap;
140 va_start(ap, fmt);
141 int n = vsnprintf(buffer + sizeof(message),
142 sizeof(buffer) - sizeof(message), fmt, ap);
143 va_end(ap);
144 if (n > 0) {
145 buffer[sizeof(buffer) - 1] = '\0';
146 if (!strchr(buffer, '\n')) {
147 buffer[sizeof(buffer) - 2] = '\0';
148 strlcat(buffer, "\n", sizeof(buffer));
149 }
150 write(fdDmesg, buffer, strlen(buffer));
151 }
152}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700153
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700154static sem_t reinit;
155static bool reinit_running = false;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700156static LogBuffer* logBuf = nullptr;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700157
Mark Salyzyn501c3732017-03-10 14:31:54 -0800158static void* reinit_thread_start(void* /*obj*/) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700159 prctl(PR_SET_NAME, "logd.daemon");
Mark Salyzynd8f01802016-10-31 13:49:44 -0700160
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700161 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzynccbadc62015-03-12 12:25:35 -0700162 if (fdDmesg >= 0) {
163 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -0800164 'l',
165 'o',
166 'g',
167 'd',
168 '.',
169 'd',
170 'a',
171 'e',
172 'm',
173 'o',
174 'n',
175 ':',
176 ' ',
177 'r',
178 'e',
179 'i',
180 'n',
181 'i',
182 't',
183 '\n' };
Mark Salyzynccbadc62015-03-12 12:25:35 -0700184 write(fdDmesg, reinit_message, sizeof(reinit_message));
185 }
186
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700187 // Anything that reads persist.<property>
188 if (logBuf) {
189 logBuf->init();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700190 logBuf->initPrune(nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700191 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700192 android::ReReadEventLogTags();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700193 }
194
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700195 return nullptr;
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700196}
197
Mark Salyzyn501c3732017-03-10 14:31:54 -0800198char* android::uidToName(uid_t u) {
Tom Cherry36f53992017-09-06 10:07:37 -0700199 struct Userdata {
200 uid_t uid;
201 char* name;
202 } userdata = {
203 .uid = u,
204 .name = nullptr,
205 };
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700206
Tom Cherry36f53992017-09-06 10:07:37 -0700207 packagelist_parse(
208 [](pkg_info* info, void* callback_parameter) {
209 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
210 bool result = true;
211 if (info->uid == userdata->uid) {
212 userdata->name = strdup(info->name);
213 // false to stop processing
214 result = false;
215 }
216 packagelist_free(info);
217 return result;
218 },
219 &userdata);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700220
Tom Cherry36f53992017-09-06 10:07:37 -0700221 return userdata.name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700222}
223
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700224// Serves as a global method to trigger reinitialization
225// and as a function that can be provided to signal().
226void reinit_signal_handler(int /*signal*/) {
227 sem_post(&reinit);
228}
229
Mark Salyzyn501c3732017-03-10 14:31:54 -0800230static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700231 if (!al && !kl) {
232 return;
233 }
234
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700235 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700236 if (rc <= 0) {
237 return;
238 }
239
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700240 // Margin for additional input race or trailing nul
241 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800242 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700243
244 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
245 if (rc <= 0) {
246 return;
247 }
248
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700249 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700250 len = rc + 1;
251 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700252 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700253
Mark Salyzynb6bee332015-09-08 08:56:32 -0700254 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700255 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700256 }
257
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700258 ssize_t sublen;
259 for (char *ptr = nullptr, *tok = buf.get();
260 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
261 tok = nullptr) {
262 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700263 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700264 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700265 }
266 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700267 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700268 }
269 }
270}
271
Mark Salyzynd8f01802016-10-31 13:49:44 -0700272static int issueReinit() {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800273 int sock = TEMP_FAILURE_RETRY(socket_local_client(
274 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700275 if (sock < 0) return -errno;
276
277 static const char reinitStr[] = "reinit";
278 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
279 if (ret < 0) return -errno;
280
281 struct pollfd p;
282 memset(&p, 0, sizeof(p));
283 p.fd = sock;
284 p.events = POLLIN;
285 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
286 if (ret < 0) return -errno;
287 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
288
289 static const char success[] = "success";
290 char buffer[sizeof(success) - 1];
291 memset(buffer, 0, sizeof(buffer));
292 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
293 if (ret < 0) return -errno;
294
295 return strncmp(buffer, success, sizeof(success) - 1) != 0;
296}
297
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700298// Foreground waits for exit of the main persistent threads
299// that are started here. The threads are created to manage
300// UNIX domain client sockets for writing, reading and
301// controlling the user space logger, and for any additional
302// logging plugins like auditd and restart control. Additional
303// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800304int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900305 // logd is written under the assumption that the timezone is UTC.
306 // If TZ is not set, persist.sys.timezone is looked up in some time utility
307 // libc functions, including mktime. It confuses the logd time handling,
308 // so here explicitly set TZ to UTC, which overrides the property.
309 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700310 // issue reinit command. KISS argument parsing.
311 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700312 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700313 }
314
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700315 static const char dev_kmsg[] = "/dev/kmsg";
316 fdDmesg = android_get_control_file(dev_kmsg);
317 if (fdDmesg < 0) {
318 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
319 }
320
321 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800322 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800323 "ro.logd.kernel",
324 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700325 if (klogd) {
326 static const char proc_kmsg[] = "/proc/kmsg";
327 fdPmesg = android_get_control_file(proc_kmsg);
328 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800329 fdPmesg = TEMP_FAILURE_RETRY(
330 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700331 }
332 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
333 }
334
Tom Cherry0b2a0112019-06-06 13:41:20 -0700335 bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
336 if (drop_privs(klogd, auditd) != 0) {
337 return EXIT_FAILURE;
338 }
339
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700340 // Reinit Thread
341 sem_init(&reinit, 0, 0);
342 pthread_attr_t attr;
343 if (!pthread_attr_init(&attr)) {
344 struct sched_param param;
345
346 memset(&param, 0, sizeof(param));
347 pthread_attr_setschedparam(&attr, &param);
348 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800349 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700350 pthread_t thread;
351 reinit_running = true;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700352 if (pthread_create(&thread, &attr, reinit_thread_start, nullptr)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700353 reinit_running = false;
354 }
355 }
356 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700357 }
358
Tom Cherry1a12ae32020-05-01 16:13:18 -0700359 // A cache of event log tags
360 LogTags log_tags;
361
Mark Salyzyn0175b072014-02-26 09:50:16 -0800362 // Serves the purpose of managing the last logs times read on a
363 // socket connection, and as a reader lock on a range of log
364 // entries.
365
Mark Salyzyn501c3732017-03-10 14:31:54 -0800366 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800367
368 // LogBuffer is the object which is responsible for holding all
369 // log entries.
370
Tom Cherry1a12ae32020-05-01 16:13:18 -0700371 logBuf = new LogBuffer(times, &log_tags);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700372
373 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800374
Mark Salyzyn501c3732017-03-10 14:31:54 -0800375 if (__android_logger_property_get_bool(
376 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
377 BOOL_DEFAULT_FLAG_ENG |
378 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700379 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700380 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800381
Mark Salyzyn0175b072014-02-26 09:50:16 -0800382 // LogReader listens on /dev/socket/logdr. When a client
383 // connects, log entries in the LogBuffer are written to the client.
384
Mark Salyzyn501c3732017-03-10 14:31:54 -0800385 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800386 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700387 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800388 }
389
390 // LogListener listens on /dev/socket/logdw for client
391 // initiated log messages. New log entries are added to LogBuffer
392 // and LogReader is notified to send updates to connected clients.
393
Mark Salyzyn501c3732017-03-10 14:31:54 -0800394 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800395 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700396 if (swl->startListener(600)) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700397 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800398 }
399
400 // Command listener listens on /dev/socket/logd for incoming logd
401 // administrative commands.
402
Tom Cherry1a12ae32020-05-01 16:13:18 -0700403 CommandListener* cl = new CommandListener(logBuf, &log_tags);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800404 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700405 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800406 }
407
William Roberts29d238d2013-02-08 09:45:26 +0900408 // LogAudit listens on NETLINK_AUDIT socket for selinux
409 // initiated log messages. New log entries are added to LogBuffer
410 // and LogReader is notified to send updates to connected clients.
411
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700412 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000413 if (auditd) {
414 al = new LogAudit(logBuf, reader,
Mark Salyzynf10e2732016-09-27 13:08:23 -0700415 __android_logger_property_get_bool(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800416 "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
417 ? fdDmesg
418 : -1);
Sami Tolvanena742d102016-06-14 18:04:43 +0000419 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700420
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700421 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700422 if (klogd) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700423 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != nullptr);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700424 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700425
Sami Tolvanena742d102016-06-14 18:04:43 +0000426 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700427
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700428 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700429
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700430 if (kl && kl->startListener()) {
431 delete kl;
432 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700433
Sami Tolvanena742d102016-06-14 18:04:43 +0000434 if (al && al->startListener()) {
435 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900436 }
437
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700438 TEMP_FAILURE_RETRY(pause());
439
Elliott Hughescef62b42018-06-13 10:33:45 -0700440 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800441}