dmnMain.c 3.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
S
Shengliang Guan 已提交
15 16

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmnInt.h"
H
refact  
Hongze Cheng 已提交
18

S
Shengliang Guan 已提交
19 20 21 22 23 24
static struct {
  bool stop;
  bool dumpConfig;
  bool generateGrant;
  bool printAuth;
  bool printVersion;
S
Shengliang Guan 已提交
25 26
  char envFile[PATH_MAX];
  char apolloUrl[PATH_MAX];
S
Shengliang Guan 已提交
27
} dmn = {0};
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29
static void dmnSigintHandle(int signum, void *info, void *ctx) {
S
Shengliang Guan 已提交
30
  uInfo("singal:%d is received", signum);
S
Shengliang Guan 已提交
31
  dmn.stop = true;
32
}
S
Shengliang Guan 已提交
33

S
Shengliang Guan 已提交
34
static void dmnSetSignalHandle() {
S
Shengliang Guan 已提交
35 36 37 38 39
  taosSetSignal(SIGTERM, dmnSigintHandle);
  taosSetSignal(SIGHUP, dmnSigintHandle);
  taosSetSignal(SIGINT, dmnSigintHandle);
  taosSetSignal(SIGABRT, dmnSigintHandle);
  taosSetSignal(SIGBREAK, dmnSigintHandle);
S
Shengliang Guan 已提交
40 41
}

S
Shengliang Guan 已提交
42 43 44 45 46 47 48 49 50
static void dmnWaitSignal() {
  dmnSetSignalHandle();
  while (!dmn.stop) {
    taosMsleep(100);
  }
}

static int32_t dmnParseOption(int32_t argc, char const *argv[]) {
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
51 52 53 54 55 56
    if (strcmp(argv[i], "-c") == 0) {
      if (i < argc - 1) {
        if (strlen(argv[++i]) >= PATH_MAX) {
          printf("config file path overflow");
          return -1;
        }
S
Shengliang Guan 已提交
57
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
58
      } else {
59
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
60 61 62
        return -1;
      }
    } else if (strcmp(argv[i], "-C") == 0) {
S
Shengliang Guan 已提交
63
      dmn.dumpConfig = true;
S
Shengliang Guan 已提交
64
    } else if (strcmp(argv[i], "-k") == 0) {
S
Shengliang Guan 已提交
65
      dmn.generateGrant = true;
S
Shengliang Guan 已提交
66
    } else if (strcmp(argv[i], "-V") == 0) {
S
Shengliang Guan 已提交
67
      dmn.printVersion = true;
S
Shengliang Guan 已提交
68 69
    } else {
    }
S
Shengliang Guan 已提交
70 71
  }

S
Shengliang Guan 已提交
72 73 74
  return 0;
}

S
Shengliang Guan 已提交
75
int32_t dmnRunDnode() {
S
config  
Shengliang Guan 已提交
76
  if (dndInit() != 0) {
S
Shengliang Guan 已提交
77
    uInfo("Failed to start TDengine, please check the log");
S
Shengliang Guan 已提交
78 79
    return -1;
  }
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81
  SDnodeObjCfg objCfg = dmnGetObjCfg();
S
config  
Shengliang Guan 已提交
82
  SDnode      *pDnode = dndCreate(&objCfg);
83
  if (pDnode == NULL) {
S
Shengliang Guan 已提交
84
    uInfo("Failed to start TDengine, please check the log");
S
Shengliang Guan 已提交
85 86 87 88 89
    return -1;
  }

  uInfo("Started TDengine service successfully.");
  dmnWaitSignal();
S
Shengliang Guan 已提交
90
  uInfo("TDengine is shut down!");
S
Shengliang Guan 已提交
91

S
Shengliang Guan 已提交
92 93
  dndClose(pDnode);
  dndCleanup();
S
Shengliang Guan 已提交
94
  taosCloseLog();
S
config  
Shengliang Guan 已提交
95
  taosCleanupCfg();
H
refact  
Hongze Cheng 已提交
96 97
  return 0;
}
S
Shengliang Guan 已提交
98 99

int main(int argc, char const *argv[]) {
100
  if (dmnParseOption(argc, argv) != 0) {
S
Shengliang Guan 已提交
101 102 103
    return -1;
  }

S
Shengliang Guan 已提交
104
  if (dmn.generateGrant) {
S
Shengliang Guan 已提交
105 106 107 108 109 110 111
    dmnGenerateGrant();
    return 0;
  }

  if (dmn.printVersion) {
    dmnPrintVersion();
    return 0;
S
Shengliang Guan 已提交
112 113
  }

S
config  
Shengliang Guan 已提交
114
  if (taosCreateLog("taosdlog", 1, configDir, dmn.envFile, dmn.apolloUrl, 0) != 0) {
S
Shengliang Guan 已提交
115
    uInfo("Failed to start TDengine since read config error");
S
Shengliang Guan 已提交
116
    return -1;
S
Shengliang Guan 已提交
117 118
  }

S
config  
Shengliang Guan 已提交
119
  if (taosInitCfg(configDir, dmn.envFile, dmn.apolloUrl, 0) != 0) {
S
Shengliang Guan 已提交
120
    uInfo("Failed to start TDengine since read config error");
S
Shengliang Guan 已提交
121 122 123
    return -1;
  }

S
Shengliang Guan 已提交
124
  if (dmn.dumpConfig) {
S
Shengliang Guan 已提交
125 126
    dmnDumpCfg();
    taosCleanupCfg();
S
Shengliang Guan 已提交
127 128 129
    return 0;
  }

S
config  
Shengliang Guan 已提交
130
  return dmnRunDnode();
S
Shengliang Guan 已提交
131
}