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 | |
| 17 | /* |
| 18 | * Implementation of the user-space ashmem API for devices, which have our |
| 19 | * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, |
| 20 | * used by the simulator. |
| 21 | */ |
| 22 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 23 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <fcntl.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 25 | #include <string.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
| 31 | #include <linux/ashmem.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 32 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | #include <cutils/ashmem.h> |
| 34 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 35 | #define ASHMEM_DEVICE "/dev/ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * ashmem_create_region - creates a new ashmem region and returns the file |
| 39 | * descriptor, or <0 on error |
| 40 | * |
| 41 | * `name' is an optional label to give the region (visible in /proc/pid/maps) |
| 42 | * `size' is the size of the region, in page-aligned bytes |
| 43 | */ |
| 44 | int ashmem_create_region(const char *name, size_t size) |
| 45 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 46 | int ret, save_errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 48 | int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR)); |
| 49 | if (fd < 0) { |
| 50 | return fd; |
| 51 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 53 | if (name) { |
| 54 | char buf[ASHMEM_NAME_LEN] = {0}; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 56 | strlcpy(buf, name, sizeof(buf)); |
| 57 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf)); |
| 58 | if (ret < 0) { |
| 59 | goto error; |
| 60 | } |
| 61 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 63 | ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size)); |
| 64 | if (ret < 0) { |
| 65 | goto error; |
| 66 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 68 | return fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | |
| 70 | error: |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 71 | save_errno = errno; |
| 72 | close(fd); |
| 73 | errno = save_errno; |
| 74 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int ashmem_set_prot_region(int fd, int prot) |
| 78 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 79 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int ashmem_pin_region(int fd, size_t offset, size_t len) |
| 83 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 84 | struct ashmem_pin pin = { offset, len }; |
| 85 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_PIN, &pin)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int ashmem_unpin_region(int fd, size_t offset, size_t len) |
| 89 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 90 | struct ashmem_pin pin = { offset, len }; |
| 91 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_UNPIN, &pin)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 93 | |
| 94 | int ashmem_get_size_region(int fd) |
| 95 | { |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame^] | 96 | return TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL)); |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 97 | } |