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

taosdata / TDengine / #4906

30 Dec 2025 10:52AM UTC coverage: 65.514% (+0.09%) from 65.423%
#4906

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

4080 existing lines in 123 files now uncovered.

193840 of 295877 relevant lines covered (65.51%)

120444601.14 hits per line

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

57.32
/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) {
126✔
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) {
126✔
122
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
126✔
123
    return terrno;
126✔
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() {
652✔
142
#if defined(WINDOWS) || defined(TD_ASTRA) // TD_ASTRA_TODO
143
  return 0;
144
#else
145
  struct termios newtio;
652✔
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));
652✔
153

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

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

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

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

173
  return 0;
652✔
174
#endif
175
}
176

177
int32_t taosGetOldTerminalMode() {
326✔
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)) {
326✔
183
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
184
    return terrno;
×
185
  }
186

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

193
  return 0;
326✔
194
#endif
195
}
196

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

210
TdCmdPtr taosOpenCmd(const char* cmd) {
853,069✔
211
  if (cmd == NULL) {
853,069✔
212
    terrno = TSDB_CODE_INVALID_PARA;
×
213
    return NULL;
×
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");
853,069✔
222
  if (NULL == p) {
853,069✔
223
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
224
  }
225
  return p;
853,069✔
226
#endif
227
}
228

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

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

246
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
853,069✔
247
  if (pCmd == NULL || ptrBuf == NULL) {
853,069✔
248
    terrno = TSDB_CODE_INVALID_PARA;
×
249
    return terrno;
×
250
  }
251
  if (*ptrBuf != NULL) {
853,069✔
252
    taosMemoryFreeClear(*ptrBuf);
×
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;
853,069✔
296
  len = getline(ptrBuf, (size_t*)&len, (FILE*)pCmd);
853,069✔
297
  if (-1 == len) {
853,069✔
298
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
853,069✔
299
    return terrno;
853,069✔
300
  }
301
  return len;
×
302
#endif
303
}
304

305
int32_t taosEOFCmd(TdCmdPtr pCmd) {
853,069✔
306
  if (pCmd == NULL) {
853,069✔
307
    return 0;
×
308
  }
309
  return feof((FILE*)pCmd);
853,069✔
310
}
311

312
void taosCloseCmd(TdCmdPtr* ppCmd) {
853,069✔
313
  if (ppCmd == NULL || *ppCmd == NULL) {
853,069✔
314
    return;
×
315
  }
316
#ifdef WINDOWS
317
  _pclose((FILE*)(*ppCmd));
318
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
319
#else
320
  (void)pclose((FILE*)(*ppCmd));
853,069✔
321
#endif
322
  *ppCmd = NULL;
853,069✔
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