diff options
-rw-r--r-- | lib/tdb/test/external-agent.c | 26 | ||||
-rw-r--r-- | lib/tdb/test/external-agent.h | 1 |
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/tdb/test/external-agent.c b/lib/tdb/test/external-agent.c index 8710b47832..0aca081fc6 100644 --- a/lib/tdb/test/external-agent.c +++ b/lib/tdb/test/external-agent.c @@ -99,29 +99,29 @@ static enum agent_return do_operation(enum operation op, const char *name) struct agent { int cmdfd, responsefd; + pid_t pid; }; /* Do this before doing any tdb stuff. Return handle, or NULL. */ struct agent *prepare_external_agent(void) { - int pid, ret; + int ret; int command[2], response[2]; char name[1+PATH_MAX]; + struct agent *agent = malloc(sizeof(*agent)); if (pipe(command) != 0 || pipe(response) != 0) { fprintf(stderr, "pipe failed: %s\n", strerror(errno)); exit(1); } - pid = fork(); - if (pid < 0) { + agent->pid = fork(); + if (agent->pid < 0) { fprintf(stderr, "fork failed: %s\n", strerror(errno)); exit(1); } - if (pid != 0) { - struct agent *agent = malloc(sizeof(*agent)); - + if (agent->pid != 0) { close(command[0]); close(response[1]); agent->cmdfd = command[1]; @@ -146,6 +146,20 @@ struct agent *prepare_external_agent(void) exit(0); } +void shutdown_agent(struct agent *agent) +{ + pid_t p; + + close(agent->cmdfd); + close(agent->responsefd); + p = waitpid(agent->pid, NULL, WNOHANG); + if (p == 0) { + kill(agent->pid, SIGKILL); + } + waitpid(agent->pid, NULL, 0); + free(agent); +} + /* Ask the external agent to try to do an operation. */ enum agent_return external_agent_operation(struct agent *agent, enum operation op, diff --git a/lib/tdb/test/external-agent.h b/lib/tdb/test/external-agent.h index dffdca962f..354f5b9352 100644 --- a/lib/tdb/test/external-agent.h +++ b/lib/tdb/test/external-agent.h @@ -17,6 +17,7 @@ enum operation { /* Do this before doing any tdb stuff. Return handle, or -1. */ struct agent *prepare_external_agent(void); +void shutdown_agent(struct agent *agent); enum agent_return { SUCCESS, |