/**
- * 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.
{
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;
}
/*