>From 00f65eed60d4e805bb03bf638289d45409b93f60 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Sat, 1 Nov 2014 23:20:27 +0100 Subject: [PATCH 2/2] Split off login_tty form forkpty --- include/utmp.h | 2 ++ src/misc/forkpty.c | 10 ++-------- src/misc/login_tty.c | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 src/misc/login_tty.c diff --git a/include/utmp.h b/include/utmp.h index e9ba23e..d3dcee7 100644 --- a/include/utmp.h +++ b/include/utmp.h @@ -43,6 +43,8 @@ void updwtmp(const char *, const struct utmp *); #define UTMP_FILENAME _PATH_UTMP #define WTMP_FILENAME _PATH_WTMP +int login_tty(int); + #ifdef __cplusplus } #endif diff --git a/src/misc/forkpty.c b/src/misc/forkpty.c index 4a1ea9b..007b369 100644 --- a/src/misc/forkpty.c +++ b/src/misc/forkpty.c @@ -1,7 +1,6 @@ #include +#include #include -#include -#include int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws) { @@ -13,12 +12,7 @@ int forkpty(int *m, char *name, const struct termios *tio, const struct winsize pid = fork(); if (!pid) { close(*m); - setsid(); - ioctl(s, TIOCSCTTY, (char *)0); - dup2(s, 0); - dup2(s, 1); - dup2(s, 2); - if (s>2) close(s); + login_tty(s); return 0; } close(s); diff --git a/src/misc/login_tty.c b/src/misc/login_tty.c new file mode 100644 index 0000000..f0be0a0 --- /dev/null +++ b/src/misc/login_tty.c @@ -0,0 +1,14 @@ +#include +#include +#include + +int login_tty(int fd) +{ + setsid(); + if (ioctl(fd, TIOCSCTTY, (char *)0)) return -1; + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + if (fd>2) close(fd); + return 0; +} -- 2.0.4