Skip to content

Commit

Permalink
add input_fd functionality to async exec-wait
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Apr 22, 2020
1 parent a6aeba2 commit 9b6ff33
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
40 changes: 30 additions & 10 deletions src/lib/server/exec.c
Expand Up @@ -757,7 +757,8 @@ int fr_exec_nowait(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs)
* @param vb as returned by xlat_frame_eval()
* @param env_pairs VPs to put into into the environment. May be NULL.
* @param[out] pid_p The PID of the child
* @param[out] fd_p The stdout FD of the child
* @param[out] input_fd The stdin FD of the child
* @param[out] output_fd The stdout FD of the child
* @return
* - <0 on error
* - 0 on success
Expand All @@ -766,13 +767,14 @@ int fr_exec_nowait(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs)
* would allow finer-grained control over the attributes to put into
* the environment.
*/
int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs, pid_t *pid_p, int *fd_p)
int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs, pid_t *pid_p, int *input_fd, int *output_fd)
{
int argc;
char **envp;
char **argv;
pid_t pid;
fr_value_box_t *first;
int to_child[2] = {-1, -1};
int from_child[2] = {-1, -1};

/*
Expand Down Expand Up @@ -810,8 +812,19 @@ int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pai
for (i = 0; i < argc; i++) RDEBUG3("arg[%d] %s", i, argv[i]);
}

if (fd_p) {
if (input_fd) {
if (pipe(to_child) < 0) {
fr_strerror_printf("Failed opening pipe to read to child - %s", fr_strerror());
return -1;
}
}

if (output_fd) {
if (pipe(from_child) < 0) {
if (input_fd) {
close(to_child[0]);
close(to_child[1]);
}
fr_strerror_printf("Failed opening pipe to read from child - %s", fr_strerror());
return -1;
}
Expand All @@ -823,9 +836,7 @@ int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pai
* The child never returns from calling fr_exec_child();
*/
if (pid == 0) {
int unused[2] = {-1, -1};

fr_exec_child(request, argv, envp, true, NULL, fd_p, unused, from_child);
fr_exec_child(request, argv, envp, true, input_fd, output_fd, to_child, from_child);
}

/*
Expand All @@ -835,7 +846,11 @@ int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pai

if (pid < 0) {
ERROR("Couldn't fork %s: %s", argv[0], fr_syserror(errno));
if (fd_p) {
if (input_fd) {
close(to_child[0]);
close(to_child[1]);
}
if (output_fd) {
close(from_child[0]);
close(from_child[1]);
}
Expand All @@ -849,9 +864,14 @@ int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pai
*/
*pid_p = pid;

if (fd_p) {
*fd_p = from_child[0];
fr_nonblock(*fd_p);
if (input_fd) {
*input_fd = to_child[1];
close(from_child[0]);
}

if (output_fd) {
*output_fd = from_child[0];
fr_nonblock(*output_fd);
close(from_child[1]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/exec.h
Expand Up @@ -61,7 +61,7 @@ int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, VALUE_PAIR **

int fr_exec_nowait(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs);

int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs, pid_t *pid_p, int *fd_p);
int fr_exec_wait_start(REQUEST *request, fr_value_box_t *vb, VALUE_PAIR *env_pairs, pid_t *pid_p, int *input_fd, int *output_fd);

void fr_exec_waitpid(pid_t pid);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/unlang/tmpl.c
Expand Up @@ -341,7 +341,7 @@ static unlang_action_t unlang_tmpl_exec_wait_resume(REQUEST *request, rlm_rcode_
state->fd = -1;
if (state->out) fd_p = &state->fd;

if (fr_exec_wait_start(request, state->box, NULL, &pid, fd_p) < 0) {
if (fr_exec_wait_start(request, state->box, NULL, &pid, NULL, fd_p) < 0) {
REDEBUG("Failed executing program - %s", fr_strerror());
fail:
*presult = RLM_MODULE_FAIL;
Expand Down

0 comments on commit 9b6ff33

Please sign in to comment.