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

taosdata / TDengine / #3529

14 Nov 2024 01:56PM UTC coverage: 60.888% (-0.02%) from 60.905%
#3529

push

travis-ci

web-flow
Merge pull request #28764 from taosdata/docs/TS-4937

doc(arch/last): new section for last/last_row cache

119990 of 252020 branches covered (47.61%)

Branch coverage included in aggregate %.

200800 of 274829 relevant lines covered (73.06%)

15624555.39 hits per line

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

45.9
/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)
79
#else
80
#include <dlfcn.h>
81
#include <termios.h>
82
#include <unistd.h>
83
#endif
84

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

89
typedef struct FILE TdCmd;
90

91
#ifdef BUILD_NO_CALL
92
void* taosLoadDll(const char* filename) {
93
#if defined(WINDOWS)
94
  return NULL;
95
#elif defined(_TD_DARWIN_64)
96
  return NULL;
97
#else
98
  void* handle = dlopen(filename, RTLD_LAZY);
99
  if (!handle) {
100
    // printf("load dll:%s failed, error:%s", filename, dlerror());
101
    return NULL;
102
  }
103

104
  // printf("dll %s loaded", filename);
105

106
  return handle;
107
#endif
108
}
109

110
void taosCloseDll(void* handle) {
111
#if defined(WINDOWS)
112
  return;
113
#elif defined(_TD_DARWIN_64)
114
  return;
115
#else
116
  if (handle) {
117
    dlclose(handle);
118
  }
119
#endif
120
}
121
#endif
122

123
int32_t taosSetConsoleEcho(bool on) {
×
124
#if defined(WINDOWS)
125
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
126
  if (hStdin == INVALID_HANDLE_VALUE) {
127
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
128
    return terrno;
129
  }
130
  DWORD  mode = 0;
131
  if(!GetConsoleMode(hStdin, &mode)){
132
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
133
    return terrno;
134
  }
135
  if (on) {
136
    mode |= ENABLE_ECHO_INPUT;
137
  } else {
138
    mode &= ~ENABLE_ECHO_INPUT;
139
  }
140
  if(!SetConsoleMode(hStdin, mode)) {
141
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
142
    return terrno;
143
  }
144

145
  return 0;
146
#else
147
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
148
  int            err;
149
  struct termios term;
150

151
  if (tcgetattr(STDIN_FILENO, &term) == -1) {
×
152
    terrno = TAOS_SYSTEM_ERROR(errno);
×
153
    return terrno;
×
154
  }
155

156
  if (on)
×
157
    term.c_lflag |= ECHOFLAGS;
×
158
  else
159
    term.c_lflag &= ~ECHOFLAGS;
×
160

161
  err = tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
×
162
  if (err == -1) {
×
163
    terrno = TAOS_SYSTEM_ERROR(errno);
×
164
    return terrno;
×
165
  }
166

167
  return 0;
×
168
#endif
169
}
170

171
int32_t taosSetTerminalMode() {
10✔
172
#if defined(WINDOWS)
173
  return 0;
174
#else
175
  struct termios newtio;
176

177
  /* if (atexit() != 0) { */
178
  /*     fprintf(stderr, "Error register exit function!\n"); */
179
  /*     exit(EXIT_FAILURE); */
180
  /* } */
181

182
  (void)memcpy(&newtio, &oldtio, sizeof(oldtio));
10✔
183

184
  // Set new terminal attributes.
185
  newtio.c_iflag &= ~(IXON | IXOFF | ICRNL | INLCR | IGNCR | IMAXBEL | ISTRIP);
10✔
186
  newtio.c_iflag |= IGNBRK;
10✔
187

188
  // newtio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
189
  newtio.c_oflag |= OPOST;
10✔
190
  newtio.c_oflag |= ONLCR;
10✔
191
  newtio.c_oflag &= ~(OCRNL | ONLRET);
10✔
192

193
  newtio.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHOE | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | ISIG);
10✔
194
  newtio.c_cc[VMIN] = 1;
10✔
195
  newtio.c_cc[VTIME] = 0;
10✔
196

197
  if (-1 == tcsetattr(0, TCSANOW, &newtio)) {
10!
198
    terrno = TAOS_SYSTEM_ERROR(errno);
×
199
    (void)fprintf(stderr, "Fail to set terminal properties!\n");
×
200
    return terrno;
×
201
  }
202

203
  return 0;
10✔
204
#endif
205
}
206

207
int32_t taosGetOldTerminalMode() {
5✔
208
#if defined(WINDOWS)
209
#else
210
  /* Make sure stdin is a terminal. */
211
  if (!isatty(STDIN_FILENO)) {
5!
212
    terrno = TAOS_SYSTEM_ERROR(errno);
×
213
    return terrno;
×
214
  }
215

216
  // Get the parameter of current terminal
217
  if (-1 == tcgetattr(0, &oldtio)) {
5!
218
    terrno = TAOS_SYSTEM_ERROR(errno);
×
219
    return terrno;
×
220
  }
221

222
  return 0;
5✔
223
#endif
224
}
225

226
int32_t taosResetTerminalMode() {
10✔
227
#if defined(WINDOWS)
228
#else
229
  if (-1 == tcsetattr(0, TCSANOW, &oldtio)) {
10✔
230
    terrno = TAOS_SYSTEM_ERROR(errno);
5✔
231
    (void)fprintf(stderr, "Fail to reset the terminal properties!\n");
5✔
232
    return terrno;
5✔
233
  }
234
#endif
235
  return 0;
5✔
236
}
237

238
TdCmdPtr taosOpenCmd(const char* cmd) {
3,892✔
239
  if (cmd == NULL) {
3,892!
240
    terrno = TSDB_CODE_INVALID_PARA;
×
241
    return NULL;
×
242
  }
243
  
244
#ifdef WINDOWS
245
  return (TdCmdPtr)_popen(cmd, "r");
246
#else
247
  TdCmdPtr p = (TdCmdPtr)popen(cmd, "r");
3,892✔
248
  if (NULL == p) {
3,892!
249
    terrno = TAOS_SYSTEM_ERROR(errno);
×
250
  }
251
  return p;
3,892✔
252
#endif
253
}
254

255
int64_t taosGetsCmd(TdCmdPtr pCmd, int32_t maxSize, char* __restrict buf) {
×
256
  if (pCmd == NULL || buf == NULL) {
×
257
    terrno = TSDB_CODE_INVALID_PARA;
×
258
    return terrno;
×
259
  }
260
  if (fgets(buf, maxSize, (FILE*)pCmd) == NULL) {
×
261
    if (feof((FILE*)pCmd)) {
×
262
      return 0;
×
263
    }
264

265
    terrno = TAOS_SYSTEM_ERROR(ferror((FILE*)pCmd));
×
266
    return terrno;
×
267
  }
268
  
269
  return strlen(buf);
×
270
}
271

272
int64_t taosGetLineCmd(TdCmdPtr pCmd, char** __restrict ptrBuf) {
3,892✔
273
  if (pCmd == NULL || ptrBuf == NULL) {
3,892!
274
    terrno = TSDB_CODE_INVALID_PARA;
×
275
    return terrno;
×
276
  }
277
  if (*ptrBuf != NULL) {
3,892!
278
    taosMemoryFreeClear(*ptrBuf);
×
279
  }
280
  
281
#ifdef WINDOWS
282
  *ptrBuf = taosMemoryMalloc(1024);
283
  if (*ptrBuf == NULL) return -1;
284
  if (fgets(*ptrBuf, 1023, (FILE*)pCmd) == NULL) {
285
    taosMemoryFreeClear(*ptrBuf);
286
    return -1;
287
  }
288
  (*ptrBuf)[1023] = 0;
289
  return strlen(*ptrBuf);
290
#else
291
  ssize_t len = 0;
3,892✔
292
  len = getline(ptrBuf, (size_t*)&len, (FILE*)pCmd);
3,892✔
293
  if (-1 == len) {
3,892!
294
    terrno = TAOS_SYSTEM_ERROR(errno);
3,892✔
295
    return terrno;
3,892✔
296
  }
297
  return len;
×
298
#endif
299
}
300

301
int32_t taosEOFCmd(TdCmdPtr pCmd) {
3,892✔
302
  if (pCmd == NULL) {
3,892!
303
    return 0;
×
304
  }
305
  return feof((FILE*)pCmd);
3,892✔
306
}
307

308
void taosCloseCmd(TdCmdPtr* ppCmd) {
3,892✔
309
  if (ppCmd == NULL || *ppCmd == NULL) {
3,892!
310
    return;
×
311
  }
312
#ifdef WINDOWS
313
  _pclose((FILE*)(*ppCmd));
314
#else
315
  (void)pclose((FILE*)(*ppCmd));
3,892✔
316
#endif
317
  *ppCmd = NULL;
3,892✔
318
}
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