Tor  0.4.7.0-alpha-dev
Data Structures | Macros | Functions | Variables
process.c File Reference

Module for working with other processes. More...

#include "lib/buf/buffers.h"
#include "lib/net/buffers_net.h"
#include "lib/container/smartlist.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/process/process.h"
#include "lib/process/process_unix.h"
#include "lib/process/process_win32.h"
#include "lib/process/env.h"

Go to the source code of this file.

Data Structures

struct  process_t
 

Functions

const char * process_status_to_string (process_status_t status)
 
const char * process_protocol_to_string (process_protocol_t protocol)
 
void tor_disable_spawning_background_processes (void)
 
void process_init (void)
 
void process_free_all (void)
 
const smartlist_tprocess_get_all_processes (void)
 
process_tprocess_new (const char *command)
 
void process_free_ (process_t *process)
 
process_status_t process_exec (process_t *process)
 
bool process_terminate (process_t *process)
 
process_pid_t process_get_pid (process_t *process)
 
void process_set_stdout_read_callback (process_t *process, process_read_callback_t callback)
 
void process_set_stderr_read_callback (process_t *process, process_read_callback_t callback)
 
void process_set_exit_callback (process_t *process, process_exit_callback_t callback)
 
const char * process_get_command (const process_t *process)
 
void process_set_protocol (process_t *process, process_protocol_t protocol)
 
process_protocol_t process_get_protocol (const process_t *process)
 
void process_set_data (process_t *process, void *data)
 
void * process_get_data (const process_t *process)
 
void process_set_status (process_t *process, process_status_t status)
 
process_status_t process_get_status (const process_t *process)
 
void process_append_argument (process_t *process, const char *argument)
 
const smartlist_tprocess_get_arguments (const process_t *process)
 
char ** process_get_argv (const process_t *process)
 
void process_reset_environment (process_t *process, const smartlist_t *env)
 
void process_set_environment (process_t *process, const char *key, const char *value)
 
process_environment_tprocess_get_environment (const process_t *process)
 
process_unix_tprocess_get_unix_process (const process_t *process)
 
void process_write (process_t *process, const uint8_t *data, size_t size)
 
void process_vprintf (process_t *process, const char *format, va_list args)
 
void process_printf (process_t *process, const char *format,...)
 
void process_notify_event_stdout (process_t *process)
 
void process_notify_event_stderr (process_t *process)
 
void process_notify_event_stdin (process_t *process)
 
void process_notify_event_exit (process_t *process, process_exit_code_t exit_code)
 
STATIC int process_read_stdout (process_t *process, buf_t *buffer)
 
STATIC int process_read_stderr (process_t *process, buf_t *buffer)
 
STATIC void process_write_stdin (process_t *process, buf_t *buffer)
 
STATIC void process_read_data (process_t *process, buf_t *buffer, process_read_callback_t callback)
 
STATIC void process_read_buffer (process_t *process, buf_t *buffer, process_read_callback_t callback)
 
STATIC void process_read_lines (process_t *process, buf_t *buffer, process_read_callback_t callback)
 

Variables

static smartlist_tprocesses
 
static int may_spawn_background_process = 1
 

Detailed Description

Module for working with other processes.

Definition in file process.c.

Function Documentation

◆ process_append_argument()

void process_append_argument ( process_t process,
const char *  argument 
)

Append an argument to the list of arguments in the given process.

Definition at line 419 of file process.c.

◆ process_exec()

process_status_t process_exec ( process_t process)

Execute the given process. This function executes the given process as a subprocess of Tor. Returns PROCESS_STATUS_RUNNING upon success.

Definition at line 253 of file process.c.

◆ process_free_()

void process_free_ ( process_t process)

Deallocate the given process in process.

Definition at line 217 of file process.c.

◆ process_free_all()

void process_free_all ( void  )

Free up all resources that is handled by the Process subsystem. Note that this call does not terminate already running processes.

Definition at line 153 of file process.c.

◆ process_get_all_processes()

const smartlist_t* process_get_all_processes ( void  )

Get a list of all processes. This function returns a smartlist of process_t containing all the currently allocated processes.

Definition at line 166 of file process.c.

◆ process_get_arguments()

const smartlist_t* process_get_arguments ( const process_t process)

Returns a list of arguments (excluding the command itself) from the given process.

Definition at line 430 of file process.c.

◆ process_get_argv()

char** process_get_argv ( const process_t process)

Returns a newly allocated Unix style argument vector. Use tor_free() to deallocate it after use.

Generate a Unix style process argument vector from our process's arguments smartlist_t.

Definition at line 439 of file process.c.

◆ process_get_command()

const char* process_get_command ( const process_t process)

Get the current command of the given process.

Definition at line 356 of file process.c.

◆ process_get_data()

void* process_get_data ( const process_t process)

Get the opaque pointer to callback data from the given process. This function allows you get the data you stored with process_set_data() in the different callback functions.

Definition at line 395 of file process.c.

Referenced by managed_proxy_stderr_callback(), and managed_proxy_stdout_callback().

◆ process_get_environment()

process_environment_t* process_get_environment ( const process_t process)

Returns a newly allocated process_environment_t containing the environment variables for the given process.

Definition at line 501 of file process.c.

◆ process_get_pid()

process_pid_t process_get_pid ( process_t process)

Returns the unique process identifier for the given process.

Definition at line 303 of file process.c.

◆ process_get_protocol()

process_protocol_t process_get_protocol ( const process_t process)

Get the currently used protocol of the given process.

Definition at line 371 of file process.c.

◆ process_get_status()

process_status_t process_get_status ( const process_t process)

Get the status of the given process.

Definition at line 411 of file process.c.

Referenced by process_terminate().

◆ process_get_unix_process()

process_unix_t* process_get_unix_process ( const process_t process)

Get the internal handle for the Unix backend.

Definition at line 510 of file process.c.

Referenced by process_unix_exec(), process_unix_get_pid(), process_unix_read_stderr(), process_unix_read_stdout(), process_unix_terminate(), and process_unix_write().

◆ process_init()

void process_init ( void  )

Initialize the Process subsystem. This function initializes the Process subsystem's global state. For cleaning up, process_free_all() should be called.

Definition at line 141 of file process.c.

◆ process_new()

process_t* process_new ( const char *  command)

Allocate and initialize a new process. This function returns a newly allocated and initialized process data, which can be used to configure and later run a subprocess of Tor. Use the various process_set_*() methods to configure it and run the process using process_exec(). Use command to specify the path to the command to run. You can either specify an absolute path to the command or relative where Tor will use the underlying operating system's functionality for finding the command to run.

Definition at line 180 of file process.c.

◆ process_notify_event_exit()

void process_notify_event_exit ( process_t process,
process_exit_code_t  exit_code 
)

This function is called by the Process backend when a given process have terminated. The exit status code is passed in exit_code. We mark the process as no longer running and calls the exit_callback with information about the process termination. The given process is free'd iff the exit_callback returns true.

Definition at line 623 of file process.c.

◆ process_notify_event_stderr()

void process_notify_event_stderr ( process_t process)

This function is called by the Process backend when a given process have data that is ready to be read from the child process's standard error handle.

Definition at line 594 of file process.c.

Referenced by stderr_read_callback().

◆ process_notify_event_stdin()

void process_notify_event_stdin ( process_t process)

This function is called by the Process backend when a given process is allowed to begin writing data to the standard input of the child process.

Definition at line 610 of file process.c.

Referenced by stdin_write_callback().

◆ process_notify_event_stdout()

void process_notify_event_stdout ( process_t process)

This function is called by the Process backend when a given process have data that is ready to be read from the child process's standard output handle.

Definition at line 577 of file process.c.

Referenced by stdout_read_callback().

◆ process_printf()

void process_printf ( process_t process,
const char *  format,
  ... 
)

As tor_snprintf(), but write the data to the given process's standard input.

Definition at line 561 of file process.c.

◆ process_protocol_to_string()

const char* process_protocol_to_string ( process_protocol_t  protocol)

Convert a given process protocol in protocol to its string representation.

Definition at line 112 of file process.c.

◆ process_read_buffer()

STATIC void process_read_buffer ( process_t process,
buf_t *  buffer,
process_read_callback_t  callback 
)

This function takes the content of the given buffer and passes it to the given callback function, but ensures that an additional zero byte is added to the end of the data such that the given callback implementation can threat the content as a ASCIIZ string.

Definition at line 722 of file process.c.

◆ process_read_data()

STATIC void process_read_data ( process_t process,
buf_t *  buffer,
process_read_callback_t  callback 
)

This function calls the protocol handlers based on the value of process_get_protocol(process). Currently we call process_read_buffer() for PROCESS_PROTOCOL_RAW and process_read_lines() for PROCESS_PROTOCOL_LINE.

Definition at line 695 of file process.c.

Referenced by process_notify_event_stderr(), and process_notify_event_stdout().

◆ process_read_lines()

STATIC void process_read_lines ( process_t process,
buf_t *  buffer,
process_read_callback_t  callback 
)

This function tries to extract complete lines from the given buffer and calls the given callback function whenever it has a complete line. Before calling callback we remove the trailing "\n" or "\r\n" from the line. If we are unable to extract a complete line we leave the data in the buffer for next call.

Definition at line 749 of file process.c.

◆ process_read_stderr()

STATIC int process_read_stderr ( process_t process,
buf_t *  buffer 
)

This function is called whenever the Process backend have notified us that there is data to be read from its standard error handle. Returns the number of bytes that have been put into the given buffer.

Definition at line 663 of file process.c.

Referenced by process_notify_event_stderr().

◆ process_read_stdout()

STATIC int process_read_stdout ( process_t process,
buf_t *  buffer 
)

This function is called whenever the Process backend have notified us that there is data to be read from its standard out handle. Returns the number of bytes that have been put into the given buffer.

Definition at line 648 of file process.c.

Referenced by process_notify_event_stdout().

◆ process_reset_environment()

void process_reset_environment ( process_t process,
const smartlist_t env 
)

This function clears the internal environment and copies over every string from env as the new environment.

Definition at line 470 of file process.c.

◆ process_set_data()

void process_set_data ( process_t process,
void *  data 
)

Set opaque pointer to data. This function allows you to store a pointer to your own data in the given process. Use process_get_data() in the various callback functions to retrieve the data again.

Note that the given process does NOT take ownership of the data and you are responsible for freeing up any resources allocated by the given data.

Definition at line 385 of file process.c.

Referenced by launch_managed_proxy(), and proxy_prepare_for_restart().

◆ process_set_environment()

void process_set_environment ( process_t process,
const char *  key,
const char *  value 
)

Set the given key/value pair as environment variable in the given process.

Definition at line 487 of file process.c.

◆ process_set_exit_callback()

void process_set_exit_callback ( process_t process,
process_exit_callback_t  callback 
)

Set the callback function for process exit notification. The callback function will be called every time your child process have terminated.

Definition at line 347 of file process.c.

Referenced by launch_managed_proxy().

◆ process_set_status()

void process_set_status ( process_t process,
process_status_t  status 
)

Set the status of a given process.

Definition at line 403 of file process.c.

◆ process_set_stderr_read_callback()

void process_set_stderr_read_callback ( process_t process,
process_read_callback_t  callback 
)

Set the callback function for output from the child process's standard error handle. This function sets the callback function which is called every time the child process have written output to its standard error file handle.

Use process_set_protocol(process, PROCESS_PROTOCOL_LINE) if you want the callback to only contain complete "\n" or "\r\n" terminated lines.

Definition at line 336 of file process.c.

Referenced by launch_managed_proxy().

◆ process_set_stdout_read_callback()

void process_set_stdout_read_callback ( process_t process,
process_read_callback_t  callback 
)

Set the callback function for output from the child process's standard out handle. This function sets the callback function which is called every time the child process have written output to its standard out file handle.

Use process_set_protocol(process, PROCESS_PROTOCOL_LINE) if you want the callback to only contain complete "\n" or "\r\n" terminated lines.

Definition at line 321 of file process.c.

Referenced by launch_managed_proxy().

◆ process_status_to_string()

const char* process_status_to_string ( process_status_t  status)

Convert a given process status in status to its string representation.

Definition at line 92 of file process.c.

◆ process_terminate()

bool process_terminate ( process_t process)

Terminate the given process. Returns true on success, otherwise false.

Definition at line 284 of file process.c.

Referenced by proxy_prepare_for_restart().

◆ process_vprintf()

void process_vprintf ( process_t process,
const char *  format,
va_list  args 
)

As tor_vsnprintf(), but write the data to the given process's standard input.

Definition at line 543 of file process.c.

Referenced by process_printf().

◆ process_write()

void process_write ( process_t process,
const uint8_t *  data,
size_t  size 
)

Write size bytes of data to the given process's standard input.

Definition at line 530 of file process.c.

Referenced by process_vprintf().

◆ process_write_stdin()

STATIC void process_write_stdin ( process_t process,
buf_t *  buffer 
)

This function calls the backend function for the given process whenever there is data to be written to the backends' file handles.

Definition at line 678 of file process.c.

Referenced by process_notify_event_stdin(), and process_write().

◆ tor_disable_spawning_background_processes()

void tor_disable_spawning_background_processes ( void  )

Turn off may_spawn_background_process, so that all future calls to tor_spawn_background are guaranteed to fail.

Definition at line 132 of file process.c.

Referenced by options_act().

Variable Documentation

◆ may_spawn_background_process

int may_spawn_background_process = 1
static

Boolean. If true, then Tor may call execve or CreateProcess via tor_spawn_background.

Definition at line 33 of file process.c.

Referenced by tor_disable_spawning_background_processes().

◆ processes

smartlist_t* processes
static

A list of all process_t instances currently allocated.

Definition at line 27 of file process.c.

Referenced by process_free_all(), process_get_all_processes(), and process_init().