8194750: Console.readPassword does not save/restore tty settings

Reviewed-by: martin, alanb
This commit is contained in:
Xueming Shen 2018-04-17 11:20:50 -07:00
parent 97979a53a1
commit 1775d925de
3 changed files with 45 additions and 6 deletions

View File

@ -311,8 +311,9 @@ public final class Console implements Flushable
char[] passwd = null;
synchronized (writeLock) {
synchronized(readLock) {
boolean echoWasOn;
try {
echoOff = echo(false);
echoWasOn = echo(false);
} catch (IOException x) {
throw new IOError(x);
}
@ -325,7 +326,7 @@ public final class Console implements Flushable
ioe = new IOError(x);
} finally {
try {
echoOff = echo(true);
echo(echoWasOn);
} catch (IOException x) {
if (ioe == null)
ioe = new IOError(x);
@ -372,8 +373,20 @@ public final class Console implements Flushable
private Charset cs;
private char[] rcb;
private static native String encoding();
/*
* Sets the console echo status to {@code on} and returns the previous
* console on/off status.
* @param on the echo status to set to. {@code true} for echo on and
* {@code false} for echo off
* @return true if the previous console echo status is on
*/
private static native boolean echo(boolean on) throws IOException;
private static boolean echoOff;
/*
* Returns the current console echo on/off status.
* @return true if the cosole echo is on
*/
private static native boolean echo0() throws IOException;
private static boolean echoOn;
private char[] readline(boolean zeroOut) throws IOException {
int len = reader.read(rcb, 0, rcb.length);
@ -527,9 +540,8 @@ public final class Console implements Flushable
new Runnable() {
public void run() {
try {
if (echoOff) {
echo(true);
}
if (cons != null)
echo(echoOn);
} catch (IOException x) { }
}
});
@ -579,5 +591,10 @@ public final class Console implements Flushable
readLock,
cs));
rcb = new char[1024];
try {
echoOn = echo0();
} catch (IOException x) {
echoOn = true;
}
}
}

View File

@ -67,3 +67,14 @@ Java_java_io_Console_echo(JNIEnv *env,
}
return old;
}
JNIEXPORT jboolean JNICALL
Java_java_io_Console_echo0(JNIEnv *env, jclass cls) {
struct termios tio;
int tty = fileno(stdin);
if (tcgetattr(tty, &tio) == -1) {
JNU_ThrowIOExceptionWithLastError(env, "tcgetattr failed");
return JNI_TRUE;
}
return (tio.c_lflag & ECHO) != 0;
}

View File

@ -82,3 +82,14 @@ Java_java_io_Console_echo(JNIEnv *env, jclass cls, jboolean on)
}
return old;
}
JNIEXPORT jboolean JNICALL
Java_java_io_Console_echo0(JNIEnv *env, jclass cls)
{
DWORD fdwMode;
if (! GetConsoleMode(hStdIn, &fdwMode)) {
JNU_ThrowIOExceptionWithLastError(env, "GetConsoleMode failed");
return JNI_TRUE;
}
return (fdwMode & ENABLE_ECHO_INPUT) != 0;
}