Tor 0.4.9.0-alpha-dev
Data Structures | Macros | Functions
process_unix.c File Reference

Module for working with Unix processes. More...

#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.

◆ PROCESS_UNIX_PRIVATE

#define PROCESS_UNIX_PRIVATE

Definition at line 11 of file process_unix.c.

Function Documentation

◆ process_unix_close_file_descriptors()

STATIC bool process_unix_close_file_descriptors ( process_unix_t unix_process)

Close the standard in, out, and error handles of the given unix_process.

Definition at line 647 of file process_unix.c.

Referenced by process_unix_free_().

◆ 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.

◆ 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.

◆ 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 380 of file process_unix.c.

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 614 of file process_unix.c.

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 442 of file process_unix.c.

Referenced by process_read_stderr().

◆ 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 427 of file process_unix.c.

Referenced by process_read_stdout().

◆ 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 588 of file process_unix.c.

◆ 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 505 of file process_unix.c.

◆ 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 532 of file process_unix.c.

◆ 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 517 of file process_unix.c.

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 546 of file process_unix.c.

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 348 of file process_unix.c.

◆ 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 564 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 391 of file process_unix.c.

Referenced by process_write_stdin().

◆ 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 475 of file process_unix.c.

◆ 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 491 of file process_unix.c.

◆ 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 459 of file process_unix.c.