TDengine/src/kit/shell/src/shellMain.c

147 lines
3.4 KiB
C
Raw Normal View History

2019-07-11 08:36:16 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2019-07-26 11:15:31 +00:00
#include "os.h"
2019-07-11 08:36:16 +00:00
#include "shell.h"
#include "tconfig.h"
2020-08-31 09:24:28 +00:00
#include "tnettest.h"
2019-07-11 08:36:16 +00:00
pthread_t pid;
2020-12-17 05:06:20 +00:00
static tsem_t cancelSem;
2019-07-11 08:36:16 +00:00
2021-01-22 11:37:17 +00:00
void shellQueryInterruptHandler(int32_t signum, void *sigInfo, void *context) {
2020-12-17 05:06:20 +00:00
tsem_post(&cancelSem);
}
void *cancelHandler(void *arg) {
while(1) {
if (tsem_wait(&cancelSem) != 0) {
taosMsleep(10);
continue;
}
#ifdef LINUX
2020-12-17 05:06:20 +00:00
int64_t rid = atomic_val_compare_exchange_64(&result, result, 0);
SSqlObj* pSql = taosAcquireRef(tscObjRef, rid);
taos_stop_query(pSql);
taosReleaseRef(tscObjRef, rid);
#else
2020-12-17 05:06:20 +00:00
printf("\nReceive ctrl+c or other signal, quit shell.\n");
exit(0);
#endif
2020-12-17 05:06:20 +00:00
}
return NULL;
2019-07-11 08:36:16 +00:00
}
int checkVersion() {
if (sizeof(int8_t) != 1) {
printf("taos int8 size is %d(!= 1)", (int)sizeof(int8_t));
return 0;
}
if (sizeof(int16_t) != 2) {
printf("taos int16 size is %d(!= 2)", (int)sizeof(int16_t));
return 0;
}
if (sizeof(int32_t) != 4) {
printf("taos int32 size is %d(!= 4)", (int)sizeof(int32_t));
return 0;
}
if (sizeof(int64_t) != 8) {
printf("taos int64 size is %d(!= 8)", (int)sizeof(int64_t));
return 0;
}
return 1;
}
// Global configurations
SShellArguments args = {
2019-12-16 10:59:18 +00:00
.host = NULL,
.password = NULL,
.user = NULL,
.database = NULL,
.timezone = NULL,
.is_raw_time = false,
.is_use_passwd = false,
.dump_config = false,
2019-12-16 10:59:18 +00:00
.file = "\0",
.dir = "\0",
.threadNum = 5,
.commands = NULL,
2020-08-31 09:24:28 +00:00
.pktLen = 1000,
.netTestRole = NULL
2019-12-16 10:59:18 +00:00
};
2019-07-11 08:36:16 +00:00
/*
* Main function.
*/
int main(int argc, char* argv[]) {
/*setlocale(LC_ALL, "en_US.UTF-8"); */
2020-02-23 02:18:04 +00:00
2019-07-11 08:36:16 +00:00
if (!checkVersion()) {
exit(EXIT_FAILURE);
}
shellParseArgument(argc, argv, &args);
if (args.dump_config) {
taosInitGlobalCfg();
taosReadGlobalLogCfg();
if (!taosReadGlobalCfg()) {
printf("TDengine read global config failed");
exit(EXIT_FAILURE);
}
taosDumpGlobalCfg();
exit(0);
}
2020-08-31 09:24:28 +00:00
if (args.netTestRole && args.netTestRole[0] != 0) {
2020-10-29 06:20:08 +00:00
taos_init();
taosNetTest(args.netTestRole, args.host, args.port, args.pktLen);
2020-08-31 09:24:28 +00:00
exit(0);
}
2019-07-11 08:36:16 +00:00
/* Initialize the shell */
2020-06-06 07:47:28 +00:00
TAOS* con = shellInit(&args);
2019-07-11 08:36:16 +00:00
if (con == NULL) {
exit(EXIT_FAILURE);
}
2020-12-17 05:06:20 +00:00
if (tsem_init(&cancelSem, 0, 0) != 0) {
printf("failed to create cancel semphore\n");
exit(EXIT_FAILURE);
}
pthread_t spid;
pthread_create(&spid, NULL, cancelHandler, NULL);
/* Interrupt handler. */
2021-01-16 04:59:40 +00:00
taosSetSignal(SIGTERM, shellQueryInterruptHandler);
taosSetSignal(SIGINT, shellQueryInterruptHandler);
taosSetSignal(SIGHUP, shellQueryInterruptHandler);
taosSetSignal(SIGABRT, shellQueryInterruptHandler);
2019-07-11 08:36:16 +00:00
2019-11-07 10:12:26 +00:00
/* Get grant information */
shellGetGrantInfo(con);
2019-07-11 08:36:16 +00:00
/* Loop to query the input. */
while (1) {
pthread_create(&pid, NULL, shellLoopQuery, con);
pthread_join(pid, NULL);
}
}