Commit 450a9a4c authored by Rusty Russell's avatar Rusty Russell

Don't rely on terrible dup2 trick, use proper infrastructure for external agent.

parent 91ddc0b7
...@@ -51,9 +51,12 @@ maybe_alarmed: ...@@ -51,9 +51,12 @@ maybe_alarmed:
return -3; return -3;
} }
struct agent {
int cmdfd, responsefd;
};
/* Do this before doing any tdb stuff. Return handle, or -1. */ /* Do this before doing any tdb stuff. Return handle, or NULL. */
int prepare_external_agent(void) struct agent *prepare_external_agent(void)
{ {
int pid; int pid;
int command[2], response[2]; int command[2], response[2];
...@@ -61,18 +64,20 @@ int prepare_external_agent(void) ...@@ -61,18 +64,20 @@ int prepare_external_agent(void)
char name[PATH_MAX]; char name[PATH_MAX];
if (pipe(command) != 0 || pipe(response) != 0) if (pipe(command) != 0 || pipe(response) != 0)
return -1; return NULL;
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
return -1; return NULL;
if (pid != 0) { if (pid != 0) {
struct agent *agent = malloc(sizeof(*agent));
close(command[0]); close(command[0]);
close(response[1]); close(response[1]);
/* FIXME: Make fds consective. */ agent->cmdfd = command[1];
dup2(command[1]+1, response[1]); agent->responsefd = response[0];
return command[1]; return agent;
} }
close(command[1]); close(command[1]);
...@@ -89,15 +94,15 @@ int prepare_external_agent(void) ...@@ -89,15 +94,15 @@ int prepare_external_agent(void)
} }
/* Ask the external agent to try to do a transaction. */ /* Ask the external agent to try to do a transaction. */
bool external_agent_transaction(int handle, const char *tdbname) bool external_agent_transaction(struct agent *agent, const char *tdbname)
{ {
int res; int res;
if (write(handle, tdbname, strlen(tdbname)+1) if (write(agent->cmdfd, tdbname, strlen(tdbname)+1)
!= strlen(tdbname)+1) != strlen(tdbname)+1)
err(1, "Writing to agent"); err(1, "Writing to agent");
if (read(handle+1, &res, sizeof(res)) != sizeof(res)) if (read(agent->responsefd, &res, sizeof(res)) != sizeof(res))
err(1, "Reading from agent"); err(1, "Reading from agent");
if (res > 1) if (res > 1)
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
#include <stdbool.h> #include <stdbool.h>
/* Do this before doing any tdb stuff. Return handle, or -1. */ /* Do this before doing any tdb stuff. Return handle, or -1. */
int prepare_external_agent(void); struct agent *prepare_external_agent(void);
/* Ask the external agent to try to do a transaction. */ /* Ask the external agent to try to do a transaction. */
bool external_agent_transaction(int handle, const char *tdbname); bool external_agent_transaction(struct agent *handle, const char *tdbname);
#endif /* TDB_TEST_EXTERNAL_TRANSACTION_H */ #endif /* TDB_TEST_EXTERNAL_TRANSACTION_H */
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <err.h> #include <err.h>
#include "external-transaction.h" #include "external-transaction.h"
static int agent; static struct agent *agent;
static bool correct_key(TDB_DATA key) static bool correct_key(TDB_DATA key)
{ {
...@@ -56,7 +56,7 @@ int main(int argc, char *argv[]) ...@@ -56,7 +56,7 @@ int main(int argc, char *argv[])
plan_tests(15); plan_tests(15);
agent = prepare_external_agent(); agent = prepare_external_agent();
if (agent < 0) if (!agent)
err(1, "preparing agent"); err(1, "preparing agent");
tdb = tdb_open("/tmp/test3.tdb", 1024, TDB_CLEAR_IF_FIRST, tdb = tdb_open("/tmp/test3.tdb", 1024, TDB_CLEAR_IF_FIRST,
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <err.h> #include <err.h>
#include "external-transaction.h" #include "external-transaction.h"
static int agent; static struct agent *agent;
static bool correct_key(TDB_DATA key) static bool correct_key(TDB_DATA key)
{ {
...@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) ...@@ -43,7 +43,7 @@ int main(int argc, char *argv[])
plan_tests(13); plan_tests(13);
agent = prepare_external_agent(); agent = prepare_external_agent();
if (agent < 0) if (!agent)
err(1, "preparing agent"); err(1, "preparing agent");
tdb = tdb_open("/tmp/test2.tdb", 1024, TDB_CLEAR_IF_FIRST, tdb = tdb_open("/tmp/test2.tdb", 1024, TDB_CLEAR_IF_FIRST,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment