The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
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: ffa15b9bb421d018734443935c09af54dacee39e $
20 *
21 * @file lib/server/exec.h
22 * @brief Asynchronous exec
23 *
24 * @copyright 2014 The FreeRADIUS server project
25 */
26RCSIDH(exec_h, "$Id: ffa15b9bb421d018734443935c09af54dacee39e $")
27
28#ifdef __cplusplus
29extern "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 <freeradius-devel/util/timer.h>
42#include <sys/types.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
53
54typedef struct {
55 fr_sbuff_t stdout_buff; //!< Expandable buffer to store process output.
56 fr_sbuff_uctx_talloc_t stdout_tctx; //!< sbuff talloc ctx data.
57
58 log_fd_event_ctx_t stdout_uctx; //!< Config for the stdout logger.
59 log_fd_event_ctx_t stderr_uctx; //!< Config for the stderr logger.
60 char stdout_prefix[sizeof("pid -9223372036854775808 (stdout)")];
61 char stderr_prefix[sizeof("pid -9223372036854775808 (stderr)")];
62
63 pid_t pid; //!< child PID
64 int stdin_fd; //!< for writing to the child.
65 bool stdin_used; //!< use stdin fd?
66 int stdout_fd; //!< for reading from the child.
67
68 bool stdout_used; //!< use stdout fd?
69 TALLOC_CTX *stdout_ctx; //!< ctx to allocate output buffers
70
71 int stderr_fd; //!< for producing error messages.
72
73 fr_timer_t *ev; //!< for timing out the child
74 fr_event_pid_t const *ev_pid; //!< for cleaning up the process
75 fr_exec_fail_t failed; //!< what kind of failure
76
77 int status; //!< return code of the program
78
79 fr_pair_list_t *env_pairs; //!< input VPs. These are inserted into
80 ///< the environment of the child as
81 ///< environmental variables.
82
83 request_t *request; //!< request this exec is related to
84
86
87/** @name Low level interface
88 *
89 * These functions are used both by fr_exec_oneshot function, and any code which
90 * needs a lower level interface for execing external programs.
91 *
92 * In the case of fr_exec_fork_wait, the caller is responsible for installing
93 * I/O handlers, cleaning up file descriptors returned, and reaping the process.
94 *
95 * @{
96 */
97int fr_exec_value_box_list_to_argv(TALLOC_CTX *ctx,
98 char ***argv_p, fr_value_box_list_t const *in);
99
100char **fr_exec_pair_to_env(request_t *request, fr_pair_list_t *env_pairs, bool env_escape);
101
103 char **argv_in, char **env_in,
104 bool env_inherit, bool debug);
105
106int fr_exec_fork_wait(pid_t *pid_p,
107 int *stdin_fd, int *stdout_fd, int *stderr_fd,
108 char **argv_in, char **env_usr,
109 bool env_inherit, bool debug);
110/** @} */
111
112/** @name High level interface
113 *
114 * These functions allow easy processing of oneshot exec calls.
115 *
116 * @{
117 */
119 fr_value_box_list_t *args, fr_pair_list_t *env_pairs,
120 bool env_escape, bool env_inherit);
121
122void fr_exec_oneshot_cleanup(fr_exec_state_t *exec, int signal);
123
124int fr_exec_oneshot(TALLOC_CTX *ctx, fr_exec_state_t *exec, request_t *request,
125 fr_value_box_list_t *args,
126 fr_pair_list_t *env_pairs, bool env_escape, bool env_inherit,
127 bool need_stdin,
128 bool store_stdout, TALLOC_CTX *stdout_ctx,
129 fr_time_delta_t timeout);
130/** @} */
131
132#ifdef __cplusplus
133}
134#endif
va_list args
Definition acutest.h:770
#define RCSIDH(h, id)
Definition build.h:486
static fr_slen_t in
Definition dict.h:831
fr_event_pid_t const * ev_pid
for cleaning up the process
Definition exec.h:74
request_t * request
request this exec is related to
Definition exec.h:83
log_fd_event_ctx_t stdout_uctx
Config for the stdout logger.
Definition exec.h:58
int stderr_fd
for producing error messages.
Definition exec.h:71
log_fd_event_ctx_t stderr_uctx
Config for the stderr logger.
Definition exec.h:59
TALLOC_CTX * stdout_ctx
ctx to allocate output buffers
Definition exec.h:69
int stdout_fd
for reading from the child.
Definition exec.h:66
int stdin_fd
for writing to the child.
Definition exec.h:64
fr_pair_list_t * env_pairs
input VPs.
Definition exec.h:79
fr_exec_fail_t
Definition exec.h:48
@ FR_EXEC_FAIL_TOO_MUCH_DATA
Definition exec.h:50
@ FR_EXEC_FAIL_TIMEOUT
Definition exec.h:51
@ FR_EXEC_FAIL_NONE
Definition exec.h:49
fr_sbuff_t stdout_buff
Expandable buffer to store process output.
Definition exec.h:55
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:63
bool stdout_used
use stdout fd?
Definition exec.h:68
fr_sbuff_uctx_talloc_t stdout_tctx
sbuff talloc ctx data.
Definition exec.h:56
int status
return code of the program
Definition exec.h:77
bool stdin_used
use stdin fd?
Definition exec.h:65
fr_exec_fail_t failed
what kind of failure
Definition exec.h:75
fr_timer_t * ev
for timing out the child
Definition exec.h:73
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
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
Context structure for the log fd event function.
Definition log.h:83
Stores all information relating to an event list.
Definition event.c:380
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:139
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
A timer event.
Definition timer.c:79
static fr_event_list_t * el