From 858adc30f925a47ab117e469af9a1a2b178b8fd0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 17 Sep 2010 04:05:32 +0200 Subject: [PATCH] login_tty: Make the replacement code work on BSD systems. * lib/login_tty.c: Include . (login_tty): Use ioctl TIOCSCTTY when available. * modules/login_tty (Depends-on): Add sys_ioctl. Reported by Mats Erik Andersson . --- ChangeLog | 8 ++++++++ lib/login_tty.c | 21 +++++++++++++++------ modules/login_tty | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 25c63a520d..e9a60cd2ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2010-09-16 Bruno Haible + + login_tty: Make the replacement code work on BSD systems. + * lib/login_tty.c: Include . + (login_tty): Use ioctl TIOCSCTTY when available. + * modules/login_tty (Depends-on): Add sys_ioctl. + Reported by Mats Erik Andersson . + 2010-09-16 Bruno Haible login_tty: Stricter unit test. diff --git a/lib/login_tty.c b/lib/login_tty.c index e5ec248ead..5bc7dbe76b 100644 --- a/lib/login_tty.c +++ b/lib/login_tty.c @@ -21,6 +21,7 @@ #include #include +#include int login_tty (int slave_fd) @@ -31,15 +32,22 @@ login_tty (int slave_fd) setsid (); /* Make fd the controlling terminal for the current process. - On Solaris: A terminal becomes the controlling terminal of a session - if it is being open()ed, at a moment when - 1. it is not already the controlling terminal of some session, and - 2. the process that open()s it is a session leader that does not have - a controlling terminal. - We assume condition 1, try to ensure condition 2, and then open() it. */ + On BSD and OSF/1: There is ioctl TIOCSCTTY for this purpose. + On Solaris: + A terminal becomes the controlling terminal of a session + if it is being open()ed, at a moment when + 1. it is not already the controlling terminal of some session, and + 2. the process that open()s it is a session leader that does not have + a controlling terminal. + We assume condition 1, try to ensure condition 2, and then open() it. + */ for (i = 0; i < 3; i++) if (i != slave_fd) close (i); +#ifdef TIOCSCTTY + if (ioctl (slave_fd, TIOCSCTTY, NULL) < 0) + return -1; +#else { char *slave_name; int dummy_fd; @@ -52,6 +60,7 @@ login_tty (int slave_fd) return -1; close (dummy_fd); } +#endif /* Assign fd to the standard input, standard output, and standard error of the current process. */ diff --git a/modules/login_tty b/modules/login_tty index 032472f17f..53c5caee1b 100644 --- a/modules/login_tty +++ b/modules/login_tty @@ -8,6 +8,7 @@ m4/pty.m4 Depends-on: pty +sys_ioctl configure.ac: gl_FUNC_LOGIN_TTY -- 2.30.2