fork() -> (pid, master_fd) Fork and make the child a session leader with a controlling terminal.
()
| 59 | |
| 60 | |
| 61 | def fork(): |
| 62 | """fork() -> (pid, master_fd) |
| 63 | Fork and make the child a session leader with a controlling terminal.""" |
| 64 | |
| 65 | try: |
| 66 | pid, fd = os.forkpty() |
| 67 | except (AttributeError, OSError): |
| 68 | pass |
| 69 | else: |
| 70 | if pid == CHILD: |
| 71 | try: |
| 72 | os.setsid() |
| 73 | except OSError: |
| 74 | # os.forkpty() already set us session leader |
| 75 | pass |
| 76 | return pid, fd |
| 77 | |
| 78 | master_fd, slave_fd = openpty() |
| 79 | pid = os.fork() |
| 80 | if pid == CHILD: |
| 81 | os.close(master_fd) |
| 82 | os.login_tty(slave_fd) |
| 83 | else: |
| 84 | os.close(slave_fd) |
| 85 | |
| 86 | # Parent and child process. |
| 87 | return pid, master_fd |
| 88 | |
| 89 | def _read(fd): |
| 90 | """Default read function.""" |