From: Igor Kotrasinski Date: Fri, 16 Mar 2018 09:33:44 +0000 (+0100) Subject: Refactor allocateSharedMemory logic flow for readability X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e9c2b6b4ce852ae2cbec63425102519b3c20f09d;p=platform%2Fcore%2Fsecurity%2Ftef-simulator.git Refactor allocateSharedMemory logic flow for readability Change-Id: If1f55c38d883370d7c6c35a3f3d9856b57809561 Signed-off-by: Igor Kotrasinski --- diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c index 8269f02..4d988d1 100644 --- a/TEECLib/src/teec_api.c +++ b/TEECLib/src/teec_api.c @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015-2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,80 +95,77 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { LOGD(TEEC_LIB, "Entry"); TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)shm->imp; + int32_t tee_result = TEEC_SUCCESS; int32_t memKey = 0; uint32_t size = shm->size; char shm_name[NAME_MAX]; int fd_shm = -1; int res; - do { + while (fd_shm < 0 && memKey < SHM_MAX_ID) { res = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, memKey); - if (res == sizeof(shm_name)) { LOGE(TEEC_LIB, "the shm object name is too long"); - return TEEC_ERROR_GENERIC; + tee_result = TEEC_ERROR_GENERIC; + goto exit; } fd_shm = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, SHM_FILE_MODE); - - if (fd_shm >= 0) { - res = fchmod(fd_shm, SHM_FILE_MODE); - - if (res == -1) { - close(fd_shm); - shm_unlink(shm_name); - LOGE(TEEC_LIB, - "Cannot change permission of the %s shared memory file, error: %s", - shm_name, strerror(errno)); - return TEEC_ERROR_GENERIC; - } - - break; - } - - if (errno != EEXIST) { - LOGE(TEEC_LIB, "Cannot create shared memory object, error: %s", - strerror(errno)); - return TEEC_ERROR_GENERIC; + if (fd_shm < 0 && errno != EEXIST) { + LOGE(TEEC_LIB, "Cannot create shared memory object '%s', error: %s", + shm_name, strerror(errno)); + tee_result = TEEC_ERROR_GENERIC; + goto exit; } + if (fd_shm < 0) + memKey++; + } - memKey++; - } while (memKey < SHM_MAX_ID); - - if (memKey == SHM_MAX_ID) { + if (fd_shm < 0) { LOGE(TEEC_LIB, "Cannot find free shared memory slot"); - return TEEC_ERROR_GENERIC; + tee_result = TEEC_ERROR_GENERIC; + goto exit; } - size = alignSize(size); + res = fchmod(fd_shm, SHM_FILE_MODE); + if (res == -1) { + LOGE(TEEC_LIB, + "Cannot change permission of the %s shared memory file, error: %s", + shm_name, strerror(errno)); + tee_result = TEEC_ERROR_GENERIC; + goto cleanup_shm; + } + size = alignSize(size); if (ftruncate(fd_shm, size) == -1) { - close(fd_shm); - shm_unlink(shm_name); LOGE(TEEC_LIB, "ftruncate failed, error: %s", strerror(errno)); - return TEEC_ERROR_OUT_OF_MEMORY; + tee_result = TEEC_ERROR_OUT_OF_MEMORY; + goto cleanup_shm; } shm->buffer = (void *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, - fd_shm, 0); + fd_shm, 0); if (shm->buffer == MAP_FAILED) { - close(fd_shm); - shm_unlink(shm_name); LOGE(TEEC_LIB, "shmat failed, error: %s", strerror(errno)); - return TEEC_ERROR_OUT_OF_MEMORY; + tee_result = TEEC_ERROR_OUT_OF_MEMORY; + goto cleanup_shm; } close(fd_shm); - memset(shm->buffer, 0x00, shm->size); // Update shared memory imp structure sharedMem_imp->shmKey = memKey; sharedMem_imp->size = size; sharedMem_imp->allocPtr = shm->buffer; + goto exit; - return TEEC_SUCCESS; +cleanup_shm: + close(fd_shm); + shm_unlink(shm_name); +exit: + return tee_result; } /*