예제 #1
0
  static bool start_process(pid_t& pid, const char* path,
                            const char* cwd,
                            const Args& args)
  {
#ifdef _WIN32
#else
    int retries = 5;
    pid_t tmp;
    while ((tmp = fork()) == -1)
    {
      fprintf(stderr, "Warning: 'fork' failed, errno: %d - ", errno);
      if (retries--)
      {
        fprintf(stderr, "retrying in 1 second...\n");
        NdbSleep_SecSleep(1);
        continue;
      }
      fprintf(stderr, "giving up...\n");
      return false;
    }

    if (tmp)
    {
      pid = tmp;
      printf("Started process: %d\n", pid);
      return true;
    }
    assert(tmp == 0);

    if (cwd && chdir(cwd) != 0)
    {
      fprintf(stderr, "Failed to change directory to '%s', errno: %d\n", cwd, errno);
      exit(1);
    }

    // Concatenate arguments
    BaseString args_str;
    args_str.assign(args.args(), " ");

    char **argv = BaseString::argify(path, args_str.c_str());
    //printf("name: %s\n", path);
    execv(path, argv);

    fprintf(stderr, "execv failed, errno: %d\n", errno);
    exit(1);
#endif
  }