def fork(self): if self.forked: return self.forked = True master, slave = os.openpty() # Note that master and slave are in blocking mode remove_cloexec(slave) fast_data_types.set_iutf8(master, True) stdin, self.stdin = self.stdin, None if stdin is not None: stdin_read_fd, stdin_write_fd = os.pipe() remove_cloexec(stdin_read_fd) else: stdin_read_fd = stdin_write_fd = -1 env = os.environ.copy() env.update(self.env) env['TERM'] = self.opts.term env['COLORTERM'] = 'truecolor' if os.path.isdir(terminfo_dir): env['TERMINFO'] = terminfo_dir env = tuple('{}={}'.format(k, v) for k, v in env.items()) argv = list(self.argv) exe = argv[0] if is_macos and exe == shell_path: # Some macOS machines need the shell to have argv[0] prefixed by # hyphen, see https://github.com/kovidgoyal/kitty/issues/247 argv[0] = ('-' + exe.split('/')[-1]) pid = fast_data_types.spawn(exe, self.cwd, tuple(argv), env, master, slave, stdin_read_fd, stdin_write_fd) os.close(slave) self.pid = pid self.child_fd = master if stdin is not None: os.close(stdin_read_fd) fast_data_types.thread_write(stdin_write_fd, stdin) return pid
def fork(self): if self.forked: return self.forked = True master, slave = os.openpty( ) # Note that master and slave are in blocking mode remove_cloexec(slave) fast_data_types.set_iutf8(master, True) stdin, self.stdin = self.stdin, None if stdin is not None: stdin_read_fd, stdin_write_fd = os.pipe() remove_cloexec(stdin_read_fd) else: stdin_read_fd = stdin_write_fd = -1 env = os.environ.copy() env.update(self.env) env['TERM'] = self.opts.term env['COLORTERM'] = 'truecolor' if os.path.isdir(terminfo_dir): env['TERMINFO'] = terminfo_dir env = tuple('{}={}'.format(k, v) for k, v in env.items()) pid = fast_data_types.spawn(self.cwd, tuple(self.argv), env, master, slave, stdin_read_fd, stdin_write_fd) os.close(slave) self.pid = pid self.child_fd = master if stdin is not None: os.close(stdin_read_fd) fast_data_types.thread_write(stdin_write_fd, stdin) return pid
def fork(self): if self.forked: return self.forked = True master, slave = os.openpty() # Note that master and slave are in blocking mode remove_cloexec(slave) fast_data_types.set_iutf8(master, True) stdin, self.stdin = self.stdin, None if stdin is not None: stdin_read_fd, stdin_write_fd = os.pipe() remove_cloexec(stdin_read_fd) pid = os.fork() if pid == 0: # child try: os.chdir(self.cwd) except EnvironmentError: os.chdir('/') os.setsid() for i in range(3): if stdin is not None and i == 0: os.dup2(stdin_read_fd, i) os.close(stdin_read_fd), os.close(stdin_write_fd) else: os.dup2(slave, i) os.close(slave), os.close(master) os.closerange(3, 200) # Establish the controlling terminal (see man 7 credentials) os.close(os.open(os.ttyname(1), os.O_RDWR)) os.environ['TERM'] = self.opts.term os.environ['COLORTERM'] = 'truecolor' if os.path.isdir(terminfo_dir): os.environ['TERMINFO'] = terminfo_dir try: os.execvp(self.argv[0], self.argv) except Exception as err: # Report he failure and exec a shell instead so that # we are not left with a forked but not execed process print('Could not launch:', self.argv[0]) print('\t', err) print('\nPress Enter to exit:', end=' ') sys.stdout.flush() os.execvp('/bin/sh', ['/bin/sh', '-c', 'read w']) else: # master os.close(slave) self.pid = pid self.child_fd = master if stdin is not None: os.close(stdin_read_fd) fast_data_types.thread_write(stdin_write_fd, stdin) return pid
def fork(self): if self.forked: return self.forked = True master, slave = os.openpty( ) # Note that master and slave are in blocking mode remove_cloexec(slave) fast_data_types.set_iutf8(master, True) stdin, self.stdin = self.stdin, None ready_read_fd, ready_write_fd = os.pipe() remove_cloexec(ready_read_fd) if stdin is not None: stdin_read_fd, stdin_write_fd = os.pipe() remove_cloexec(stdin_read_fd) else: stdin_read_fd = stdin_write_fd = -1 env = default_env().copy() env.update(self.env) env['TERM'] = self.opts.term env['COLORTERM'] = 'truecolor' if self.cwd: # needed incase cwd is a symlink, in which case shells # can use it to display the current directory name rather # than the resolved path env['PWD'] = self.cwd if os.path.isdir(terminfo_dir): env['TERMINFO'] = terminfo_dir env = tuple('{}={}'.format(k, v) for k, v in env.items()) argv = list(self.argv) exe = argv[0] if is_macos and exe == shell_path: # Some macOS machines need the shell to have argv[0] prefixed by # hyphen, see https://github.com/kovidgoyal/kitty/issues/247 argv[0] = ('-' + exe.split('/')[-1]) pid = fast_data_types.spawn(exe, self.cwd, tuple(argv), env, master, slave, stdin_read_fd, stdin_write_fd, ready_read_fd, ready_write_fd) os.close(slave) self.pid = pid self.child_fd = master if stdin is not None: os.close(stdin_read_fd) fast_data_types.thread_write(stdin_write_fd, stdin) os.close(ready_read_fd) self.terminal_ready_fd = ready_write_fd fcntl.fcntl(self.child_fd, fcntl.F_SETFL, fcntl.fcntl(self.child_fd, fcntl.F_GETFL) | os.O_NONBLOCK) return pid
def fork(self): if self.forked: return self.forked = True master, slave = os.openpty( ) # Note that master and slave are in blocking mode remove_cloexec(slave) fast_data_types.set_iutf8(master, True) stdin, self.stdin = self.stdin, None ready_read_fd, ready_write_fd = os.pipe() remove_cloexec(ready_read_fd) if stdin is not None: stdin_read_fd, stdin_write_fd = os.pipe() remove_cloexec(stdin_read_fd) else: stdin_read_fd = stdin_write_fd = -1 env = self.final_env env = tuple('{}={}'.format(k, v) for k, v in env.items()) argv = list(self.argv) exe = argv[0] if is_macos and exe == shell_path: # Some macOS machines need the shell to have argv[0] prefixed by # hyphen, see https://github.com/kovidgoyal/kitty/issues/247 argv[0] = ('-' + exe.split('/')[-1]) pid = fast_data_types.spawn(exe, self.cwd, tuple(argv), env, master, slave, stdin_read_fd, stdin_write_fd, ready_read_fd, ready_write_fd) os.close(slave) self.pid = pid self.child_fd = master if stdin is not None: os.close(stdin_read_fd) fast_data_types.thread_write(stdin_write_fd, stdin) os.close(ready_read_fd) self.terminal_ready_fd = ready_write_fd fcntl.fcntl(self.child_fd, fcntl.F_SETFL, fcntl.fcntl(self.child_fd, fcntl.F_GETFL) | os.O_NONBLOCK) return pid