diff --git a/include/cutils/ashmem.h b/include/cutils/ashmem.h index acedf738b..d80caa698 100644 --- a/include/cutils/ashmem.h +++ b/include/cutils/ashmem.h @@ -20,6 +20,7 @@ extern "C" { #endif +int ashmem_valid(int fd); int ashmem_create_region(const char *name, size_t size); int ashmem_set_prot_region(int fd, int prot); int ashmem_pin_region(int fd, size_t offset, size_t len); diff --git a/libcutils/ashmem-dev.c b/libcutils/ashmem-dev.c index 4a07d66bb..09fa09a09 100644 --- a/libcutils/ashmem-dev.c +++ b/libcutils/ashmem-dev.c @@ -85,7 +85,7 @@ static int __ashmem_open() } /* Make sure file descriptor references ashmem, negative number means false */ -static int __ashmem_is_ashmem(int fd) +static int __ashmem_is_ashmem(int fd, int fatal) { dev_t rdev; struct stat st; @@ -117,22 +117,29 @@ static int __ashmem_is_ashmem(int fd) } } - if (rdev) { - LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d", - fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), - S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP, - major(rdev), minor(rdev)); - } else { - LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o", - fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), - S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP); + if (fatal) { + if (rdev) { + LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d", + fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), + S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP, + major(rdev), minor(rdev)); + } else { + LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o", + fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), + S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP); + } + /* NOTREACHED */ } - /* NOTREACHED */ errno = ENOTTY; return -1; } +int ashmem_valid(int fd) +{ + return __ashmem_is_ashmem(fd, 0) >= 0; +} + /* * ashmem_create_region - creates a new ashmem region and returns the file * descriptor, or <0 on error @@ -175,7 +182,7 @@ error: int ashmem_set_prot_region(int fd, int prot) { - int ret = __ashmem_is_ashmem(fd); + int ret = __ashmem_is_ashmem(fd, 1); if (ret < 0) { return ret; } @@ -187,7 +194,7 @@ int ashmem_pin_region(int fd, size_t offset, size_t len) { struct ashmem_pin pin = { offset, len }; - int ret = __ashmem_is_ashmem(fd); + int ret = __ashmem_is_ashmem(fd, 1); if (ret < 0) { return ret; } @@ -199,7 +206,7 @@ int ashmem_unpin_region(int fd, size_t offset, size_t len) { struct ashmem_pin pin = { offset, len }; - int ret = __ashmem_is_ashmem(fd); + int ret = __ashmem_is_ashmem(fd, 1); if (ret < 0) { return ret; } @@ -209,7 +216,7 @@ int ashmem_unpin_region(int fd, size_t offset, size_t len) int ashmem_get_size_region(int fd) { - int ret = __ashmem_is_ashmem(fd); + int ret = __ashmem_is_ashmem(fd, 1); if (ret < 0) { return ret; }