+class _PostforkProcessReadyHelper:
+ """A helper to use with _postfork_fn in RunCmd.
+
+ It makes sure a process has reached a certain state by reading from a fifo.
+
+ @ivar write_fd: The fd number to write to
+
+ """
+ def __init__(self, timeout):
+ """Initialize the helper.
+
+ @param fifo_dir: The dir where we can create the fifo
+ @param timeout: The time in seconds to wait before giving up
+
+ """
+ self.timeout = timeout
+ (self.read_fd, self.write_fd) = os.pipe()
+
+ def Ready(self, pid):
+ """Waits until the process is ready.
+
+ @param pid: The pid of the process
+
+ """
+ (read_ready, _, _) = select.select([self.read_fd], [], [], self.timeout)
+
+ if not read_ready:
+ # We hit the timeout
+ raise AssertionError("Timeout %d reached while waiting for process %d"
+ " to become ready" % (self.timeout, pid))
+
+ def Cleanup(self):
+ """Cleans up the helper.
+
+ """
+ os.close(self.read_fd)
+ os.close(self.write_fd)
+
+