Merge "fake_log_device: long lived allocations"

This commit is contained in:
Mark Salyzyn 2016-02-17 21:19:48 +00:00 committed by Gerrit Code Review
commit 3fd5649424
2 changed files with 24 additions and 22 deletions

View file

@ -69,7 +69,7 @@ typedef struct LogState {
int fakeFd; int fakeFd;
/* a printable name for this fake device */ /* a printable name for this fake device */
char *debugName; char debugName[sizeof("/dev/log/security")];
/* nonzero if this is a binary log */ /* nonzero if this is a binary log */
int isBinary; int isBinary;
@ -123,8 +123,8 @@ static void unlock()
* File descriptor management. * File descriptor management.
*/ */
#define FAKE_FD_BASE 10000 #define FAKE_FD_BASE 10000
#define MAX_OPEN_LOGS 16 #define MAX_OPEN_LOGS 8
static LogState *openLogTable[MAX_OPEN_LOGS]; static LogState openLogTable[MAX_OPEN_LOGS];
/* /*
* Allocate an fd and associate a new LogState with it. * Allocate an fd and associate a new LogState with it.
@ -134,11 +134,10 @@ static LogState *createLogState()
{ {
size_t i; size_t i;
for (i = 0; i < sizeof(openLogTable); i++) { for (i = 0; i < (sizeof(openLogTable) / sizeof(openLogTable[0])); i++) {
if (openLogTable[i] == NULL) { if (openLogTable[i].fakeFd == 0) {
openLogTable[i] = calloc(1, sizeof(LogState)); openLogTable[i].fakeFd = FAKE_FD_BASE + i;
openLogTable[i]->fakeFd = FAKE_FD_BASE + i; return &openLogTable[i];
return openLogTable[i];
} }
} }
return NULL; return NULL;
@ -150,7 +149,7 @@ static LogState *createLogState()
static LogState *fdToLogState(int fd) static LogState *fdToLogState(int fd)
{ {
if (fd >= FAKE_FD_BASE && fd < FAKE_FD_BASE + MAX_OPEN_LOGS) { if (fd >= FAKE_FD_BASE && fd < FAKE_FD_BASE + MAX_OPEN_LOGS) {
return openLogTable[fd - FAKE_FD_BASE]; return &openLogTable[fd - FAKE_FD_BASE];
} }
return NULL; return NULL;
} }
@ -166,9 +165,7 @@ static void deleteFakeFd(int fd)
ls = fdToLogState(fd); ls = fdToLogState(fd);
if (ls != NULL) { if (ls != NULL) {
openLogTable[fd - FAKE_FD_BASE] = NULL; memset(&openLogTable[fd - FAKE_FD_BASE], 0, sizeof(openLogTable[0]));
free(ls->debugName);
free(ls);
} }
unlock(); unlock();
@ -191,10 +188,12 @@ static void configureInitialState(const char* pathName, LogState* logState)
{ {
static const int kDevLogLen = sizeof("/dev/log/") - 1; static const int kDevLogLen = sizeof("/dev/log/") - 1;
logState->debugName = strdup(pathName); strncpy(logState->debugName, pathName, sizeof(logState->debugName));
logState->debugName[sizeof(logState->debugName) - 1] = '\0';
/* identify binary logs */ /* identify binary logs */
if (strcmp(pathName + kDevLogLen, "events") == 0) { if (!strcmp(pathName + kDevLogLen, "events") ||
!strcmp(pathName + kDevLogLen, "security")) {
logState->isBinary = 1; logState->isBinary = 1;
} }
@ -218,8 +217,7 @@ static void configureInitialState(const char* pathName, LogState* logState)
i = 0; i = 0;
while (*tags != '\0' && !isspace(*tags) && *tags != ':' && while (*tags != '\0' && !isspace(*tags) && *tags != ':' &&
i < kMaxTagLen) i < kMaxTagLen) {
{
tagName[i++] = *tags++; tagName[i++] = *tags++;
} }
if (i == kMaxTagLen) { if (i == kMaxTagLen) {
@ -320,9 +318,9 @@ static const char* getPriorityString(int priority)
}; };
int idx; int idx;
idx = (int) priority - (int) ANDROID_LOG_VERBOSE; idx = (int)priority - (int)ANDROID_LOG_VERBOSE;
if (idx < 0 || if (idx < 0 ||
idx >= (int) (sizeof(priorityStrings) / sizeof(priorityStrings[0]))) idx >= (int)(sizeof(priorityStrings) / sizeof(priorityStrings[0])))
return "?unknown?"; return "?unknown?";
return priorityStrings[idx]; return priorityStrings[idx];
} }
@ -454,13 +452,15 @@ static void showLog(LogState *state,
while (p < end) { while (p < end) {
if (*p++ == '\n') numLines++; if (*p++ == '\n') numLines++;
} }
if (p > msg && *(p-1) != '\n') numLines++; if (p > msg && *(p-1) != '\n') {
numLines++;
}
/* /*
* Create an array of iovecs large enough to write all of * Create an array of iovecs large enough to write all of
* the lines with a prefix and a suffix. * the lines with a prefix and a suffix.
*/ */
const size_t INLINE_VECS = 6; const size_t INLINE_VECS = 64;
const size_t MAX_LINES = ((size_t)~0)/(3*sizeof(struct iovec*)); const size_t MAX_LINES = ((size_t)~0)/(3*sizeof(struct iovec*));
struct iovec stackVec[INLINE_VECS]; struct iovec stackVec[INLINE_VECS];
struct iovec* vec = stackVec; struct iovec* vec = stackVec;
@ -494,7 +494,9 @@ static void showLog(LogState *state,
v++; v++;
} }
const char* start = p; const char* start = p;
while (p < end && *p != '\n') p++; while (p < end && *p != '\n') {
p++;
}
if ((p-start) > 0) { if ((p-start) > 0) {
v->iov_base = (void*)start; v->iov_base = (void*)start;
v->iov_len = p-start; v->iov_len = p-start;

View file

@ -125,7 +125,7 @@ static int __write_to_log_initialize()
#if FAKE_LOG_DEVICE #if FAKE_LOG_DEVICE
for (i = 0; i < LOG_ID_MAX; i++) { for (i = 0; i < LOG_ID_MAX; i++) {
char buf[sizeof("/dev/log_system")]; char buf[sizeof("/dev/log_security")];
snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i)); snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
log_fds[i] = fakeLogOpen(buf, O_WRONLY); log_fds[i] = fakeLogOpen(buf, O_WRONLY);
} }