From b619152148dadeda1acef702420f43c30e7d6d30 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Tue, 2 Feb 2016 08:05:54 -0800 Subject: libcutils: ashmem fortify and comply with Android coding standard - sort header order and in groups - remove all tabs, use only spaces - use TEMP_FAILURE_RETRY in system calls - preserve errno for -1 return Bug: 26871259 Change-Id: I94fffbcaeba01fcc18a3ed07c02389c06c54d3b7 --- libcutils/ashmem-dev.c | 63 +++++++++++++++++++++++++++---------------------- libcutils/ashmem-host.c | 7 ++++-- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/libcutils/ashmem-dev.c b/libcutils/ashmem-dev.c index 3089a94..a5203e1 100644 --- a/libcutils/ashmem-dev.c +++ b/libcutils/ashmem-dev.c @@ -20,17 +20,19 @@ * used by the simulator. */ -#include +#include +#include #include -#include -#include #include -#include +#include +#include +#include #include + #include -#define ASHMEM_DEVICE "/dev/ashmem" +#define ASHMEM_DEVICE "/dev/ashmem" /* * ashmem_create_region - creates a new ashmem region and returns the file @@ -41,50 +43,55 @@ */ int ashmem_create_region(const char *name, size_t size) { - int fd, ret; + int ret, save_errno; - fd = open(ASHMEM_DEVICE, O_RDWR); - if (fd < 0) - return fd; + int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR)); + if (fd < 0) { + return fd; + } - if (name) { - char buf[ASHMEM_NAME_LEN] = {0}; + if (name) { + char buf[ASHMEM_NAME_LEN] = {0}; - strlcpy(buf, name, sizeof(buf)); - ret = ioctl(fd, ASHMEM_SET_NAME, buf); - if (ret < 0) - goto error; - } + strlcpy(buf, name, sizeof(buf)); + ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf)); + if (ret < 0) { + goto error; + } + } - ret = ioctl(fd, ASHMEM_SET_SIZE, size); - if (ret < 0) - goto error; + ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size)); + if (ret < 0) { + goto error; + } - return fd; + return fd; error: - close(fd); - return ret; + save_errno = errno; + close(fd); + errno = save_errno; + return ret; } int ashmem_set_prot_region(int fd, int prot) { - return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); + return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)); } int ashmem_pin_region(int fd, size_t offset, size_t len) { - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_PIN, &pin); + struct ashmem_pin pin = { offset, len }; + return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)); } int ashmem_unpin_region(int fd, size_t offset, size_t len) { - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_UNPIN, &pin); + struct ashmem_pin pin = { offset, len }; + return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)); } int ashmem_get_size_region(int fd) { - return ioctl(fd, ASHMEM_GET_SIZE, NULL); + return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)); } diff --git a/libcutils/ashmem-host.c b/libcutils/ashmem-host.c index abc4f94..99a2759 100644 --- a/libcutils/ashmem-host.c +++ b/libcutils/ashmem-host.c @@ -73,8 +73,11 @@ int ashmem_get_size_region(int fd) return -1; } - // Check if this is an "ashmem" region. - // TODO: This is very hacky, and can easily break. We need some reliable indicator. + /* + * Check if this is an "ashmem" region. + * TODO: This is very hacky, and can easily break. + * We need some reliable indicator. + */ if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) { errno = ENOTTY; return -1; -- cgit v1.1