osProc.c 1.8 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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/>.
 */

#define ALLOW_FORBID_FUNC
#define _DEFAULT_SOURCE
#include "os.h"

S
shm  
Shengliang Guan 已提交
20
int32_t taosNewProc(char **args) {
wafwerar's avatar
wafwerar 已提交
21
#ifdef WINDOWS
22
  assert(0);
wafwerar's avatar
wafwerar 已提交
23 24
  return 0;
#else
S
shm  
Shengliang Guan 已提交
25 26 27
  int32_t pid = fork();
  if (pid == 0) {
    args[0] = tsProcPath;
S
Shengliang Guan 已提交
28
    // close(STDIN_FILENO);
29
    // close(STDOUT_FILENO);
S
Shengliang Guan 已提交
30
    // close(STDERR_FILENO);
S
shm  
Shengliang Guan 已提交
31 32 33 34
    return execvp(tsProcPath, args);
  } else {
    return pid;
  }
wafwerar's avatar
wafwerar 已提交
35
#endif
S
shm  
Shengliang Guan 已提交
36 37
}

S
shm  
Shengliang Guan 已提交
38
void taosWaitProc(int32_t pid) {
wafwerar's avatar
wafwerar 已提交
39
#ifdef WINDOWS
40
  assert(0);
wafwerar's avatar
wafwerar 已提交
41
#else
S
shm  
Shengliang Guan 已提交
42
  int32_t status = -1;
S
shm  
Shengliang Guan 已提交
43
  waitpid(pid, &status, 0);
wafwerar's avatar
wafwerar 已提交
44
#endif
S
shm  
Shengliang Guan 已提交
45 46
}

wafwerar's avatar
wafwerar 已提交
47 48
void taosKillProc(int32_t pid) { 
#ifdef WINDOWS
49
  assert(0);
wafwerar's avatar
wafwerar 已提交
50 51 52 53
#else
  kill(pid, SIGINT); 
#endif
}
S
shm  
Shengliang Guan 已提交
54 55

bool taosProcExist(int32_t pid) {
wafwerar's avatar
wafwerar 已提交
56
#ifdef WINDOWS
57
  assert(0);
wafwerar's avatar
wafwerar 已提交
58 59
  return false;
#else
S
shm  
Shengliang Guan 已提交
60 61
  int32_t p = getpgid(pid);
  return p >= 0;
wafwerar's avatar
wafwerar 已提交
62
#endif
S
shm  
Shengliang Guan 已提交
63 64
}

S
shm  
Shengliang Guan 已提交
65 66
// the length of the new name must be less than the original name to take effect
void taosSetProcName(int32_t argc, char **argv, const char *name) {
wafwerar's avatar
wafwerar 已提交
67
  setThreadName(name);
S
shm  
Shengliang Guan 已提交
68 69 70 71 72 73 74

  for (int32_t i = 0; i < argc; ++i) {
    int32_t len = strlen(argv[i]);
    for (int32_t j = 0; j < len; ++j) {
      argv[i][j] = 0;
    }
    if (i == 0) {
S
shm  
Shengliang Guan 已提交
75
      tstrncpy(argv[0], name, len + 1);
S
shm  
Shengliang Guan 已提交
76 77 78 79 80
    }
  }
}

void taosSetProcPath(int32_t argc, char **argv) { tsProcPath = argv[0]; }