def attachio(self): """Attach to client's stdio passed via unix domain socket; all channels except cresult will no longer be used """ # tell client to sendmsg() with 1-byte payload, which makes it # distinctive from "attachio\n" command consumed by client.read() self.clientsock.sendall(struct.pack('>cI', 'I', 1)) clientfds = osutil.recvfds(self.clientsock.fileno()) _log('received fds: %r\n' % clientfds) ui = self.ui ui.flush() first = self._saveio() for fd, (cn, fn, mode) in zip(clientfds, _iochannels): assert fd > 0 fp = getattr(ui, fn) os.dup2(fd, fp.fileno()) os.close(fd) if not first: continue # reset buffering mode when client is first attached. as we want # to see output immediately on pager, the mode stays unchanged # when client re-attached. ferr is unchanged because it should # be unbuffered no matter if it is a tty or not. if fn == 'ferr': newfp = fp else: # make it line buffered explicitly because the default is # decided on first write(), where fout could be a pager. if fp.isatty(): bufsize = 1 # line buffered else: bufsize = -1 # system default newfp = os.fdopen(fp.fileno(), mode, bufsize) setattr(ui, fn, newfp) setattr(self, cn, newfp) self.cresult.write(struct.pack('>i', len(clientfds)))