mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-26 15:42:34 +01:00
Port managed proxy launching code to the new subprocess API.
This commit is contained in:
parent
3b704fd166
commit
45307ff980
3 changed files with 187 additions and 50 deletions
|
@ -371,9 +371,6 @@ int tor_spawn_background(const char *const filename, const char **argv,
|
||||||
HANDLE load_windows_system_library(const TCHAR *library_name);
|
HANDLE load_windows_system_library(const TCHAR *library_name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef UTIL_PRIVATE
|
|
||||||
/* Prototypes for private functions only used by util.c (and unit tests) */
|
|
||||||
|
|
||||||
/* Values of process_handle_t.status. PROCESS_STATUS_NOTRUNNING must be
|
/* Values of process_handle_t.status. PROCESS_STATUS_NOTRUNNING must be
|
||||||
* 0 because tor_check_port_forwarding depends on this being the initial
|
* 0 because tor_check_port_forwarding depends on this being the initial
|
||||||
* statue of the static instance of process_handle_t */
|
* statue of the static instance of process_handle_t */
|
||||||
|
@ -416,6 +413,9 @@ ssize_t tor_read_all_from_process_stderr(
|
||||||
const process_handle_t *process_handle, char *buf, size_t count);
|
const process_handle_t *process_handle, char *buf, size_t count);
|
||||||
char *tor_join_win_cmdline(const char *argv[]);
|
char *tor_join_win_cmdline(const char *argv[]);
|
||||||
|
|
||||||
|
#ifdef UTIL_PRIVATE
|
||||||
|
/* Prototypes for private functions only used by util.c (and unit tests) */
|
||||||
|
|
||||||
void format_helper_exit_status(unsigned char child_state,
|
void format_helper_exit_status(unsigned char child_state,
|
||||||
int saved_errno, char *hex_errno);
|
int saved_errno, char *hex_errno);
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,14 @@
|
||||||
#include "transports.h"
|
#include "transports.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#ifdef MS_WINDOWS
|
||||||
|
static void set_managed_proxy_environment(const managed_proxy_t *mp);
|
||||||
|
static void reset_environment(const managed_proxy_t *mp);
|
||||||
|
#else
|
||||||
static void set_managed_proxy_environment(char ***envp,
|
static void set_managed_proxy_environment(char ***envp,
|
||||||
const managed_proxy_t *mp);
|
const managed_proxy_t *mp);
|
||||||
|
#endif
|
||||||
|
|
||||||
static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
|
static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
|
||||||
|
|
||||||
static void managed_proxy_destroy(managed_proxy_t *mp);
|
static void managed_proxy_destroy(managed_proxy_t *mp);
|
||||||
|
@ -229,12 +235,15 @@ proxy_prepare_for_restart(managed_proxy_t *mp)
|
||||||
transport_t *t_tmp = NULL;
|
transport_t *t_tmp = NULL;
|
||||||
|
|
||||||
tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
|
tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
|
||||||
tor_assert(mp->pid);
|
|
||||||
|
|
||||||
/* kill the old obfsproxy process */
|
/* kill the old obfsproxy process */
|
||||||
tor_terminate_process(mp->pid);
|
#ifdef MS_WINDOWS
|
||||||
mp->pid = 0;
|
tor_terminate_process(mp->process_handle->pid.dwProcessId);
|
||||||
fclose(mp->_stdout);
|
#else
|
||||||
|
tor_terminate_process(mp->process_handle->pid);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
memset(mp->process_handle, 0, sizeof(process_handle_t));
|
||||||
|
|
||||||
/* destroy all its old transports. we no longer use them. */
|
/* destroy all its old transports. we no longer use them. */
|
||||||
SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
|
SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
|
||||||
|
@ -256,52 +265,54 @@ proxy_prepare_for_restart(managed_proxy_t *mp)
|
||||||
static int
|
static int
|
||||||
launch_managed_proxy(managed_proxy_t *mp)
|
launch_managed_proxy(managed_proxy_t *mp)
|
||||||
{
|
{
|
||||||
(void) mp;
|
#ifdef MS_WINDOWS
|
||||||
(void) set_managed_proxy_environment;
|
/* In Windows, we set the managed proxy environment on the tor
|
||||||
return -1;
|
process, and then we spawn a managed proxy with tor's
|
||||||
#if 0
|
environment. After the managed proxy is launched, we reset the
|
||||||
/* XXXX023 we must reenable this code for managed proxies to work.
|
environment to what it was previously. */
|
||||||
* "All it needs" is revision to work with the new tor_spawn_background
|
|
||||||
* API. */
|
set_managed_proxy_environment(mp);
|
||||||
|
|
||||||
|
/* Passing NULL as lpApplicationName makes Windows search for the .exe */
|
||||||
|
tor_spawn_background(NULL, (const char **)mp->argv, NULL,
|
||||||
|
mp->process_handle);
|
||||||
|
|
||||||
|
reset_environment(mp);
|
||||||
|
|
||||||
|
#else
|
||||||
|
/* In Linux, we create an environment for the managed proxy process
|
||||||
|
in the envp variable. We then pass it to tor_spawn_background()
|
||||||
|
so that it can be used with execve(). */
|
||||||
|
|
||||||
char **envp=NULL;
|
char **envp=NULL;
|
||||||
int pid;
|
|
||||||
process_handle_t proc;
|
|
||||||
FILE *stdout_read = NULL;
|
|
||||||
int stdout_pipe=-1, stderr_pipe=-1;
|
|
||||||
|
|
||||||
/* prepare the environment variables for the managed proxy */
|
/* prepare the environment variables for the managed proxy */
|
||||||
set_managed_proxy_environment(&envp, mp);
|
set_managed_proxy_environment(&envp, mp);
|
||||||
|
|
||||||
pid = tor_spawn_background(mp->argv[0], (const char **)mp->argv,
|
tor_spawn_background(mp->argv[0], (const char **)mp->argv,
|
||||||
(const char **)envp, &proc);
|
(const char **)envp, mp->process_handle);
|
||||||
if (pid < 0) {
|
|
||||||
|
/* free the memory allocated by set_managed_proxy_environment(). */
|
||||||
|
free_execve_args(envp);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (mp->process_handle->status == PROCESS_STATUS_ERROR) {
|
||||||
log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
|
log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
|
||||||
mp->argv[0]);
|
mp->argv[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free the memory allocated by set_managed_proxy_environment(). */
|
#ifdef MS_WINDOWS
|
||||||
free_execve_args(envp);
|
log_info(LD_CONFIG, "Managed proxy at '%s' has spawned.",
|
||||||
|
mp->argv[0]);
|
||||||
/* Set stdout/stderr pipes to be non-blocking */
|
|
||||||
#ifdef _WIN32
|
|
||||||
{
|
|
||||||
u_long nonblocking = 1;
|
|
||||||
ioctlsocket(stdout_pipe, FIONBIO, &nonblocking);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
|
log_info(LD_CONFIG, "Managed proxy at '%s' has spawned with pid %d.",
|
||||||
|
mp->argv[0], mp->process_handle->pid);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Open the buffered IO streams */
|
|
||||||
stdout_read = fdopen(stdout_pipe, "r");
|
|
||||||
|
|
||||||
log_info(LD_CONFIG, "Managed proxy has spawned at PID %d.", pid);
|
|
||||||
|
|
||||||
mp->conf_state = PT_PROTO_LAUNCHED;
|
mp->conf_state = PT_PROTO_LAUNCHED;
|
||||||
mp->_stdout = stdout_read;
|
|
||||||
mp->pid = pid;
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,6 +354,61 @@ pt_configure_remaining_proxies(void)
|
||||||
} SMARTLIST_FOREACH_END(mp);
|
} SMARTLIST_FOREACH_END(mp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef MS_WINDOWS
|
||||||
|
|
||||||
|
/** Attempt to continue configuring managed proxy <b>mp</b>. */
|
||||||
|
static void
|
||||||
|
configure_proxy(managed_proxy_t *mp)
|
||||||
|
{
|
||||||
|
int pos;
|
||||||
|
char stdout_buf[200];
|
||||||
|
smartlist_t *lines = NULL;
|
||||||
|
|
||||||
|
/* if we haven't launched the proxy yet, do it now */
|
||||||
|
if (mp->conf_state == PT_PROTO_INFANT) {
|
||||||
|
if (launch_managed_proxy(mp) < 0) { /* launch fail */
|
||||||
|
mp->conf_state = PT_PROTO_BROKEN;
|
||||||
|
handle_finished_proxy(mp);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tor_assert(mp->conf_state != PT_PROTO_INFANT);
|
||||||
|
|
||||||
|
pos = tor_read_all_handle(mp->process_handle->stdout_pipe,
|
||||||
|
stdout_buf, sizeof(stdout_buf) - 1, NULL);
|
||||||
|
if (pos < 0) {
|
||||||
|
log_notice(LD_GENERAL, "Failed to read data from managed proxy");
|
||||||
|
mp->conf_state = PT_PROTO_BROKEN;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos == 0) /* proxy has nothing interesting to say. */
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* End with a null even if there isn't a \r\n at the end */
|
||||||
|
/* TODO: What if this is a partial line? */
|
||||||
|
stdout_buf[pos] = '\0';
|
||||||
|
|
||||||
|
/* Split up the buffer */
|
||||||
|
lines = smartlist_create();
|
||||||
|
tor_split_lines(lines, stdout_buf, pos);
|
||||||
|
|
||||||
|
/* Handle lines. */
|
||||||
|
SMARTLIST_FOREACH(lines, const char *, line,
|
||||||
|
handle_proxy_line(line, mp));
|
||||||
|
|
||||||
|
done:
|
||||||
|
/* if the proxy finished configuring, exit the loop. */
|
||||||
|
if (proxy_configuration_finished(mp))
|
||||||
|
handle_finished_proxy(mp);
|
||||||
|
|
||||||
|
if (lines)
|
||||||
|
smartlist_free(lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else /* Unix version: */
|
||||||
|
|
||||||
/** Attempt to continue configuring managed proxy <b>mp</b>. */
|
/** Attempt to continue configuring managed proxy <b>mp</b>. */
|
||||||
static void
|
static void
|
||||||
configure_proxy(managed_proxy_t *mp)
|
configure_proxy(managed_proxy_t *mp)
|
||||||
|
@ -352,15 +418,18 @@ configure_proxy(managed_proxy_t *mp)
|
||||||
|
|
||||||
/* if we haven't launched the proxy yet, do it now */
|
/* if we haven't launched the proxy yet, do it now */
|
||||||
if (mp->conf_state == PT_PROTO_INFANT) {
|
if (mp->conf_state == PT_PROTO_INFANT) {
|
||||||
launch_managed_proxy(mp);
|
if (launch_managed_proxy(mp) < 0) { /* launch fail */
|
||||||
|
mp->conf_state = PT_PROTO_BROKEN;
|
||||||
|
handle_finished_proxy(mp);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tor_assert(mp->conf_state != PT_PROTO_INFANT);
|
tor_assert(mp->conf_state != PT_PROTO_INFANT);
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
r = get_string_from_pipe(mp->_stdout, stdout_buf,
|
r = get_string_from_pipe(mp->process_handle->stdout_handle,
|
||||||
sizeof(stdout_buf) - 1);
|
stdout_buf, sizeof(stdout_buf) - 1);
|
||||||
|
|
||||||
if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
|
if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
|
||||||
handle_proxy_line((const char *)stdout_buf, mp);
|
handle_proxy_line((const char *)stdout_buf, mp);
|
||||||
|
@ -382,6 +451,8 @@ configure_proxy(managed_proxy_t *mp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* MS_WINDOWS */
|
||||||
|
|
||||||
/** Register server managed proxy <b>mp</b> transports to state */
|
/** Register server managed proxy <b>mp</b> transports to state */
|
||||||
static void
|
static void
|
||||||
register_server_proxy(managed_proxy_t *mp)
|
register_server_proxy(managed_proxy_t *mp)
|
||||||
|
@ -470,15 +541,19 @@ managed_proxy_destroy(managed_proxy_t *mp)
|
||||||
/* remove it from the list of managed proxies */
|
/* remove it from the list of managed proxies */
|
||||||
smartlist_remove(managed_proxy_list, mp);
|
smartlist_remove(managed_proxy_list, mp);
|
||||||
|
|
||||||
/* close its stdout stream */
|
|
||||||
if (mp->_stdout)
|
|
||||||
fclose(mp->_stdout);
|
|
||||||
|
|
||||||
/* free the argv */
|
/* free the argv */
|
||||||
free_execve_args(mp->argv);
|
free_execve_args(mp->argv);
|
||||||
|
|
||||||
if (mp->pid)
|
/* If proxy is launched, terminate it. */
|
||||||
tor_terminate_process(mp->pid);
|
if (mp->conf_state != PT_PROTO_INFANT) {
|
||||||
|
#ifdef MS_WINDOWS
|
||||||
|
tor_terminate_process(mp->process_handle->pid.dwProcessId);
|
||||||
|
#else
|
||||||
|
tor_terminate_process(mp->process_handle->pid);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
tor_free(mp->process_handle);
|
||||||
|
|
||||||
tor_free(mp);
|
tor_free(mp);
|
||||||
}
|
}
|
||||||
|
@ -858,6 +933,64 @@ get_bindaddr_for_proxy(const managed_proxy_t *mp)
|
||||||
return bindaddr;
|
return bindaddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef MS_WINDOWS
|
||||||
|
/** Set the managed proxy environment for the managed proxy
|
||||||
|
* <b>mp</b>. */
|
||||||
|
static void
|
||||||
|
set_managed_proxy_environment(const managed_proxy_t *mp)
|
||||||
|
{
|
||||||
|
const or_options_t *options = get_options();
|
||||||
|
char *state_loc=NULL;
|
||||||
|
char *transports_to_launch=NULL;
|
||||||
|
char *bindaddr=NULL;
|
||||||
|
char *orport=NULL;
|
||||||
|
|
||||||
|
state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
|
||||||
|
transports_to_launch =
|
||||||
|
smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
|
||||||
|
|
||||||
|
SetEnvironmentVariable("TOR_PT_STATE_LOCATION", state_loc);
|
||||||
|
|
||||||
|
SetEnvironmentVariable("TOR_PT_MANAGED_TRANSPORT_VER", "1");
|
||||||
|
|
||||||
|
if (mp->is_server) {
|
||||||
|
tor_asprintf(&orport, "127.0.0.1:%d", options->ORPort);
|
||||||
|
SetEnvironmentVariable("TOR_PT_ORPORT", orport);
|
||||||
|
|
||||||
|
bindaddr = get_bindaddr_for_proxy(mp);
|
||||||
|
SetEnvironmentVariable("TOR_PT_SERVER_BINDADDR", bindaddr);
|
||||||
|
|
||||||
|
SetEnvironmentVariable("TOR_PT_SERVER_TRANSPORTS", transports_to_launch);
|
||||||
|
SetEnvironmentVariable("TOR_PT_EXTENDED_SERVER_PORT", "127.0.0.1:4200");
|
||||||
|
} else {
|
||||||
|
SetEnvironmentVariable("TOR_PT_CLIENT_TRANSPORTS", transports_to_launch);
|
||||||
|
}
|
||||||
|
|
||||||
|
tor_free(state_loc);
|
||||||
|
tor_free(transports_to_launch);
|
||||||
|
tor_free(bindaddr);
|
||||||
|
tor_free(orport);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Unsets the managed proxy environment variables. */
|
||||||
|
static void
|
||||||
|
reset_environment(const managed_proxy_t *mp)
|
||||||
|
{
|
||||||
|
SetEnvironmentVariable("TOR_PT_STATE_LOCATION", NULL);
|
||||||
|
SetEnvironmentVariable("TOR_PT_MANAGED_TRANSPORT_VER", NULL);
|
||||||
|
|
||||||
|
if (mp->is_server) {
|
||||||
|
SetEnvironmentVariable("TOR_PT_ORPORT", NULL);
|
||||||
|
SetEnvironmentVariable("TOR_PT_SERVER_BINDADDR", NULL);
|
||||||
|
SetEnvironmentVariable("TOR_PT_SERVER_TRANSPORTS", NULL);
|
||||||
|
SetEnvironmentVariable("TOR_PT_EXTENDED_SERVER_PORT", NULL);
|
||||||
|
} else {
|
||||||
|
SetEnvironmentVariable("TOR_PT_CLIENT_TRANSPORTS", NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else /* Unix version: */
|
||||||
|
|
||||||
/** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
|
/** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
|
||||||
static void
|
static void
|
||||||
set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
|
set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
|
||||||
|
@ -901,6 +1034,8 @@ set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
|
||||||
tor_free(bindaddr);
|
tor_free(bindaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Create and return a new managed proxy for <b>transport</b> using
|
/** Create and return a new managed proxy for <b>transport</b> using
|
||||||
* <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
|
* <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
|
||||||
* managed proxy. */
|
* managed proxy. */
|
||||||
|
@ -918,6 +1053,8 @@ managed_proxy_create(const smartlist_t *transport_list,
|
||||||
SMARTLIST_FOREACH(transport_list, const char *, transport,
|
SMARTLIST_FOREACH(transport_list, const char *, transport,
|
||||||
add_transport_to_proxy(transport, mp));
|
add_transport_to_proxy(transport, mp));
|
||||||
|
|
||||||
|
mp->process_handle = tor_malloc_zero(sizeof(process_handle_t));
|
||||||
|
|
||||||
/* register the managed proxy */
|
/* register the managed proxy */
|
||||||
if (!managed_proxy_list)
|
if (!managed_proxy_list)
|
||||||
managed_proxy_list = smartlist_create();
|
managed_proxy_list = smartlist_create();
|
||||||
|
|
|
@ -47,8 +47,8 @@ typedef struct {
|
||||||
|
|
||||||
int is_server; /* is it a server proxy? */
|
int is_server; /* is it a server proxy? */
|
||||||
|
|
||||||
FILE *_stdout; /* a stream to its stdout
|
/* A pointer to the process handle of this managed proxy. */
|
||||||
(closed in managed_proxy_destroy()) */
|
process_handle_t *process_handle;
|
||||||
|
|
||||||
int pid; /* The Process ID this managed proxy is using. */
|
int pid; /* The Process ID this managed proxy is using. */
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue