osProc.c 1.6 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 21 22 23 24 25
static char *tsProcPath = NULL;

int32_t taosNewProc(char **args) {
  int32_t pid = fork();
  if (pid == 0) {
    args[0] = tsProcPath;
S
Shengliang Guan 已提交
26
    // close(STDIN_FILENO);
S
Shengliang Guan 已提交
27
    close(STDOUT_FILENO);
S
Shengliang Guan 已提交
28
    // close(STDERR_FILENO);
S
shm  
Shengliang Guan 已提交
29 30 31 32
    return execvp(tsProcPath, args);
  } else {
    return pid;
  }
S
shm  
Shengliang Guan 已提交
33 34
}

S
shm  
Shengliang Guan 已提交
35
void taosWaitProc(int32_t pid) {
S
shm  
Shengliang Guan 已提交
36
  int32_t status = -1;
S
shm  
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45 46
  waitpid(pid, &status, 0);
}

void taosKillProc(int32_t pid) { kill(pid, SIGINT); }

bool taosProcExist(int32_t pid) {
  int32_t p = getpgid(pid);
  return p >= 0;
}

S
shm  
Shengliang Guan 已提交
47 48
// 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) {
S
shm  
Shengliang Guan 已提交
49
  prctl(PR_SET_NAME, name);
S
shm  
Shengliang Guan 已提交
50 51 52 53 54 55 56

  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 已提交
57
      tstrncpy(argv[0], name, len + 1);
S
shm  
Shengliang Guan 已提交
58 59 60 61 62
    }
  }
}

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