From e9bebd0eb1845f0c6009ce2edc5aeb47bf89e397 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Thu, 3 Apr 2014 09:55:26 -0700 Subject: [PATCH] logd: auditd: add logd.auditd.dmesg property Change-Id: If4a579c2221eec99cf3f6acf59ead8c2d5230517 --- logd/LogAudit.cpp | 22 ++++++++++++++++++---- logd/LogAudit.h | 5 +++-- logd/main.cpp | 10 +++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp index cc3e58329..ea6eecea6 100644 --- a/logd/LogAudit.cpp +++ b/logd/LogAudit.cpp @@ -19,15 +19,18 @@ #include #include #include +#include #include "libaudit.h" #include "LogAudit.h" -LogAudit::LogAudit(LogBuffer *buf, LogReader *reader) +LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg) : SocketListener(getLogSocket(), false) , logbuf(buf) - , reader(reader) { - logDmsg(); + , reader(reader) + , fdDmesg(-1) { + logDmesg(); + fdDmesg = fdDmsg; } bool LogAudit::onDataAvailable(SocketClient *cli) { @@ -62,6 +65,17 @@ int LogAudit::logPrint(const char *fmt, ...) { return rc; } + if (fdDmesg >= 0) { + struct iovec iov[2]; + + iov[0].iov_base = str; + iov[0].iov_len = strlen(str); + iov[1].iov_base = const_cast("\n"); + iov[1].iov_len = 1; + + writev(fdDmesg, iov, sizeof(iov) / sizeof(iov[0])); + } + pid_t pid = getpid(); pid_t tid = gettid(); uid_t uid = getuid(); @@ -141,7 +155,7 @@ int LogAudit::logPrint(const char *fmt, ...) { return rc; } -void LogAudit::logDmsg() { +void LogAudit::logDmesg() { int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0); if (len <= 0) { return; diff --git a/logd/LogAudit.h b/logd/LogAudit.h index 3e57eedc9..111030a89 100644 --- a/logd/LogAudit.h +++ b/logd/LogAudit.h @@ -23,16 +23,17 @@ class LogAudit : public SocketListener { LogBuffer *logbuf; LogReader *reader; + int fdDmesg; public: - LogAudit(LogBuffer *buf, LogReader *reader); + LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg); protected: virtual bool onDataAvailable(SocketClient *cli); private: static int getLogSocket(); - void logDmsg(); + void logDmesg(); int logPrint(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3))); }; diff --git a/logd/main.cpp b/logd/main.cpp index 83ec6c0aa..7346e2fa9 100644 --- a/logd/main.cpp +++ b/logd/main.cpp @@ -84,6 +84,13 @@ static int drop_privs() { // space logger. Additional transitory per-client threads are created // for each reader once they register. int main() { + int fdDmesg = -1; + char dmesg[PROPERTY_VALUE_MAX]; + property_get("logd.auditd.dmesg", dmesg, "1"); + if (atol(dmesg)) { + fdDmesg = open("/dev/kmsg", O_WRONLY); + } + if (drop_privs() != 0) { return -1; } @@ -136,9 +143,10 @@ int main() { // and LogReader is notified to send updates to connected clients. // failure is an option ... messages are in dmesg (required by standard) - LogAudit *al = new LogAudit(logBuf, reader); + LogAudit *al = new LogAudit(logBuf, reader, fdDmesg); if (al->startListener()) { delete al; + close(fdDmesg); } pause();