xref: /aosp_15_r20/frameworks/native/include/android/sharedmem.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker /**
18*38e8c45fSAndroid Build Coastguard Worker  * @addtogroup Memory
19*38e8c45fSAndroid Build Coastguard Worker  * @{
20*38e8c45fSAndroid Build Coastguard Worker  */
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker /**
23*38e8c45fSAndroid Build Coastguard Worker  * @file sharedmem.h
24*38e8c45fSAndroid Build Coastguard Worker  * @brief Shared memory buffers that can be shared between processes.
25*38e8c45fSAndroid Build Coastguard Worker  */
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #ifndef ANDROID_SHARED_MEMORY_H
28*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_SHARED_MEMORY_H
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker #include <stddef.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <sys/cdefs.h>
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker /******************************************************************
34*38e8c45fSAndroid Build Coastguard Worker  *
35*38e8c45fSAndroid Build Coastguard Worker  * IMPORTANT NOTICE:
36*38e8c45fSAndroid Build Coastguard Worker  *
37*38e8c45fSAndroid Build Coastguard Worker  *   This file is part of Android's set of stable system headers
38*38e8c45fSAndroid Build Coastguard Worker  *   exposed by the Android NDK (Native Development Kit).
39*38e8c45fSAndroid Build Coastguard Worker  *
40*38e8c45fSAndroid Build Coastguard Worker  *   Third-party source AND binary code relies on the definitions
41*38e8c45fSAndroid Build Coastguard Worker  *   here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
42*38e8c45fSAndroid Build Coastguard Worker  *
43*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
44*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
45*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
46*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
47*38e8c45fSAndroid Build Coastguard Worker  */
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
50*38e8c45fSAndroid Build Coastguard Worker extern "C" {
51*38e8c45fSAndroid Build Coastguard Worker #endif
52*38e8c45fSAndroid Build Coastguard Worker 
53*38e8c45fSAndroid Build Coastguard Worker /**
54*38e8c45fSAndroid Build Coastguard Worker  * Create a shared memory region.
55*38e8c45fSAndroid Build Coastguard Worker  *
56*38e8c45fSAndroid Build Coastguard Worker  * Create a shared memory region and returns a file descriptor.  The resulting file descriptor can be
57*38e8c45fSAndroid Build Coastguard Worker  * mapped into the process' memory using mmap(2) with `PROT_READ | PROT_WRITE | PROT_EXEC`.
58*38e8c45fSAndroid Build Coastguard Worker  * Access to shared memory regions can be restricted with {@link ASharedMemory_setProt}.
59*38e8c45fSAndroid Build Coastguard Worker  *
60*38e8c45fSAndroid Build Coastguard Worker  * Use close(2) to release the shared memory region.
61*38e8c45fSAndroid Build Coastguard Worker  *
62*38e8c45fSAndroid Build Coastguard Worker  * Use <a href="/reference/android/os/ParcelFileDescriptor">android.os.ParcelFileDescriptor</a>
63*38e8c45fSAndroid Build Coastguard Worker  * to pass the file descriptor to another process. File descriptors may also be sent to other
64*38e8c45fSAndroid Build Coastguard Worker  * processes over a Unix domain socket with sendmsg(2) and `SCM_RIGHTS`. See sendmsg(3) and
65*38e8c45fSAndroid Build Coastguard Worker  * cmsg(3) man pages for more information.
66*38e8c45fSAndroid Build Coastguard Worker  *
67*38e8c45fSAndroid Build Coastguard Worker  * If you intend to share this file descriptor with a child process after
68*38e8c45fSAndroid Build Coastguard Worker  * calling exec(3), note that you will need to use fcntl(2) with `F_SETFD`
69*38e8c45fSAndroid Build Coastguard Worker  * to clear the `FD_CLOEXEC` flag for this to work on all versions of Android.
70*38e8c45fSAndroid Build Coastguard Worker  *
71*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 26.
72*38e8c45fSAndroid Build Coastguard Worker  *
73*38e8c45fSAndroid Build Coastguard Worker  * \param name an optional name.
74*38e8c45fSAndroid Build Coastguard Worker  * \param size size of the shared memory region
75*38e8c45fSAndroid Build Coastguard Worker  * \return file descriptor that denotes the shared memory;
76*38e8c45fSAndroid Build Coastguard Worker  *         -1 and sets `errno` on failure, or `-EINVAL` if the error is that size was 0.
77*38e8c45fSAndroid Build Coastguard Worker  */
78*38e8c45fSAndroid Build Coastguard Worker int ASharedMemory_create(const char *name, size_t size) __INTRODUCED_IN(26);
79*38e8c45fSAndroid Build Coastguard Worker 
80*38e8c45fSAndroid Build Coastguard Worker /**
81*38e8c45fSAndroid Build Coastguard Worker  * Get the size of the shared memory region.
82*38e8c45fSAndroid Build Coastguard Worker  *
83*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 26.
84*38e8c45fSAndroid Build Coastguard Worker  *
85*38e8c45fSAndroid Build Coastguard Worker  * \param fd file descriptor of the shared memory region
86*38e8c45fSAndroid Build Coastguard Worker  * \return size in bytes; 0 if `fd` is not a valid shared memory file descriptor.
87*38e8c45fSAndroid Build Coastguard Worker  */
88*38e8c45fSAndroid Build Coastguard Worker size_t ASharedMemory_getSize(int fd) __INTRODUCED_IN(26);
89*38e8c45fSAndroid Build Coastguard Worker 
90*38e8c45fSAndroid Build Coastguard Worker /**
91*38e8c45fSAndroid Build Coastguard Worker  * Restrict access of shared memory region.
92*38e8c45fSAndroid Build Coastguard Worker  *
93*38e8c45fSAndroid Build Coastguard Worker  * This function restricts access of a shared memory region. Access can only be removed. The effect
94*38e8c45fSAndroid Build Coastguard Worker  * applies globally to all file descriptors in all processes across the system that refer to this
95*38e8c45fSAndroid Build Coastguard Worker  * shared memory region. Existing memory mapped regions are not affected.
96*38e8c45fSAndroid Build Coastguard Worker  *
97*38e8c45fSAndroid Build Coastguard Worker  * It is a common use case to create a shared memory region, map it read/write locally to intialize
98*38e8c45fSAndroid Build Coastguard Worker  * content, and then send the shared memory to another process with read only access. Code example
99*38e8c45fSAndroid Build Coastguard Worker  * as below (error handling omited).
100*38e8c45fSAndroid Build Coastguard Worker  *
101*38e8c45fSAndroid Build Coastguard Worker  *
102*38e8c45fSAndroid Build Coastguard Worker  *     int fd = ASharedMemory_create("memory", 128);
103*38e8c45fSAndroid Build Coastguard Worker  *
104*38e8c45fSAndroid Build Coastguard Worker  *     // By default it has PROT_READ | PROT_WRITE | PROT_EXEC.
105*38e8c45fSAndroid Build Coastguard Worker  *     size_t memSize = ASharedMemory_getSize(fd);
106*38e8c45fSAndroid Build Coastguard Worker  *     char *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
107*38e8c45fSAndroid Build Coastguard Worker  *
108*38e8c45fSAndroid Build Coastguard Worker  *     strcpy(buffer, "This is an example."); // trivially initialize content
109*38e8c45fSAndroid Build Coastguard Worker  *
110*38e8c45fSAndroid Build Coastguard Worker  *     // limit access to read only
111*38e8c45fSAndroid Build Coastguard Worker  *     ASharedMemory_setProt(fd, PROT_READ);
112*38e8c45fSAndroid Build Coastguard Worker  *
113*38e8c45fSAndroid Build Coastguard Worker  *     // share fd with another process here and the other process can only map with PROT_READ.
114*38e8c45fSAndroid Build Coastguard Worker  *
115*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 26.
116*38e8c45fSAndroid Build Coastguard Worker  *
117*38e8c45fSAndroid Build Coastguard Worker  * \param fd   file descriptor of the shared memory region.
118*38e8c45fSAndroid Build Coastguard Worker  * \param prot any bitwise-or'ed combination of `PROT_READ`, `PROT_WRITE`, `PROT_EXEC` denoting
119*38e8c45fSAndroid Build Coastguard Worker  *             updated access. Note access can only be removed, but not added back.
120*38e8c45fSAndroid Build Coastguard Worker  * \return 0 for success, -1 and sets `errno` on failure.
121*38e8c45fSAndroid Build Coastguard Worker  */
122*38e8c45fSAndroid Build Coastguard Worker int ASharedMemory_setProt(int fd, int prot) __INTRODUCED_IN(26);
123*38e8c45fSAndroid Build Coastguard Worker 
124*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
125*38e8c45fSAndroid Build Coastguard Worker };
126*38e8c45fSAndroid Build Coastguard Worker #endif
127*38e8c45fSAndroid Build Coastguard Worker 
128*38e8c45fSAndroid Build Coastguard Worker #endif // ANDROID_SHARED_MEMORY_H
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker /** @} */
131