Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 30 Jan 2016 00:09:31 +0000 (UTC)
From:      Maxim Sobolev <sobomax@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r295055 - stable/10/lib/libc/gen
Message-ID:  <201601300009.u0U09V8w096550@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: sobomax
Date: Sat Jan 30 00:09:30 2016
New Revision: 295055
URL: https://svnweb.freebsd.org/changeset/base/295055

Log:
  MFC r294691:
  
  Fix readpassphrase(3) when it's called with stdin being closed.
  
  Approved by:	re (delphij)

Modified:
  stable/10/lib/libc/gen/readpassphrase.c

Modified: stable/10/lib/libc/gen/readpassphrase.c
==============================================================================
--- stable/10/lib/libc/gen/readpassphrase.c	Fri Jan 29 21:42:27 2016	(r295054)
+++ stable/10/lib/libc/gen/readpassphrase.c	Sat Jan 30 00:09:30 2016	(r295055)
@@ -46,7 +46,7 @@ char *
 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
 {
 	ssize_t nr;
-	int input, output, save_errno, i, need_restart;
+	int input, output, save_errno, i, need_restart, input_is_tty;
 	char ch, *p, *end;
 	struct termios term, oterm;
 	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
@@ -68,12 +68,20 @@ restart:
 	 * Read and write to /dev/tty if available.  If not, read from
 	 * stdin and write to stderr unless a tty is required.
 	 */
-	if ((flags & RPP_STDIN) ||
-	    (input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
-		if (flags & RPP_REQUIRE_TTY) {
-			errno = ENOTTY;
-			return(NULL);
+	input_is_tty = 0;
+	if (!(flags & RPP_STDIN)) {
+        	input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC);
+		if (input == -1) {
+			if (flags & RPP_REQUIRE_TTY) {
+				errno = ENOTTY;
+				return(NULL);
+			}
+			input = STDIN_FILENO;
+			output = STDERR_FILENO;
+		} else {
+			input_is_tty = 1;
 		}
+	} else {
 		input = STDIN_FILENO;
 		output = STDERR_FILENO;
 	}
@@ -83,7 +91,7 @@ restart:
 	 * If we are using a tty but are not the foreground pgrp this will
 	 * generate SIGTTOU, so do it *before* installing the signal handlers.
 	 */
-	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
+	if (input_is_tty && tcgetattr(input, &oterm) == 0) {
 		memcpy(&term, &oterm, sizeof(term));
 		if (!(flags & RPP_ECHO_ON))
 			term.c_lflag &= ~(ECHO | ECHONL);
@@ -152,7 +160,7 @@ restart:
 	(void)__libc_sigaction(SIGTSTP, &savetstp, NULL);
 	(void)__libc_sigaction(SIGTTIN, &savettin, NULL);
 	(void)__libc_sigaction(SIGTTOU, &savettou, NULL);
-	if (input != STDIN_FILENO)
+	if (input_is_tty)
 		(void)_close(input);
 
 	/*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201601300009.u0U09V8w096550>