refactor error code

This commit is contained in:
Yihao Deng 2024-07-16 11:00:12 +00:00
parent 640eada5b1
commit 21de23e14b
6 changed files with 276 additions and 174 deletions

View File

@ -202,7 +202,7 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
global.generateGrant = true; global.generateGrant = true;
} else if (strcmp(argv[i], "-y") == 0) { } else if (strcmp(argv[i], "-y") == 0) {
global.generateCode = true; global.generateCode = true;
if(i < argc - 1) { if (i < argc - 1) {
int32_t len = strlen(argv[++i]); int32_t len = strlen(argv[++i]);
if (len < ENCRYPT_KEY_LEN_MIN) { if (len < ENCRYPT_KEY_LEN_MIN) {
printf("ERROR: Encrypt key should be at least %d characters\n", 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) { 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(); taosCleanupArgs();
return code; return code;
} }
@ -367,7 +367,7 @@ int mainWindows(int argc, char **argv) {
printf("memory dbg enabled\n"); printf("memory dbg enabled\n");
} }
#endif #endif
if(global.generateCode) { if (global.generateCode) {
bool toLogFile = false; bool toLogFile = false;
if ((code = taosReadDataFolder(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs)) != 0) { 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", 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; return code;
}; };
TdFilePtr pFile; TdFilePtr pFile;
if ((code = dmCheckRunningWrapper(tsDataDir, &pFile)) != 0) { if ((code = dmCheckRunning(tsDataDir, &pFile)) != 0) {
encryptError("failed to generate encrypt code since taosd is running, please stop it first, reason:%s", tstrerror(code)); encryptError("failed to generate encrypt code since taosd is running, please stop it first, reason:%s",
tstrerror(code));
return code; return code;
} }
int ret = dmUpdateEncryptKey(global.encryptKey, toLogFile); int ret = dmUpdateEncryptKey(global.encryptKey, toLogFile);
@ -437,7 +438,7 @@ int mainWindows(int argc, char **argv) {
osSetProcPath(argc, (char **)argv); osSetProcPath(argc, (char **)argv);
taosCleanupArgs(); taosCleanupArgs();
if(dmGetEncryptKey() != 0){ if (dmGetEncryptKey() != 0) {
dError("failed to start since failed to get encrypt key"); dError("failed to start since failed to get encrypt key");
taosCloseLog(); taosCloseLog();
taosCleanupArgs(); taosCleanupArgs();
@ -461,7 +462,8 @@ int mainWindows(int argc, char **argv) {
dmSetSignalHandle(); dmSetSignalHandle();
tsDndStart = taosGetTimestampMs(); tsDndStart = taosGetTimestampMs();
tsDndStartOsUptime = taosGetOsUptime(); tsDndStartOsUptime = taosGetOsUptime();
int32_t code = dmRun();
code = dmRun();
dInfo("shutting down the service"); dInfo("shutting down the service");
dmCleanup(); dmCleanup();

View File

@ -14,11 +14,13 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "audit.h" // clang-format off
#include "dmMgmt.h" #include "dmMgmt.h"
#include "audit.h"
#include "libs/function/tudf.h" #include "libs/function/tudf.h"
#include "tcompare.h"
#include "tgrant.h" #include "tgrant.h"
#include "tcompare.h"
// clang-format on
#define DM_INIT_AUDIT() \ #define DM_INIT_AUDIT() \
do { \ do { \

View File

@ -72,7 +72,7 @@ int32_t dmInitDnode(SDnode *pDnode) {
pWrapper->required = dmRequireNode(pDnode, pWrapper); pWrapper->required = dmRequireNode(pDnode, pWrapper);
} }
code = dmCheckRunningWrapper(tsDataDir, &pDnode->lockfile); code = dmCheckRunning(tsDataDir, &pDnode->lockfile);
if (code != 0) { if (code != 0) {
goto _OVER; goto _OVER;
} }

View File

@ -23,7 +23,7 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
if (taosMkDir(pWrapper->path) != 0) { if (taosMkDir(pWrapper->path) != 0) {
code = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, tstrerror(code)); dError("node:%s, failed to create dir:%s since %s", pWrapper->name, pWrapper->path, tstrerror(code));
return terrno = code; return code;
} }
SMgmtOutputOpt output = {0}; SMgmtOutputOpt output = {0};
@ -32,8 +32,8 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
dInfo("node:%s, start to open", pWrapper->name); dInfo("node:%s, start to open", pWrapper->name);
tmsgSetDefault(&input.msgCb); tmsgSetDefault(&input.msgCb);
if ((code = (*pWrapper->func.openFp)(&input, &output)) != 0) { if ((code = (*pWrapper->func.openFp)(&input, &output)) != 0) {
dError("node:%s, failed to open since %s", pWrapper->name, terrstr()); dError("node:%s, failed to open since %s", pWrapper->name, tstrerror(code));
return -1; return code;
} }
dInfo("node:%s, has been opened", pWrapper->name); dInfo("node:%s, has been opened", pWrapper->name);
pWrapper->deployed = true; pWrapper->deployed = true;
@ -47,11 +47,12 @@ int32_t dmOpenNode(SMgmtWrapper *pWrapper) {
} }
int32_t dmStartNode(SMgmtWrapper *pWrapper) { int32_t dmStartNode(SMgmtWrapper *pWrapper) {
int32_t code = 0;
if (pWrapper->func.startFp != NULL) { if (pWrapper->func.startFp != NULL) {
dDebug("node:%s, start to start", pWrapper->name); dDebug("node:%s, start to start", pWrapper->name);
if ((*pWrapper->func.startFp)(pWrapper->pMgmt) != 0) { if ((code = (*pWrapper->func.startFp)(pWrapper->pMgmt)) != 0) {
dError("node:%s, failed to start since %s", pWrapper->name, terrstr()); dError("node:%s, failed to start since %s", pWrapper->name, tstrerror(code));
return -1; return code;
} }
dDebug("node:%s, has been started", pWrapper->name); dDebug("node:%s, has been started", pWrapper->name);
} }
@ -87,12 +88,13 @@ void dmCloseNode(SMgmtWrapper *pWrapper) {
} }
static int32_t dmOpenNodes(SDnode *pDnode) { static int32_t dmOpenNodes(SDnode *pDnode) {
int32_t code = 0;
for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
if (!pWrapper->required) continue; if (!pWrapper->required) continue;
if (dmOpenNode(pWrapper) != 0) { if ((code = dmOpenNode(pWrapper)) != 0) {
dError("node:%s, failed to open since %s", pWrapper->name, terrstr()); dError("node:%s, failed to open since %s", pWrapper->name, tstrerror(code));
return -1; return code;
} }
} }
@ -101,12 +103,13 @@ static int32_t dmOpenNodes(SDnode *pDnode) {
} }
static int32_t dmStartNodes(SDnode *pDnode) { static int32_t dmStartNodes(SDnode *pDnode) {
int32_t code = 0;
for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];
if (!pWrapper->required) continue; if (!pWrapper->required) continue;
if (dmStartNode(pWrapper) != 0) { if ((code = dmStartNode(pWrapper)) != 0) {
dError("node:%s, failed to start since %s", pWrapper->name, terrstr()); dError("node:%s, failed to start since %s", pWrapper->name, tstrerror(code));
return -1; return code;
} }
} }
@ -130,19 +133,20 @@ static void dmCloseNodes(SDnode *pDnode) {
} }
int32_t dmRunDnode(SDnode *pDnode) { int32_t dmRunDnode(SDnode *pDnode) {
int32_t code = 0;
int32_t count = 0; int32_t count = 0;
if (dmOpenNodes(pDnode) != 0) { if ((code = dmOpenNodes(pDnode)) != 0) {
dError("failed to open nodes since %s", terrstr()); dError("failed to open nodes since %s", tstrerror(code));
dmCloseNodes(pDnode); dmCloseNodes(pDnode);
return -1; return code;
} }
if (dmStartNodes(pDnode) != 0) { if ((code = dmStartNodes(pDnode)) != 0) {
dError("failed to start nodes since %s", terrstr()); dError("failed to start nodes since %s", tstrerror(code));
dmSetStatus(pDnode, DND_STAT_STOPPED); dmSetStatus(pDnode, DND_STAT_STOPPED);
dmStopNodes(pDnode); dmStopNodes(pDnode);
dmCloseNodes(pDnode); dmCloseNodes(pDnode);
return -1; return code;
} }
while (1) { while (1) {

View File

@ -145,20 +145,20 @@ typedef struct {
} SDnodeData; } SDnodeData;
typedef struct { typedef struct {
const char *path; const char *path;
const char *name; const char *name;
STfs *pTfs; STfs *pTfs;
SDnodeData *pData; SDnodeData *pData;
SMsgCb msgCb; SMsgCb msgCb;
ProcessCreateNodeFp processCreateNodeFp; ProcessCreateNodeFp processCreateNodeFp;
ProcessAlterNodeTypeFp processAlterNodeTypeFp; ProcessAlterNodeTypeFp processAlterNodeTypeFp;
ProcessDropNodeFp processDropNodeFp; ProcessDropNodeFp processDropNodeFp;
SendMonitorReportFp sendMonitorReportFp; SendMonitorReportFp sendMonitorReportFp;
SendAuditRecordsFp sendAuditRecordFp; SendAuditRecordsFp sendAuditRecordFp;
GetVnodeLoadsFp getVnodeLoadsFp; GetVnodeLoadsFp getVnodeLoadsFp;
GetVnodeLoadsFp getVnodeLoadsLiteFp; GetVnodeLoadsFp getVnodeLoadsLiteFp;
GetMnodeLoadsFp getMnodeLoadsFp; GetMnodeLoadsFp getMnodeLoadsFp;
GetQnodeLoadsFp getQnodeLoadsFp; GetQnodeLoadsFp getQnodeLoadsFp;
} SMgmtInputOpt; } SMgmtInputOpt;
typedef struct { typedef struct {
@ -203,10 +203,10 @@ void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, boo
void dmGetMonitorSystemInfo(SMonSysInfo *pInfo); void dmGetMonitorSystemInfo(SMonSysInfo *pInfo);
// dmFile.c // dmFile.c
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed); int32_t dmReadFile(const char *path, const char *name, bool *pDeployed);
int32_t dmWriteFile(const char *path, const char *name, bool deployed); int32_t dmWriteFile(const char *path, const char *name, bool deployed);
TdFilePtr dmCheckRunning(const char *dataDir); int32_t dmCheckRunning(const char *dataDir, TdFilePtr *pFile);
int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile); //int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile);
// dmodule.c // dmodule.c
int32_t dmInitDndInfo(SDnodeData *pData); int32_t dmInitDndInfo(SDnodeData *pData);

View File

@ -14,23 +14,23 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "dmUtil.h"
#include "tjson.h"
#include "tgrant.h"
#include "crypt.h" #include "crypt.h"
#include "dmUtil.h"
#include "tchecksum.h" #include "tchecksum.h"
#include "tgrant.h"
#include "tjson.h"
#define MAXLEN 1024 #define MAXLEN 1024
#define DM_KEY_INDICATOR "this indicator!" #define DM_KEY_INDICATOR "this indicator!"
#define DM_ENCRYPT_CODE_FILE "encryptCode.cfg" #define DM_ENCRYPT_CODE_FILE "encryptCode.cfg"
#define DM_CHECK_CODE_FILE "checkCode.bin" #define DM_CHECK_CODE_FILE "checkCode.bin"
static int32_t dmDecodeFile(SJson *pJson, bool *deployed) { static int32_t dmDecodeFile(SJson *pJson, bool *deployed) {
int32_t code = 0; int32_t code = 0;
int32_t value = 0; int32_t value = 0;
tjsonGetInt32ValueFromDouble(pJson, "deployed", value, code); tjsonGetInt32ValueFromDouble(pJson, "deployed", value, code);
if (code < 0) return -1; if (code < 0) return code;
*deployed = (value != 0); *deployed = (value != 0);
return code; return code;
@ -42,7 +42,11 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
char *content = NULL; char *content = NULL;
SJson *pJson = NULL; SJson *pJson = NULL;
char file[PATH_MAX] = {0}; 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) { if (taosStatFile(file, NULL, NULL, NULL) < 0) {
dInfo("file:%s not exist", file); 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); pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) { if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to open file:%s since %s", file, terrstr()); dError("failed to open file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
int64_t size = 0; int64_t size = 0;
if (taosFStatFile(pFile, &size, NULL) < 0) { if (taosFStatFile(pFile, &size, NULL) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to fstat file:%s since %s", file, terrstr()); dError("failed to fstat file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
content = taosMemoryMalloc(size + 1); content = taosMemoryMalloc(size + 1);
if (content == NULL) { if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER; goto _OVER;
} }
if (taosReadFile(pFile, content, size) != size) { if (taosReadFile(pFile, content, size) != size) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to read file:%s since %s", file, terrstr()); dError("failed to read file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
@ -80,12 +84,12 @@ int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
pJson = tjsonParse(content); pJson = tjsonParse(content);
if (pJson == NULL) { if (pJson == NULL) {
terrno = TSDB_CODE_INVALID_JSON_FORMAT; code = TSDB_CODE_INVALID_JSON_FORMAT;
goto _OVER; goto _OVER;
} }
if (dmDecodeFile(pJson, pDeployed) < 0) { if (dmDecodeFile(pJson, pDeployed) < 0) {
terrno = TSDB_CODE_INVALID_JSON_FORMAT; code = TSDB_CODE_INVALID_JSON_FORMAT;
goto _OVER; goto _OVER;
} }
@ -98,13 +102,13 @@ _OVER:
if (pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) { 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; return code;
} }
static int32_t dmEncodeFile(SJson *pJson, bool deployed) { 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; return 0;
} }
@ -115,26 +119,57 @@ int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
TdFilePtr pFile = NULL; TdFilePtr pFile = NULL;
char file[PATH_MAX] = {0}; char file[PATH_MAX] = {0};
char realfile[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(); pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER; if (pJson == NULL) {
if (dmEncodeFile(pJson, deployed) != 0) goto _OVER; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER;
}
if ((code = dmEncodeFile(pJson, deployed)) != 0) goto _OVER;
buffer = tjsonToString(pJson); buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER; if (buffer == NULL) {
terrno = 0; code = TSDB_CODE_INVALID_JSON_FORMAT;
goto _OVER;
}
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH); 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); int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER; if (taosWriteFile(pFile, buffer, len) <= 0) {
if (taosFsyncFile(pFile) < 0) goto _OVER; code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
if (taosFsyncFile(pFile) < 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
taosCloseFile(&pFile); if (taosCloseFile(&pFile) != 0) {
if (taosRenameFile(file, realfile) != 0) goto _OVER; code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
if (taosRenameFile(file, realfile) != 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
code = 0; code = 0;
dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed); dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed);
@ -145,60 +180,58 @@ _OVER:
if (pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) { if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to write file:%s since %s, deloyed:%d", realfile, tstrerror(code), deployed);
dError("failed to write file:%s since %s, deloyed:%d", realfile, terrstr(), deployed);
} }
return code; return code;
} }
int32_t dmCheckRunningWrapper(const char *dataDir, TdFilePtr *pFile) { int32_t dmCheckRunning(const char *dataDir, TdFilePtr *pFile) {
*pFile = dmCheckRunning(dataDir); int32_t code = 0;
if (*pFile == NULL) return terrno; char filepath[PATH_MAX] = {0};
return 0;
}
TdFilePtr dmCheckRunning(const char *dataDir) {
char filepath[PATH_MAX] = {0};
snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP); 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); *pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_CLOEXEC);
if (pFile == NULL) { if (*pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to open file:%s since %s", filepath, terrstr()); dError("failed to open file:%s since %s", filepath, tstrerror(code));
return NULL; return code;
} }
int32_t retryTimes = 0; int32_t retryTimes = 0;
int32_t ret = 0; int32_t ret = 0;
do { do {
ret = taosLockFile(pFile); ret = taosLockFile(*pFile);
if (ret == 0) break; if (ret == 0) break;
terrno = TAOS_SYSTEM_ERROR(errno);
code = TAOS_SYSTEM_ERROR(errno);
taosMsleep(1000); taosMsleep(1000);
retryTimes++; 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); } while (retryTimes < 12);
if (ret < 0) { if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
taosCloseFile(&pFile); taosCloseFile(pFile);
return NULL; *pFile = NULL;
return code;
} }
terrno = 0;
dDebug("lock file:%s to prevent repeated starts", filepath); 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); 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; TdFilePtr pFile = NULL;
char *result = NULL; char *result = NULL;
int32_t code = -1; int32_t code = -1;
int32_t len = ENCRYPTED_LEN(sizeof(DM_KEY_INDICATOR)); int32_t len = ENCRYPTED_LEN(sizeof(DM_KEY_INDICATOR));
result = taosMemoryMalloc(len); result = taosMemoryMalloc(len);
if (result == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
SCryptOpts opts; SCryptOpts opts;
strncpy(opts.key, key, ENCRYPT_KEY_LEN); 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); CBC_Encrypt(&opts);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH); 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 (taosWriteFile(pFile, opts.result, len) <= 0) {
if (taosFsyncFile(pFile) < 0) goto _OVER; code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
taosCloseFile(&pFile); if (taosFsyncFile(pFile) < 0) {
if (taosRenameFile(file, realfile) != 0) goto _OVER; 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); encryptDebug("succeed to write checkCode file:%s", realfile);
code = 0; code = 0;
_OVER: _OVER:
if(pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if(result != NULL) taosMemoryFree(result); if (result != NULL) taosMemoryFree(result);
return code; 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; TdFilePtr pFile = NULL;
int32_t code = -1; int32_t code = -1;
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH); 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); int32_t len = strlen(encryptCode);
if (taosWriteFile(pFile, encryptCode, len) <= 0) goto _OVER; if (taosWriteFile(pFile, encryptCode, len) <= 0) {
if (taosFsyncFile(pFile) < 0) goto _OVER; code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
if (taosFsyncFile(pFile) < 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
taosCloseFile(&pFile); if (taosCloseFile(&pFile) != 0) {
if (taosRenameFile(file, realfile) != 0) goto _OVER; 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); encryptDebug("succeed to write encryptCode file:%s", realfile);
code = 0; code = 0;
_OVER: _OVER:
if(pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
return code; 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; char *content = NULL;
int64_t size = 0; int64_t size = 0;
TdFilePtr pFile = NULL; TdFilePtr pFile = NULL;
@ -259,31 +325,31 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
pFile = taosOpenFile(file, TD_FILE_READ); pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) { if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
encryptError("failed to open dnode file:%s since %s", file, terrstr()); encryptError("failed to open dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
if (taosFStatFile(pFile, &size, NULL) < 0) { if (taosFStatFile(pFile, &size, NULL) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
encryptError("failed to fstat dnode file:%s since %s", file, terrstr()); encryptError("failed to fstat dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
content = taosMemoryMalloc(size); content = taosMemoryMalloc(size);
if (content == NULL) { if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER; goto _OVER;
} }
if (taosReadFile(pFile, content, size) != size) { if (taosReadFile(pFile, content, size) != size) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
encryptError("failed to read dnode file:%s since %s", file, terrstr()); encryptError("failed to read dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
encryptDebug("succeed to read checkCode file:%s", file); encryptDebug("succeed to read checkCode file:%s", file);
int len = ENCRYPTED_LEN(size); int len = ENCRYPTED_LEN(size);
result = taosMemoryMalloc(len); result = taosMemoryMalloc(len);
@ -295,8 +361,8 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
opts.unitLen = 16; opts.unitLen = 16;
CBC_Decrypt(&opts); CBC_Decrypt(&opts);
if(strcmp(opts.result, DM_KEY_INDICATOR) != 0) { if (strcmp(opts.result, DM_KEY_INDICATOR) != 0) {
terrno = TSDB_CODE_DNODE_ENCRYPTKEY_CHANGED; code = TSDB_CODE_DNODE_ENCRYPTKEY_CHANGED;
encryptError("failed to compare decrypted result"); encryptError("failed to compare decrypted result");
goto _OVER; goto _OVER;
} }
@ -304,9 +370,9 @@ static int32_t dmCompareEncryptKey(char* file, char* key, bool toLogFile){
encryptDebug("succeed to compare checkCode file:%s", file); encryptDebug("succeed to compare checkCode file:%s", file);
code = 0; code = 0;
_OVER: _OVER:
if(result != NULL) taosMemoryFree(result); if (result != NULL) taosMemoryFree(result);
if(content != NULL) taosMemoryFree(content); if (content != NULL) taosMemoryFree(content);
if(pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
return code; return code;
} }
@ -325,40 +391,57 @@ int32_t dmUpdateEncryptKey(char *key, bool toLogFile) {
char checkFile[PATH_MAX] = {0}; char checkFile[PATH_MAX] = {0};
char realCheckFile[PATH_MAX] = {0}; char realCheckFile[PATH_MAX] = {0};
snprintf(folder, sizeof(folder), "%s%sdnode", tsDataDir, TD_DIRSEP); int32_t nBytes = snprintf(folder, sizeof(folder), "%s%sdnode", tsDataDir, TD_DIRSEP);
snprintf(encryptFile, sizeof(realEncryptFile), "%s%s%s.bak", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE); if (nBytes <= 0 || nBytes >= PATH_MAX) {
snprintf(realEncryptFile, sizeof(realEncryptFile), "%s%s%s", folder, TD_DIRSEP, DM_ENCRYPT_CODE_FILE); return TSDB_CODE_OUT_OF_BUFFER;
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);
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) { if (taosMkDir(folder) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
encryptError("failed to create dir:%s since %s", folder, terrstr()); encryptError("failed to create dir:%s since %s", folder, tstrerror(code));
goto _OVER; goto _OVER;
} }
if(taosCheckExistFile(realCheckFile)){ if (taosCheckExistFile(realCheckFile)) {
if(dmCompareEncryptKey(realCheckFile, key, toLogFile) != 0){ if ((code = dmCompareEncryptKey(realCheckFile, key, toLogFile)) != 0) {
goto _OVER; goto _OVER;
} }
} }
if (!(machineId = tGetMachineId())) { if (!(machineId = tGetMachineId())) {
terrno = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER; goto _OVER;
} }
if (generateEncryptCode(key, machineId, &encryptCode) != 0) { if ((code = generateEncryptCode(key, machineId, &encryptCode)) != 0) {
goto _OVER; goto _OVER;
} }
if(dmWriteEncryptCodeFile(encryptFile, realEncryptFile, encryptCode, toLogFile) != 0){ if ((code = dmWriteEncryptCodeFile(encryptFile, realEncryptFile, encryptCode, toLogFile)) != 0) {
goto _OVER; goto _OVER;
} }
if(dmWriteCheckCodeFile(checkFile, realCheckFile, key, toLogFile) != 0){ if ((code = dmWriteCheckCodeFile(checkFile, realCheckFile, key, toLogFile)) != 0) {
goto _OVER; goto _OVER;
} }
@ -369,8 +452,7 @@ _OVER:
taosMemoryFree(encryptCode); taosMemoryFree(encryptCode);
taosMemoryFree(machineId); taosMemoryFree(machineId);
if (code != 0) { if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno); encryptError("failed to update encrypt key since %s", tstrerror(code));
encryptError("failed to update encrypt key since %s", terrstr());
} }
return code; return code;
#else #else
@ -380,89 +462,102 @@ _OVER:
extern int32_t checkAndGetCryptKey(const char *encryptCode, const char *machineId, char **key); 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; TdFilePtr pFile = NULL;
int32_t code = -1; int32_t code = -1;
char *content = NULL; char *content = NULL;
pFile = taosOpenFile(file, TD_FILE_READ); pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) { if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to open dnode file:%s since %s", file, terrstr()); dError("failed to open dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
int64_t size = 0; int64_t size = 0;
if (taosFStatFile(pFile, &size, NULL) < 0) { if (taosFStatFile(pFile, &size, NULL) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to fstat dnode file:%s since %s", file, terrstr()); dError("failed to fstat dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
content = taosMemoryMalloc(size + 1); content = taosMemoryMalloc(size + 1);
if (content == NULL) { if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER; goto _OVER;
} }
if (taosReadFile(pFile, content, size) != size) { if (taosReadFile(pFile, content, size) != size) {
terrno = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
dError("failed to read dnode file:%s since %s", file, terrstr()); dError("failed to read dnode file:%s since %s", file, tstrerror(code));
goto _OVER; goto _OVER;
} }
content[size] = '\0'; content[size] = '\0';
*output = content; *output = content;
content = NULL;
dInfo("succeed to read encryptCode file:%s", file); dInfo("succeed to read encryptCode file:%s", file);
code = 0; code = 0;
_OVER: _OVER:
if(pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
taosMemoryFree(content);
return code; return code;
} }
int32_t dmGetEncryptKey(){ int32_t dmGetEncryptKey() {
#ifdef TD_ENTERPRISE #ifdef TD_ENTERPRISE
int32_t code = -1; int32_t code = -1;
char encryptFile[PATH_MAX] = {0}; char encryptFile[PATH_MAX] = {0};
char checkFile[PATH_MAX] = {0}; char checkFile[PATH_MAX] = {0};
char *machineId = NULL; char *machineId = NULL;
char *encryptKey = NULL; char *encryptKey = NULL;
char *content = NULL; char *content = NULL;
snprintf(encryptFile, sizeof(encryptFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP, DM_ENCRYPT_CODE_FILE); int32_t nBytes = snprintf(encryptFile, sizeof(encryptFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP,
snprintf(checkFile, sizeof(checkFile), "%s%sdnode%s%s", tsDataDir, TD_DIRSEP, TD_DIRSEP, DM_CHECK_CODE_FILE); 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); dInfo("no exist, checkCode file:%s", encryptFile);
return 0; return 0;
} }
if(dmReadEncryptCodeFile(encryptFile, &content) != 0){ if ((code = dmReadEncryptCodeFile(encryptFile, &content)) != 0) {
goto _OVER; goto _OVER;
} }
if (!(machineId = tGetMachineId())) { if (!(machineId = tGetMachineId())) {
terrno = TSDB_CODE_OUT_OF_MEMORY; code = TSDB_CODE_OUT_OF_MEMORY;
goto _OVER; goto _OVER;
} }
if(checkAndGetCryptKey(content, machineId, &encryptKey) != 0){ if ((code = checkAndGetCryptKey(content, machineId, &encryptKey)) != 0) {
goto _OVER; goto _OVER;
} }
taosMemoryFreeClear(machineId); taosMemoryFreeClear(machineId);
taosMemoryFreeClear(content); taosMemoryFreeClear(content);
if(encryptKey[0] == '\0'){ if (encryptKey[0] == '\0') {
terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY; code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
dError("failed to read key since %s", terrstr()); dError("failed to read key since %s", tstrerror(code));
goto _OVER; goto _OVER;
} }
if(dmCompareEncryptKey(checkFile, encryptKey, true) != 0){ if ((code = dmCompareEncryptKey(checkFile, encryptKey, true)) != 0) {
goto _OVER; goto _OVER;
} }
@ -477,8 +572,7 @@ _OVER:
if (encryptKey != NULL) taosMemoryFree(encryptKey); if (encryptKey != NULL) taosMemoryFree(encryptKey);
if (machineId != NULL) taosMemoryFree(machineId); if (machineId != NULL) taosMemoryFree(machineId);
if (code != 0) { if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to get encrypt key since %s", tstrerror(code));
dError("failed to get encrypt key since %s", terrstr());
} }
return code; return code;
#else #else