#include "feature/api/tor_api.h"
#include "feature/api/tor_api_internal.h"
#include "orconfig.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifndef __GNUC__
#define __attribute__(x)
#endif
static void child(const tor_main_configuration_t *cfg)
__attribute__((noreturn));
const char *
tor_api_get_provider_version(void)
{
return "libtorrunner " VERSION;
}
int
tor_run_main(const tor_main_configuration_t *cfg)
{
pid_t pid = fork();
if (pid == 0) {
child(cfg);
exit(0);
}
pid_t stopped_pid;
int status = 0;
do {
stopped_pid = waitpid(pid, &status, 0);
} while (stopped_pid == -1);
if (stopped_pid != pid) {
return -99999;
}
if (WIFSTOPPED(status)) {
return WEXITSTATUS(status);
}
if (WIFSIGNALED(status)) {
return -WTERMSIG(status);
}
return -999988;
}
#define real_calloc calloc
#define real_free free
static void
child(const tor_main_configuration_t *cfg)
{
char **args = real_calloc(cfg->argc + cfg->argc_owned+1, sizeof(char *));
memcpy(args, cfg->argv, cfg->argc * sizeof(char *));
if (cfg->argc_owned)
memcpy(args + cfg->argc, cfg->argv_owned,
cfg->argc_owned * sizeof(char *));
args[cfg->argc + cfg->argc_owned] = NULL;
int rv = execv(BINDIR "/tor", args);
if (rv < 0) {
real_free(args);
exit(254);
} else {
abort();
}
}