dmMain.c 6.8 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 "dmMgmt.h"
S
Shengliang Guan 已提交
18
#include "mnode.h"
H
Hongze Cheng 已提交
19
#include "tconfig.h"
H
refact  
Hongze Cheng 已提交
20

H
Hongze Cheng 已提交
21
// clang-format off
22 23 24
#define DM_APOLLO_URL    "The apollo string to use when configuring the server, such as: -a 'jsonFile:./tests/cfg.json', cfg.json text can be '{\"fqdn\":\"td1\"}'."
#define DM_CFG_DIR       "Configuration directory."
#define DM_DMP_CFG       "Dump configuration."
S
Shengliang Guan 已提交
25
#define DM_SDB_INFO      "Dump sdb info."
26 27 28 29 30
#define DM_ENV_CMD       "The env cmd variable string to use when configuring the server, such as: -e 'TAOS_FQDN=td1'."
#define DM_ENV_FILE      "The env variable file path to use when configuring the server, default is './.env', .env text can be 'TAOS_FQDN=td1'."
#define DM_MACHINE_CODE  "Get machine code."
#define DM_VERSION       "Print program version."
#define DM_EMAIL         "<support@taosdata.com>"
H
Hongze Cheng 已提交
31
// clang-format on
S
Shengliang Guan 已提交
32
static struct {
wafwerar's avatar
wafwerar 已提交
33
#ifdef WINDOWS
H
Hongze Cheng 已提交
34
  bool winServiceMode;
wafwerar's avatar
wafwerar 已提交
35
#endif
S
Shengliang Guan 已提交
36
  bool         dumpConfig;
S
Shengliang Guan 已提交
37
  bool         dumpSdb;
S
Shengliang Guan 已提交
38 39 40
  bool         generateGrant;
  bool         printAuth;
  bool         printVersion;
41
  bool         printHelp;
S
Shengliang Guan 已提交
42 43
  char         envFile[PATH_MAX];
  char         apolloUrl[PATH_MAX];
wafwerar's avatar
wafwerar 已提交
44
  const char **envCmd;
S
Shengliang Guan 已提交
45
  SArray      *pArgs;  // SConfigPair
S
shm  
Shengliang Guan 已提交
46
} global = {0};
S
Shengliang Guan 已提交
47

48
static void dmStopDnode(int signum, void *info, void *ctx) { dmStop(); }
S
Shengliang Guan 已提交
49

S
Shengliang Guan 已提交
50 51 52 53 54 55
static void dmSetSignalHandle() {
  taosSetSignal(SIGTERM, dmStopDnode);
  taosSetSignal(SIGHUP, dmStopDnode);
  taosSetSignal(SIGINT, dmStopDnode);
  taosSetSignal(SIGABRT, dmStopDnode);
  taosSetSignal(SIGBREAK, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
56 57
#ifndef WINDOWS
  taosSetSignal(SIGTSTP, dmStopDnode);
S
Shengliang Guan 已提交
58
  taosSetSignal(SIGQUIT, dmStopDnode);
wafwerar's avatar
wafwerar 已提交
59
#endif
S
Shengliang Guan 已提交
60 61
}

S
Shengliang Guan 已提交
62
static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
wafwerar's avatar
wafwerar 已提交
63
  int32_t cmdEnvIndex = 0;
wafwerar's avatar
wafwerar 已提交
64
  if (argc < 2) return 0;
65 66
  global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *));
  memset(global.envCmd, 0, (argc - 1) * sizeof(char *));
S
Shengliang Guan 已提交
67
  for (int32_t i = 1; i < argc; ++i) {
S
Shengliang Guan 已提交
68 69 70 71 72 73
    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 已提交
74
        tstrncpy(configDir, argv[i], PATH_MAX);
S
Shengliang Guan 已提交
75
      } else {
76
        printf("'-c' requires a parameter, default is %s\n", configDir);
S
Shengliang Guan 已提交
77 78
        return -1;
      }
S
Shengliang Guan 已提交
79 80
    } else if (strcmp(argv[i], "-a") == 0) {
      tstrncpy(global.apolloUrl, argv[++i], PATH_MAX);
S
Shengliang Guan 已提交
81 82
    } else if (strcmp(argv[i], "-s") == 0) {
      global.dumpSdb = true;
wafwerar's avatar
wafwerar 已提交
83
    } else if (strcmp(argv[i], "-E") == 0) {
S
Shengliang Guan 已提交
84
      tstrncpy(global.envFile, argv[++i], PATH_MAX);
S
shm  
Shengliang Guan 已提交
85 86
    } else if (strcmp(argv[i], "-k") == 0) {
      global.generateGrant = true;
S
Shengliang Guan 已提交
87 88
    } else if (strcmp(argv[i], "-C") == 0) {
      global.dumpConfig = true;
S
Shengliang Guan 已提交
89
    } else if (strcmp(argv[i], "-V") == 0) {
S
shm  
Shengliang Guan 已提交
90
      global.printVersion = true;
H
Hongze Cheng 已提交
91
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
92 93
    } else if (strcmp(argv[i], "--win_service") == 0) {
      global.winServiceMode = true;
H
Hongze Cheng 已提交
94
#endif
wafwerar's avatar
wafwerar 已提交
95 96 97
    } else if (strcmp(argv[i], "-e") == 0) {
      global.envCmd[cmdEnvIndex] = argv[++i];
      cmdEnvIndex++;
98 99
    } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ||
               strcmp(argv[i], "-?")) {
100
      global.printHelp = true;
S
Shengliang Guan 已提交
101 102
    } else {
    }
S
Shengliang Guan 已提交
103 104
  }

S
Shengliang Guan 已提交
105 106 107
  return 0;
}

S
Shengliang Guan 已提交
108
static void dmGenerateGrant() { mndGenerateMachineCode(); }
S
Shengliang Guan 已提交
109

S
Shengliang Guan 已提交
110
static void dmPrintVersion() {
S
Shengliang Guan 已提交
111 112 113 114 115 116 117 118 119 120
#ifdef TD_ENTERPRISE
  char *releaseName = "enterprise";
#else
  char *releaseName = "community";
#endif
  printf("%s version: %s compatible_version: %s\n", releaseName, version, compatible_version);
  printf("gitinfo: %s\n", gitinfo);
  printf("buildInfo: %s\n", buildinfo);
}

121 122 123 124 125
static void dmPrintHelp() {
  char indent[] = "  ";
  printf("Usage: taosd [OPTION...] \n\n");
  printf("%s%s%s%s\n", indent, "-a,", indent, DM_APOLLO_URL);
  printf("%s%s%s%s\n", indent, "-c,", indent, DM_CFG_DIR);
S
Shengliang Guan 已提交
126
  printf("%s%s%s%s\n", indent, "-s,", indent, DM_SDB_INFO);
127 128 129 130 131 132 133 134 135
  printf("%s%s%s%s\n", indent, "-C,", indent, DM_DMP_CFG);
  printf("%s%s%s%s\n", indent, "-e,", indent, DM_ENV_CMD);
  printf("%s%s%s%s\n", indent, "-E,", indent, DM_ENV_FILE);
  printf("%s%s%s%s\n", indent, "-k,", indent, DM_MACHINE_CODE);
  printf("%s%s%s%s\n", indent, "-V,", indent, DM_VERSION);

  printf("\n\nReport bugs to %s.\n", DM_EMAIL);
}

S
Shengliang Guan 已提交
136
static void dmDumpCfg() {
S
Shengliang Guan 已提交
137
  SConfig *pCfg = taosGetCfg();
138
  cfgDumpCfg(pCfg, 0, true);
S
Shengliang Guan 已提交
139 140
}

S
Shengliang Guan 已提交
141
static int32_t dmInitLog() {
142
  return taosCreateLog("taosdlog", 1, configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0);
S
shm  
Shengliang Guan 已提交
143 144
}

wafwerar's avatar
wafwerar 已提交
145 146 147 148
static void taosCleanupArgs() {
  if (global.envCmd != NULL) taosMemoryFree(global.envCmd);
}

S
Shengliang Guan 已提交
149
int main(int argc, char const *argv[]) {
D
dapan1121 已提交
150 151
  if (!taosCheckSystemIsLittleEnd()) {
    printf("failed to start since on non-little-end machines\n");
152 153 154
    return -1;
  }

S
Shengliang Guan 已提交
155
  if (dmParseArgs(argc, argv) != 0) {
S
Shengliang Guan 已提交
156
    printf("failed to start since parse args error\n");
wafwerar's avatar
wafwerar 已提交
157
    taosCleanupArgs();
S
Shengliang Guan 已提交
158 159 160
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
161
#ifdef WINDOWS
H
Hongze Cheng 已提交
162
  int mainWindows(int argc, char **argv);
wafwerar's avatar
wafwerar 已提交
163 164 165 166 167 168 169
  if (global.winServiceMode) {
    stratWindowsService(mainWindows);
  } else {
    return mainWindows(argc, argv);
  }
  return 0;
}
H
Hongze Cheng 已提交
170
int mainWindows(int argc, char **argv) {
wafwerar's avatar
wafwerar 已提交
171 172
#endif

S
shm  
Shengliang Guan 已提交
173
  if (global.generateGrant) {
S
Shengliang Guan 已提交
174
    dmGenerateGrant();
wafwerar's avatar
wafwerar 已提交
175
    taosCleanupArgs();
S
Shengliang Guan 已提交
176 177 178
    return 0;
  }

179 180 181 182 183 184
  if (global.printHelp) {
    dmPrintHelp();
    taosCleanupArgs();
    return 0;
  }

S
shm  
Shengliang Guan 已提交
185
  if (global.printVersion) {
S
Shengliang Guan 已提交
186
    dmPrintVersion();
wafwerar's avatar
wafwerar 已提交
187
    taosCleanupArgs();
S
Shengliang Guan 已提交
188
    return 0;
S
Shengliang Guan 已提交
189 190
  }

S
Shengliang Guan 已提交
191
  if (dmInitLog() != 0) {
S
Shengliang Guan 已提交
192
    printf("failed to start since init log error\n");
wafwerar's avatar
wafwerar 已提交
193
    taosCleanupArgs();
S
Shengliang Guan 已提交
194
    return -1;
S
Shengliang Guan 已提交
195 196
  }

wafwerar's avatar
wafwerar 已提交
197
  if (taosInitCfg(configDir, global.envCmd, global.envFile, global.apolloUrl, global.pArgs, 0) != 0) {
S
Shengliang Guan 已提交
198
    dError("failed to start since read config error");
S
Shengliang Guan 已提交
199
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
200
    taosCleanupArgs();
S
Shengliang Guan 已提交
201 202
    return -1;
  }
H
Hongze Cheng 已提交
203

204
  taosConvInit();
S
Shengliang Guan 已提交
205

S
shm  
Shengliang Guan 已提交
206
  if (global.dumpConfig) {
S
Shengliang Guan 已提交
207
    dmDumpCfg();
S
Shengliang Guan 已提交
208
    taosCleanupCfg();
S
shm  
Shengliang Guan 已提交
209
    taosCloseLog();
wafwerar's avatar
wafwerar 已提交
210
    taosCleanupArgs();
211
    taosConvDestroy();
S
Shengliang Guan 已提交
212 213 214
    return 0;
  }

S
Shengliang Guan 已提交
215 216 217 218 219
  if (global.dumpSdb) {
    mndDumpSdb();
    taosCleanupCfg();
    taosCloseLog();
    taosCleanupArgs();
220
    taosConvDestroy();
S
Shengliang Guan 已提交
221 222 223
    return 0;
  }

224
  osSetProcPath(argc, (char **)argv);
wafwerar's avatar
wafwerar 已提交
225
  taosCleanupArgs();
226

227
  if (dmInit() != 0) {
228 229 230 231
    dError("failed to init dnode since %s", terrstr());
    return -1;
  }

S
Shengliang Guan 已提交
232
  dInfo("start to init service");
233 234 235 236 237 238
  dmSetSignalHandle();
  int32_t code = dmRun();
  dInfo("shutting down the service");

  dmCleanup();
  return code;
S
Shengliang Guan 已提交
239
}