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

Commit 8edbbe1d authored by Mark Salyzyn's avatar Mark Salyzyn
Browse files

fake_log_device: long lived allocations

(cherry pick from commit 0085a135)

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
parent 48972c80
Loading
Loading
Loading
Loading
+23 −21
Original line number Original line Diff line number Diff line
@@ -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) {
@@ -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;
+1 −1
Original line number Original line Diff line number Diff line
@@ -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);
    }
    }