refactor error code
This commit is contained in:
parent
640eada5b1
commit
21de23e14b
|
@ -202,7 +202,7 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
|
|||
global.generateGrant = true;
|
||||
} else if (strcmp(argv[i], "-y") == 0) {
|
||||
global.generateCode = true;
|
||||
if(i < argc - 1) {
|
||||
if (i < argc - 1) {
|
||||
int32_t len = strlen(argv[++i]);
|
||||
if (len < ENCRYPT_KEY_LEN_MIN) {
|
||||
printf("ERROR: Encrypt key should be at least %d characters\n", ENCRYPT_KEY_LEN_MIN);
|
||||
|
@ -321,7 +321,7 @@ int main(int argc, char const *argv[]) {
|
|||
}
|
||||
|
||||
if ((code = dmParseArgs(argc, argv)) != 0) {
|
||||
//printf("failed to start since parse args error\n");
|
||||
// printf("failed to start since parse args error\n");
|
||||
taosCleanupArgs();
|
||||
return code;
|
||||
}
|
||||
|
@ -367,7 +367,7 @@ int mainWindows(int argc, char **argv) {
|
|||
printf("memory dbg enabled\n");
|
||||
}
|
||||
#endif
|
||||
if(global.generateCode) {
|
||||
if (global.generateCode) {
|
||||
bool toLogFile = false;
|
||||
if ((code = taosReadDataFolder(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs)) != 0) {
|
||||
encryptError("failed to generate encrypt code since dataDir can not be set from cfg file,reason:%s",
|
||||
|
@ -375,8 +375,9 @@ int mainWindows(int argc, char **argv) {
|
|||
return code;
|
||||
};
|
||||
TdFilePtr pFile;
|
||||
if ((code = dmCheckRunningWrapper(tsDataDir, &pFile)) != 0) {
|
||||
encryptError("failed to generate encrypt code since taosd is running, please stop it first, reason:%s", tstrerror(code));
|
||||
if ((code = dmCheckRunning(tsDataDir, &pFile)) != 0) {
|
||||
encryptError("failed to generate encrypt code since taosd is running, please stop it first, reason:%s",
|
||||
tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
int ret = dmUpdateEncryptKey(global.encryptKey, toLogFile);
|
||||
|
@ -437,7 +438,7 @@ int mainWindows(int argc, char **argv) {
|
|||
osSetProcPath(argc, (char **)argv);
|
||||
taosCleanupArgs();
|
||||
|
||||
if(dmGetEncryptKey() != 0){
|
||||
if (dmGetEncryptKey() != 0) {
|
||||
dError("failed to start since failed to get encrypt key");
|
||||
taosCloseLog();
|
||||
taosCleanupArgs();
|
||||
|
@ -461,7 +462,8 @@ int mainWindows(int argc, char **argv) {
|
|||
dmSetSignalHandle();
|
||||
tsDndStart = taosGetTimestampMs();
|
||||
tsDndStartOsUptime = taosGetOsUptime();
|
||||
int32_t code = dmRun();
|
||||
|
||||
code = dmRun();
|
||||
dInfo("shutting down the service");
|
||||
|
||||
dmCleanup();
|
||||
|
|
|
@ -14,11 +14,13 @@
|
|||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "audit.h"
|
||||
// clang-format off
|
||||
#include "dmMgmt.h"
|
||||
#include "audit.h"
|
||||
#include "libs/function/tudf.h"
|
||||
#include "tcompare.h"
|
||||
#include "tgrant.h"
|
||||
#include "tcompare.h"
|
||||
// clang-format on
|
||||
|
||||
#define DM_INIT_AUDIT() \
|
||||
do { \
|
||||
|
|
|
@ -72,7 +72,7 @@ int32_t dmInitDnode(SDnode *pDnode) {
|
|||
pWrapper->required = dmRequireNode(pDnode, pWrapper);
|
||||
}
|
||||
|
||||
code = dmCheckRunningWrapper(tsDataDir, &pDnode->lockfile);
|
||||
code = dmCheckRunning(tsDataDir, &pDnode->lockfile);
|
||||
if (code != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
|
|||
if (taosMkDir(pWrapper->path) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, tstrerror(code));
|
||||
return terrno = code;
|
||||
return code;
|
||||
}
|
||||
|
||||
SMgmtOutputOpt output = {0};
|
||||
|
@ -32,8 +32,8 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
|
|||
dInfo("node:%s, start to open", pWrapper->name);
|
||||
tmsgSetDefault(&input.msgCb);
|
||||
if ((code = (*pWrapper->func.openFp)(&input, &output)) != 0) {
|
||||
dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
|
||||
return -1;
|
||||
dError("node:%s, failed to open since %s", pWrapper->name, tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
dInfo("node:%s, has been opened", pWrapper->name);
|
||||
pWrapper->deployed = true;
|
||||
|
@ -47,11 +47,12 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
|
|||
}
|
||||
|
||||
int32_t dmStartNode(SMgmtWrapper *pWrapper) {
|
||||
int32_t code = 0;
|
||||
if (pWrapper->func.startFp != NULL) {
|
||||
dDebug("node:%s, start to start", pWrapper->name);
|
||||
if ((*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) {
|
||||
dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
|
||||
return -1;
|
||||
if ((code = (*pWrapper->func.startFp)(pWrapper->pMgmt)) != 0) {
|
||||
dError("node:%s, failed to start since %s", pWrapper->name, tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
dDebug("node:%s, has been started", pWrapper->name);
|
||||
}
|
||||
|
@ -87,12 +88,13 @@ void dmCloseNode(SMgmtWrapper *pWrapper) {
|
|||
}
|
||||
|
||||
static int32_t dmOpenNodes(SDnode *pDnode) {
|
||||
int32_t code = 0;
|
||||
for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
|
||||
SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
|
||||
if (!pWrapper->required) continue;
|
||||
if (dmOpenNode(pWrapper) != 0) {
|
||||
dError("node:%s, failed to open since %s", pWrapper->name, terrstr());
|
||||
return -1;
|
||||
if ((code = dmOpenNode(pWrapper)) != 0) {
|
||||
dError("node:%s, failed to open since %s", pWrapper->name, tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,12 +103,13 @@ static int32_t dmOpenNodes(SDnode *pDnode) {
|
|||
}
|
||||
|
||||
static int32_t dmStartNodes(SDnode *pDnode) {
|
||||
int32_t code = 0;
|
||||
for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
|
||||
SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
|
||||
if (!pWrapper->required) continue;
|
||||
if (dmStartNode(pWrapper) != 0) {
|
||||
dError("node:%s, failed to start since %s", pWrapper->name, terrstr());
|
||||
return -1;
|
||||
if ((code = dmStartNode(pWrapper)) != 0) {
|
||||
dError("node:%s, failed to start since %s", pWrapper->name, tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,19 +133,20 @@ static void dmCloseNodes(SDnode *pDnode) {
|
|||
}
|
||||
|
||||
int32_t dmRunDnode(SDnode *pDnode) {
|
||||
int32_t code = 0;
|
||||
int32_t count = 0;
|
||||
if (dmOpenNodes(pDnode) != 0) {
|
||||
dError("failed to open nodes since %s", terrstr());
|
||||
if ((code = dmOpenNodes(pDnode)) != 0) {
|
||||
dError("failed to open nodes since %s", tstrerror(code));
|
||||
dmCloseNodes(pDnode);
|
||||
return -1;
|
||||
return code;
|
||||
}
|
||||
|
||||
if (dmStartNodes(pDnode) != 0) {
|
||||
dError("failed to start nodes since %s", terrstr());
|
||||
if ((code = dmStartNodes(pDnode)) != 0) {
|
||||
dError("failed to start nodes since %s", tstrerror(code));
|
||||
dmSetStatus(pDnode, DND_STAT_STOPPED);
|
||||
dmStopNodes(pDnode);
|
||||
dmCloseNodes(pDnode);
|
||||
return -1;
|
||||
return code;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
|
|
|
@ -145,20 +145,20 @@ typedef struct {
|
|||
} SDnodeData;
|
||||
|
||||
typedef struct {
|
||||
const char *path;
|
||||
const char *name;
|
||||
STfs *pTfs;
|
||||
SDnodeData *pData;
|
||||
SMsgCb msgCb;
|
||||
ProcessCreateNodeFp processCreateNodeFp;
|
||||
const char *path;
|
||||
const char *name;
|
||||
STfs *pTfs;
|
||||
SDnodeData *pData;
|
||||
SMsgCb msgCb;
|
||||
ProcessCreateNodeFp processCreateNodeFp;
|
||||
ProcessAlterNodeTypeFp processAlterNodeTypeFp;
|
||||
ProcessDropNodeFp processDropNodeFp;
|
||||
SendMonitorReportFp sendMonitorReportFp;
|
||||
SendAuditRecordsFp sendAuditRecordFp;
|
||||
GetVnodeLoadsFp getVnodeLoadsFp;
|
||||
GetVnodeLoadsFp getVnodeLoadsLiteFp;
|
||||
GetMnodeLoadsFp getMnodeLoadsFp;
|
||||
GetQnodeLoadsFp getQnodeLoadsFp;
|
||||
ProcessDropNodeFp processDropNodeFp;
|
||||
SendMonitorReportFp sendMonitorReportFp;
|
||||
SendAuditRecordsFp sendAuditRecordFp;
|
||||
GetVnodeLoadsFp getVnodeLoadsFp;
|
||||
GetVnodeLoadsFp getVnodeLoadsLiteFp;
|
||||
GetMnodeLoadsFp getMnodeLoadsFp;
|
||||
GetQnodeLoadsFp getQnodeLoadsFp;
|
||||
} SMgmtInputOpt;
|
||||
|
||||
typedef struct {
|
||||
|
@ -203,10 +203,10 @@ void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, boo
|
|||
void dmGetMonitorSystemInfo(SMonSysInfo *pInfo);
|
||||
|
||||
// dmFile.c
|
||||
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed);
|
||||
int32_t dmWriteFile(const char *path, const char *name, bool deployed);
|
||||
TdFilePtr dmCheckRunning(const char *dataDir);
|
||||
int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile);
|
||||
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed);
|
||||
int32_t dmWriteFile(const char *path, const char *name, bool deployed);
|
||||
int32_t dmCheckRunning(const char *dataDir, TdFilePtr *pFile);
|
||||
//int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile);
|
||||
|
||||
// dmodule.c
|
||||
int32_t dmInitDndInfo(SDnodeData *pData);
|
||||
|
|
|
@ -14,23 +14,23 @@
|
|||
*/
|
||||
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "dmUtil.h"
|
||||
#include "tjson.h"
|
||||
#include "tgrant.h"
|
||||
#include "crypt.h"
|
||||
#include "dmUtil.h"
|
||||
#include "tchecksum.h"
|
||||
#include "tgrant.h"
|
||||
#include "tjson.h"
|
||||
|
||||
#define MAXLEN 1024
|
||||
#define DM_KEY_INDICATOR "this indicator!"
|
||||
#define DM_ENCRYPT_CODE_FILE "encryptCode.cfg"
|
||||
#define DM_CHECK_CODE_FILE "checkCode.bin"
|
||||
#define MAXLEN 1024
|
||||
#define DM_KEY_INDICATOR "this indicator!"
|
||||
#define DM_ENCRYPT_CODE_FILE "encryptCode.cfg"
|
||||
#define DM_CHECK_CODE_FILE "checkCode.bin"
|
||||
|
||||
static int32_t dmDecodeFile(SJson *pJson, bool *deployed) {
|
||||
int32_t code = 0;
|
||||
int32_t value = 0;
|
||||
|
||||
tjsonGetInt32ValueFromDouble(pJson, "deployed", value, code);
|
||||
if (code < 0) return -1;
|
||||
if (code < 0) return code;
|
||||
|
||||
*deployed = (value != 0);
|
||||
return code;
|
||||
|
@ -42,7 +42,11 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
|
|||
char *content = NULL;
|
||||
SJson *pJson = NULL;
|
||||
char file[PATH_MAX] = {0};
|
||||
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
int32_t nBytes = snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
code = TSDB_CODE_OUT_OF_BUFFER;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosStatFile(file, NULL, NULL, NULL) < 0) {
|
||||
dInfo("file:%s not exist", file);
|
||||
|
@ -52,27 +56,27 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
|
|||
|
||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||
if (pFile == NULL) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
int64_t size = 0;
|
||||
if (taosFStatFile(pFile, &size, NULL) < 0) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to fstat file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to fstat file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
content = taosMemoryMalloc(size + 1);
|
||||
if (content == NULL) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosReadFile(pFile, content, size) != size) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to read file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to read file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
|
@ -80,12 +84,12 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
|
|||
|
||||
pJson = tjsonParse(content);
|
||||
if (pJson == NULL) {
|
||||
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
code = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (dmDecodeFile(pJson, pDeployed) < 0) {
|
||||
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
code = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
|
@ -98,13 +102,13 @@ _OVER:
|
|||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
|
||||
if (code != 0) {
|
||||
dError("failed to read dnode file:%s since %s", file, terrstr());
|
||||
dError("failed to read dnode file:%s since %s", file, tstrerror(code));
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
static int32_t dmEncodeFile(SJson *pJson, bool deployed) {
|
||||
if (tjsonAddDoubleToObject(pJson, "deployed", deployed) < 0) return -1;
|
||||
if (tjsonAddDoubleToObject(pJson, "deployed", deployed) < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -115,26 +119,57 @@ int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
|
|||
TdFilePtr pFile = NULL;
|
||||
char file[PATH_MAX] = {0};
|
||||
char realfile[PATH_MAX] = {0};
|
||||
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
int32_t nBytes = snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
code = TSDB_CODE_OUT_OF_BUFFER;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
nBytes = snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
code = TSDB_CODE_OUT_OF_BUFFER;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
pJson = tjsonCreateObject();
|
||||
if (pJson == NULL) goto _OVER;
|
||||
if (dmEncodeFile(pJson, deployed) != 0) goto _OVER;
|
||||
if (pJson == NULL) {
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if ((code = dmEncodeFile(pJson, deployed)) != 0) goto _OVER;
|
||||
|
||||
buffer = tjsonToString(pJson);
|
||||
if (buffer == NULL) goto _OVER;
|
||||
terrno = 0;
|
||||
if (buffer == NULL) {
|
||||
code = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
|
||||
if (pFile == NULL) goto _OVER;
|
||||
if (pFile == NULL) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
int32_t len = strlen(buffer);
|
||||
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
|
||||
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||
if (taosWriteFile(pFile, buffer, len) <= 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
if (taosFsyncFile(pFile) < 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
taosCloseFile(&pFile);
|
||||
if (taosRenameFile(file, realfile) != 0) goto _OVER;
|
||||
if (taosCloseFile(&pFile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
if (taosRenameFile(file, realfile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
code = 0;
|
||||
dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed);
|
||||
|
@ -145,60 +180,58 @@ _OVER:
|
|||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
|
||||
if (code != 0) {
|
||||
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to write file:%s since %s, deloyed:%d", realfile, terrstr(), deployed);
|
||||
dError("failed to write file:%s since %s, deloyed:%d", realfile, tstrerror(code), deployed);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile) {
|
||||
*pFile = dmCheckRunning(dataDir);
|
||||
if (*pFile == NULL) return terrno;
|
||||
|
||||
return 0;
|
||||
}
|
||||
TdFilePtr dmCheckRunning(const char *dataDir) {
|
||||
char filepath[PATH_MAX] = {0};
|
||||
int32_t dmCheckRunning(const char *dataDir, TdFilePtr *pFile) {
|
||||
int32_t code = 0;
|
||||
char filepath[PATH_MAX] = {0};
|
||||
snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);
|
||||
|
||||
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_CLOEXEC);
|
||||
if (pFile == NULL) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open file:%s since %s", filepath, terrstr());
|
||||
return NULL;
|
||||
*pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_CLOEXEC);
|
||||
if (*pFile == NULL) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open file:%s since %s", filepath, tstrerror(code));
|
||||
return code;
|
||||
}
|
||||
|
||||
int32_t retryTimes = 0;
|
||||
int32_t ret = 0;
|
||||
do {
|
||||
ret = taosLockFile(pFile);
|
||||
ret = taosLockFile(*pFile);
|
||||
if (ret == 0) break;
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
taosMsleep(1000);
|
||||
retryTimes++;
|
||||
dError("failed to lock file:%s since %s, retryTimes:%d", filepath, terrstr(), retryTimes);
|
||||
dError("failed to lock file:%s since %s, retryTimes:%d", filepath, tstrerror(code), retryTimes);
|
||||
} while (retryTimes < 12);
|
||||
|
||||
if (ret < 0) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
taosCloseFile(&pFile);
|
||||
return NULL;
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
taosCloseFile(pFile);
|
||||
*pFile = NULL;
|
||||
return code;
|
||||
}
|
||||
|
||||
terrno = 0;
|
||||
dDebug("lock file:%s to prevent repeated starts", filepath);
|
||||
return pFile;
|
||||
return code;
|
||||
}
|
||||
|
||||
extern int32_t generateEncryptCode(const char *key, const char *machineId, char **encryptCode);
|
||||
|
||||
static int32_t dmWriteCheckCodeFile(char* file, char* realfile, char* key, bool toLogFile){
|
||||
static int32_t dmWriteCheckCodeFile(char *file, char *realfile, char *key, bool toLogFile) {
|
||||
TdFilePtr pFile = NULL;
|
||||
char *result = NULL;
|
||||
int32_t code = -1;
|
||||
|
||||
int32_t len = ENCRYPTED_LEN(sizeof(DM_KEY_INDICATOR));
|
||||
result = taosMemoryMalloc(len);
|
||||
if (result == NULL) {
|
||||
return TSDB_CODE_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
SCryptOpts opts;
|
||||
strncpy(opts.key, key, ENCRYPT_KEY_LEN);
|
||||
|
@ -209,48 +242,81 @@ static int32_t dmWriteCheckCodeFile(char* file, char* realfile, char* key, bool
|
|||
CBC_Encrypt(&opts);
|
||||
|
||||
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
|
||||
if (pFile == NULL) goto _OVER;
|
||||
if (pFile == NULL) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosWriteFile(pFile, opts.result, len) <= 0) goto _OVER;
|
||||
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||
if (taosWriteFile(pFile, opts.result, len) <= 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
taosCloseFile(&pFile);
|
||||
if (taosRenameFile(file, realfile) != 0) goto _OVER;
|
||||
if (taosFsyncFile(pFile) < 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosCloseFile(&pFile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosRenameFile(file, realfile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
encryptDebug("succeed to write checkCode file:%s", realfile);
|
||||
|
||||
code = 0;
|
||||
_OVER:
|
||||
if(pFile != NULL) taosCloseFile(&pFile);
|
||||
if(result != NULL) taosMemoryFree(result);
|
||||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
if (result != NULL) taosMemoryFree(result);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static int32_t dmWriteEncryptCodeFile(char* file, char* realfile, char* encryptCode, bool toLogFile){
|
||||
static int32_t dmWriteEncryptCodeFile(char *file, char *realfile, char *encryptCode, bool toLogFile) {
|
||||
TdFilePtr pFile = NULL;
|
||||
int32_t code = -1;
|
||||
|
||||
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
|
||||
if (pFile == NULL) goto _OVER;
|
||||
if (pFile == NULL) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
int32_t len = strlen(encryptCode);
|
||||
if (taosWriteFile(pFile, encryptCode, len) <= 0) goto _OVER;
|
||||
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||
if (taosWriteFile(pFile, encryptCode, len) <= 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
if (taosFsyncFile(pFile) < 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
taosCloseFile(&pFile);
|
||||
if (taosRenameFile(file, realfile) != 0) goto _OVER;
|
||||
if (taosCloseFile(&pFile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosRenameFile(file, realfile) != 0) {
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
encryptDebug("succeed to write encryptCode file:%s", realfile);
|
||||
|
||||
code = 0;
|
||||
_OVER:
|
||||
if(pFile != NULL) taosCloseFile(&pFile);
|
||||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
|
||||
static int32_t dmCompareEncryptKey(char *file, char *key, bool toLogFile) {
|
||||
char *content = NULL;
|
||||
int64_t size = 0;
|
||||
TdFilePtr pFile = NULL;
|
||||
|
@ -259,26 +325,26 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
|
|||
|
||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||
if (pFile == NULL) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to open dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to open dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosFStatFile(pFile, &size, NULL) < 0) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to fstat dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to fstat dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
content = taosMemoryMalloc(size);
|
||||
if (content == NULL) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosReadFile(pFile, content, size) != size) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to read dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to read dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
|
@ -295,8 +361,8 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
|
|||
opts.unitLen = 16;
|
||||
CBC_Decrypt(&opts);
|
||||
|
||||
if(strcmp(opts.result, DM_KEY_INDICATOR) != 0) {
|
||||
terrno = TSDB_CODE_DNODE_ENCRYPTKEY_CHANGED;
|
||||
if (strcmp(opts.result, DM_KEY_INDICATOR) != 0) {
|
||||
code = TSDB_CODE_DNODE_ENCRYPTKEY_CHANGED;
|
||||
encryptError("failed to compare decrypted result");
|
||||
goto _OVER;
|
||||
}
|
||||
|
@ -304,9 +370,9 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
|
|||
encryptDebug("succeed to compare checkCode file:%s", file);
|
||||
code = 0;
|
||||
_OVER:
|
||||
if(result != NULL) taosMemoryFree(result);
|
||||
if(content != NULL) taosMemoryFree(content);
|
||||
if(pFile != NULL) taosCloseFile(&pFile);
|
||||
if (result != NULL) taosMemoryFree(result);
|
||||
if (content != NULL) taosMemoryFree(content);
|
||||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
@ -325,40 +391,57 @@ int32_t dmUpdateEncryptKey(char *key, bool toLogFile) {
|
|||
char checkFile[PATH_MAX] = {0};
|
||||
char realCheckFile[PATH_MAX] = {0};
|
||||
|
||||
snprintf(folder, sizeof(folder), "%s%sdnode", tsDataDir, TD_DIRSEP);
|
||||
snprintf(encryptFile, sizeof(realEncryptFile), "%s%s%s.bak", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE);
|
||||
snprintf(realEncryptFile, sizeof(realEncryptFile), "%s%s%s", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE);
|
||||
snprintf(checkFile, sizeof(checkFile), "%s%s%s.bak", folder, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
snprintf(realCheckFile, sizeof(realCheckFile), "%s%s%s", folder, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
int32_t nBytes = snprintf(folder, sizeof(folder), "%s%sdnode", tsDataDir, TD_DIRSEP);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
return TSDB_CODE_OUT_OF_BUFFER;
|
||||
}
|
||||
|
||||
terrno = 0;
|
||||
nBytes = snprintf(encryptFile, sizeof(realEncryptFile), "%s%s%s.bak", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
return TSDB_CODE_OUT_OF_BUFFER;
|
||||
}
|
||||
|
||||
nBytes = snprintf(realEncryptFile, sizeof(realEncryptFile), "%s%s%s", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
return TSDB_CODE_OUT_OF_BUFFER;
|
||||
}
|
||||
|
||||
nBytes = snprintf(checkFile, sizeof(checkFile), "%s%s%s.bak", folder, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
return TSDB_CODE_OUT_OF_BUFFER;
|
||||
}
|
||||
|
||||
snprintf(realCheckFile, sizeof(realCheckFile), "%s%s%s", folder, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= PATH_MAX) {
|
||||
return TSDB_CODE_OUT_OF_BUFFER;
|
||||
}
|
||||
|
||||
if (taosMkDir(folder) != 0) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to create dir:%s since %s", folder, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to create dir:%s since %s", folder, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if(taosCheckExistFile(realCheckFile)){
|
||||
if(dmCompareEncryptKey(realCheckFile, key, toLogFile) != 0){
|
||||
if (taosCheckExistFile(realCheckFile)) {
|
||||
if ((code = dmCompareEncryptKey(realCheckFile, key, toLogFile)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(machineId = tGetMachineId())) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (generateEncryptCode(key, machineId, &encryptCode) != 0) {
|
||||
if ((code = generateEncryptCode(key, machineId, &encryptCode)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if(dmWriteEncryptCodeFile(encryptFile, realEncryptFile, encryptCode, toLogFile) != 0){
|
||||
if ((code = dmWriteEncryptCodeFile(encryptFile, realEncryptFile, encryptCode, toLogFile)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if(dmWriteCheckCodeFile(checkFile, realCheckFile, key, toLogFile) != 0){
|
||||
if ((code = dmWriteCheckCodeFile(checkFile, realCheckFile, key, toLogFile)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
|
@ -369,8 +452,7 @@ _OVER:
|
|||
taosMemoryFree(encryptCode);
|
||||
taosMemoryFree(machineId);
|
||||
if (code != 0) {
|
||||
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
encryptError("failed to update encrypt key since %s", terrstr());
|
||||
encryptError("failed to update encrypt key since %s", tstrerror(code));
|
||||
}
|
||||
return code;
|
||||
#else
|
||||
|
@ -380,89 +462,102 @@ _OVER:
|
|||
|
||||
extern int32_t checkAndGetCryptKey(const char *encryptCode, const char *machineId, char **key);
|
||||
|
||||
static int32_t dmReadEncryptCodeFile(char* file, char** output){
|
||||
static int32_t dmReadEncryptCodeFile(char *file, char **output) {
|
||||
TdFilePtr pFile = NULL;
|
||||
int32_t code = -1;
|
||||
char *content = NULL;
|
||||
|
||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||
if (pFile == NULL) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to open dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
int64_t size = 0;
|
||||
if (taosFStatFile(pFile, &size, NULL) < 0) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to fstat dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to fstat dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
content = taosMemoryMalloc(size + 1);
|
||||
if (content == NULL) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (taosReadFile(pFile, content, size) != size) {
|
||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to read dnode file:%s since %s", file, terrstr());
|
||||
code = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to read dnode file:%s since %s", file, tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
content[size] = '\0';
|
||||
|
||||
*output = content;
|
||||
content = NULL;
|
||||
|
||||
dInfo("succeed to read encryptCode file:%s", file);
|
||||
code = 0;
|
||||
_OVER:
|
||||
if(pFile != NULL) taosCloseFile(&pFile);
|
||||
if (pFile != NULL) taosCloseFile(&pFile);
|
||||
taosMemoryFree(content);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
int32_t dmGetEncryptKey(){
|
||||
int32_t dmGetEncryptKey() {
|
||||
#ifdef TD_ENTERPRISE
|
||||
int32_t code = -1;
|
||||
char encryptFile[PATH_MAX] = {0};
|
||||
char checkFile[PATH_MAX] = {0};
|
||||
char *machineId = NULL;
|
||||
char *encryptKey = NULL;
|
||||
char *content = NULL;
|
||||
int32_t code = -1;
|
||||
char encryptFile[PATH_MAX] = {0};
|
||||
char checkFile[PATH_MAX] = {0};
|
||||
char *machineId = NULL;
|
||||
char *encryptKey = NULL;
|
||||
char *content = NULL;
|
||||
|
||||
snprintf(encryptFile, sizeof(encryptFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP, DM_ENCRYPT_CODE_FILE);
|
||||
snprintf(checkFile, sizeof(checkFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
int32_t nBytes = snprintf(encryptFile, sizeof(encryptFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP,
|
||||
DM_ENCRYPT_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= sizeof(encryptFile)) {
|
||||
code = TSDB_CODE_OUT_OF_BUFFER;
|
||||
return code;
|
||||
}
|
||||
|
||||
if(!taosCheckExistFile(encryptFile)){
|
||||
nBytes = snprintf(checkFile, sizeof(checkFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP, DM_CHECK_CODE_FILE);
|
||||
if (nBytes <= 0 || nBytes >= sizeof(checkFile)) {
|
||||
code = TSDB_CODE_OUT_OF_BUFFER;
|
||||
return code;
|
||||
}
|
||||
|
||||
if (!taosCheckExistFile(encryptFile)) {
|
||||
code = TSDB_CODE_DNODE_INVALID_ENCRYPT_CONFIG;
|
||||
dInfo("no exist, checkCode file:%s", encryptFile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(dmReadEncryptCodeFile(encryptFile, &content) != 0){
|
||||
if ((code = dmReadEncryptCodeFile(encryptFile, &content)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if (!(machineId = tGetMachineId())) {
|
||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if(checkAndGetCryptKey(content, machineId, &encryptKey) != 0){
|
||||
if ((code = checkAndGetCryptKey(content, machineId, &encryptKey)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
taosMemoryFreeClear(machineId);
|
||||
taosMemoryFreeClear(content);
|
||||
|
||||
if(encryptKey[0] == '\0'){
|
||||
terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
|
||||
dError("failed to read key since %s", terrstr());
|
||||
if (encryptKey[0] == '\0') {
|
||||
code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
|
||||
dError("failed to read key since %s", tstrerror(code));
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
if(dmCompareEncryptKey(checkFile, encryptKey, true) != 0){
|
||||
if ((code = dmCompareEncryptKey(checkFile, encryptKey, true)) != 0) {
|
||||
goto _OVER;
|
||||
}
|
||||
|
||||
|
@ -477,8 +572,7 @@ _OVER:
|
|||
if (encryptKey != NULL) taosMemoryFree(encryptKey);
|
||||
if (machineId != NULL) taosMemoryFree(machineId);
|
||||
if (code != 0) {
|
||||
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
|
||||
dError("failed to get encrypt key since %s", terrstr());
|
||||
dError("failed to get encrypt key since %s", tstrerror(code));
|
||||
}
|
||||
return code;
|
||||
#else
|
||||
|
|
Loading…
Reference in New Issue