+
+void external_agent_operation_start(struct agent *agent,
+ enum operation op, const char *tdbname)
+{
+ char string[1 + strlen(tdbname) + 1];
+
+ string[0] = op;
+ strcpy(string+1, tdbname);
+
+ if (write(agent->cmdfd, string, sizeof(string)) != sizeof(string))
+ err(1, "Writing to agent");
+}
+
+bool external_agent_operation_check(struct agent *agent, bool block, int *res)
+{
+ int flags = fcntl(agent->responsefd, F_GETFL);
+
+ if (block)
+ fcntl(agent->responsefd, F_SETFL, flags & ~O_NONBLOCK);
+ else
+ fcntl(agent->responsefd, F_SETFL, flags | O_NONBLOCK);
+
+ switch (read(agent->responsefd, res, sizeof(*res))) {
+ case sizeof(*res):
+ break;
+ case 0:
+ errx(1, "Agent died?");
+ default:
+ if (!block && (errno == EAGAIN || errno == EWOULDBLOCK))
+ return false;
+ err(1, "%slocking reading from agent", block ? "B" : "Non-b");
+ }
+
+ if (*res > 1)
+ errx(1, "Agent returned %u\n", *res);
+
+ return true;
+}