bool http_proxy_server_config::load_config()
{
    std::string config_data;
#ifdef _WIN32
    wchar_t path_buffer[MAX_PATH];
    if (GetModuleFileNameW(NULL, path_buffer, MAX_PATH) == 0 || GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
        std::cerr << "Failed to get retrieve the path of the executable file" << std::endl;
    }
    std::wstring config_file_path(path_buffer);
    config_file_path.resize(config_file_path.find_last_of(L'\\') + 1);
    config_file_path += L"server.json";
    std::shared_ptr<std::remove_pointer<HANDLE>::type> config_file_handle(
        CreateFileW(config_file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL),
        [](HANDLE native_handle) {
            if (native_handle != INVALID_HANDLE_VALUE) {
                CloseHandle(native_handle);
            }
    });
    if (config_file_handle.get() == INVALID_HANDLE_VALUE) {
        std::cerr << "Failed to open config file \"server.json\"" << std::endl;
        return false;
    }
    char ch;
    DWORD size_read = 0;
    BOOL read_result =  ReadFile(config_file_handle.get(), &ch, 1, &size_read, NULL);
    while (read_result != FALSE && size_read != 0) {
        config_data.push_back(ch);
        read_result =  ReadFile(config_file_handle.get(), &ch, 1, &size_read, NULL);
    }
    if (read_result == FALSE) {
        std::cerr << "Failed to read data from config file" << std::endl;
        return false;
    }
#else
    auto bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (bufsize == -1) {
        bufsize = 16384;
    }
    std::unique_ptr<char[]> buf(new char[bufsize]);
    passwd pwd, *result = nullptr;
    getpwuid_r(getuid(), &pwd, buf.get(), bufsize, &result);
    if (result == nullptr) {
        return false;
    }
    std::string config_path = pwd.pw_dir;
    config_path += "/.ahps/server.json";
    std::ifstream ifile(config_path.c_str());
    if (!ifile.is_open()) {
        std::cerr << "Failed to open \"" << config_path << "\"" << std::endl;
        return false;
    }
    char ch;
    while (ifile.get(ch)) {
        config_data.push_back(ch);
    }
#endif
    return this->load_config(config_data);
}
Exemplo n.º 2
0
/*
 * main function
 */
int main(int argc, char** argv) {
  //daemonize();

  /*FILE *fp = NULL;*/
  /*fp = fopen("/Users/bjohnson/Desktop/daemon.log", "a");*/
  /*while(1) {*/
    /*sleep(1);*/
    /*_log(fp, "Daemon logging");*/
    /*fflush(fp);*/
  /*}*/

  char* config_file = config_file_path();
  configuration* config;
  if (config_file) {
    config = parse_config(config_file);
  } else {
    fprintf(stderr, "Error: no configuration file specified? (see -c flag)\n");
    return -1;
  }

  printf("log file: %s\n", config->log);
  process_configuration *p_config = config->process_config;
  int i;
  while (p_config) {
    printf("\nProcess: %s\n", p_config->name);
    printf("  cd: %s\n", p_config->cd);
    printf("  exec: %s\n", p_config->exec);
    printf("  pid_file: %s\n", p_config->pid_file);
    printf("  args: ");
    for(i=0; p_config->args[i]; i++) {
      printf("%s ", p_config->args[i]);
      fflush(stdout);
    }
    printf("\n");
    p_config = p_config->next_process;
  }

  monitor_processes(config);

  return 0;
}