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

Commit ed860ff4 authored by Tom Cherry's avatar Tom Cherry
Browse files

Make android_logger_set_prune_list() sane

The current version requires callers to supply a string with 32 extra
bytes for liblog to internally prepend "setPruneList ", and to have
enough space to parse logd's return string.  That is an unacceptable
requirement on callers.

This change removes that requirement by having liblog allocate the
needed std::string in any case.

It also stops writing back the 'success' or 'Invalid' string to the
caller's buffer, since that is redundant as well.

Test: changing prune settings works.
Change-Id: Ic0f03a229f0b9a77d03adcb91288370c3bd42903
parent 72a4e088
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -139,8 +139,7 @@ ssize_t android_logger_get_statistics(struct logger_list* logger_list,
                                      char* buf, size_t len);
ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
                                      char* buf, size_t len);
int android_logger_set_prune_list(struct logger_list* logger_list, char* buf,
                                  size_t len);
int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len);

#define ANDROID_LOG_RDONLY O_RDONLY
#define ANDROID_LOG_WRONLY O_WRONLY
+5 −11
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include <time.h>
#include <unistd.h>

#include <string>

#include <cutils/sockets.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>
@@ -249,22 +251,14 @@ ssize_t android_logger_get_prune_list(struct logger_list* logger_list, char* buf
  return SendLogdControlMessage(buf, len);
}

int android_logger_set_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len) {
  if (logger_list->mode & ANDROID_LOG_PSTORE) {
    return -EINVAL;
  }

  const char cmd[] = "setPruneList ";
  const size_t cmdlen = sizeof(cmd) - 1;

  if (strlen(buf) > (len - cmdlen)) {
    return -ENOMEM; /* KISS */
  }
  memmove(buf + cmdlen, buf, len - cmdlen);
  buf[len - 1] = '\0';
  memcpy(buf, cmd, cmdlen);
  std::string cmd = "setPruneList " + std::string{buf, len};

  return check_log_success(buf, SendLogdControlMessage(buf, len));
  return check_log_success(cmd.data(), SendLogdControlMessage(cmd.data(), cmd.size()));
}

static int logdOpen(struct logger_list* logger_list) {
+2 −11
Original line number Diff line number Diff line
@@ -1075,18 +1075,9 @@ int Logcat::Run(int argc, char** argv) {

    if (setPruneList) {
        size_t len = strlen(setPruneList);
        // extra 32 bytes are needed by android_logger_set_prune_list
        size_t bLen = len + 32;
        char* buf = nullptr;
        if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
            buf[len] = '\0';
            if (android_logger_set_prune_list(logger_list.get(), buf, bLen)) {
        if (android_logger_set_prune_list(logger_list.get(), setPruneList, len)) {
            error(EXIT_FAILURE, 0, "Failed to set the prune list.");
        }
            free(buf);
        } else {
            error(EXIT_FAILURE, 0, "Failed to set the prune list (alloc).");
        }
        return EXIT_SUCCESS;
    }