• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/source/os/src/osSystem.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define ALLOW_FORBID_FUNC
17
#define _DEFAULT_SOURCE
18
#include "os.h"
19

20
#if defined(WINDOWS)
21
typedef void (*MainWindows)(int argc, char** argv);
22
MainWindows mainWindowsFunc = NULL;
23

24
SERVICE_STATUS        ServiceStatus;
25
SERVICE_STATUS_HANDLE hServiceStatusHandle;
26
void WINAPI           windowsServiceCtrlHandle(DWORD request) {
27
            switch (request) {
28
              case SERVICE_CONTROL_STOP:
29
              case SERVICE_CONTROL_SHUTDOWN:
30
      raise(SIGINT);
31
      ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
32
      if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
33
                  DWORD nError = GetLastError();
34
                  fprintf(stderr, "failed to send stopped status to windows service: %d", nError);
35
      }
36
      break;
37
              default:
38
      return;
39
  }
40
}
41
void WINAPI mainWindowsService(int argc, char** argv) {
42
  int ret = 0;
43
  ServiceStatus.dwServiceType = SERVICE_WIN32;
44
  ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
45
  ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
46
  ServiceStatus.dwWin32ExitCode = 0;
47
  ServiceStatus.dwCheckPoint = 0;
48
  ServiceStatus.dwWaitHint = 0;
49
  ServiceStatus.dwServiceSpecificExitCode = 0;
50
  hServiceStatusHandle = RegisterServiceCtrlHandler("taosd", &windowsServiceCtrlHandle);
51
  if (hServiceStatusHandle == 0) {
52
    DWORD nError = GetLastError();
53
    fprintf(stderr, "failed to register windows service ctrl handler: %d", nError);
54
  }
55

56
  ServiceStatus.dwCurrentState = SERVICE_RUNNING;
57
  if (SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
58
    DWORD nError = GetLastError();
59
    fprintf(stderr, "failed to send running status to windows service: %d", nError);
60
  }
61
  if (mainWindowsFunc != NULL) mainWindowsFunc(argc, argv);
62
  ServiceStatus.dwCurrentState = SERVICE_STOPPED;
63
  if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus)) {
64
    DWORD nError = GetLastError();
65
    fprintf(stderr, "failed to send stopped status to windows service: %d", nError);
66
  }
67
}
68
void stratWindowsService(MainWindows mainWindows) {
69
  mainWindowsFunc = mainWindows;
70
  SERVICE_TABLE_ENTRY ServiceTable[2];
71
  ServiceTable[0].lpServiceName = "taosd";
72
  ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)mainWindowsService;
73
  ServiceTable[1].lpServiceName = NULL;
74
  ServiceTable[1].lpServiceProc = NULL;
75
  StartServiceCtrlDispatcher(ServiceTable);
76
}
77

78
#elif defined(_TD_DARWIN_64) || defined(TD_ASTRA)
79
#else
80
#include <dlfcn.h>
81
#include <termios.h>
82
#include <unistd.h>
83
#endif
84

85
#if !defined(WINDOWS) && !defined(TD_ASTRA)
86
struct termios oldtio;
87
#endif
88

89
typedef struct FILE TdCmd;
90

91
int32_t taosSetConsoleEcho(bool on) {
4✔
92
#if defined(WINDOWS)
93
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
94
  if (hStdin == INVALID_HANDLE_VALUE) {
95
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
96
    return terrno;
97
  }
98
  DWORD  mode = 0;
99
  if(!GetConsoleMode(hStdin, &mode)){
100
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
101
    return terrno;
102
  }
103
  if (on) {
104
    mode |= ENABLE_ECHO_INPUT;
105
  } else {
106
    mode &= ~ENABLE_ECHO_INPUT;
107
  }
108
  if(!SetConsoleMode(hStdin, mode)) {
109
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
110
    return terrno;
111
  }
112

113
  return 0;
114
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
115
  return 0;
116
#else
117
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
118
  int            err;
119
  struct termios term;
120

121
  if (tcgetattr(STDIN_FILENO, &term) == -1) {
4!
122
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
4✔
123
    return terrno;
4✔
124
  }
125

126
  if (on)
×
127
    term.c_lflag |= ECHOFLAGS;
×
128
  else
129
    term.c_lflag &= ~ECHOFLAGS;
×
130

131
  err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
×
132
  if (err == -1) {
×
133
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
134
    return terrno;
×
135
  }
136

137
  return 0;
×
138
#endif
139
}
140

141
int32_t taosSetTerminalMode() {
×
142
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
143
  return 0;
144
#else
145
  struct termios newtio;
146

147
  /* if (atexit() != 0) { */
148
  /*     fprintf(stderr, "Error register exit function!\n"); */
149
  /*     exit(EXIT_FAILURE); */
150
  /* } */
151

152
  (void)memcpy(&newtio, &oldtio, sizeof(oldtio));
×
153

154
  // Set new terminal attributes.
155
  newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
×
156
  newtio.c_iflag |= IGNBRK;
×
157

158
  // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
159
  newtio.c_oflag |= OPOST;
×
160
  newtio.c_oflag |= ONLCR;
×
161
  newtio.c_oflag &= ~(OCRNL | ONLRET);
×
162

163
  newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG);
×
164
  newtio.c_cc[VMIN] = 1;
×
165
  newtio.c_cc[VTIME] = 0;
×
166

167
  if (-1 == tcsetattr(0, TCSANOW, &newtio)) {
×
168
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
169
    (void)fprintf(stderr, "Fail to set terminal properties!\n");
×
170
    return terrno;
×
171
  }
172

173
  return 0;
×
174
#endif
175
}
176

177
int32_t taosGetOldTerminalMode() {
2✔
178
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
179
  return 0;
180
#else
181
  /* Make sure stdin is a terminal. */
182
  if (!isatty(STDIN_FILENO)) {
2!
183
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
2✔
184
    return terrno;
2✔
185
  }
186

187
  // Get the parameter of current terminal
188
  if (-1 == tcgetattr(0, &oldtio)) {
×
189
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
190
    return terrno;
×
191
  }
192

193
  return 0;
×
194
#endif
195
}
196

197
int32_t taosResetTerminalMode() {
×
198
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
199
  return 0;
200
#else
201
  if (-1 == tcsetattr(0, TCSANOW, &oldtio)) {
×
202
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
203
    (void)fprintf(stderr, "Fail to reset the terminal properties!\n");
×
204
    return terrno;
×
205
  }
206
#endif
207
  return 0;
×
208
}
209

210
TdCmdPtr taosOpenCmd(const char* cmd) {
374✔
211
  if (cmd == NULL) {
374✔
212
    terrno = TSDB_CODE_INVALID_PARA;
2✔
213
    return NULL;
2✔
214
  }
215
  
216
#ifdef WINDOWS
217
  return (TdCmdPtr)_popen(cmd, "r");
218
#elif defined(TD_ASTRA)
219
  return NULL;
220
#else
221
  TdCmdPtr p = (TdCmdPtr)popen(cmd, "r");
372✔
222
  if (NULL == p) {
372!
223
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
224
  }
225
  return p;
372✔
226
#endif
227
}
228

229
int64_t taosGetsCmd(TdCmdPtr pCmd, int32_t maxSize, char* __restrict buf) {
8✔
230
  if (pCmd == NULL || buf == NULL) {
8✔
231
    terrno = TSDB_CODE_INVALID_PARA;
4✔
232
    return terrno;
4✔
233
  }
234
  if (fgets(buf, maxSize, (FILE*)pCmd) == NULL) {
4✔
235
    if (feof((FILE*)pCmd)) {
2!
236
      return 0;
2✔
237
    }
238

239
    terrno = TAOS_SYSTEM_ERROR(ferror((FILE*)pCmd));
×
240
    return terrno;
×
241
  }
242
  
243
  return strlen(buf);
2✔
244
}
245

246
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
372✔
247
  if (pCmd == NULL || ptrBuf == NULL) {
372!
248
    terrno = TSDB_CODE_INVALID_PARA;
4✔
249
    return terrno;
4✔
250
  }
251
  if (*ptrBuf != NULL) {
368✔
252
    taosMemoryFreeClear(*ptrBuf);
2!
253
  }
254
  
255
#ifdef WINDOWS
256
  *ptrBuf = taosMemoryMalloc(1024);
257
  if (*ptrBuf == NULL) return -1;
258
  if (fgets(*ptrBuf, 1023, (FILE*)pCmd) == NULL) {
259
    taosMemoryFreeClear(*ptrBuf);
260
    return -1;
261
  }
262
  (*ptrBuf)[1023] = 0;
263
  return strlen(*ptrBuf);
264
#elif defined(TD_ASTRA)
265
  size_t bufsize = 128;
266
  size_t pos = 0;
267
  int    c;
268
  if (*ptrBuf == NULL) {
269
    *ptrBuf = (char*)taosMemoryMalloc(bufsize);
270
    if (*ptrBuf == NULL) {
271
      return terrno;
272
    }
273
  }
274
  while ((c = fgetc((FILE*)pCmd)) != EOF) {
275
    if (pos + 1 >= bufsize) {
276
      size_t new_size = bufsize << 1;
277
      char*  new_line = (char*)taosMemoryRealloc(*ptrBuf, new_size);
278
      if (new_line == NULL) {
279
        return terrno;
280
      }
281
      *ptrBuf = new_line;
282
      bufsize = new_size;
283
    }
284
    (*ptrBuf)[pos++] = (char)c;
285
    if (c == '\n') {
286
      break;
287
    }
288
  }
289
  if (pos == 0 && c == EOF) {
290
    return TSDB_CODE_INVALID_PARA;
291
  }
292
  (*ptrBuf)[pos] = '\0';
293
  return (ssize_t)pos;
294
#else
295
  ssize_t len = 0;
368✔
296
  len = getline(ptrBuf, (size_t*)&len, (FILE*)pCmd);
368✔
297
  if (-1 == len) {
368✔
298
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
366✔
299
    return terrno;
366✔
300
  }
301
  return len;
2✔
302
#endif
303
}
304

305
int32_t taosEOFCmd(TdCmdPtr pCmd) {
366✔
306
  if (pCmd == NULL) {
366✔
307
    return 0;
2✔
308
  }
309
  return feof((FILE*)pCmd);
364✔
310
}
311

312
void taosCloseCmd(TdCmdPtr* ppCmd) {
376✔
313
  if (ppCmd == NULL || *ppCmd == NULL) {
376✔
314
    return;
4✔
315
  }
316
#ifdef WINDOWS
317
  _pclose((FILE*)(*ppCmd));
318
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
319
#else
320
  (void)pclose((FILE*)(*ppCmd));
372✔
321
#endif
322
  *ppCmd = NULL;
372✔
323
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc