Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68261eec authored by Tom Cherry's avatar Tom Cherry
Browse files

logd: remove users of __android_logger_property_get_bool()

__android_logger_property_get_bool() has a clunky API and doesn't
belong in liblog, since a vast majority of liblog users will never
query this property.

Specifically
1) Replace with GetBoolProperty() when completely equivalent.
2) Remove checking if property values are 'eng' or 'svelte', since
   there's no evidence that those values were ever used.
3) Remove checking 'persist.logd.statistics' and 'ro.logd.statistics',
   since there's no evidence that those values were ever used.
4) Set ro.logd.kernel explicitly, so other processes don't need to
   replicate the defaults that logd uses.

Test: build
Change-Id: I7c37af64ba7754e839185f46da66bf077f09d9c3
parent 5ce34992
Loading
Loading
Loading
Loading
+18 −20
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@

#include <string>

#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/log.h>  // minimal logging API
#include <gtest/gtest.h>
@@ -29,6 +30,8 @@
// Do not use anything in log/log_time.h despite side effects of the above.
#include <private/android_logger.h>

using android::base::GetBoolProperty;

TEST(liblog, android_logger_get_) {
#ifdef __ANDROID__
  // This test assumes the log buffers are filled with noise from
@@ -38,31 +41,27 @@ TEST(liblog, android_logger_get_) {

  for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
    log_id_t id = static_cast<log_id_t>(i);
    const char* name = android_log_id_to_name(id);
    if (id != android_name_to_log_id(name)) {
      continue;
    }
    fprintf(stderr, "log buffer %s\r", name);
    std::string name = android_log_id_to_name(id);
    fprintf(stderr, "log buffer %s\r", name.c_str());
    struct logger* logger;
    EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
    EXPECT_EQ(id, android_logger_get_id(logger));
    ssize_t get_log_size = android_logger_get_log_size(logger);
    /* security buffer is allowed to be denied */
    if (strcmp("security", name)) {
      EXPECT_LT(0, get_log_size);
    if (name != "security") {
      EXPECT_GT(get_log_size, 0);
      // crash buffer is allowed to be empty, that is actually healthy!
      // kernel buffer is allowed to be empty on "user" builds
      // stats buffer is allowed to be empty TEMPORARILY.
      // TODO: remove stats buffer from here once we start to use it in
      // framework (b/68266385).
      EXPECT_LE(  // boolean 1 or 0 depending on expected content or empty
          !!((strcmp("crash", name) != 0) &&
             ((strcmp("kernel", name) != 0) ||
              __android_logger_property_get_bool(
                  "ro.logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG |
                                        BOOL_DEFAULT_FLAG_SVELTE)) &&
             (strcmp("stats", name) != 0)),
          android_logger_get_log_readable_size(logger));
      // stats buffer is no longer in use.
      if (name == "crash" || name == "stats") {
        continue;
      }

      // kernel buffer is empty if ro.logd.kernel is false
      if (name == "kernel" && !GetBoolProperty("ro.logd.kernel", false)) {
        continue;
      }

      EXPECT_LE(0, android_logger_get_log_readable_size(logger));
    } else {
      EXPECT_NE(0, get_log_size);
      if (get_log_size < 0) {
@@ -71,7 +70,6 @@ TEST(liblog, android_logger_get_) {
        EXPECT_LE(0, android_logger_get_log_readable_size(logger));
      }
    }
    EXPECT_LT(0, android_logger_get_log_version(logger));
  }

  android_logger_list_close(logger_list);
+5 −3
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@
#include <sstream>

#include <android-base/macros.h>
#include <log/log_properties.h>
#include <android-base/properties.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>

@@ -40,6 +40,8 @@
#include "LogUtils.h"
#include "libaudit.h"

using android::base::GetBoolProperty;

#define KMSG_PRIORITY(PRI)                               \
    '<', '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) / 10, \
        '0' + LOG_MAKEPRI(LOG_AUTH, LOG_PRI(PRI)) % 10, '>'
@@ -48,8 +50,8 @@ LogAudit::LogAudit(LogBuffer* buf, int fdDmesg, LogStatistics* stats)
    : SocketListener(getLogSocket(), false),
      logbuf(buf),
      fdDmesg(fdDmesg),
      main(__android_logger_property_get_bool("ro.logd.auditd.main", BOOL_DEFAULT_TRUE)),
      events(__android_logger_property_get_bool("ro.logd.auditd.events", BOOL_DEFAULT_TRUE)),
      main(GetBoolProperty("ro.logd.auditd.main", true)),
      events(GetBoolProperty("ro.logd.auditd.events", true)),
      initialized(false),
      stats_(stats) {
    static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
+3 −6
Original line number Diff line number Diff line
@@ -7,8 +7,8 @@ ro.logd.auditd.main bool true selinux audit messages sent to main.
ro.logd.auditd.events      bool   true   selinux audit messages sent to events.
persist.logd.security      bool   false  Enable security buffer.
ro.organization_owned      bool   false  Override persist.logd.security to false
ro.logd.kernel             bool+ svelte+ Enable klogd daemon
ro.logd.statistics         bool+ svelte+ Enable logcat -S statistics.
ro.logd.kernel             bool  svelte+ Enable klogd daemon
logd.statistics            bool  svelte+ Enable logcat -S statistics.
ro.debuggable              number        if not "1", logd.statistics &
                                         ro.logd.kernel default false.
logd.logpersistd.enable    bool   auto   Safe to start logpersist daemon service
@@ -57,11 +57,8 @@ logd.buffer_type string (empty) Set the log buffer type. Current choi

NB:
- auto - managed by /init
- bool+ - "true", "false" and comma separated list of "eng" (forced false if
  ro.debuggable is not "1") or "svelte" (forced false if ro.config.low_ram is
  true).
- svelte - see ro.config.low_ram for details.
- svelte+ - see ro.config.low_ram and ro.debuggable for details.
- svelte+ - If empty, default to true if `ro.config.low_ram == false && ro.debuggable == true`
- ro - <base property> temporary override, ro.<base property> platform default.
- persist - <base property> override, persist.<base property> platform default.
- build - VERBOSE for native, DEBUG for jvm isLoggable, or developer option.
+20 −14
Original line number Diff line number Diff line
@@ -62,7 +62,9 @@
#include "SerializedLogBuffer.h"
#include "SimpleLogBuffer.h"

using android::base::GetBoolProperty;
using android::base::GetProperty;
using android::base::SetProperty;

#define KMSG_PRIORITY(PRI)                                 \
    '<', '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
@@ -82,10 +84,11 @@ static void DropPrivs(bool klogd, bool auditd) {
        PLOG(FATAL) << "failed to set batch scheduler";
    }

    if (!__android_logger_property_get_bool("ro.debuggable", BOOL_DEFAULT_FALSE) &&
        prctl(PR_SET_DUMPABLE, 0) == -1) {
    if (!GetBoolProperty("ro.debuggable", false)) {
        if (prctl(PR_SET_DUMPABLE, 0) == -1) {
            PLOG(FATAL) << "failed to clear PR_SET_DUMPABLE";
        }
    }

    std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free);
    if (cap_clear(caps.get()) < 0) {
@@ -110,6 +113,14 @@ static void DropPrivs(bool klogd, bool auditd) {
    }
}

// GetBoolProperty that defaults to true if `ro.debuggable == true && ro.config.low_rawm == false`.
static bool GetBoolPropertyEngSvelteDefault(const std::string& name) {
    bool default_value =
            GetBoolProperty("ro.debuggable", false) && !GetBoolProperty("ro.config.low_ram", false);

    return GetBoolProperty(name, default_value);
}

char* android::uidToName(uid_t u) {
    struct Userdata {
        uid_t uid;
@@ -236,10 +247,9 @@ int main(int argc, char* argv[]) {
    }

    int fdPmesg = -1;
    bool klogd = __android_logger_property_get_bool(
        "ro.logd.kernel",
        BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
    bool klogd = GetBoolPropertyEngSvelteDefault("ro.logd.kernel");
    if (klogd) {
        SetProperty("ro.logd.kernel", "true");
        static const char proc_kmsg[] = "/proc/kmsg";
        fdPmesg = android_get_control_file(proc_kmsg);
        if (fdPmesg < 0) {
@@ -249,7 +259,7 @@ int main(int argc, char* argv[]) {
        if (fdPmesg < 0) PLOG(ERROR) << "Failed to open " << proc_kmsg;
    }

    bool auditd = __android_logger_property_get_bool("ro.logd.auditd", BOOL_DEFAULT_TRUE);
    bool auditd = GetBoolProperty("ro.logd.auditd", true);
    DropPrivs(klogd, auditd);

    // A cache of event log tags
@@ -261,10 +271,8 @@ int main(int argc, char* argv[]) {
    std::string buffer_type = GetProperty("logd.buffer_type", "serialized");

    // Partial (required for chatty) or full logging statistics.
    bool enable_full_log_statistics = __android_logger_property_get_bool(
            "logd.statistics", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_PERSIST |
                                       BOOL_DEFAULT_FLAG_ENG | BOOL_DEFAULT_FLAG_SVELTE);
    LogStatistics log_statistics(enable_full_log_statistics, buffer_type == "serialized");
    LogStatistics log_statistics(GetBoolPropertyEngSvelteDefault("logd.statistics"),
                                 buffer_type == "serialized");

    // 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.
@@ -309,9 +317,7 @@ int main(int argc, char* argv[]) {
    // and LogReader is notified to send updates to connected clients.
    LogAudit* al = nullptr;
    if (auditd) {
        int dmesg_fd = __android_logger_property_get_bool("ro.logd.auditd.dmesg", BOOL_DEFAULT_TRUE)
                               ? fdDmesg
                               : -1;
        int dmesg_fd = GetBoolProperty("ro.logd.auditd.dmesg", true) ? fdDmesg : -1;
        al = new LogAudit(log_buffer, dmesg_fd, &log_statistics);
    }