Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | * @addtogroup Memory |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file sharedmem.h |
Elliott Hughes | 8a6843d | 2019-11-12 11:54:46 -0800 | [diff] [blame] | 24 | * @brief Shared memory buffers that can be shared between processes. |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #ifndef ANDROID_SHARED_MEMORY_H |
| 28 | #define ANDROID_SHARED_MEMORY_H |
| 29 | |
| 30 | #include <stddef.h> |
Ryan Prichard | 2fb1d47 | 2018-07-19 20:32:19 -0700 | [diff] [blame] | 31 | #include <sys/cdefs.h> |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 32 | |
| 33 | /****************************************************************** |
| 34 | * |
| 35 | * IMPORTANT NOTICE: |
| 36 | * |
| 37 | * This file is part of Android's set of stable system headers |
| 38 | * exposed by the Android NDK (Native Development Kit). |
| 39 | * |
| 40 | * Third-party source AND binary code relies on the definitions |
| 41 | * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. |
| 42 | * |
| 43 | * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) |
| 44 | * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS |
| 45 | * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY |
| 46 | * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES |
| 47 | */ |
| 48 | |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Ryan Prichard | 2fb1d47 | 2018-07-19 20:32:19 -0700 | [diff] [blame] | 53 | #if __ANDROID_API__ >= 26 |
| 54 | |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Create a shared memory region. |
| 57 | * |
| 58 | * Create shared memory region and returns an file descriptor. The resulting file descriptor can be |
| 59 | * mmap'ed to process memory space with PROT_READ | PROT_WRITE | PROT_EXEC. Access to shared memory |
| 60 | * region can be restricted with {@link ASharedMemory_setProt}. |
| 61 | * |
| 62 | * Use close() to release the shared memory region. |
| 63 | * |
Elliott Hughes | 8a6843d | 2019-11-12 11:54:46 -0800 | [diff] [blame] | 64 | * Use {@link android.os.ParcelFileDescriptor} to pass the file descriptor to |
John Reck | 58e18f9 | 2019-11-13 10:44:52 -0800 | [diff] [blame] | 65 | * another process. File descriptors may also be sent to other processes over a Unix domain |
| 66 | * socket with sendmsg and SCM_RIGHTS. See sendmsg(3) and cmsg(3) man pages for more information. |
Elliott Hughes | 8a6843d | 2019-11-12 11:54:46 -0800 | [diff] [blame] | 67 | * |
Elliott Hughes | f74f0b7 | 2020-09-03 11:02:17 -0700 | [diff] [blame^] | 68 | * If you intend to share this file descriptor with a child process after |
| 69 | * calling exec(3), note that you will need to use fcntl(2) with FD_SETFD |
| 70 | * to clear the FD_CLOEXEC flag for this to work on all versions of Android. |
| 71 | * |
Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 72 | * Available since API level 26. |
| 73 | * |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 74 | * \param name an optional name. |
| 75 | * \param size size of the shared memory region |
Elliott Hughes | 8a6843d | 2019-11-12 11:54:46 -0800 | [diff] [blame] | 76 | * \return file descriptor that denotes the shared memory; -1 and sets errno on failure, or -EINVAL if the error is that size was 0. |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 77 | */ |
Elliott Hughes | 78e548d | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 78 | int ASharedMemory_create(const char *name, size_t size) __INTRODUCED_IN(26); |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Get the size of the shared memory region. |
| 82 | * |
Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 83 | * Available since API level 26. |
| 84 | * |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 85 | * \param fd file descriptor of the shared memory region |
| 86 | * \return size in bytes; 0 if fd is not a valid shared memory file descriptor. |
| 87 | */ |
Elliott Hughes | 78e548d | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 88 | size_t ASharedMemory_getSize(int fd) __INTRODUCED_IN(26); |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Restrict access of shared memory region. |
| 92 | * |
| 93 | * This function restricts access of a shared memory region. Access can only be removed. The effect |
| 94 | * applies globally to all file descriptors in all processes across the system that refer to this |
| 95 | * shared memory region. Existing memory mapped regions are not affected. |
| 96 | * |
| 97 | * It is a common use case to create a shared memory region, map it read/write locally to intialize |
| 98 | * content, and then send the shared memory to another process with read only access. Code example |
Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 99 | * as below (error handling omited). |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 100 | * |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 101 | * |
Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 102 | * int fd = ASharedMemory_create("memory", 128); |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 103 | * |
Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 104 | * // By default it has PROT_READ | PROT_WRITE | PROT_EXEC. |
Phong Tran | e0df58f | 2018-10-14 09:39:10 +0700 | [diff] [blame] | 105 | * size_t memSize = ASharedMemory_getSize(fd); |
| 106 | * char *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 107 | * |
Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 108 | * strcpy(buffer, "This is an example."); // trivially initialize content |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 109 | * |
Quddus Chong | 5163357 | 2017-05-09 10:39:23 -0700 | [diff] [blame] | 110 | * // limit access to read only |
| 111 | * ASharedMemory_setProt(fd, PROT_READ); |
| 112 | * |
| 113 | * // share fd with another process here and the other process can only map with PROT_READ. |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 114 | * |
Elliott Hughes | 0c75712 | 2018-07-31 12:10:19 -0700 | [diff] [blame] | 115 | * Available since API level 26. |
| 116 | * |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 117 | * \param fd file descriptor of the shared memory region. |
| 118 | * \param prot any bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting |
| 119 | * updated access. Note access can only be removed, but not added back. |
Elliott Hughes | 8a6843d | 2019-11-12 11:54:46 -0800 | [diff] [blame] | 120 | * \return 0 for success, -1 and sets errno on failure. |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 121 | */ |
Elliott Hughes | 78e548d | 2018-06-18 12:28:46 -0700 | [diff] [blame] | 122 | int ASharedMemory_setProt(int fd, int prot) __INTRODUCED_IN(26); |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 123 | |
Ryan Prichard | 2fb1d47 | 2018-07-19 20:32:19 -0700 | [diff] [blame] | 124 | #endif // __ANDROID_API__ >= 26 |
| 125 | |
Peng Xu | e1e7e38 | 2017-02-24 01:30:16 -0800 | [diff] [blame] | 126 | #ifdef __cplusplus |
| 127 | }; |
| 128 | #endif |
| 129 | |
| 130 | #endif // ANDROID_SHARED_MEMORY_H |
| 131 | |
| 132 | /** @} */ |