2009-11-25 02:24:14 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-01-28 16:51:44 +00:00
|
|
|
#include <not-cancel.h>
|
|
|
|
|
2009-11-25 02:24:14 +00:00
|
|
|
#include "pty-private.h"
|
|
|
|
|
2013-07-19 06:42:03 +00:00
|
|
|
#if HAVE_PT_CHOWN
|
2009-11-25 02:24:14 +00:00
|
|
|
/* Close all file descriptors except the one specified. */
|
|
|
|
static void
|
|
|
|
close_all_fds (void)
|
|
|
|
{
|
2009-11-25 02:47:26 +00:00
|
|
|
DIR *dir = __opendir ("/proc/self/fd");
|
2009-11-25 02:24:14 +00:00
|
|
|
if (dir != NULL)
|
|
|
|
{
|
|
|
|
struct dirent64 *d;
|
2009-11-25 02:47:26 +00:00
|
|
|
while ((d = __readdir64 (dir)) != NULL)
|
2009-11-25 02:24:14 +00:00
|
|
|
if (isdigit (d->d_name[0]))
|
|
|
|
{
|
|
|
|
char *endp;
|
|
|
|
long int fd = strtol (d->d_name, &endp, 10);
|
|
|
|
if (*endp == '\0' && fd != PTY_FILENO && fd != dirfd (dir))
|
2017-07-03 18:22:58 +00:00
|
|
|
__close_nocancel_nostatus (fd);
|
2009-11-25 02:24:14 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 02:47:26 +00:00
|
|
|
__closedir (dir);
|
2009-11-25 02:24:14 +00:00
|
|
|
|
2017-07-03 14:11:24 +00:00
|
|
|
int nullfd = __open_nocancel (_PATH_DEVNULL, O_RDONLY);
|
2009-11-25 02:24:14 +00:00
|
|
|
assert (nullfd == STDIN_FILENO);
|
2017-07-03 14:11:24 +00:00
|
|
|
nullfd = __open_nocancel (_PATH_DEVNULL, O_WRONLY);
|
2009-11-25 02:24:14 +00:00
|
|
|
assert (nullfd == STDOUT_FILENO);
|
|
|
|
__dup2 (STDOUT_FILENO, STDERR_FILENO);
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 06:42:03 +00:00
|
|
|
# define CLOSE_ALL_FDS() close_all_fds()
|
|
|
|
#endif
|
2009-11-25 02:24:14 +00:00
|
|
|
|
|
|
|
#include <sysdeps/unix/grantpt.c>
|