osProc.c 1.7 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 22 23
#ifdef WINDOWS
  return 0;
#else
S
shm  
Shengliang Guan 已提交
24 25 26
  int32_t pid = fork();
  if (pid == 0) {
    args[0] = tsProcPath;
S
Shengliang Guan 已提交
27
    // close(STDIN_FILENO);
28
    // close(STDOUT_FILENO);
S
Shengliang Guan 已提交
29
    // close(STDERR_FILENO);
S
shm  
Shengliang Guan 已提交
30 31 32 33
    return execvp(tsProcPath, args);
  } else {
    return pid;
  }
wafwerar's avatar
wafwerar 已提交
34
#endif
S
shm  
Shengliang Guan 已提交
35 36
}

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

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

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

S
shm  
Shengliang Guan 已提交
61 62
// 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 已提交
63
  setThreadName(name);
S
shm  
Shengliang Guan 已提交
64 65 66 67 68 69 70

  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 已提交
71
      tstrncpy(argv[0], name, len + 1);
S
shm  
Shengliang Guan 已提交
72 73 74 75 76
    }
  }
}

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