| 1 | #include <assert.h> |
| 2 | #include <ctype.h> |
| 3 | #include <dirent.h> |
| 4 | #include <errno.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <paths.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <not-cancel.h> |
| 11 | |
| 12 | #include "pty-private.h" |
| 13 | |
| 14 | #if HAVE_PT_CHOWN |
| 15 | /* Close all file descriptors except the one specified. */ |
| 16 | static void |
| 17 | close_all_fds (void) |
| 18 | { |
| 19 | DIR *dir = __opendir ("/proc/self/fd" ); |
| 20 | if (dir != NULL) |
| 21 | { |
| 22 | struct dirent64 *d; |
| 23 | while ((d = __readdir64 (dir)) != NULL) |
| 24 | if (isdigit (d->d_name[0])) |
| 25 | { |
| 26 | char *endp; |
| 27 | long int fd = strtol (d->d_name, &endp, 10); |
| 28 | if (*endp == '\0' && fd != PTY_FILENO && fd != dirfd (dir)) |
| 29 | __close_nocancel_nostatus (fd); |
| 30 | } |
| 31 | |
| 32 | __closedir (dir); |
| 33 | |
| 34 | int nullfd = __open_nocancel (_PATH_DEVNULL, O_RDONLY); |
| 35 | assert (nullfd == STDIN_FILENO); |
| 36 | nullfd = __open_nocancel (_PATH_DEVNULL, O_WRONLY); |
| 37 | assert (nullfd == STDOUT_FILENO); |
| 38 | __dup2 (STDOUT_FILENO, STDERR_FILENO); |
| 39 | } |
| 40 | } |
| 41 | # define CLOSE_ALL_FDS() close_all_fds() |
| 42 | #endif |
| 43 | |
| 44 | #include <sysdeps/unix/grantpt.c> |
| 45 | |