blob: 23bbf864d73a2ab8b65e65aa20eff920882e1de0 [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"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070055#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080056
Mark Salyzyn501c3732017-03-10 14:31:54 -080057#define KMSG_PRIORITY(PRI) \
58 '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
59 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, '>'
Mark Salyzynccbadc62015-03-12 12:25:35 -070060
Tom Cherry0b2a0112019-06-06 13:41:20 -070061// The service is designed to be run by init, it does not respond well to starting up manually. Init
62// has a 'sigstop' feature that sends SIGSTOP to a service immediately before calling exec(). This
63// allows debuggers, etc to be attached to logd at the very beginning, while still having init
64// handle the user, groups, capabilities, files, etc setup.
Mark Salyzynd2b32912016-10-28 15:11:46 -070065static int drop_privs(bool klogd, bool auditd) {
Elliott Hughescef62b42018-06-13 10:33:45 -070066 sched_param param = {};
Mark Salyzyn882f8562013-12-26 15:13:36 -080067
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080068 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070069 android::prdebug("failed to set background scheduling policy");
Elliott Hughescef62b42018-06-13 10:33:45 -070070 return -1;
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080071 }
72
Mark Salyzyn501c3732017-03-10 14:31:54 -080073 if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
Mark Salyzyn107e29a2016-10-28 15:51:03 -070074 android::prdebug("failed to set batch scheduler");
Elliott Hughescef62b42018-06-13 10:33:45 -070075 return -1;
Mark Salyzyn882f8562013-12-26 15:13:36 -080076 }
77
Mark Salyzyn4b42ea52018-08-15 12:17:18 -070078 if (!__android_logger_property_get_bool("ro.debuggable",
79 BOOL_DEFAULT_FALSE) &&
Elliott Hughescef62b42018-06-13 10:33:45 -070080 prctl(PR_SET_DUMPABLE, 0) == -1) {
Mark Salyzyn6a70ded2016-10-28 14:49:53 -070081 android::prdebug("failed to clear PR_SET_DUMPABLE");
82 return -1;
83 }
84
Tom Cherry0b2a0112019-06-06 13:41:20 -070085 std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
86 if (cap_clear(caps.get()) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -080087 return -1;
88 }
Tom Cherry0b2a0112019-06-06 13:41:20 -070089 std::vector<cap_value_t> cap_value;
90 if (klogd) {
91 cap_value.emplace_back(CAP_SYSLOG);
92 }
93 if (auditd) {
94 cap_value.emplace_back(CAP_AUDIT_CONTROL);
95 }
96
97 if (cap_set_flag(caps.get(), CAP_PERMITTED, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
98 return -1;
99 }
100 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, cap_value.size(), cap_value.data(), CAP_SET) < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800101 return -1;
102 }
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700103 if (cap_set_proc(caps.get()) < 0) {
Tom Cherry0b2a0112019-06-06 13:41:20 -0700104 android::prdebug("failed to set CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno);
Elliott Hughescef62b42018-06-13 10:33:45 -0700105 return -1;
Mark Salyzynf0b8e1b2016-10-28 14:49:53 -0700106 }
107
Mark Salyzyn0175b072014-02-26 09:50:16 -0800108 return 0;
109}
110
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700111// Property helper
Mark Salyzyn501c3732017-03-10 14:31:54 -0800112static bool check_flag(const char* prop, const char* flag) {
113 const char* cp = strcasestr(prop, flag);
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800114 if (!cp) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700115 return false;
116 }
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800117 // We only will document comma (,)
118 static const char sep[] = ",:;|+ \t\f";
119 if ((cp != prop) && !strchr(sep, cp[-1])) {
120 return false;
121 }
122 cp += strlen(flag);
123 return !*cp || !!strchr(sep, *cp);
124}
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700125
Mark Salyzynccbadc62015-03-12 12:25:35 -0700126static int fdDmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800127void android::prdebug(const char* fmt, ...) {
Mark Salyzynd048f112016-02-08 10:28:12 -0800128 if (fdDmesg < 0) {
129 return;
130 }
131
132 static const char message[] = {
133 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
134 };
135 char buffer[256];
136 memcpy(buffer, message, sizeof(message));
137
138 va_list ap;
139 va_start(ap, fmt);
140 int n = vsnprintf(buffer + sizeof(message),
141 sizeof(buffer) - sizeof(message), fmt, ap);
142 va_end(ap);
143 if (n > 0) {
144 buffer[sizeof(buffer) - 1] = '\0';
145 if (!strchr(buffer, '\n')) {
146 buffer[sizeof(buffer) - 2] = '\0';
147 strlcat(buffer, "\n", sizeof(buffer));
148 }
149 write(fdDmesg, buffer, strlen(buffer));
150 }
151}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700152
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700153static sem_t reinit;
154static bool reinit_running = false;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700155static LogBuffer* logBuf = nullptr;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700156
Mark Salyzyn501c3732017-03-10 14:31:54 -0800157static void* reinit_thread_start(void* /*obj*/) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700158 prctl(PR_SET_NAME, "logd.daemon");
Mark Salyzynd8f01802016-10-31 13:49:44 -0700159
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700160 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzynccbadc62015-03-12 12:25:35 -0700161 if (fdDmesg >= 0) {
162 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
Mark Salyzyn501c3732017-03-10 14:31:54 -0800163 'l',
164 'o',
165 'g',
166 'd',
167 '.',
168 'd',
169 'a',
170 'e',
171 'm',
172 'o',
173 'n',
174 ':',
175 ' ',
176 'r',
177 'e',
178 'i',
179 'n',
180 'i',
181 't',
182 '\n' };
Mark Salyzynccbadc62015-03-12 12:25:35 -0700183 write(fdDmesg, reinit_message, sizeof(reinit_message));
184 }
185
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700186 // Anything that reads persist.<property>
187 if (logBuf) {
188 logBuf->init();
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700189 logBuf->initPrune(nullptr);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700190 }
Mark Salyzyn61e9ce62016-09-12 14:51:54 -0700191 android::ReReadEventLogTags();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700192 }
193
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700194 return nullptr;
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700195}
196
Mark Salyzyn501c3732017-03-10 14:31:54 -0800197char* android::uidToName(uid_t u) {
Tom Cherry36f53992017-09-06 10:07:37 -0700198 struct Userdata {
199 uid_t uid;
200 char* name;
201 } userdata = {
202 .uid = u,
203 .name = nullptr,
204 };
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700205
Tom Cherry36f53992017-09-06 10:07:37 -0700206 packagelist_parse(
207 [](pkg_info* info, void* callback_parameter) {
208 auto userdata = reinterpret_cast<Userdata*>(callback_parameter);
209 bool result = true;
210 if (info->uid == userdata->uid) {
211 userdata->name = strdup(info->name);
212 // false to stop processing
213 result = false;
214 }
215 packagelist_free(info);
216 return result;
217 },
218 &userdata);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700219
Tom Cherry36f53992017-09-06 10:07:37 -0700220 return userdata.name;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700221}
222
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700223// Serves as a global method to trigger reinitialization
224// and as a function that can be provided to signal().
225void reinit_signal_handler(int /*signal*/) {
226 sem_post(&reinit);
227}
228
Mark Salyzyn501c3732017-03-10 14:31:54 -0800229static void readDmesg(LogAudit* al, LogKlog* kl) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700230 if (!al && !kl) {
231 return;
232 }
233
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700234 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700235 if (rc <= 0) {
236 return;
237 }
238
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700239 // Margin for additional input race or trailing nul
240 ssize_t len = rc + 1024;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800241 std::unique_ptr<char[]> buf(new char[len]);
Mark Salyzynea1a2412015-09-02 07:39:53 -0700242
243 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
244 if (rc <= 0) {
245 return;
246 }
247
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700248 if (rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700249 len = rc + 1;
250 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700251 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700252
Mark Salyzynb6bee332015-09-08 08:56:32 -0700253 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700254 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700255 }
256
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700257 ssize_t sublen;
258 for (char *ptr = nullptr, *tok = buf.get();
259 (rc >= 0) && !!(tok = android::log_strntok_r(tok, len, ptr, sublen));
260 tok = nullptr) {
261 if ((sublen <= 0) || !*tok) continue;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700262 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700263 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700264 }
265 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700266 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700267 }
268 }
269}
270
Mark Salyzynd8f01802016-10-31 13:49:44 -0700271static int issueReinit() {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800272 int sock = TEMP_FAILURE_RETRY(socket_local_client(
273 "logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
Mark Salyzynd8f01802016-10-31 13:49:44 -0700274 if (sock < 0) return -errno;
275
276 static const char reinitStr[] = "reinit";
277 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinitStr, sizeof(reinitStr)));
278 if (ret < 0) return -errno;
279
280 struct pollfd p;
281 memset(&p, 0, sizeof(p));
282 p.fd = sock;
283 p.events = POLLIN;
284 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
285 if (ret < 0) return -errno;
286 if ((ret == 0) || !(p.revents & POLLIN)) return -ETIME;
287
288 static const char success[] = "success";
289 char buffer[sizeof(success) - 1];
290 memset(buffer, 0, sizeof(buffer));
291 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
292 if (ret < 0) return -errno;
293
294 return strncmp(buffer, success, sizeof(success) - 1) != 0;
295}
296
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700297// Foreground waits for exit of the main persistent threads
298// that are started here. The threads are created to manage
299// UNIX domain client sockets for writing, reading and
300// controlling the user space logger, and for any additional
301// logging plugins like auditd and restart control. Additional
302// transitory per-client threads are created for each reader.
Mark Salyzyn501c3732017-03-10 14:31:54 -0800303int main(int argc, char* argv[]) {
Hidehiko Abe352476e2017-03-29 17:41:17 +0900304 // logd is written under the assumption that the timezone is UTC.
305 // If TZ is not set, persist.sys.timezone is looked up in some time utility
306 // libc functions, including mktime. It confuses the logd time handling,
307 // so here explicitly set TZ to UTC, which overrides the property.
308 setenv("TZ", "UTC", 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700309 // issue reinit command. KISS argument parsing.
310 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
Mark Salyzynd8f01802016-10-31 13:49:44 -0700311 return issueReinit();
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700312 }
313
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700314 static const char dev_kmsg[] = "/dev/kmsg";
315 fdDmesg = android_get_control_file(dev_kmsg);
316 if (fdDmesg < 0) {
317 fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
318 }
319
320 int fdPmesg = -1;
Mark Salyzyn501c3732017-03-10 14:31:54 -0800321 bool klogd = __android_logger_property_get_bool(
Siarhei Vishniakoue8ed36b2017-12-28 14:13:22 -0800322 "ro.logd.kernel",
323 BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700324 if (klogd) {
325 static const char proc_kmsg[] = "/proc/kmsg";
326 fdPmesg = android_get_control_file(proc_kmsg);
327 if (fdPmesg < 0) {
Mark Salyzyn501c3732017-03-10 14:31:54 -0800328 fdPmesg = TEMP_FAILURE_RETRY(
329 open(proc_kmsg, O_RDONLY | O_NDELAY | O_CLOEXEC));
Mark Salyzyne0b8ccd2016-10-27 08:21:35 -0700330 }
331 if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
332 }
333
Tom Cherry0b2a0112019-06-06 13:41:20 -0700334 bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
335 if (drop_privs(klogd, auditd) != 0) {
336 return EXIT_FAILURE;
337 }
338
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700339 // Reinit Thread
340 sem_init(&reinit, 0, 0);
341 pthread_attr_t attr;
342 if (!pthread_attr_init(&attr)) {
343 struct sched_param param;
344
345 memset(&param, 0, sizeof(param));
346 pthread_attr_setschedparam(&attr, &param);
347 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
Mark Salyzyn501c3732017-03-10 14:31:54 -0800348 if (!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700349 pthread_t thread;
350 reinit_running = true;
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700351 if (pthread_create(&thread, &attr, reinit_thread_start, nullptr)) {
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700352 reinit_running = false;
353 }
354 }
355 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700356 }
357
Mark Salyzyn0175b072014-02-26 09:50:16 -0800358 // Serves the purpose of managing the last logs times read on a
359 // socket connection, and as a reader lock on a range of log
360 // entries.
361
Mark Salyzyn501c3732017-03-10 14:31:54 -0800362 LastLogTimes* times = new LastLogTimes();
Mark Salyzyn0175b072014-02-26 09:50:16 -0800363
364 // LogBuffer is the object which is responsible for holding all
365 // log entries.
366
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700367 logBuf = new LogBuffer(times);
368
369 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800370
Mark Salyzyn501c3732017-03-10 14:31:54 -0800371 if (__android_logger_property_get_bool(
372 "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
373 BOOL_DEFAULT_FLAG_ENG |
374 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700375 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700376 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800377
Mark Salyzyn0175b072014-02-26 09:50:16 -0800378 // LogReader listens on /dev/socket/logdr. When a client
379 // connects, log entries in the LogBuffer are written to the client.
380
Mark Salyzyn501c3732017-03-10 14:31:54 -0800381 LogReader* reader = new LogReader(logBuf);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800382 if (reader->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700383 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800384 }
385
386 // LogListener listens on /dev/socket/logdw for client
387 // initiated log messages. New log entries are added to LogBuffer
388 // and LogReader is notified to send updates to connected clients.
389
Mark Salyzyn501c3732017-03-10 14:31:54 -0800390 LogListener* swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800391 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700392 if (swl->startListener(600)) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700393 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800394 }
395
396 // Command listener listens on /dev/socket/logd for incoming logd
397 // administrative commands.
398
Mark Salyzyn501c3732017-03-10 14:31:54 -0800399 CommandListener* cl = new CommandListener(logBuf, reader, swl);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800400 if (cl->startListener()) {
Elliott Hughescef62b42018-06-13 10:33:45 -0700401 return EXIT_FAILURE;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800402 }
403
William Roberts29d238d2013-02-08 09:45:26 +0900404 // LogAudit listens on NETLINK_AUDIT socket for selinux
405 // initiated log messages. New log entries are added to LogBuffer
406 // and LogReader is notified to send updates to connected clients.
407
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700408 LogAudit* al = nullptr;
Sami Tolvanena742d102016-06-14 18:04:43 +0000409 if (auditd) {
410 al = new LogAudit(logBuf, reader,
Mark Salyzynf10e2732016-09-27 13:08:23 -0700411 __android_logger_property_get_bool(
Mark Salyzyn501c3732017-03-10 14:31:54 -0800412 "ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
413 ? fdDmesg
414 : -1);
Sami Tolvanena742d102016-06-14 18:04:43 +0000415 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700416
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700417 LogKlog* kl = nullptr;
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700418 if (klogd) {
Mark Salyzyn0484b3b2016-08-11 08:02:06 -0700419 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != nullptr);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700420 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700421
Sami Tolvanena742d102016-06-14 18:04:43 +0000422 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700423
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700424 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700425
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700426 if (kl && kl->startListener()) {
427 delete kl;
428 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700429
Sami Tolvanena742d102016-06-14 18:04:43 +0000430 if (al && al->startListener()) {
431 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900432 }
433
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700434 TEMP_FAILURE_RETRY(pause());
435
Elliott Hughescef62b42018-06-13 10:33:45 -0700436 return EXIT_SUCCESS;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800437}