diff options
author | Elliott Hughes <enh@google.com> | 2015-02-10 16:52:44 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-02-10 16:52:44 +0000 |
commit | 7faec394d1cda4132d53f763c7bef7acd7c9d1ba (patch) | |
tree | 16799da0870c9984828e13dbc66c0225b0755074 /libcutils | |
parent | 167c1dffc723a5052020cea52f3ce69a67fb7f22 (diff) | |
parent | 3bc8ae63ce3bbcc0ab61def99a4e9b4822ba3f51 (diff) | |
download | system_core-7faec394d1cda4132d53f763c7bef7acd7c9d1ba.zip system_core-7faec394d1cda4132d53f763c7bef7acd7c9d1ba.tar.gz system_core-7faec394d1cda4132d53f763c7bef7acd7c9d1ba.tar.bz2 |
am 3bc8ae63: Merge "Use mkstemp for host ashmem."
* commit '3bc8ae63ce3bbcc0ab61def99a4e9b4822ba3f51':
Use mkstemp for host ashmem.
Diffstat (limited to 'libcutils')
-rw-r--r-- | libcutils/ashmem-host.c | 50 |
1 files changed, 7 insertions, 43 deletions
diff --git a/libcutils/ashmem-host.c b/libcutils/ashmem-host.c index 4ac4f57..abc4f94 100644 --- a/libcutils/ashmem-host.c +++ b/libcutils/ashmem-host.c @@ -22,7 +22,6 @@ #include <errno.h> #include <fcntl.h> #include <limits.h> -#include <pthread.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -33,51 +32,18 @@ #include <unistd.h> #include <cutils/ashmem.h> +#include <utils/Compat.h> #ifndef __unused #define __unused __attribute__((__unused__)) #endif -static pthread_once_t seed_initialized = PTHREAD_ONCE_INIT; -static void initialize_random() { - srand(time(NULL) + getpid()); -} - int ashmem_create_region(const char *ignored __unused, size_t size) { - static const char txt[] = "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - char name[64]; - unsigned int retries = 0; - pid_t pid = getpid(); - int fd; - if (pthread_once(&seed_initialized, &initialize_random) != 0) { - return -1; - } - do { - /* not beautiful, its just wolf-like loop unrolling */ - snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c", - pid, - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))], - txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))]); - - /* open O_EXCL & O_CREAT: we are either the sole owner or we fail */ - fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd == -1) { - /* unlikely, but if we failed because `name' exists, retry */ - if (errno != EEXIST || ++retries >= 6) { - return -1; - } - } - } while (fd == -1); - /* truncate the file to `len' bytes */ - if (ftruncate(fd, size) != -1 && unlink(name) != -1) { + char template[PATH_MAX]; + snprintf(template, sizeof(template), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid()); + int fd = mkstemp(template); + if (fd != -1 && TEMP_FAILURE_RETRY(ftruncate(fd, size)) != -1 && unlink(template) != -1) { return fd; } close(fd); @@ -102,9 +68,7 @@ int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __un int ashmem_get_size_region(int fd) { struct stat buf; - int result; - - result = fstat(fd, &buf); + int result = fstat(fd, &buf); if (result == -1) { return -1; } @@ -116,5 +80,5 @@ int ashmem_get_size_region(int fd) return -1; } - return (int)buf.st_size; // TODO: care about overflow (> 2GB file)? + return buf.st_size; } |