blob: 19946b7f14f197356a86b5eabdd0196f7e634f5f [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>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070020#include <poll.h>
Mark Salyzyn882f8562013-12-26 15:13:36 -080021#include <sched.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070022#include <semaphore.h>
23#include <signal.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/capability.h>
Mark Salyzyneb06de72014-10-13 09:59:37 -070028#include <sys/klog.h>
Elliott Hughese5a0f202014-07-18 17:39:41 -070029#include <sys/prctl.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070030#include <sys/resource.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080031#include <sys/stat.h>
32#include <sys/types.h>
Mark Salyzynccbadc62015-03-12 12:25:35 -070033#include <syslog.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080034#include <unistd.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080035
William Robertsaeca97b2015-07-31 13:10:36 -070036#include <cstdbool>
Mark Salyzynd5600fd2015-06-12 14:59:42 -070037#include <memory>
38
Mark Salyzyne457b742014-02-19 17:18:31 -080039#include <cutils/properties.h>
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080040#include <cutils/sched_policy.h>
Mark Salyzyn11e55cb2015-03-10 16:45:17 -070041#include <cutils/sockets.h>
Mark Salyzynff32f3c2015-04-13 14:24:45 -070042#include <log/event_tag_map.h>
William Robertsaeca97b2015-07-31 13:10:36 -070043#include <packagelistparser/packagelistparser.h>
Mark Salyzyne3aeeee2015-03-17 07:56:32 -070044#include <private/android_filesystem_config.h>
Riley Andrewsd98f4e82015-06-08 23:36:34 -070045#include <utils/threads.h>
Mark Salyzyne457b742014-02-19 17:18:31 -080046
Mark Salyzyn0175b072014-02-26 09:50:16 -080047#include "CommandListener.h"
48#include "LogBuffer.h"
49#include "LogListener.h"
William Roberts29d238d2013-02-08 09:45:26 +090050#include "LogAudit.h"
Mark Salyzyna1aacb72014-10-15 08:49:39 -070051#include "LogKlog.h"
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070052#include "LogUtils.h"
Mark Salyzyn0175b072014-02-26 09:50:16 -080053
Mark Salyzynccbadc62015-03-12 12:25:35 -070054#define KMSG_PRIORITY(PRI) \
55 '<', \
56 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
57 '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
58 '>'
59
Mark Salyzyndfc47e82014-03-24 10:26:47 -070060//
61// The service is designed to be run by init, it does not respond well
62// to starting up manually. When starting up manually the sockets will
63// fail to open typically for one of the following reasons:
64// EADDRINUSE if logger is running.
65// EACCESS if started without precautions (below)
66//
67// Here is a cookbook procedure for starting up logd manually assuming
68// init is out of the way, pedantically all permissions and selinux
69// security is put back in place:
70//
71// setenforce 0
72// rm /dev/socket/logd*
73// chmod 777 /dev/socket
74// # here is where you would attach the debugger or valgrind for example
75// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
76// sleep 1
77// chmod 755 /dev/socket
78// chown logd.logd /dev/socket/logd*
79// restorecon /dev/socket/logd*
80// setenforce 1
81//
82// If minimalism prevails, typical for debugging and security is not a concern:
83//
84// setenforce 0
85// chmod 777 /dev/socket
86// logd
87//
88
Mark Salyzyn0175b072014-02-26 09:50:16 -080089static int drop_privs() {
Mark Salyzyn882f8562013-12-26 15:13:36 -080090 struct sched_param param;
91 memset(&param, 0, sizeof(param));
92
Mark Salyzyn56ba4b52015-01-30 15:19:48 -080093 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
94 return -1;
95 }
96
Mark Salyzyn882f8562013-12-26 15:13:36 -080097 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
98 return -1;
99 }
100
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700101 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
102 return -1;
103 }
104
Mark Salyzyn0175b072014-02-26 09:50:16 -0800105 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
106 return -1;
107 }
108
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800109 gid_t groups[] = { AID_READPROC };
110
111 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) == -1) {
Jeff Vander Stoep3f62a022015-07-23 15:18:36 -0700112 return -1;
113 }
114
Mark Salyzyn0175b072014-02-26 09:50:16 -0800115 if (setgid(AID_LOGD) != 0) {
116 return -1;
117 }
118
119 if (setuid(AID_LOGD) != 0) {
120 return -1;
121 }
122
123 struct __user_cap_header_struct capheader;
124 struct __user_cap_data_struct capdata[2];
125 memset(&capheader, 0, sizeof(capheader));
126 memset(&capdata, 0, sizeof(capdata));
127 capheader.version = _LINUX_CAPABILITY_VERSION_3;
128 capheader.pid = 0;
129
130 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
William Roberts29d238d2013-02-08 09:45:26 +0900131 capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL);
132
133 capdata[0].effective = capdata[0].permitted;
134 capdata[1].effective = capdata[1].permitted;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800135 capdata[0].inheritable = 0;
136 capdata[1].inheritable = 0;
137
138 if (capset(&capheader, &capdata[0]) < 0) {
139 return -1;
140 }
141
142 return 0;
143}
144
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700145// Property helper
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800146static bool check_flag(const char *prop, const char *flag) {
147 const char *cp = strcasestr(prop, flag);
148 if (!cp) {
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700149 return false;
150 }
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800151 // We only will document comma (,)
152 static const char sep[] = ",:;|+ \t\f";
153 if ((cp != prop) && !strchr(sep, cp[-1])) {
154 return false;
155 }
156 cp += strlen(flag);
157 return !*cp || !!strchr(sep, *cp);
158}
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700159
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800160bool property_get_bool(const char *key, int flag) {
161 char def[PROPERTY_VALUE_MAX];
162 char property[PROPERTY_VALUE_MAX];
163 def[0] = '\0';
164 if (flag & BOOL_DEFAULT_FLAG_PERSIST) {
165 char newkey[PROPERTY_KEY_MAX];
166 snprintf(newkey, sizeof(newkey), "ro.%s", key);
167 property_get(newkey, property, "");
Mark Salyzyn07522c62016-03-02 07:51:48 -0800168 // persist properties set by /data require inoculation with
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800169 // logd-reinit. They may be set in init.rc early and function, but
170 // otherwise are defunct unless reset. Do not rely on persist
171 // properties for startup-only keys unless you are willing to restart
172 // logd daemon (not advised).
173 snprintf(newkey, sizeof(newkey), "persist.%s", key);
174 property_get(newkey, def, property);
175 }
176
177 property_get(key, property, def);
178
179 if (check_flag(property, "true")) {
180 return true;
181 }
182 if (check_flag(property, "false")) {
183 return false;
184 }
185 if (check_flag(property, "eng")) {
186 flag |= BOOL_DEFAULT_FLAG_ENG;
187 }
188 // this is really a "not" flag
189 if (check_flag(property, "svelte")) {
190 flag |= BOOL_DEFAULT_FLAG_SVELTE;
191 }
192
193 // Sanity Check
194 if (flag & (BOOL_DEFAULT_FLAG_SVELTE | BOOL_DEFAULT_FLAG_ENG)) {
195 flag &= ~BOOL_DEFAULT_FLAG_TRUE_FALSE;
196 flag |= BOOL_DEFAULT_TRUE;
197 }
198
199 if ((flag & BOOL_DEFAULT_FLAG_SVELTE)
200 && property_get_bool("ro.config.low_ram",
201 BOOL_DEFAULT_FALSE)) {
202 return false;
203 }
204 if (flag & BOOL_DEFAULT_FLAG_ENG) {
Mark Salyzynd1f41d62016-02-09 15:49:29 -0800205 property_get("ro.debuggable", property, "");
206 if (strcmp(property, "1")) {
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800207 return false;
208 }
209 }
210
211 return (flag & BOOL_DEFAULT_FLAG_TRUE_FALSE) != BOOL_DEFAULT_FALSE;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700212}
213
Mark Salyzynccbadc62015-03-12 12:25:35 -0700214static int fdDmesg = -1;
Mark Salyzynd048f112016-02-08 10:28:12 -0800215void inline android::prdebug(const char *fmt, ...) {
216 if (fdDmesg < 0) {
217 return;
218 }
219
220 static const char message[] = {
221 KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
222 };
223 char buffer[256];
224 memcpy(buffer, message, sizeof(message));
225
226 va_list ap;
227 va_start(ap, fmt);
228 int n = vsnprintf(buffer + sizeof(message),
229 sizeof(buffer) - sizeof(message), fmt, ap);
230 va_end(ap);
231 if (n > 0) {
232 buffer[sizeof(buffer) - 1] = '\0';
233 if (!strchr(buffer, '\n')) {
234 buffer[sizeof(buffer) - 2] = '\0';
235 strlcat(buffer, "\n", sizeof(buffer));
236 }
237 write(fdDmesg, buffer, strlen(buffer));
238 }
239}
Mark Salyzynccbadc62015-03-12 12:25:35 -0700240
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700241static sem_t uidName;
242static uid_t uid;
243static char *name;
244
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700245static sem_t reinit;
246static bool reinit_running = false;
247static LogBuffer *logBuf = NULL;
Mark Salyzyne0fa2912014-04-28 16:39:04 -0700248
William Robertsaeca97b2015-07-31 13:10:36 -0700249static bool package_list_parser_cb(pkg_info *info, void * /* userdata */) {
250
251 bool rc = true;
252 if (info->uid == uid) {
253 name = strdup(info->name);
254 // false to stop processing
255 rc = false;
256 }
257
258 packagelist_free(info);
259 return rc;
260}
261
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700262static void *reinit_thread_start(void * /*obj*/) {
263 prctl(PR_SET_NAME, "logd.daemon");
264 set_sched_policy(0, SP_BACKGROUND);
Riley Andrewsd98f4e82015-06-08 23:36:34 -0700265 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700266
Mark Salyzyn07522c62016-03-02 07:51:48 -0800267 // If we are AID_ROOT, we should drop to AID_SYSTEM, if we are anything
268 // else, we have even lesser privileges and accept our fate. Not worth
269 // checking for error returns setting this thread's privileges.
270 (void)setgid(AID_SYSTEM);
271 (void)setuid(AID_SYSTEM);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700272
273 while (reinit_running && !sem_wait(&reinit) && reinit_running) {
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700274
275 // uidToName Privileged Worker
276 if (uid) {
277 name = NULL;
278
William Robertsaeca97b2015-07-31 13:10:36 -0700279 packagelist_parse(package_list_parser_cb, NULL);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700280
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700281 uid = 0;
282 sem_post(&uidName);
283 continue;
284 }
285
Mark Salyzynccbadc62015-03-12 12:25:35 -0700286 if (fdDmesg >= 0) {
287 static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
288 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
289 ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
290 write(fdDmesg, reinit_message, sizeof(reinit_message));
291 }
292
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700293 // Anything that reads persist.<property>
294 if (logBuf) {
295 logBuf->init();
Mark Salyzyn932f7ac2015-08-28 08:02:59 -0700296 logBuf->initPrune(NULL);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700297 }
298 }
299
300 return NULL;
301}
302
Mark Salyzyn95108f12015-04-20 07:26:27 -0700303static sem_t sem_name;
304
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700305char *android::uidToName(uid_t u) {
306 if (!u || !reinit_running) {
307 return NULL;
308 }
309
Mark Salyzyn95108f12015-04-20 07:26:27 -0700310 sem_wait(&sem_name);
311
312 // Not multi-thread safe, we use sem_name to protect
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700313 uid = u;
314
315 name = NULL;
316 sem_post(&reinit);
317 sem_wait(&uidName);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700318 char *ret = name;
319
320 sem_post(&sem_name);
321
322 return ret;
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700323}
324
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700325// Serves as a global method to trigger reinitialization
326// and as a function that can be provided to signal().
327void reinit_signal_handler(int /*signal*/) {
328 sem_post(&reinit);
329}
330
Mark Salyzynff32f3c2015-04-13 14:24:45 -0700331// tagToName converts an events tag into a name
332const char *android::tagToName(uint32_t tag) {
333 static const EventTagMap *map;
334
335 if (!map) {
336 sem_wait(&sem_name);
337 if (!map) {
338 map = android_openEventTagMap(EVENT_TAG_MAP_FILE);
339 }
340 sem_post(&sem_name);
341 if (!map) {
342 return NULL;
343 }
344 }
345 return android_lookupEventTag(map, tag);
346}
347
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700348static void readDmesg(LogAudit *al, LogKlog *kl) {
349 if (!al && !kl) {
350 return;
351 }
352
Mark Salyzynea1a2412015-09-02 07:39:53 -0700353 int rc = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700354 if (rc <= 0) {
355 return;
356 }
357
Mark Salyzynea1a2412015-09-02 07:39:53 -0700358 size_t len = rc + 1024; // Margin for additional input race or trailing nul
359 std::unique_ptr<char []> buf(new char[len]);
360
361 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
362 if (rc <= 0) {
363 return;
364 }
365
366 if ((size_t)rc < len) {
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700367 len = rc + 1;
368 }
Mark Salyzynea1a2412015-09-02 07:39:53 -0700369 buf[--len] = '\0';
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700370
Mark Salyzynb6bee332015-09-08 08:56:32 -0700371 if (kl && kl->isMonotonic()) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700372 kl->synchronize(buf.get(), len);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700373 }
374
Mark Salyzynea1a2412015-09-02 07:39:53 -0700375 size_t sublen;
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700376 for (char *ptr = NULL, *tok = buf.get();
Mark Salyzynea1a2412015-09-02 07:39:53 -0700377 (rc >= 0) && ((tok = log_strntok_r(tok, &len, &ptr, &sublen)));
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700378 tok = NULL) {
379 if (al) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700380 rc = al->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700381 }
382 if (kl) {
Mark Salyzyn151beac2015-09-04 11:37:42 -0700383 rc = kl->log(tok, sublen);
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700384 }
385 }
386}
387
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700388// Foreground waits for exit of the main persistent threads
389// that are started here. The threads are created to manage
390// UNIX domain client sockets for writing, reading and
391// controlling the user space logger, and for any additional
392// logging plugins like auditd and restart control. Additional
393// transitory per-client threads are created for each reader.
394int main(int argc, char *argv[]) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700395 int fdPmesg = -1;
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800396 bool klogd = property_get_bool("logd.kernel",
397 BOOL_DEFAULT_TRUE |
398 BOOL_DEFAULT_FLAG_PERSIST |
399 BOOL_DEFAULT_FLAG_ENG |
400 BOOL_DEFAULT_FLAG_SVELTE);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700401 if (klogd) {
402 fdPmesg = open("/proc/kmsg", O_RDONLY | O_NDELAY);
403 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700404 fdDmesg = open("/dev/kmsg", O_WRONLY);
405
406 // issue reinit command. KISS argument parsing.
407 if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
408 int sock = TEMP_FAILURE_RETRY(
409 socket_local_client("logd",
410 ANDROID_SOCKET_NAMESPACE_RESERVED,
411 SOCK_STREAM));
412 if (sock < 0) {
413 return -errno;
414 }
415 static const char reinit[] = "reinit";
416 ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
417 if (ret < 0) {
418 return -errno;
419 }
420 struct pollfd p;
421 memset(&p, 0, sizeof(p));
422 p.fd = sock;
423 p.events = POLLIN;
Mark Salyzynf011a332015-12-10 11:27:03 -0800424 ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700425 if (ret < 0) {
426 return -errno;
427 }
428 if ((ret == 0) || !(p.revents & POLLIN)) {
429 return -ETIME;
430 }
431 static const char success[] = "success";
432 char buffer[sizeof(success) - 1];
433 memset(buffer, 0, sizeof(buffer));
434 ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
435 if (ret < 0) {
436 return -errno;
437 }
438 return strncmp(buffer, success, sizeof(success) - 1) != 0;
439 }
440
441 // Reinit Thread
442 sem_init(&reinit, 0, 0);
Mark Salyzyn08739ba2015-03-16 08:26:05 -0700443 sem_init(&uidName, 0, 0);
Mark Salyzyn95108f12015-04-20 07:26:27 -0700444 sem_init(&sem_name, 0, 1);
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700445 pthread_attr_t attr;
446 if (!pthread_attr_init(&attr)) {
447 struct sched_param param;
448
449 memset(&param, 0, sizeof(param));
450 pthread_attr_setschedparam(&attr, &param);
451 pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
452 if (!pthread_attr_setdetachstate(&attr,
453 PTHREAD_CREATE_DETACHED)) {
454 pthread_t thread;
455 reinit_running = true;
456 if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
457 reinit_running = false;
458 }
459 }
460 pthread_attr_destroy(&attr);
Mark Salyzyne9bebd02014-04-03 09:55:26 -0700461 }
462
Mark Salyzyn0175b072014-02-26 09:50:16 -0800463 if (drop_privs() != 0) {
464 return -1;
465 }
466
467 // Serves the purpose of managing the last logs times read on a
468 // socket connection, and as a reader lock on a range of log
469 // entries.
470
471 LastLogTimes *times = new LastLogTimes();
472
473 // LogBuffer is the object which is responsible for holding all
474 // log entries.
475
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700476 logBuf = new LogBuffer(times);
477
478 signal(SIGHUP, reinit_signal_handler);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800479
Mark Salyzyn9c66a582015-12-14 16:40:12 -0800480 if (property_get_bool("logd.statistics",
481 BOOL_DEFAULT_TRUE |
482 BOOL_DEFAULT_FLAG_PERSIST |
483 BOOL_DEFAULT_FLAG_ENG |
484 BOOL_DEFAULT_FLAG_SVELTE)) {
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700485 logBuf->enableStatistics();
Mark Salyzynf5fc5092014-09-21 14:22:18 -0700486 }
Mark Salyzyne457b742014-02-19 17:18:31 -0800487
Mark Salyzyn0175b072014-02-26 09:50:16 -0800488 // LogReader listens on /dev/socket/logdr. When a client
489 // connects, log entries in the LogBuffer are written to the client.
490
491 LogReader *reader = new LogReader(logBuf);
492 if (reader->startListener()) {
493 exit(1);
494 }
495
496 // LogListener listens on /dev/socket/logdw for client
497 // initiated log messages. New log entries are added to LogBuffer
498 // and LogReader is notified to send updates to connected clients.
499
500 LogListener *swl = new LogListener(logBuf, reader);
Mark Salyzyn581edc12013-11-20 13:38:52 -0800501 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Mark Salyzyn39944c82015-09-08 11:24:07 -0700502 if (swl->startListener(600)) {
Mark Salyzyn0175b072014-02-26 09:50:16 -0800503 exit(1);
504 }
505
506 // Command listener listens on /dev/socket/logd for incoming logd
507 // administrative commands.
508
509 CommandListener *cl = new CommandListener(logBuf, reader, swl);
510 if (cl->startListener()) {
511 exit(1);
512 }
513
William Roberts29d238d2013-02-08 09:45:26 +0900514 // LogAudit listens on NETLINK_AUDIT socket for selinux
515 // initiated log messages. New log entries are added to LogBuffer
516 // and LogReader is notified to send updates to connected clients.
517
Sami Tolvanena742d102016-06-14 18:04:43 +0000518 bool auditd = property_get_bool("logd.auditd",
519 BOOL_DEFAULT_TRUE |
520 BOOL_DEFAULT_FLAG_PERSIST);
521 LogAudit *al = NULL;
522 if (auditd) {
523 al = new LogAudit(logBuf, reader,
524 property_get_bool("logd.auditd.dmesg",
525 BOOL_DEFAULT_TRUE |
526 BOOL_DEFAULT_FLAG_PERSIST)
527 ? fdDmesg
528 : -1);
529 }
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700530
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700531 LogKlog *kl = NULL;
532 if (klogd) {
Sami Tolvanena742d102016-06-14 18:04:43 +0000533 kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL);
Mark Salyzyna1aacb72014-10-15 08:49:39 -0700534 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700535
Sami Tolvanena742d102016-06-14 18:04:43 +0000536 readDmesg(al, kl);
Mark Salyzyneb06de72014-10-13 09:59:37 -0700537
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700538 // failure is an option ... messages are in dmesg (required by standard)
Mark Salyzyneb06de72014-10-13 09:59:37 -0700539
Mark Salyzynd5600fd2015-06-12 14:59:42 -0700540 if (kl && kl->startListener()) {
541 delete kl;
542 }
Mark Salyzyneb06de72014-10-13 09:59:37 -0700543
Sami Tolvanena742d102016-06-14 18:04:43 +0000544 if (al && al->startListener()) {
545 delete al;
William Roberts29d238d2013-02-08 09:45:26 +0900546 }
547
Mark Salyzyn11e55cb2015-03-10 16:45:17 -0700548 TEMP_FAILURE_RETRY(pause());
549
Mark Salyzyn0175b072014-02-26 09:50:16 -0800550 exit(0);
551}