blob: ee35b72df39f36a9a46a82714ff76dbc4dadb2f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/* Copyright (c) 2001 Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "tor_api.h"
#include "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));
int
tor_run_main(const tor_main_configuration_t *cfg)
{
pid_t pid = fork();
if (pid == 0) {
child(cfg);
exit(0); /* Unreachable */
}
pid_t stopped_pid;
int status = 0;
do {
stopped_pid = waitpid(pid, &status, 0);
} while (stopped_pid == -1);
/* Note: these return values are not documented. No return value is
* documented! */
if (stopped_pid != pid) {
return -99999;
}
if (WIFSTOPPED(status)) {
return WEXITSTATUS(status);
}
if (WIFSIGNALED(status)) {
return -WTERMSIG(status);
}
return -999988;
}
static void
child(const tor_main_configuration_t *cfg)
{
/* XXXX Close unused file descriptors. */
char **args = calloc(cfg->argc+1, sizeof(char *));
memcpy(args, cfg->argv, cfg->argc * sizeof(char *));
args[cfg->argc] = NULL;
int rv = execv(BINDIR "/tor", args);
if (rv < 0) {
exit(254);
} else {
abort(); /* Unreachable */
}
}
|