fake_log_device: long lived allocations

Use static space for long lived allocations as they
will appear to act like a memory leak. Resort to a
larger on-stack iovec to reduce the chances of an
allocation. Fix bug in writer where not enough size
was available for "security" buffer name. Minor
transitions to more consistent coding style.

Bug: 27107691
Change-Id: I68c918e7b916b1ae3b04829d48b3eddaa0a7e739
This commit is contained in:
Mark Salyzyn 2016-02-17 09:54:47 -08:00
parent fd9619004b
commit 0085a135b9
2 changed files with 24 additions and 22 deletions

View File

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

View File

@ -125,7 +125,7 @@ static int __write_to_log_initialize()
#if FAKE_LOG_DEVICE
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));
log_fds[i] = fakeLogOpen(buf, O_WRONLY);
}