The FreeRADIUS server
$Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
Execute external programs. More...
#include <stdint.h>
#include <freeradius-devel/server/log.h>
#include <freeradius-devel/server/exec.h>
#include <freeradius-devel/server/exec_priv.h>
#include <freeradius-devel/server/request.h>
#include <freeradius-devel/server/util.h>
#include <freeradius-devel/util/debug.h>
Go to the source code of this file.
Macros | |
#define | MAX_ENVP 1024 |
Functions | |
static char ** | exec_build_env (char **env_in, bool env_inherit) |
Merge extra environmental variables and potentially the inherited environment. More... | |
static NEVER_RETURNS void | exec_child (char **argv, char **envp, bool exec_wait, bool debug, int stdin_pipe[static 2], int stdout_pipe[static 2], int stderr_pipe[static 2]) |
Start a child process. More... | |
static void | exec_debug (request_t *request, char **argv_in, char **env_in, bool env_inherit) |
Print debug information showing the arguments and environment for a process. More... | |
static int | exec_pair_to_env (char **env_p, size_t env_len, fr_sbuff_t *env_sbuff, fr_sbuff_marker_t env_m[], request_t *request, fr_pair_list_t *env_pairs, bool env_escape) |
Convert pairs from a request and a list of pairs into environmental variables. More... | |
static void | exec_reap (fr_event_list_t *el, pid_t pid, int status, void *uctx) |
static void | exec_stdout_read (UNUSED fr_event_list_t *el, int fd, int flags, void *uctx) |
static void | exec_timeout (UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx) |
int | fr_exec_fork_nowait (fr_event_list_t *el, char **argv_in, char **env_in, bool env_inherit, bool debug) |
Execute a program without waiting for the program to finish. More... | |
int | fr_exec_fork_wait (pid_t *pid_p, int *stdin_fd, int *stdout_fd, int *stderr_fd, char **argv_in, char **env_in, bool env_inherit, bool debug) |
Execute a program assuming that the caller waits for it to finish. More... | |
int | fr_exec_oneshot (TALLOC_CTX *ctx, fr_exec_state_t *exec, request_t *request, fr_value_box_list_t *args, fr_pair_list_t *env_pairs, bool env_escape, bool env_inherit, bool need_stdin, bool store_stdout, TALLOC_CTX *stdout_ctx, fr_time_delta_t timeout) |
Call an child program, optionally reading it's output. More... | |
void | fr_exec_oneshot_cleanup (fr_exec_state_t *exec, int signal) |
Cleans up an exec'd process on error. More... | |
int | fr_exec_oneshot_nowait (request_t *request, fr_value_box_list_t *args, fr_pair_list_t *env_pairs, bool env_escape, bool env_inherit) |
Similar to fr_exec_oneshot, but does not attempt to parse output. More... | |
char ** | fr_exec_pair_to_env (request_t *request, fr_pair_list_t *env_pairs, bool env_escape) |
Convert env_pairs into an array of environmental variables using thread local buffers. More... | |
int | fr_exec_value_box_list_to_argv (TALLOC_CTX *ctx, char ***argv_p, fr_value_box_list_t const *in) |
Flatten a list into individual "char *" argv-style array. More... | |
Variables | |
static _Thread_local char * | env_exec_arr [MAX_ENVP] |
Execute external programs.
Definition in file exec.c.
|
static |
|
static |
Start a child process.
We try to be fail-safe here. So if ANYTHING goes wrong, we exit with status 1.
[in] | argv | array of arguments to pass to child. |
[in] | envp | array of environment variables in form <attr>=<val> |
[in] | exec_wait | if true, redirect child process' stdin, stdout, stderr to the pipes provided, redirecting any to /dev/null where no pipe was provided. If false redirect stdin, and stdout to /dev/null. |
[in] | debug | If true, and !exec_wait, don't molest stderr. |
[in] | stdin_pipe | the pipe used to write data to the process. STDIN will be set to stdin_pipe[0], stdin_pipe[1] will be closed. |
[in] | stdout_pipe | the pipe used to read data from the process. STDOUT will be set to stdout_pipe[1], stdout_pipe[0] will be closed. |
[in] | stderr_pipe | the pipe used to read error text from the process. STDERR will be set to stderr_pipe[1], stderr_pip[0] will be closed. |
Definition at line 291 of file exec.c.
|
inlinestatic |
Print debug information showing the arguments and environment for a process.
[in] | request | The current request, may be NULL. |
[in] | argv_in | arguments to pass to process. |
[in] | env_in | environment to pass to process. |
[in] | env_inherit | print debug for the environment from the environment. |
Definition at line 95 of file exec.c.
|
inlinestatic |
Convert pairs from a request and a list of pairs into environmental variables.
[out] | env_p | Where to write an array of \0 terminated strings. |
[in] | env_len | Length of env_p. |
[out] | env_sbuff | To write environmental variables too. Each variable will be written to the buffer, and separated with a '\0'. |
[in] | env_m | an array of markers of the same length as env_len. |
[in] | request | Will look for &control.Exec-Export items to convert to env vars. |
[in] | env_pairs | Other items to convert to environmental variables. The dictionary attribute name will be converted to uppercase, and all '-' converted to '_' and will form the variable name. |
[in] | env_escape | Wrap string values in double quotes, and apply doublequote escaping to all environmental variable values. |
Definition at line 125 of file exec.c.
|
static |
|
static |
|
static |
int fr_exec_fork_nowait | ( | fr_event_list_t * | el, |
char ** | argv_in, | ||
char ** | env_in, | ||
bool | env_inherit, | ||
bool | debug | ||
) |
Execute a program without waiting for the program to finish.
[in] | el | event list to insert reaper child into. |
[in] | argv_in | arg[0] is the path to the program, arg[...] are arguments to pass to the program. |
[in] | env_in | any additional environmental variables to pass to the program. |
[in] | env_inherit | Inherit the environment from the current process. This will be merged with any variables from env_pairs. |
[in] | debug | If true, STDERR will be left open and pointing to the stderr descriptor of the parent. |
Definition at line 461 of file exec.c.
int fr_exec_fork_wait | ( | pid_t * | pid_p, |
int * | stdin_fd, | ||
int * | stdout_fd, | ||
int * | stderr_fd, | ||
char ** | argv_in, | ||
char ** | env_in, | ||
bool | env_inherit, | ||
bool | debug | ||
) |
Execute a program assuming that the caller waits for it to finish.
The caller takes responsibility for calling waitpid() on the returned PID.
The caller takes responsibility for reading from the returned FD, and closing it.
[out] | pid_p | The PID of the child |
[out] | stdin_fd | The stdin FD of the child. |
[out] | stdout_fd | The stdout FD of the child. |
[out] | stderr_fd | The stderr FD of the child. |
[in] | argv_in | arg[0] is the path to the program, arg[...] are arguments to pass to the program. |
[in] | env_in | Environmental variables to pass to the program. |
[in] | env_inherit | Inherit the environment from the current process. This will be merged with any variables from env_pairs. |
[in] | debug | If true, STDERR will be left open and pointing to the stderr descriptor of the parent, if no stderr_fd pointer is provided. |
Definition at line 528 of file exec.c.
int fr_exec_oneshot | ( | TALLOC_CTX * | ctx, |
fr_exec_state_t * | exec, | ||
request_t * | request, | ||
fr_value_box_list_t * | args, | ||
fr_pair_list_t * | env_pairs, | ||
bool | env_escape, | ||
bool | env_inherit, | ||
bool | need_stdin, | ||
bool | store_stdout, | ||
TALLOC_CTX * | stdout_ctx, | ||
fr_time_delta_t | timeout | ||
) |
Call an child program, optionally reading it's output.
[in] | ctx | to allocate events in. |
[in,out] | exec | structure holding the state of the external call. |
[in] | request | currently being processed, may be NULL. |
[in] | args | to call as a fr_value_box_list_t. Program will be the first box and arguments in the subsequent boxes. |
[in] | env_pairs | list of pairs to be presented as environment variables to the child. |
[in] | env_escape | Wrap string values in double quotes, and apply doublequote escaping to all environmental variable values. |
[in] | env_inherit | Inherit the environment from the current process. This will be merged with any variables from env_pairs. |
[in] | need_stdin | If true, allocate a pipe that will allow us to send data to the process. |
[in] | store_stdout | if true keep a copy of stdout in addition to logging it if RDEBUG_ENABLED2. |
[in] | stdout_ctx | ctx to alloc stdout data in. |
[in] | timeout | to wait for child to complete. |
Definition at line 984 of file exec.c.
void fr_exec_oneshot_cleanup | ( | fr_exec_state_t * | exec, |
int | signal | ||
) |
Cleans up an exec'd process on error.
This function is intended to be called at any point after a successful fr_exec_oneshot call in order to release resources and cleanup zombie processes.
[in] | exec | state to cleanup. |
[in] | signal | If non-zero, and we think the process is still running, send it a signal to cause it to exit. The PID reaper we insert here will cleanup its state so it doesn't become a zombie. |
Definition at line 666 of file exec.c.
int fr_exec_oneshot_nowait | ( | request_t * | request, |
fr_value_box_list_t * | args, | ||
fr_pair_list_t * | env_pairs, | ||
bool | env_escape, | ||
bool | env_inherit | ||
) |
Similar to fr_exec_oneshot, but does not attempt to parse output.
[in] | request | currently being processed, may be NULL. |
[in] | args | to call as a fr_value_box_list_t. Program will be the first box and arguments in the subsequent boxes. |
[in] | env_pairs | list of pairs to be presented as environment variables to the child. |
[in] | env_escape | Wrap string values in double quotes, and apply doublequote escaping to all environmental variable values. |
[in] | env_inherit | Inherit the environment from the current process. This will be merged with any variables from env_pairs. |
Definition at line 623 of file exec.c.
char** fr_exec_pair_to_env | ( | request_t * | request, |
fr_pair_list_t * | env_pairs, | ||
bool | env_escape | ||
) |
Convert env_pairs into an array of environmental variables using thread local buffers.
[in] | request | Will be searched for control.Exec-Export pairs. |
[in] | env_pairs | env_pairs to put into into the environment. May be NULL. |
[in] | env_escape | Wrap string values in double quotes, and apply doublequote escaping to all environmental variable values. |
Definition at line 258 of file exec.c.
int fr_exec_value_box_list_to_argv | ( | TALLOC_CTX * | ctx, |
char *** | argv_p, | ||
fr_value_box_list_t const * | in | ||
) |
Flatten a list into individual "char *" argv-style array.
[in] | ctx | to allocate boxes in. |
[out] | argv_p | where output strings go |
[in] | in | boxes to flatten |
Definition at line 50 of file exec.c.