!703 fix: 解决dmesg -s参数double lock问题

Merge pull request !703 from Kiita/1109_dmesg
This commit is contained in:
openharmony_ci 2021-12-08 10:56:48 +00:00 committed by Gitee
commit 2fa85a2c72
1 changed files with 35 additions and 14 deletions

View File

@ -177,13 +177,17 @@ STATIC INT32 OsCopyToNew(const VOID *addr, UINT32 size)
CHAR *newBuf = (CHAR *)addr + sizeof(DmesgInfo); CHAR *newBuf = (CHAR *)addr + sizeof(DmesgInfo);
UINT32 bufSize = size - sizeof(DmesgInfo); UINT32 bufSize = size - sizeof(DmesgInfo);
INT32 ret; INT32 ret;
UINT32 intSave;
LOS_SpinLockSave(&g_dmesgSpin, &intSave);
if (g_dmesgInfo->logSize == 0) { if (g_dmesgInfo->logSize == 0) {
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
return 0; return 0;
} }
temp = (CHAR *)malloc(g_dmesgInfo->logSize); temp = (CHAR *)malloc(g_dmesgInfo->logSize);
if (temp == NULL) { if (temp == NULL) {
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
return -1; return -1;
} }
@ -195,21 +199,24 @@ STATIC INT32 OsCopyToNew(const VOID *addr, UINT32 size)
ret = OsDmesgRead(temp, g_dmesgInfo->logSize); ret = OsDmesgRead(temp, g_dmesgInfo->logSize);
if (ret <= 0) { if (ret <= 0) {
PRINT_ERR("%s,%d failed, err:%d!\n", __FUNCTION__, __LINE__, ret); goto FREE_OUT;
free(temp);
return -1;
} }
/* if new buf size smaller than logSize */ /* if new buf size smaller than logSize */
ret = memcpy_s(newBuf, bufSize, temp + copyStart, copyLen); ret = memcpy_s(newBuf, bufSize, temp + copyStart, copyLen);
if (ret != EOK) { if (ret != EOK) {
PRINT_ERR("%s,%d memcpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret); goto FREE_OUT;
free(temp);
return -1;
} }
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
free(temp); free(temp);
return (INT32)copyLen; return (INT32)copyLen;
FREE_OUT:
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
PRINT_ERR("%s,%d failed, err:%d!\n", __FUNCTION__, __LINE__, ret);
free(temp);
return -1;
} }
STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size) STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size)
@ -224,12 +231,13 @@ STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size)
LOS_SpinLockSave(&g_dmesgSpin, &intSave); LOS_SpinLockSave(&g_dmesgSpin, &intSave);
temp = g_dmesgInfo; temp = g_dmesgInfo;
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
copyLen = OsCopyToNew(addr, size); copyLen = OsCopyToNew(addr, size);
if (copyLen < 0) { if (copyLen < 0) {
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
return LOS_NOK; return LOS_NOK;
} }
LOS_SpinLockSave(&g_dmesgSpin, &intSave);
g_logBufSize = size - sizeof(DmesgInfo); g_logBufSize = size - sizeof(DmesgInfo);
g_dmesgInfo = (DmesgInfo *)addr; g_dmesgInfo = (DmesgInfo *)addr;
g_dmesgInfo->logBuf = (CHAR *)addr + sizeof(DmesgInfo); g_dmesgInfo->logBuf = (CHAR *)addr + sizeof(DmesgInfo);
@ -239,12 +247,17 @@ STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size)
/* if old mem came from malloc */ /* if old mem came from malloc */
if (temp == g_mallocAddr) { if (temp == g_mallocAddr) {
g_mallocAddr = NULL; goto FREE_OUT;
free(temp);
} }
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
return LOS_OK; return LOS_OK;
FREE_OUT:
g_mallocAddr = NULL;
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
free(temp);
return LOS_OK;
} }
STATIC UINT32 OsDmesgChangeSize(UINT32 size) STATIC UINT32 OsDmesgChangeSize(UINT32 size)
@ -265,14 +278,14 @@ STATIC UINT32 OsDmesgChangeSize(UINT32 size)
LOS_SpinLockSave(&g_dmesgSpin, &intSave); LOS_SpinLockSave(&g_dmesgSpin, &intSave);
temp = g_dmesgInfo; temp = g_dmesgInfo;
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
copyLen = OsCopyToNew(newString, size + sizeof(DmesgInfo)); copyLen = OsCopyToNew(newString, size + sizeof(DmesgInfo));
if (copyLen < 0) { if (copyLen < 0) {
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); goto ERR_OUT;
free(newString);
return LOS_NOK;
} }
LOS_SpinLockSave(&g_dmesgSpin, &intSave);
g_logBufSize = size; g_logBufSize = size;
g_dmesgInfo = (DmesgInfo *)newString; g_dmesgInfo = (DmesgInfo *)newString;
g_dmesgInfo->logBuf = (CHAR *)newString + sizeof(DmesgInfo); g_dmesgInfo->logBuf = (CHAR *)newString + sizeof(DmesgInfo);
@ -281,13 +294,21 @@ STATIC UINT32 OsDmesgChangeSize(UINT32 size)
g_dmesgInfo->logHead = 0; g_dmesgInfo->logHead = 0;
if (temp == g_mallocAddr) { if (temp == g_mallocAddr) {
g_mallocAddr = NULL; goto FREE_OUT;
free(temp);
} }
g_mallocAddr = newString; g_mallocAddr = newString;
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
return LOS_OK; return LOS_OK;
ERR_OUT:
free(newString);
return LOS_NOK;
FREE_OUT:
g_mallocAddr = newString;
LOS_SpinUnlockRestore(&g_dmesgSpin, intSave);
free(temp);
return LOS_OK;
} }
UINT32 OsCheckConsoleLock(VOID) UINT32 OsCheckConsoleLock(VOID)