Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / grantpt.c
blob8cebde36ed0b3484b3a335d7121c5e125f45478f
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>
10 #include <not-cancel.h>
12 #include "pty-private.h"
14 #if HAVE_PT_CHOWN
15 /* Close all file descriptors except the one specified. */
16 static void
17 close_all_fds (void)
19 DIR *dir = __opendir ("/proc/self/fd");
20 if (dir != NULL)
22 struct dirent64 *d;
23 while ((d = __readdir64 (dir)) != NULL)
24 if (isdigit (d->d_name[0]))
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_not_cancel_no_status (fd);
32 __closedir (dir);
34 int nullfd = open_not_cancel_2 (_PATH_DEVNULL, O_RDONLY);
35 assert (nullfd == STDIN_FILENO);
36 nullfd = open_not_cancel_2 (_PATH_DEVNULL, O_WRONLY);
37 assert (nullfd == STDOUT_FILENO);
38 __dup2 (STDOUT_FILENO, STDERR_FILENO);
41 # define CLOSE_ALL_FDS() close_all_fds()
42 #endif
44 #include <sysdeps/unix/grantpt.c>