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
This commit is contained in:
Tom Cherry 2019-12-06 11:01:51 -08:00
parent 72a4e08864
commit ed860ff4bf
3 changed files with 8 additions and 24 deletions

View File

@ -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

View File

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

View File

@ -1075,17 +1075,8 @@ 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)) {
error(EXIT_FAILURE, 0, "Failed to set the prune list.");
}
free(buf);
} else {
error(EXIT_FAILURE, 0, "Failed to set the prune list (alloc).");
if (android_logger_set_prune_list(logger_list.get(), setPruneList, len)) {
error(EXIT_FAILURE, 0, "Failed to set the prune list.");
}
return EXIT_SUCCESS;
}