Refactor allocateSharedMemory logic flow for readability 73/174873/2
authorIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Fri, 16 Mar 2018 09:33:44 +0000 (10:33 +0100)
committerIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Tue, 22 May 2018 13:09:05 +0000 (15:09 +0200)
Change-Id: If1f55c38d883370d7c6c35a3f3d9856b57809561
Signed-off-by: Igor Kotrasinski <i.kotrasinsk@partner.samsung.com>
TEECLib/src/teec_api.c

index 8269f0299f2e91b3f1f135ad285f79f43d6298c0..4d988d1bbe8afb23fc50c4fb0ea6a13d1b88163f 100644 (file)
@@ -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;
 }
 
 /*