tor  0.4.2.1-alpha-dev
Data Structures | Macros | Functions
process_unix.c File Reference
#include "lib/intmath/cmp.h"
#include "lib/buf/buffers.h"
#include "lib/net/buffers_net.h"
#include "lib/container/smartlist.h"
#include "lib/evloop/compat_libevent.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/waitpid.h"
#include "lib/process/env.h"
#include <stdio.h>

Go to the source code of this file.

Data Structures

struct  process_unix_handle_t
 
struct  process_unix_t
 

Macros

#define PROCESS_UNIX_PRIVATE
 
#define DEFAULT_MAX_FD   256
 

Functions

process_unix_tprocess_unix_new (void)
 
void process_unix_free_ (process_unix_t *unix_process)
 
process_status_t process_unix_exec (process_t *process)
 
bool process_unix_terminate (process_t *process)
 
process_pid_t process_unix_get_pid (process_t *process)
 
int process_unix_write (process_t *process, buf_t *buffer)
 
int process_unix_read_stdout (process_t *process, buf_t *buffer)
 
int process_unix_read_stderr (process_t *process, buf_t *buffer)
 
STATIC void stdout_read_callback (evutil_socket_t fd, short event, void *data)
 
STATIC void stderr_read_callback (evutil_socket_t fd, short event, void *data)
 
STATIC void stdin_write_callback (evutil_socket_t fd, short event, void *data)
 
STATIC void process_unix_start_reading (process_unix_handle_t *handle)
 
STATIC void process_unix_stop_reading (process_unix_handle_t *handle)
 
STATIC void process_unix_start_writing (process_unix_handle_t *handle)
 
STATIC void process_unix_stop_writing (process_unix_handle_t *handle)
 
STATIC void process_unix_waitpid_callback (int status, void *data)
 
STATIC void process_unix_setup_handle (process_t *process, process_unix_handle_t *handle, short flags, event_callback_fn callback)
 
STATIC int process_unix_read_handle (process_t *process, process_unix_handle_t *handle, buf_t *buffer)
 
STATIC bool process_unix_close_file_descriptors (process_unix_t *unix_process)
 

Detailed Description

Module for working with Unix processes.

Definition in file process_unix.c.

Macro Definition Documentation

◆ DEFAULT_MAX_FD

#define DEFAULT_MAX_FD   256

Maximum number of file descriptors, if we cannot get it via sysconf()

Definition at line 53 of file process_unix.c.

Function Documentation

◆ process_unix_close_file_descriptors()

STATIC bool process_unix_close_file_descriptors ( process_unix_t unix_process)

◆ process_unix_exec()

process_status_t process_unix_exec ( process_t process)

Executes the given process as a child process of Tor. This function is responsible for setting up the child process and run it. This includes setting up pipes for interprocess communication, initialize the waitpid callbacks, and finally run fork() followed by execve(). Returns PROCESS_STATUS_RUNNING upon success.

Cleanup standard in pipe.

Cleanup standard in pipe.

Cleanup standard out pipe.

Cleanup standard in pipe.

Cleanup standard out pipe.

Cleanup standard error pipe.

Definition at line 131 of file process_unix.c.

References process_get_unix_process().

◆ process_unix_free_()

void process_unix_free_ ( process_unix_t unix_process)

Deallocates the given unix_process.

Definition at line 104 of file process_unix.c.

References clear_waitpid_callback(), process_unix_close_file_descriptors(), and process_unix_t::waitpid.

◆ process_unix_get_pid()

process_pid_t process_unix_get_pid ( process_t process)

Returns the unique process identifier for the given process.

Definition at line 387 of file process_unix.c.

References process_unix_t::pid, process_get_unix_process(), and tor_assert().

Referenced by process_get_pid().

◆ process_unix_new()

process_unix_t* process_unix_new ( void  )

Returns a newly allocated process_unix_t.

Definition at line 90 of file process_unix.c.

◆ process_unix_read_handle()

STATIC int process_unix_read_handle ( process_t process,
process_unix_handle_t handle,
buf_t *  buffer 
)

This function reads data from the given handle and puts it into buffer. Returns the number of bytes read this way.

Definition at line 621 of file process_unix.c.

References buf_read_from_pipe(), process_unix_handle_t::fd, PROCESS_MAX_READ, and tor_assert().

Referenced by process_unix_read_stderr(), and process_unix_read_stdout().

◆ process_unix_read_stderr()

int process_unix_read_stderr ( process_t process,
buf_t *  buffer 
)

Read data from the given process's standard error and put it into buffer. Returns the number of bytes read.

Definition at line 449 of file process_unix.c.

References process_get_unix_process(), process_unix_read_handle(), process_unix_t::stderr_handle, and tor_assert().

◆ process_unix_read_stdout()

int process_unix_read_stdout ( process_t process,
buf_t *  buffer 
)

Read data from the given process's standard output and put it into buffer. Returns the number of bytes read.

Definition at line 434 of file process_unix.c.

References process_get_unix_process(), process_unix_read_handle(), process_unix_t::stdout_handle, and tor_assert().

◆ process_unix_setup_handle()

STATIC void process_unix_setup_handle ( process_t process,
process_unix_handle_t handle,
short  flags,
event_callback_fn  callback 
)

This function sets the file descriptor in the handle as non-blocking and configures the libevent event structure based on the given flags to ensure that callback is called whenever we have events on the given handle.

Definition at line 595 of file process_unix.c.

References process_unix_handle_t::fd, and tor_assert().

◆ process_unix_start_reading()

STATIC void process_unix_start_reading ( process_unix_handle_t handle)

This function tells libevent that we are interested in receiving read events from the given handle.

Definition at line 512 of file process_unix.c.

References process_unix_handle_t::event, and tor_assert().

◆ process_unix_start_writing()

STATIC void process_unix_start_writing ( process_unix_handle_t handle)

This function tells libevent that we are interested in receiving write events from the given handle.

Definition at line 539 of file process_unix.c.

References process_unix_handle_t::event, and tor_assert().

◆ process_unix_stop_reading()

STATIC void process_unix_stop_reading ( process_unix_handle_t handle)

This function tells libevent that we are no longer interested in receiving read events from the given handle.

Definition at line 524 of file process_unix.c.

References process_unix_handle_t::event, and tor_assert().

Referenced by process_unix_close_file_descriptors().

◆ process_unix_stop_writing()

STATIC void process_unix_stop_writing ( process_unix_handle_t handle)

This function tells libevent that we are no longer interested in receiving write events from the given handle.

Definition at line 553 of file process_unix.c.

References process_unix_handle_t::event, and tor_assert().

Referenced by process_unix_close_file_descriptors().

◆ process_unix_terminate()

bool process_unix_terminate ( process_t process)

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

Definition at line 355 of file process_unix.c.

References process_get_unix_process(), and tor_assert().

◆ process_unix_waitpid_callback()

STATIC void process_unix_waitpid_callback ( int  status,
void *  data 
)

This function is called when the waitpid system have detected that our process have terminated. We disable the waitpid system and notify the Process subsystem that we have terminated.

Definition at line 571 of file process_unix.c.

◆ process_unix_write()

int process_unix_write ( process_t process,
buf_t *  buffer 
)

Write the given buffer as input to the given process's standard input. Returns the number of bytes written.

Definition at line 398 of file process_unix.c.

References process_get_unix_process(), and tor_assert().

◆ stderr_read_callback()

STATIC void stderr_read_callback ( evutil_socket_t  fd,
short  event,
void *  data 
)

This function is called whenever libevent thinks we have data that could be read from the child process's standard error. We notify the Process subsystem, which is then responsible for calling back to us for doing the actual reading of the data.

Definition at line 482 of file process_unix.c.

References process_notify_event_stderr(), and tor_assert().

◆ stdin_write_callback()

STATIC void stdin_write_callback ( evutil_socket_t  fd,
short  event,
void *  data 
)

This function is called whenever libevent thinks we have data that could be written the child process's standard input. We notify the Process subsystem, which is then responsible for calling back to us for doing the actual write of the data.

Definition at line 498 of file process_unix.c.

References process_notify_event_stdin(), and tor_assert().

◆ stdout_read_callback()

STATIC void stdout_read_callback ( evutil_socket_t  fd,
short  event,
void *  data 
)

This function is called whenever libevent thinks we have data that could be read from the child process's standard output. We notify the Process subsystem, which is then responsible for calling back to us for doing the actual reading of the data.

Definition at line 466 of file process_unix.c.

References process_notify_event_stdout(), and tor_assert().