2014-02-27 01:50:16 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-03-11 07:45:17 +08:00
|
|
|
#include <poll.h>
|
2013-12-27 07:13:36 +08:00
|
|
|
#include <sched.h>
|
2015-03-11 07:45:17 +08:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
2014-02-27 01:50:16 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/capability.h>
|
2014-10-14 00:59:37 +08:00
|
|
|
#include <sys/klog.h>
|
2014-07-19 08:39:41 +08:00
|
|
|
#include <sys/prctl.h>
|
2015-06-09 14:36:34 +08:00
|
|
|
#include <sys/resource.h>
|
2014-02-27 01:50:16 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2015-03-13 03:25:35 +08:00
|
|
|
#include <syslog.h>
|
2014-02-20 09:18:31 +08:00
|
|
|
#include <unistd.h>
|
2014-02-27 01:50:16 +08:00
|
|
|
|
2015-08-01 04:10:36 +08:00
|
|
|
#include <cstdbool>
|
2015-06-13 05:59:42 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2016-07-16 01:57:08 +08:00
|
|
|
#include <android-base/macros.h>
|
2014-02-20 09:18:31 +08:00
|
|
|
#include <cutils/properties.h>
|
2015-01-31 07:19:48 +08:00
|
|
|
#include <cutils/sched_policy.h>
|
2016-10-27 23:21:35 +08:00
|
|
|
#include <cutils/files.h>
|
2015-03-11 07:45:17 +08:00
|
|
|
#include <cutils/sockets.h>
|
2016-07-16 01:57:08 +08:00
|
|
|
#include <libminijail.h>
|
2015-04-14 05:24:45 +08:00
|
|
|
#include <log/event_tag_map.h>
|
2015-08-01 04:10:36 +08:00
|
|
|
#include <packagelistparser/packagelistparser.h>
|
2015-03-17 22:56:32 +08:00
|
|
|
#include <private/android_filesystem_config.h>
|
2016-03-29 06:42:08 +08:00
|
|
|
#include <private/android_logger.h>
|
2016-07-16 01:57:08 +08:00
|
|
|
#include <scoped_minijail.h>
|
2015-06-09 14:36:34 +08:00
|
|
|
#include <utils/threads.h>
|
2014-02-20 09:18:31 +08:00
|
|
|
|
2014-02-27 01:50:16 +08:00
|
|
|
#include "CommandListener.h"
|
|
|
|
#include "LogBuffer.h"
|
|
|
|
#include "LogListener.h"
|
2013-02-08 08:45:26 +08:00
|
|
|
#include "LogAudit.h"
|
2014-10-15 23:49:39 +08:00
|
|
|
#include "LogKlog.h"
|
2015-08-28 23:02:59 +08:00
|
|
|
#include "LogUtils.h"
|
2014-02-27 01:50:16 +08:00
|
|
|
|
2015-03-13 03:25:35 +08:00
|
|
|
#define KMSG_PRIORITY(PRI) \
|
|
|
|
'<', \
|
|
|
|
'0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
|
|
|
|
'0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
|
|
|
|
'>'
|
|
|
|
|
2014-03-25 01:26:47 +08:00
|
|
|
//
|
2016-07-16 01:57:08 +08:00
|
|
|
// The service is designed to be run by init, it does not respond well
|
2014-03-25 01:26:47 +08:00
|
|
|
// to starting up manually. When starting up manually the sockets will
|
|
|
|
// fail to open typically for one of the following reasons:
|
|
|
|
// EADDRINUSE if logger is running.
|
|
|
|
// EACCESS if started without precautions (below)
|
|
|
|
//
|
|
|
|
// Here is a cookbook procedure for starting up logd manually assuming
|
2016-07-16 01:57:08 +08:00
|
|
|
// init is out of the way, pedantically all permissions and SELinux
|
2014-03-25 01:26:47 +08:00
|
|
|
// security is put back in place:
|
|
|
|
//
|
|
|
|
// setenforce 0
|
|
|
|
// rm /dev/socket/logd*
|
|
|
|
// chmod 777 /dev/socket
|
|
|
|
// # here is where you would attach the debugger or valgrind for example
|
|
|
|
// runcon u:r:logd:s0 /system/bin/logd </dev/null >/dev/null 2>&1 &
|
|
|
|
// sleep 1
|
|
|
|
// chmod 755 /dev/socket
|
|
|
|
// chown logd.logd /dev/socket/logd*
|
|
|
|
// restorecon /dev/socket/logd*
|
|
|
|
// setenforce 1
|
|
|
|
//
|
|
|
|
// If minimalism prevails, typical for debugging and security is not a concern:
|
|
|
|
//
|
|
|
|
// setenforce 0
|
|
|
|
// chmod 777 /dev/socket
|
|
|
|
// logd
|
|
|
|
//
|
|
|
|
|
2014-02-27 01:50:16 +08:00
|
|
|
static int drop_privs() {
|
2013-12-27 07:13:36 +08:00
|
|
|
struct sched_param param;
|
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
|
2015-01-31 07:19:48 +08:00
|
|
|
if (set_sched_policy(0, SP_BACKGROUND) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-27 07:13:36 +08:00
|
|
|
if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:36:34 +08:00
|
|
|
if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-29 05:49:53 +08:00
|
|
|
if (prctl(PR_SET_DUMPABLE, 0) < 0) {
|
|
|
|
android::prdebug("failed to clear PR_SET_DUMPABLE");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-08 08:52:17 +08:00
|
|
|
gid_t groups[] = { AID_READPROC };
|
2016-07-16 01:57:08 +08:00
|
|
|
ScopedMinijail j(minijail_new());
|
|
|
|
minijail_set_supplementary_gids(j.get(), arraysize(groups), groups);
|
|
|
|
minijail_change_uid(j.get(), AID_LOGD);
|
|
|
|
minijail_change_gid(j.get(), AID_LOGD);
|
|
|
|
minijail_use_caps(j.get(), CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_AUDIT_CONTROL));
|
|
|
|
minijail_enter(j.get());
|
2014-02-27 01:50:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:39:04 +08:00
|
|
|
// Property helper
|
2015-12-15 08:40:12 +08:00
|
|
|
static bool check_flag(const char *prop, const char *flag) {
|
|
|
|
const char *cp = strcasestr(prop, flag);
|
|
|
|
if (!cp) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We only will document comma (,)
|
|
|
|
static const char sep[] = ",:;|+ \t\f";
|
|
|
|
if ((cp != prop) && !strchr(sep, cp[-1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cp += strlen(flag);
|
|
|
|
return !*cp || !!strchr(sep, *cp);
|
|
|
|
}
|
2014-04-29 07:39:04 +08:00
|
|
|
|
2015-03-13 03:25:35 +08:00
|
|
|
static int fdDmesg = -1;
|
2016-07-16 00:55:41 +08:00
|
|
|
void android::prdebug(const char *fmt, ...) {
|
2016-02-09 02:28:12 +08:00
|
|
|
if (fdDmesg < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char message[] = {
|
|
|
|
KMSG_PRIORITY(LOG_DEBUG), 'l', 'o', 'g', 'd', ':', ' '
|
|
|
|
};
|
|
|
|
char buffer[256];
|
|
|
|
memcpy(buffer, message, sizeof(message));
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int n = vsnprintf(buffer + sizeof(message),
|
|
|
|
sizeof(buffer) - sizeof(message), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (n > 0) {
|
|
|
|
buffer[sizeof(buffer) - 1] = '\0';
|
|
|
|
if (!strchr(buffer, '\n')) {
|
|
|
|
buffer[sizeof(buffer) - 2] = '\0';
|
|
|
|
strlcat(buffer, "\n", sizeof(buffer));
|
|
|
|
}
|
|
|
|
write(fdDmesg, buffer, strlen(buffer));
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 03:25:35 +08:00
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
static sem_t uidName;
|
|
|
|
static uid_t uid;
|
|
|
|
static char *name;
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
static sem_t reinit;
|
|
|
|
static bool reinit_running = false;
|
|
|
|
static LogBuffer *logBuf = NULL;
|
|
|
|
|
2015-08-01 04:10:36 +08:00
|
|
|
static bool package_list_parser_cb(pkg_info *info, void * /* userdata */) {
|
|
|
|
|
|
|
|
bool rc = true;
|
|
|
|
if (info->uid == uid) {
|
|
|
|
name = strdup(info->name);
|
|
|
|
// false to stop processing
|
|
|
|
rc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
packagelist_free(info);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
static void *reinit_thread_start(void * /*obj*/) {
|
|
|
|
prctl(PR_SET_NAME, "logd.daemon");
|
|
|
|
set_sched_policy(0, SP_BACKGROUND);
|
2015-06-09 14:36:34 +08:00
|
|
|
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND);
|
2014-04-29 07:39:04 +08:00
|
|
|
|
2016-10-27 23:21:35 +08:00
|
|
|
// If we are AID_ROOT, we should drop to AID_LOGD+AID_SYSTEM, if we are
|
|
|
|
// anything else, we have even lesser privileges and accept our fate. Not
|
|
|
|
// worth checking for error returns setting this thread's privileges.
|
|
|
|
(void)setgid(AID_SYSTEM); // readonly access to /data/system/packages.list
|
|
|
|
(void)setuid(AID_LOGD); // access to everything logd.
|
2015-03-11 07:45:17 +08:00
|
|
|
|
|
|
|
while (reinit_running && !sem_wait(&reinit) && reinit_running) {
|
2015-03-16 23:26:05 +08:00
|
|
|
|
|
|
|
// uidToName Privileged Worker
|
|
|
|
if (uid) {
|
|
|
|
name = NULL;
|
|
|
|
|
2015-08-01 04:10:36 +08:00
|
|
|
packagelist_parse(package_list_parser_cb, NULL);
|
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
uid = 0;
|
|
|
|
sem_post(&uidName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-13 03:25:35 +08:00
|
|
|
if (fdDmesg >= 0) {
|
|
|
|
static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO),
|
|
|
|
'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':',
|
|
|
|
' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' };
|
|
|
|
write(fdDmesg, reinit_message, sizeof(reinit_message));
|
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
// Anything that reads persist.<property>
|
|
|
|
if (logBuf) {
|
|
|
|
logBuf->init();
|
2015-08-28 23:02:59 +08:00
|
|
|
logBuf->initPrune(NULL);
|
2015-03-11 07:45:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-20 22:26:27 +08:00
|
|
|
static sem_t sem_name;
|
|
|
|
|
2015-03-16 23:26:05 +08:00
|
|
|
char *android::uidToName(uid_t u) {
|
|
|
|
if (!u || !reinit_running) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-20 22:26:27 +08:00
|
|
|
sem_wait(&sem_name);
|
|
|
|
|
|
|
|
// Not multi-thread safe, we use sem_name to protect
|
2015-03-16 23:26:05 +08:00
|
|
|
uid = u;
|
|
|
|
|
|
|
|
name = NULL;
|
|
|
|
sem_post(&reinit);
|
|
|
|
sem_wait(&uidName);
|
2015-04-20 22:26:27 +08:00
|
|
|
char *ret = name;
|
|
|
|
|
|
|
|
sem_post(&sem_name);
|
|
|
|
|
|
|
|
return ret;
|
2015-03-16 23:26:05 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
// Serves as a global method to trigger reinitialization
|
|
|
|
// and as a function that can be provided to signal().
|
|
|
|
void reinit_signal_handler(int /*signal*/) {
|
|
|
|
sem_post(&reinit);
|
|
|
|
}
|
|
|
|
|
2015-04-14 05:24:45 +08:00
|
|
|
// tagToName converts an events tag into a name
|
2016-09-23 00:56:51 +08:00
|
|
|
const char *android::tagToName(size_t *len, uint32_t tag) {
|
2015-04-14 05:24:45 +08:00
|
|
|
static const EventTagMap *map;
|
|
|
|
|
|
|
|
if (!map) {
|
|
|
|
sem_wait(&sem_name);
|
|
|
|
if (!map) {
|
|
|
|
map = android_openEventTagMap(EVENT_TAG_MAP_FILE);
|
|
|
|
}
|
|
|
|
sem_post(&sem_name);
|
|
|
|
if (!map) {
|
2016-09-23 00:56:51 +08:00
|
|
|
if (len) len = 0;
|
2015-04-14 05:24:45 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 00:56:51 +08:00
|
|
|
return android_lookupEventTag_len(map, len, tag);
|
2015-04-14 05:24:45 +08:00
|
|
|
}
|
|
|
|
|
2015-06-13 05:59:42 +08:00
|
|
|
static void readDmesg(LogAudit *al, LogKlog *kl) {
|
|
|
|
if (!al && !kl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:39:53 +08:00
|
|
|
int rc = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
|
|
|
|
if (rc <= 0) {
|
2015-06-13 05:59:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:39:53 +08:00
|
|
|
size_t len = rc + 1024; // Margin for additional input race or trailing nul
|
2015-06-13 05:59:42 +08:00
|
|
|
std::unique_ptr<char []> buf(new char[len]);
|
|
|
|
|
2015-09-02 22:39:53 +08:00
|
|
|
rc = klogctl(KLOG_READ_ALL, buf.get(), len);
|
2015-06-13 05:59:42 +08:00
|
|
|
if (rc <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:39:53 +08:00
|
|
|
if ((size_t)rc < len) {
|
2015-06-13 05:59:42 +08:00
|
|
|
len = rc + 1;
|
|
|
|
}
|
2015-09-02 22:39:53 +08:00
|
|
|
buf[--len] = '\0';
|
2015-06-13 05:59:42 +08:00
|
|
|
|
2015-09-08 23:56:32 +08:00
|
|
|
if (kl && kl->isMonotonic()) {
|
2015-09-05 02:37:42 +08:00
|
|
|
kl->synchronize(buf.get(), len);
|
2015-06-13 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2015-09-02 22:39:53 +08:00
|
|
|
size_t sublen;
|
2015-06-13 05:59:42 +08:00
|
|
|
for (char *ptr = NULL, *tok = buf.get();
|
2015-09-02 22:39:53 +08:00
|
|
|
(rc >= 0) && ((tok = log_strntok_r(tok, &len, &ptr, &sublen)));
|
2015-06-13 05:59:42 +08:00
|
|
|
tok = NULL) {
|
|
|
|
if (al) {
|
2015-09-05 02:37:42 +08:00
|
|
|
rc = al->log(tok, sublen);
|
2015-06-13 05:59:42 +08:00
|
|
|
}
|
|
|
|
if (kl) {
|
2015-09-05 02:37:42 +08:00
|
|
|
rc = kl->log(tok, sublen);
|
2015-06-13 05:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
// Foreground waits for exit of the main persistent threads
|
|
|
|
// that are started here. The threads are created to manage
|
|
|
|
// UNIX domain client sockets for writing, reading and
|
|
|
|
// controlling the user space logger, and for any additional
|
|
|
|
// logging plugins like auditd and restart control. Additional
|
|
|
|
// transitory per-client threads are created for each reader.
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
// issue reinit command. KISS argument parsing.
|
|
|
|
if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) {
|
|
|
|
int sock = TEMP_FAILURE_RETRY(
|
|
|
|
socket_local_client("logd",
|
|
|
|
ANDROID_SOCKET_NAMESPACE_RESERVED,
|
|
|
|
SOCK_STREAM));
|
|
|
|
if (sock < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
static const char reinit[] = "reinit";
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(write(sock, reinit, sizeof(reinit)));
|
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
struct pollfd p;
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
|
|
p.fd = sock;
|
|
|
|
p.events = POLLIN;
|
2015-12-11 03:27:03 +08:00
|
|
|
ret = TEMP_FAILURE_RETRY(poll(&p, 1, 1000));
|
2015-03-11 07:45:17 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if ((ret == 0) || !(p.revents & POLLIN)) {
|
|
|
|
return -ETIME;
|
|
|
|
}
|
|
|
|
static const char success[] = "success";
|
|
|
|
char buffer[sizeof(success) - 1];
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
ret = TEMP_FAILURE_RETRY(read(sock, buffer, sizeof(buffer)));
|
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return strncmp(buffer, success, sizeof(success) - 1) != 0;
|
|
|
|
}
|
|
|
|
|
2016-10-27 23:21:35 +08:00
|
|
|
static const char dev_kmsg[] = "/dev/kmsg";
|
|
|
|
fdDmesg = android_get_control_file(dev_kmsg);
|
|
|
|
if (fdDmesg < 0) {
|
|
|
|
fdDmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY | O_CLOEXEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
int fdPmesg = -1;
|
|
|
|
bool klogd = __android_logger_property_get_bool("logd.kernel",
|
|
|
|
BOOL_DEFAULT_TRUE |
|
|
|
|
BOOL_DEFAULT_FLAG_PERSIST |
|
|
|
|
BOOL_DEFAULT_FLAG_ENG |
|
|
|
|
BOOL_DEFAULT_FLAG_SVELTE);
|
|
|
|
if (klogd) {
|
|
|
|
static const char proc_kmsg[] = "/proc/kmsg";
|
|
|
|
fdPmesg = android_get_control_file(proc_kmsg);
|
|
|
|
if (fdPmesg < 0) {
|
|
|
|
fdPmesg = TEMP_FAILURE_RETRY(open(proc_kmsg,
|
|
|
|
O_RDONLY | O_NDELAY | O_CLOEXEC));
|
|
|
|
}
|
|
|
|
if (fdPmesg < 0) android::prdebug("Failed to open %s\n", proc_kmsg);
|
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
// Reinit Thread
|
|
|
|
sem_init(&reinit, 0, 0);
|
2015-03-16 23:26:05 +08:00
|
|
|
sem_init(&uidName, 0, 0);
|
2015-04-20 22:26:27 +08:00
|
|
|
sem_init(&sem_name, 0, 1);
|
2015-03-11 07:45:17 +08:00
|
|
|
pthread_attr_t attr;
|
|
|
|
if (!pthread_attr_init(&attr)) {
|
|
|
|
struct sched_param param;
|
|
|
|
|
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
|
|
|
|
if (!pthread_attr_setdetachstate(&attr,
|
|
|
|
PTHREAD_CREATE_DETACHED)) {
|
|
|
|
pthread_t thread;
|
|
|
|
reinit_running = true;
|
|
|
|
if (pthread_create(&thread, &attr, reinit_thread_start, NULL)) {
|
|
|
|
reinit_running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_attr_destroy(&attr);
|
2014-04-04 00:55:26 +08:00
|
|
|
}
|
|
|
|
|
2014-02-27 01:50:16 +08:00
|
|
|
if (drop_privs() != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serves the purpose of managing the last logs times read on a
|
|
|
|
// socket connection, and as a reader lock on a range of log
|
|
|
|
// entries.
|
|
|
|
|
|
|
|
LastLogTimes *times = new LastLogTimes();
|
|
|
|
|
|
|
|
// LogBuffer is the object which is responsible for holding all
|
|
|
|
// log entries.
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
logBuf = new LogBuffer(times);
|
|
|
|
|
|
|
|
signal(SIGHUP, reinit_signal_handler);
|
2014-02-27 01:50:16 +08:00
|
|
|
|
2016-09-28 04:08:23 +08:00
|
|
|
if (__android_logger_property_get_bool("logd.statistics",
|
|
|
|
BOOL_DEFAULT_TRUE |
|
|
|
|
BOOL_DEFAULT_FLAG_PERSIST |
|
|
|
|
BOOL_DEFAULT_FLAG_ENG |
|
|
|
|
BOOL_DEFAULT_FLAG_SVELTE)) {
|
2014-10-15 23:49:39 +08:00
|
|
|
logBuf->enableStatistics();
|
2014-09-22 05:22:18 +08:00
|
|
|
}
|
2014-02-20 09:18:31 +08:00
|
|
|
|
2014-02-27 01:50:16 +08:00
|
|
|
// LogReader listens on /dev/socket/logdr. When a client
|
|
|
|
// connects, log entries in the LogBuffer are written to the client.
|
|
|
|
|
|
|
|
LogReader *reader = new LogReader(logBuf);
|
|
|
|
if (reader->startListener()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogListener listens on /dev/socket/logdw for client
|
|
|
|
// initiated log messages. New log entries are added to LogBuffer
|
|
|
|
// and LogReader is notified to send updates to connected clients.
|
|
|
|
|
|
|
|
LogListener *swl = new LogListener(logBuf, reader);
|
2013-11-21 05:38:52 +08:00
|
|
|
// Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
|
2015-09-09 02:24:07 +08:00
|
|
|
if (swl->startListener(600)) {
|
2014-02-27 01:50:16 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command listener listens on /dev/socket/logd for incoming logd
|
|
|
|
// administrative commands.
|
|
|
|
|
|
|
|
CommandListener *cl = new CommandListener(logBuf, reader, swl);
|
|
|
|
if (cl->startListener()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-02-08 08:45:26 +08:00
|
|
|
// LogAudit listens on NETLINK_AUDIT socket for selinux
|
|
|
|
// initiated log messages. New log entries are added to LogBuffer
|
|
|
|
// and LogReader is notified to send updates to connected clients.
|
|
|
|
|
2016-09-28 04:08:23 +08:00
|
|
|
bool auditd = __android_logger_property_get_bool("logd.auditd",
|
|
|
|
BOOL_DEFAULT_TRUE |
|
|
|
|
BOOL_DEFAULT_FLAG_PERSIST);
|
2016-06-15 02:04:43 +08:00
|
|
|
LogAudit *al = NULL;
|
|
|
|
if (auditd) {
|
|
|
|
al = new LogAudit(logBuf, reader,
|
2016-09-28 04:08:23 +08:00
|
|
|
__android_logger_property_get_bool(
|
|
|
|
"logd.auditd.dmesg",
|
|
|
|
BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST)
|
|
|
|
? fdDmesg
|
|
|
|
: -1);
|
2016-06-15 02:04:43 +08:00
|
|
|
}
|
2015-03-11 07:45:17 +08:00
|
|
|
|
2014-10-15 23:49:39 +08:00
|
|
|
LogKlog *kl = NULL;
|
|
|
|
if (klogd) {
|
2016-06-15 02:04:43 +08:00
|
|
|
kl = new LogKlog(logBuf, reader, fdDmesg, fdPmesg, al != NULL);
|
2014-10-15 23:49:39 +08:00
|
|
|
}
|
2014-10-14 00:59:37 +08:00
|
|
|
|
2016-06-15 02:04:43 +08:00
|
|
|
readDmesg(al, kl);
|
2014-10-14 00:59:37 +08:00
|
|
|
|
2015-06-13 05:59:42 +08:00
|
|
|
// failure is an option ... messages are in dmesg (required by standard)
|
2014-10-15 23:49:39 +08:00
|
|
|
|
2015-06-13 05:59:42 +08:00
|
|
|
if (kl && kl->startListener()) {
|
|
|
|
delete kl;
|
|
|
|
}
|
2014-10-15 23:49:39 +08:00
|
|
|
|
2016-06-15 02:04:43 +08:00
|
|
|
if (al && al->startListener()) {
|
|
|
|
delete al;
|
2013-02-08 08:45:26 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 07:45:17 +08:00
|
|
|
TEMP_FAILURE_RETRY(pause());
|
|
|
|
|
2014-02-27 01:50:16 +08:00
|
|
|
exit(0);
|
|
|
|
}
|