| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/ashmem.h> | 
|  | 18 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | /* | 
|  | 20 | * Implementation of the user-space ashmem API for the simulator, which lacks | 
|  | 21 | * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version. | 
|  | 22 | */ | 
|  | 23 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <errno.h> | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 25 | #include <fcntl.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <limits.h> | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 27 | #include <stdio.h> | 
|  | 28 | #include <stdlib.h> | 
|  | 29 | #include <string.h> | 
|  | 30 | #include <sys/stat.h> | 
|  | 31 | #include <sys/types.h> | 
|  | 32 | #include <time.h> | 
|  | 33 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 |  | 
| Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 35 | #include <utils/Compat.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 |  | 
| Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 37 | int ashmem_create_region(const char* /*ignored*/, size_t size) { | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 38 | char pattern[PATH_MAX]; | 
|  | 39 | snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid()); | 
|  | 40 | int fd = mkstemp(pattern); | 
| Elliott Hughes | 3918090 | 2015-12-16 08:33:52 -0800 | [diff] [blame] | 41 | if (fd == -1) return -1; | 
|  | 42 |  | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 43 | unlink(pattern); | 
| Elliott Hughes | 3918090 | 2015-12-16 08:33:52 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) { | 
|  | 46 | close(fd); | 
|  | 47 | return -1; | 
| Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 48 | } | 
| Elliott Hughes | 3918090 | 2015-12-16 08:33:52 -0800 | [diff] [blame] | 49 |  | 
|  | 50 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 53 | int ashmem_set_prot_region(int /*fd*/, int /*prot*/) { | 
| Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 54 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 57 | int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) { | 
| Elliott Hughes | 427c605 | 2016-05-13 14:47:30 -0700 | [diff] [blame] | 58 | return 0 /*ASHMEM_NOT_PURGED*/; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Elliott Hughes | 38d2567 | 2017-11-30 16:23:51 -0800 | [diff] [blame] | 61 | int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) { | 
| Elliott Hughes | 427c605 | 2016-05-13 14:47:30 -0700 | [diff] [blame] | 62 | return 0 /*ASHMEM_IS_UNPINNED*/; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } | 
| Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 64 |  | 
|  | 65 | int ashmem_get_size_region(int fd) | 
|  | 66 | { | 
| Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 67 | struct stat buf; | 
| Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 68 | int result = fstat(fd, &buf); | 
| Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 69 | if (result == -1) { | 
|  | 70 | return -1; | 
|  | 71 | } | 
| Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 72 |  | 
| Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 73 | /* | 
|  | 74 | * Check if this is an "ashmem" region. | 
|  | 75 | * TODO: This is very hacky, and can easily break. | 
|  | 76 | * We need some reliable indicator. | 
|  | 77 | */ | 
| Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 78 | if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) { | 
|  | 79 | errno = ENOTTY; | 
|  | 80 | return -1; | 
|  | 81 | } | 
| Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 82 |  | 
| Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 83 | return buf.st_size; | 
| Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 84 | } | 
| Tim Murray | 8879ed7 | 2019-04-04 09:16:32 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | void ashmem_init() {} |