2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief system common functions
23 /*---------------------------------------------------------------------------------*
25 *---------------------------------------------------------------------------------*/
26 #include <vehicle_service/positioning_base_library.h>
31 #include "_pbInternalProc.h"
32 #include "WPF_STD_private.h"
34 /*---------------------------------------------------------------------------------*
36 *---------------------------------------------------------------------------------*/
39 * Create Shared Memory
41 * Create a file mapping object with the specified shared memory name and size, then <br>
42 * maps it to the invoking process space. If the shared memory that has already been <br>
43 * created is specified, an error is returned.<br>
44 * The shared memory management information allocates a heap area each time it is <br>
45 * created, and return this address as a shared memory handle.
47 * @param[in] TCHAR* name
48 * @param[in] DWORD size
51 * Except NULL Handle of the created shared memory(Management information pointer)<br>
54 HANDLE CreateSharedMemory(TCHAR* name, DWORD size) {
56 RET_API ret_api = RET_ERROR;
57 int32 fd = POSITIONINGBASELIBRARY_NON_FD;
58 SHARED_MEMORY* p_shm = NULL;
59 char c_file_path[PATH_MAX + 1] = SHARED_MEMORY_DIRECTORY;
61 if ((name != NULL) && (size != 0)) { // LCOV_EXCL_BR_LINE 6:name can not be NULL, size can not be 0
62 /* Allocate the shared memory management information area in the heap. */
63 p_shm = reinterpret_cast<SHARED_MEMORY *>(malloc(sizeof(SHARED_MEMORY)));
64 if (p_shm == NULL) { // LCOV_EXCL_BR_LINE 5: malloc's error case
71 /* Create directory */
72 (void)mkdir(SHARED_MEMORY_DIRECTORY, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
74 /* Create shared memory with the specified size and name.(Error if it already exists) */
75 (void)strncat(c_file_path, (const char*)name, 128);
76 fd = open(c_file_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO); // LCOV_EXCL_BR_LINE 5: standard lib error // NOLINT(whitespace/line_length)
77 if (fd > POSITIONINGBASELIBRARY_NON_FD) /* Coverity CID: 18775 compliant */ { // LCOV_EXCL_BR_LINE 5: standard lib error // NOLINT(whitespace/line_length)
79 lret = ftruncate(fd, size);
80 if (lret == EOK) { // LCOV_EXCL_BR_LINE 5: ftruncate's error case
81 /* Map the created shared memory to the invoking process space. */
82 p_shm->p_memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
83 if (p_shm->p_memory != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: standard lib error
91 if (fd > POSITIONINGBASELIBRARY_NON_FD) /* Coverity CID: 18775 compliant */ {
95 if (ret_api == RET_NORMAL) { // LCOV_EXCL_BR_LINE 200: can not be not normal
96 /* Indicate that it is the creator of the shared memory */
99 // LCOV_EXCL_START 200: can not be not normal
100 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
101 /* In case of an error */
102 if (fd != POSITIONINGBASELIBRARY_NON_FD) {
103 /* Delete shared memory object if created */
115 return (HANDLE)p_shm;
122 * Create a file mapping object with the specified shared memory name and size, <br>
123 * then map it to the invoking process space. Since this is not shared memory <br>
124 * creation processing, an error is returned if it is not a file mapping object.<br>
125 * The shared memory management information allocates a heap area each time <br>
126 * it is created, and return this address as a shared memory handle.
128 * @param[in] TCHAR* name
129 * @param[in] DWORD size
132 * Except NULL Handle of the acquired shared memory(Management information pointer)<br>
135 HANDLE OpenSharedMemory(TCHAR* name, DWORD size) {
136 RET_API ret_api = RET_ERROR;
137 int32 fd = POSITIONINGBASELIBRARY_NON_FD;
138 SHARED_MEMORY* p_shm = NULL;
139 char c_file_path[PATH_MAX + 1] = SHARED_MEMORY_DIRECTORY;
141 if ((name != NULL) && (size != 0)) { // LCOV_EXCL_BR_LINE 6:name can not be NULL, size can not be 0
142 /* Allocate the shared memory management information area in the heap. */
143 p_shm = reinterpret_cast<SHARED_MEMORY *>(malloc(sizeof(SHARED_MEMORY)));
144 if (p_shm == NULL) { // LCOV_EXCL_BR_LINE 5: malloc's error case
146 p_shm->h_heap = NULL;
151 /* Open shared memory with the specified size and name(Error if not exists) */
152 (void)strncat(c_file_path, (const char*)name, 128);
153 fd = open(c_file_path, O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO); // LCOV_EXCL_BR_LINE 5: standard lib error
154 if (fd > POSITIONINGBASELIBRARY_NON_FD) /* Coverity CID: 18774 compliant */ {
155 /* Map open shared memory to invoking process space. */
156 p_shm->p_memory = reinterpret_cast<SHARED_MEMORY *>(mmap(0, size, PROT_READ | PROT_WRITE, \
157 MAP_SHARED | POSITIONINGBASELIBRARY_MAP_NON_INIT, fd, 0));
158 if (p_shm->p_memory != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: standard lib error
159 ret_api = RET_NORMAL;
165 if (fd > POSITIONINGBASELIBRARY_NON_FD) /* Coverity CID: 18774 compliant */ {
169 if (ret_api == RET_NORMAL) {
170 /* Indicate that the opener of the shared memory not the creator of it. */
171 p_shm->owner = FALSE;
173 /* In case of an error */
181 return (HANDLE)p_shm;
184 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
185 * MODULE : DeleteAllSharedMemory()
186 * ABSTRACT : Delete all shared memory objects
187 * NOTE : Delete all shared memory objects
188 * : However, the heap area allocated at the time of shared memory creation or open is not released.
191 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
193 DeleteAllSharedMemory(void) { // LCOV_EXCL_START 8:dead code
194 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
197 char sz_cwd[PATH_MAX + 1] = {0};
199 struct dirent* p_dir_ent = NULL;
201 /* Get path of current directory */
202 cwd = getcwd(sz_cwd, PATH_MAX + 1);
205 /* Move to shared memory directory */
206 lret = chdir(SHARED_MEMORY_DIRECTORY);
209 /* Open shared memory directory */
210 p_dir = opendir(SHARED_MEMORY_DIRECTORY);
213 /* Get directory entry */
214 p_dir_ent = readdir(p_dir);
215 if (p_dir_ent == NULL) {
219 if ((_pb_strcmp(p_dir_ent->d_name, ".") != 0) &&
220 (_pb_strcmp(p_dir_ent->d_name, "..") != 0)) {
221 /* Delete shared memory objects */
222 shm_unlink(p_dir_ent->d_name);
226 /* Close shared memory directory */
230 /* Go to original directory */
232 if (lret != EOK) /* Coverity CID: 18816 compliant */ {
233 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "chdir ERROR [lret:%d]", lret);
238 /* Delete shared memory directory */
239 lret = rmdir(SHARED_MEMORY_DIRECTORY);
245 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
246 * MODULE : GetSharedMemoryPtr()
247 * ABSTRACT : Shared memory start address acquisition processing
248 * NOTE : Return the start address of the shared memory that is allocated/mapped at the
249 * : time of shared memory creation or open
250 * ARGUMENT : HANDLE h_shm Shared memory handle(Management information pointer)
251 * RETURN : void* Except NULL Pointer to the acquired shared memory
253 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
255 GetSharedMemoryPtr(HANDLE h_shm) {
257 SHARED_MEMORY *p_shm = reinterpret_cast<SHARED_MEMORY *>(h_shm);
259 if (p_shm == NULL) /* If the shared memory handle that is not created or not opened is specified, */ { // LCOV_EXCL_BR_LINE 200: p_shm can not be NULL // NOLINT(whitespace/line_length)
261 p_vret = p_shm->p_memory;
267 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
268 * MODULE : CloseSharedMemory()
269 * ABSTRACT : Shared memory close processing
270 * NOTE : Close the shared memory that is allocated/mapped at the time of its creation or open
271 * ARGUMENT : HANDLE h_shm Shared memory handle(Management information pointer)
273 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
275 CloseSharedMemory(HANDLE h_shm) {
276 SHARED_MEMORY* p_shm = reinterpret_cast<SHARED_MEMORY *>(h_shm);
279 if (p_shm == NULL) /* If the shared memory handle that is not created or not opened is specified, */ {
280 } else if (p_shm->phy_addr != 0) /* If the physical memory area address is specified,(Currently never comes here) */ {
281 if (p_shm->p_memory != NULL) {
282 /* Release physical memory area */
284 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
285 "_CWORD64_api.dll:%s:LINE %d\r\n munmap_device_memory ERROR In PbFreePhysicalMem\r\n", \
286 LTEXT(__FILE__), __LINE__);
287 _pb_Exit(); /* System recovery processing(Exception execution) */
291 /* Release the shared memory object mapped to invoking process space */
292 if (p_shm->p_memory != NULL) {
293 lret = munmap(p_shm->p_memory, p_shm->size);
294 p_shm->p_memory = NULL;
297 /* Release the shared memory management information allocated in the heap */
304 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
305 * MODULE : DeleteSharedMemory()
306 * ABSTRACT : Shared memory object delete processing
307 * NOTE : Delete the shared memory object that is specified name
308 * ARGUMENT : TCHAR* name Pointer to the name of the shared memory to be deleted
310 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
312 DeleteSharedMemory(TCHAR* name) { // LCOV_EXCL_START 8:dead code
313 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
316 char sz_cwd[PATH_MAX + 1] = {0};
319 /* Get path of current directory */
320 cwd = getcwd(sz_cwd, PATH_MAX + 1);
323 /* Move to shared memory directory */
324 lret = chdir(SHARED_MEMORY_DIRECTORY);
327 /* Delete shared memory object */
328 lret = shm_unlink(name);
330 /* Go to original directory */
342 OpenSharedMemoryAtPhysical(DWORD physical_address, DWORD size, DWORD protect) { // LCOV_EXCL_START 8:dead code
343 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert