diff options
author | Mark Salyzyn <salyzyn@google.com> | 2015-01-26 10:46:44 -0800 |
---|---|---|
committer | Mark Salyzyn <salyzyn@google.com> | 2015-02-25 17:44:18 +0000 |
commit | 2d3f38a6b8e724749b59d201a01b35fa0951141e (patch) | |
tree | 99855d3dd759307ced68e36ad7206747e55bf51d | |
parent | 72bf2a710089b5fff42059d5946fe7bdd6b12480 (diff) | |
download | system_core-2d3f38a6b8e724749b59d201a01b35fa0951141e.zip system_core-2d3f38a6b8e724749b59d201a01b35fa0951141e.tar.gz system_core-2d3f38a6b8e724749b59d201a01b35fa0951141e.tar.bz2 |
liblog: introduce ANDROID_LOG_* flags
Move away from using POSIX open(2) flags and introduce ANDROID_LOG_* flags to
replace them. Add security by preventing random mode flags from getting into
underlying POSIX calls. ANDROID_LOG_* flags overlap POSIX O_* flag definitions.
Change-Id: Ib32bb64c287e8bf150be62242e1ba46bb37839fc
-rw-r--r-- | debuggerd/tombstone.cpp | 2 | ||||
-rw-r--r-- | include/log/logger.h | 6 | ||||
-rw-r--r-- | liblog/README | 13 | ||||
-rw-r--r-- | liblog/log_read.c | 12 | ||||
-rw-r--r-- | liblog/log_read_kern.c | 22 | ||||
-rw-r--r-- | liblog/tests/libc_test.cpp | 4 | ||||
-rw-r--r-- | liblog/tests/liblog_benchmark.cpp | 4 | ||||
-rw-r--r-- | liblog/tests/liblog_test.cpp | 14 | ||||
-rw-r--r-- | logcat/logcat.cpp | 8 |
9 files changed, 46 insertions, 39 deletions
diff --git a/debuggerd/tombstone.cpp b/debuggerd/tombstone.cpp index 4233d46..e927ea3 100644 --- a/debuggerd/tombstone.cpp +++ b/debuggerd/tombstone.cpp @@ -453,7 +453,7 @@ static void dump_log_file( } logger_list = android_logger_list_open( - android_name_to_log_id(filename), O_RDONLY | O_NONBLOCK, tail, pid); + android_name_to_log_id(filename), ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tail, pid); if (!logger_list) { ALOGE("Unable to open %s: %s\n", filename, strerror(errno)); diff --git a/include/log/logger.h b/include/log/logger.h index 53be1d3..570f02b 100644 --- a/include/log/logger.h +++ b/include/log/logger.h @@ -154,6 +154,12 @@ ssize_t android_logger_get_prune_list(struct logger_list *logger_list, int android_logger_set_prune_list(struct logger_list *logger_list, char *buf, size_t len); +#define ANDROID_LOG_RDONLY O_RDONLY +#define ANDROID_LOG_WRONLY O_WRONLY +#define ANDROID_LOG_RDWR O_RDWR +#define ANDROID_LOG_ACCMODE O_ACCMODE +#define ANDROID_LOG_NONBLOCK O_NONBLOCK + struct logger_list *android_logger_list_alloc(int mode, unsigned int tail, pid_t pid); diff --git a/liblog/README b/liblog/README index d7472e4..0676aec 100644 --- a/liblog/README +++ b/liblog/README @@ -111,20 +111,21 @@ DESCRIPTION ger_list_alloc, calling in turn the android_logger_open for each log id. Each entry can be retrieved with android_logger_list_read. The log(s) can be closed with android_logger_list_free. The logs should be - opened with an O_RDONLY mode. O_NDELAY mode will report when the log - reading is done with an EAGAIN error return code, otherwise the - android_logger_list_read call will block for new entries. + opened with an ANDROID_LOG_RDONLY mode. ANDROID_LOG_NONBLOCK mode + will report when the log reading is done with an EAGAIN error return + code, otherwise the android_logger_list_read call will block for new + entries. The value returned by android_logger_open can be used as a parameter to the android_logger_clear function to empty the sub-log. It is recom‐ - mended to only open log O_WRONLY. + mended to only open log ANDROID_LOG_WRONLY in that case. The value returned by android_logger_open can be used as a parameter to the android_logger_get_log_(size|readable_size|version) to retrieve the sub-log maximum size, readable size and log buffer format protocol ver‐ sion respectively. android_logger_get_id returns the id that was used - when opening the sub-log. It is recommended to open the log O_RDONLY - in these cases. + when opening the sub-log. It is recommended to open the log + ANDROID_LOG_RDONLY in these cases. SEE ALSO syslogd(8) diff --git a/liblog/log_read.c b/liblog/log_read.c index dbed886..0b126cf 100644 --- a/liblog/log_read.c +++ b/liblog/log_read.c @@ -582,7 +582,7 @@ int android_logger_list_read(struct logger_list *logger_list, return -EINVAL; } - if (logger_list->mode & O_NONBLOCK) { + if (logger_list->mode & ANDROID_LOG_NONBLOCK) { memset(&ignore, 0, sizeof(ignore)); ignore.sa_handler = caught_signal; sigemptyset(&ignore.sa_mask); @@ -602,7 +602,7 @@ int android_logger_list_read(struct logger_list *logger_list, } strcpy(buffer, - (logger_list->mode & O_NONBLOCK) ? "dumpAndClose" : "stream"); + (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream"); cp = buffer + strlen(buffer); strcpy(cp, " lids"); @@ -640,14 +640,14 @@ int android_logger_list_read(struct logger_list *logger_list, cp += ret; } - if (logger_list->mode & O_NONBLOCK) { + if (logger_list->mode & ANDROID_LOG_NONBLOCK) { /* Deal with an unresponsive logd */ sigaction(SIGALRM, &ignore, &old_sigaction); old_alarm = alarm(30); } ret = write(sock, buffer, cp - buffer); e = errno; - if (logger_list->mode & O_NONBLOCK) { + if (logger_list->mode & ANDROID_LOG_NONBLOCK) { if (e == EINTR) { e = ETIMEDOUT; } @@ -673,7 +673,7 @@ int android_logger_list_read(struct logger_list *logger_list, while(1) { memset(log_msg, 0, sizeof(*log_msg)); - if (logger_list->mode & O_NONBLOCK) { + if (logger_list->mode & ANDROID_LOG_NONBLOCK) { /* particularily useful if tombstone is reporting for logd */ sigaction(SIGALRM, &ignore, &old_sigaction); old_alarm = alarm(30); @@ -681,7 +681,7 @@ int android_logger_list_read(struct logger_list *logger_list, /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */ ret = recv(logger_list->sock, log_msg, LOGGER_ENTRY_MAX_LEN, 0); e = errno; - if (logger_list->mode & O_NONBLOCK) { + if (logger_list->mode & ANDROID_LOG_NONBLOCK) { if ((ret == 0) || (e == EINTR)) { e = EAGAIN; ret = -1; diff --git a/liblog/log_read_kern.c b/liblog/log_read_kern.c index 41b8a51..bdc7b18 100644 --- a/liblog/log_read_kern.c +++ b/liblog/log_read_kern.c @@ -75,10 +75,10 @@ const char *android_log_id_to_name(log_id_t log_id) static int accessmode(int mode) { - if ((mode & O_ACCMODE) == O_WRONLY) { + if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_WRONLY) { return W_OK; } - if ((mode & O_ACCMODE) == O_RDWR) { + if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR) { return R_OK | W_OK; } return R_OK; @@ -117,7 +117,7 @@ log_id_t android_name_to_log_id(const char *logName) ++b; } - ret = check_allocate_accessible(&n, b, O_RDONLY); + ret = check_allocate_accessible(&n, b, ANDROID_LOG_RDONLY); free(n); if (ret) { return ret; @@ -201,8 +201,8 @@ static int logger_ioctl(struct logger *logger, int cmd, int mode) return -EFAULT; } - if (((mode & O_ACCMODE) == O_RDWR) - || (((mode ^ logger->top->mode) & O_ACCMODE) == 0)) { + if (((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR) + || (((mode ^ logger->top->mode) & ANDROID_LOG_ACCMODE) == 0)) { return ioctl(logger->fd, cmd); } @@ -227,13 +227,13 @@ static int logger_ioctl(struct logger *logger, int cmd, int mode) int android_logger_clear(struct logger *logger) { - return logger_ioctl(logger, LOGGER_FLUSH_LOG, O_WRONLY); + return logger_ioctl(logger, LOGGER_FLUSH_LOG, ANDROID_LOG_WRONLY); } /* returns the total size of the log's ring buffer */ long android_logger_get_log_size(struct logger *logger) { - return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, O_RDWR); + return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, ANDROID_LOG_RDWR); } int android_logger_set_log_size(struct logger *logger __unused, @@ -248,7 +248,7 @@ int android_logger_set_log_size(struct logger *logger __unused, */ long android_logger_get_log_readable_size(struct logger *logger) { - return logger_ioctl(logger, LOGGER_GET_LOG_LEN, O_RDONLY); + return logger_ioctl(logger, LOGGER_GET_LOG_LEN, ANDROID_LOG_RDONLY); } /* @@ -256,7 +256,7 @@ long android_logger_get_log_readable_size(struct logger *logger) */ int android_logger_get_log_version(struct logger *logger) { - int ret = logger_ioctl(logger, LOGGER_GET_VERSION, O_RDWR); + int ret = logger_ioctl(logger, LOGGER_GET_VERSION, ANDROID_LOG_RDWR); return (ret < 0) ? 1 : ret; } @@ -342,7 +342,7 @@ struct logger *android_logger_open(struct logger_list *logger_list, goto err_name; } - logger->fd = open(n, logger_list->mode); + logger->fd = open(n, logger_list->mode & (ANDROID_LOG_ACCMODE | ANDROID_LOG_NONBLOCK)); if (logger->fd < 0) { goto err_name; } @@ -565,7 +565,7 @@ int android_logger_list_read(struct logger_list *logger_list, if (result <= 0) { if (result) { error = errno; - } else if (logger_list->mode & O_NDELAY) { + } else if (logger_list->mode & ANDROID_LOG_NONBLOCK) { error = EAGAIN; } else { logger_list->timeout_ms = LOG_TIMEOUT_NEVER; diff --git a/liblog/tests/libc_test.cpp b/liblog/tests/libc_test.cpp index 9839729..29501be 100644 --- a/liblog/tests/libc_test.cpp +++ b/liblog/tests/libc_test.cpp @@ -39,7 +39,7 @@ TEST(libc, __libc_android_log_event_int) { pid_t pid = getpid(); ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid))); + LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -99,7 +99,7 @@ TEST(libc, __libc_fatal_no_abort) { pid_t pid = getpid(); ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - (log_id_t)LOG_ID_CRASH, O_RDONLY | O_NDELAY, 1000, pid))); + (log_id_t)LOG_ID_CRASH, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); char b[80]; struct timespec ts; diff --git a/liblog/tests/liblog_benchmark.cpp b/liblog/tests/liblog_benchmark.cpp index 549d79e..979aded 100644 --- a/liblog/tests/liblog_benchmark.cpp +++ b/liblog/tests/liblog_benchmark.cpp @@ -130,7 +130,7 @@ static void BM_log_latency(int iters) { pid_t pid = getpid(); struct logger_list * logger_list = android_logger_list_open(LOG_ID_EVENTS, - O_RDONLY, 0, pid); + ANDROID_LOG_RDONLY, 0, pid); if (!logger_list) { fprintf(stderr, "Unable to open events log: %s\n", strerror(errno)); @@ -208,7 +208,7 @@ static void BM_log_delay(int iters) { pid_t pid = getpid(); struct logger_list * logger_list = android_logger_list_open(LOG_ID_EVENTS, - O_RDONLY, 0, pid); + ANDROID_LOG_RDONLY, 0, pid); if (!logger_list) { fprintf(stderr, "Unable to open events log: %s\n", strerror(errno)); diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index 393e2cd..33f6481 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -122,7 +122,7 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { pid_t pid = getpid(); ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid))); + LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); log_time ts(CLOCK_MONOTONIC); @@ -223,7 +223,7 @@ TEST(liblog, android_logger_list_read__cpu) { v += pid & 0xFFFF; ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - LOG_ID_EVENTS, O_RDONLY, 1000, pid))); + LOG_ID_EVENTS, ANDROID_LOG_RDONLY, 1000, pid))); int count = 0; @@ -443,7 +443,7 @@ TEST(liblog, max_payload) { struct logger_list *logger_list; ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - LOG_ID_SYSTEM, O_RDONLY, 100, 0))); + LOG_ID_SYSTEM, ANDROID_LOG_RDONLY, 100, 0))); bool matches = false; ssize_t max_len = 0; @@ -505,7 +505,7 @@ TEST(liblog, too_big_payload) { struct logger_list *logger_list; ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( - LOG_ID_SYSTEM, O_RDONLY | O_NDELAY, 100, 0))); + LOG_ID_SYSTEM, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 100, 0))); ssize_t max_len = 0; @@ -552,12 +552,12 @@ TEST(liblog, dual_reader) { // >25 messages due to liblog.__android_log_buf_print__concurrentXX above. ASSERT_TRUE(NULL != (logger_list1 = android_logger_list_open( - LOG_ID_MAIN, O_RDONLY | O_NDELAY, 25, 0))); + LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 25, 0))); struct logger_list *logger_list2; if (NULL == (logger_list2 = android_logger_list_open( - LOG_ID_MAIN, O_RDONLY | O_NDELAY, 15, 0))) { + LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 15, 0))) { android_logger_list_close(logger_list1); ASSERT_TRUE(NULL != logger_list2); } @@ -595,7 +595,7 @@ TEST(liblog, dual_reader) { } TEST(liblog, android_logger_get_) { - struct logger_list * logger_list = android_logger_list_alloc(O_WRONLY, 0, 0); + struct logger_list * logger_list = android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0); for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { log_id_t id = static_cast<log_id_t>(i); diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index 79f2ebd..829a0af 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp @@ -324,7 +324,7 @@ int main(int argc, char **argv) int getPruneList = 0; char *setPruneList = NULL; int printStatistics = 0; - int mode = O_RDONLY; + int mode = ANDROID_LOG_RDONLY; const char *forceFilters = NULL; log_device_t* devices = NULL; log_device_t* dev; @@ -359,15 +359,15 @@ int main(int argc, char **argv) case 'c': clearLog = 1; - mode = O_WRONLY; + mode |= ANDROID_LOG_WRONLY; break; case 'd': - mode = O_RDONLY | O_NDELAY; + mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; break; case 't': - mode = O_RDONLY | O_NDELAY; + mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; /* FALLTHRU */ case 'T': if (strspn(optarg, "0123456789") != strlen(optarg)) { |