The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
exec.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 7f5335e8e504121f52f0f1d4909d63700993ebb8 $
20  *
21  * @file lib/server/exec.h
22  * @brief Asynchronous exec
23  *
24  * @copyright 2014 The FreeRADIUS server project
25  */
26 RCSIDH(exec_h, "$Id: 7f5335e8e504121f52f0f1d4909d63700993ebb8 $")
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define EXEC_TIMEOUT 10 //!< Default wait time for exec calls (in seconds).
33 
34 #ifdef __cplusplus
35 }
36 #endif
37 
38 #include <freeradius-devel/server/request.h>
39 #include <freeradius-devel/util/pair.h>
40 #include <freeradius-devel/util/talloc.h>
41 #include <sys/types.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 typedef enum {
52 
53 typedef struct {
54  fr_sbuff_t stdout_buff; //!< Expandable buffer to store process output.
55  fr_sbuff_uctx_talloc_t stdout_tctx; //!< sbuff talloc ctx data.
56 
57  log_fd_event_ctx_t stdout_uctx; //!< Config for the stdout logger.
58  log_fd_event_ctx_t stderr_uctx; //!< Config for the stderr logger.
59  char stdout_prefix[sizeof("pid -9223372036854775808 (stdout)")];
60  char stderr_prefix[sizeof("pid -9223372036854775808 (stderr)")];
61 
62  pid_t pid; //!< child PID
63  int stdin_fd; //!< for writing to the child.
64  bool stdin_used; //!< use stdin fd?
65  int stdout_fd; //!< for reading from the child.
66 
67  bool stdout_used; //!< use stdout fd?
68  TALLOC_CTX *stdout_ctx; //!< ctx to allocate output buffers
69 
70  int stderr_fd; //!< for producing error messages.
71 
72  fr_event_timer_t const *ev; //!< for timing out the child
73  fr_event_pid_t const *ev_pid; //!< for cleaning up the process
74  fr_exec_fail_t failed; //!< what kind of failure
75 
76  int status; //!< return code of the program
77 
78  fr_pair_list_t *env_pairs; //!< input VPs. These are inserted into
79  ///< the environment of the child as
80  ///< environmental variables.
81 
82  request_t *request; //!< request this exec is related to
83 
85 
86 /** @name Low level interface
87  *
88  * These functions are used both by fr_exec_oneshot function, and any code which
89  * needs a lower level interface for execing external programs.
90  *
91  * In the case of fr_exec_fork_wait, the caller is responsible for installing
92  * I/O handlers, cleaning up file descriptors returned, and reaping the process.
93  *
94  * @{
95  */
96 int fr_exec_value_box_list_to_argv(TALLOC_CTX *ctx,
97  char ***argv_p, fr_value_box_list_t const *in);
98 
99 char **fr_exec_pair_to_env(request_t *request, fr_pair_list_t *env_pairs, bool env_escape);
100 
102  char **argv_in, char **env_in,
103  bool env_inherit, bool debug);
104 
105 int fr_exec_fork_wait(pid_t *pid_p,
106  int *stdin_fd, int *stdout_fd, int *stderr_fd,
107  char **argv_in, char **env_usr,
108  bool env_inherit, bool debug);
109 /** @} */
110 
111 /** @name High level interface
112  *
113  * These functions allow easy processing of oneshot exec calls.
114  *
115  * @{
116  */
117 int fr_exec_oneshot_nowait(request_t *request,
118  fr_value_box_list_t *args, fr_pair_list_t *env_pairs,
119  bool env_escape, bool env_inherit);
120 
121 void fr_exec_oneshot_cleanup(fr_exec_state_t *exec, int signal);
122 
123 int fr_exec_oneshot(TALLOC_CTX *ctx, fr_exec_state_t *exec, request_t *request,
124  fr_value_box_list_t *args,
125  fr_pair_list_t *env_pairs, bool env_escape, bool env_inherit,
126  bool need_stdin,
127  bool store_stdout, TALLOC_CTX *stdout_ctx,
129 /** @} */
130 
131 #ifdef __cplusplus
132 }
133 #endif
va_list args
Definition: acutest.h:770
#define RCSIDH(h, id)
Definition: build.h:482
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
static fr_slen_t in
Definition: dict.h:821
fr_event_pid_t const * ev_pid
for cleaning up the process
Definition: exec.h:73
request_t * request
request this exec is related to
Definition: exec.h:82
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.
Definition: exec.c:258
log_fd_event_ctx_t stdout_uctx
Config for the stdout logger.
Definition: exec.h:57
int stderr_fd
for producing error messages.
Definition: exec.h:70
log_fd_event_ctx_t stderr_uctx
Config for the stderr logger.
Definition: exec.h:58
TALLOC_CTX * stdout_ctx
ctx to allocate output buffers
Definition: exec.h:68
int stdout_fd
for reading from the child.
Definition: exec.h:65
int stdin_fd
for writing to the child.
Definition: exec.h:63
fr_pair_list_t * env_pairs
input VPs.
Definition: exec.h:78
fr_exec_fail_t
Definition: exec.h:47
@ FR_EXEC_FAIL_TOO_MUCH_DATA
Definition: exec.h:49
@ FR_EXEC_FAIL_TIMEOUT
Definition: exec.h:50
@ FR_EXEC_FAIL_NONE
Definition: exec.h:48
fr_sbuff_t stdout_buff
Expandable buffer to store process output.
Definition: exec.h:54
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.
Definition: exec.c:984
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.
Definition: exec.c:623
pid_t pid
child PID
Definition: exec.h:62
bool stdout_used
use stdout fd?
Definition: exec.h:67
fr_sbuff_uctx_talloc_t stdout_tctx
sbuff talloc ctx data.
Definition: exec.h:55
int status
return code of the program
Definition: exec.h:76
bool stdin_used
use stdin fd?
Definition: exec.h:64
fr_exec_fail_t failed
what kind of failure
Definition: exec.h:74
fr_event_timer_t const * ev
for timing out the child
Definition: exec.h:72
int fr_exec_fork_wait(pid_t *pid_p, int *stdin_fd, int *stdout_fd, int *stderr_fd, char **argv_in, char **env_usr, bool env_inherit, bool debug)
Execute a program assuming that the caller waits for it to finish.
Definition: exec.c:528
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.
Definition: exec.c:461
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.
Definition: exec.c:50
void fr_exec_oneshot_cleanup(fr_exec_state_t *exec, int signal)
Cleans up an exec'd process on error.
Definition: exec.c:666
Context structure for the log fd event function.
Definition: log.h:83
Stores all information relating to an event list.
Definition: event.c:411
A timer event.
Definition: event.c:102
static int stdout_fd
The original unmolested stdout file descriptor.
Definition: log.c:51
static fr_log_fd_event_ctx_t stdout_ctx
Logging ctx for stdout.
Definition: log.c:53
static int stderr_fd
The original unmolested stderr file descriptor.
Definition: log.c:50
Talloc sbuff extension structure.
Definition: sbuff.h:114
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
static fr_event_list_t * el