tdb/test: add shutdown_agent() helper function
authorVolker Lendecke <vl@samba.org>
Thu, 21 Feb 2013 15:34:32 +0000 (16:34 +0100)
committerJeremy Allison <jra@samba.org>
Thu, 22 May 2014 19:05:15 +0000 (21:05 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/tdb/test/external-agent.c
lib/tdb/test/external-agent.h

index 8710b478320782a640d462af931bf7e141daa001..0aca081fc698959fce5fdadb8c966a80d08f893b 100644 (file)
@@ -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,
index dffdca962f6ff962af3d392ea44550aad7f4e6fc..354f5b9352ba99261499eea2d4a6fe8d33a110ba 100644 (file)
@@ -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,