LibComCom  0.1.0
Run an OS command with given input and receive output
Functions
libcomcom.h File Reference
#include <stddef.h>
#include <signal.h>

Go to the source code of this file.

Functions

int libcomcom_init (void)
 
int libcomcom_init2 (struct sigaction *old)
 
int libcomcom_init_stratum (void)
 
int libcomcom_run_command (const char *input, size_t input_len, const char **output, size_t *output_len, const char *file, char *const argv[], char *const envp[], int timeout)
 
int libcomcom_destroy (void)
 
int libcomcom_terminate (void)
 
int libcomcom_set_default_terminate (void)
 
int libcomcom_reset_default_terminate (void)
 
int libcomcom_set_default_terminate2 (void)
 
int libcomcom_reset_default_terminate2 (void)
 

Function Documentation

◆ libcomcom_destroy()

int libcomcom_destroy ( void  )

Should be run for normal termination (not in SIGTERM/SIGINT handler) of our program.

Returns
0 on success and -1 on error (also sets errno).

◆ libcomcom_init()

int libcomcom_init ( void  )

Initialize the library. Call it before libcomcom_run_command(). Note that this erases the old SIGCHLD handler (if any).

Returns
0 on success and -1 on error (also sets errno).

You should usually also initialize SIGTERM/SIGINT signal handlers.

◆ libcomcom_init2()

int libcomcom_init2 ( struct sigaction *  old)

Initialize the library. Call it before libcomcom_run_command(). The old signal action is stored internally (and restored by libcomcom_destroy() or libcomcom_terminate()). The old signal handler (the one obtained from old variable) is also called from our SIGCHLD handler.

Returns
0 on success and -1 on error (also sets errno).

You should usually also initialize SIGTERM/SIGINT signal handlers.

◆ libcomcom_init_stratum()

int libcomcom_init_stratum ( void  )

Initialize the library. Call it before libcomcom_run_command(). This function is like libcomcom_init2(), but the old SIGCHLD signal handler is obtained automatically (by sigaction() library function).

WARNING: If before calling this SIGCHLD handler was set by signal() (not by sigaction()), then this function may not work (leads to undefined behavior) on some non-Unix systems.

Returns
0 on success and -1 on error (also sets errno).

You should usually also initialize SIGTERM/SIGINT signal handlers.

◆ libcomcom_reset_default_terminate()

int libcomcom_reset_default_terminate ( void  )

Uninstall SIGTERM and SIGINT handler which calls libcomcom_terminate().

Returns
0 on success and -1 on error (also sets errno).

You are recommended to use libcomcom_reset_default_terminate2() instead.

◆ libcomcom_reset_default_terminate2()

int libcomcom_reset_default_terminate2 ( void  )

Resets to signal handlers which were before calling libcomcom_set_default_terminate2().

Returns
0 on success and -1 on error (also sets errno).

◆ libcomcom_run_command()

int libcomcom_run_command ( const char *  input,
size_t  input_len,
const char **  output,
size_t *  output_len,
const char *  file,
char *const  argv[],
char *const  envp[],
int  timeout 
)

Runs an OS command.

Parameters
inputpassed to command stdin
input_lenthe length of the string passed to stdin
outputat this location is stored the command's stdout (call free() after use)
output_lenat this location is stored the length of command's stdout
filethe command to run (PATH used)
argvarguments for the command to run
envpenvironment for the command to run (pass NULL to duplicate our environment)
timeouttimeout in milliseconds, -1 means infinite timeout
Returns
0 on success and -1 on error (also sets errno).

◆ libcomcom_set_default_terminate()

int libcomcom_set_default_terminate ( void  )

Install SIGTERM and SIGINT handler which calls libcomcom_terminate().

Returns
0 on success and -1 on error (also sets errno).

You are recommended to use libcomcom_set_default_terminate2() instead.

◆ libcomcom_set_default_terminate2()

int libcomcom_set_default_terminate2 ( void  )

Install SIGTERM and SIGINT handler which calls libcomcom_terminate().

Returns
0 on success and -1 on error (also sets errno).

The installed signal handler also calls old signal handler automatically.

WARNING: If before calling these handlers were set by signal() (not by sigaction()), then this function may not work (leads to undefined behavior) on some non-Unix systems.

◆ libcomcom_terminate()

int libcomcom_terminate ( void  )

Usually should be run in SIGTERM and SIGINT handlers.

Returns
0 on success and -1 on error (also sets errno).