How does a (mysql) process know âitsâ terminal if not from stdio?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
One thing with the mysql program caught my attention. It can be told to ask for password interactively with the -p flag, in which case, as you would expect, a prompt appears in your terminal and whatever is put in is accepted as the password. What surprised me though is that this happens even if you redirect all three of stdin,out,err:
$ mysql -p </dev/null >/dev/null 2>/dev/null
Enter password:
It reads the password properly too. If you redirect stderr to a file, you can check if the login failed.
How does the process know which terminal to ask for the password on? Does it check for attached terminals to parent processes?
terminal process io-redirection mysql
add a comment |Â
up vote
0
down vote
favorite
One thing with the mysql program caught my attention. It can be told to ask for password interactively with the -p flag, in which case, as you would expect, a prompt appears in your terminal and whatever is put in is accepted as the password. What surprised me though is that this happens even if you redirect all three of stdin,out,err:
$ mysql -p </dev/null >/dev/null 2>/dev/null
Enter password:
It reads the password properly too. If you redirect stderr to a file, you can check if the login failed.
How does the process know which terminal to ask for the password on? Does it check for attached terminals to parent processes?
terminal process io-redirection mysql
you can put a password in a~/.my.cnffile if using from a script.
â danblack
Sep 4 at 7:33
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
One thing with the mysql program caught my attention. It can be told to ask for password interactively with the -p flag, in which case, as you would expect, a prompt appears in your terminal and whatever is put in is accepted as the password. What surprised me though is that this happens even if you redirect all three of stdin,out,err:
$ mysql -p </dev/null >/dev/null 2>/dev/null
Enter password:
It reads the password properly too. If you redirect stderr to a file, you can check if the login failed.
How does the process know which terminal to ask for the password on? Does it check for attached terminals to parent processes?
terminal process io-redirection mysql
One thing with the mysql program caught my attention. It can be told to ask for password interactively with the -p flag, in which case, as you would expect, a prompt appears in your terminal and whatever is put in is accepted as the password. What surprised me though is that this happens even if you redirect all three of stdin,out,err:
$ mysql -p </dev/null >/dev/null 2>/dev/null
Enter password:
It reads the password properly too. If you redirect stderr to a file, you can check if the login failed.
How does the process know which terminal to ask for the password on? Does it check for attached terminals to parent processes?
terminal process io-redirection mysql
terminal process io-redirection mysql
edited Aug 23 at 12:41
asked Aug 23 at 11:15
Karel Vlk
686
686
you can put a password in a~/.my.cnffile if using from a script.
â danblack
Sep 4 at 7:33
add a comment |Â
you can put a password in a~/.my.cnffile if using from a script.
â danblack
Sep 4 at 7:33
you can put a password in a
~/.my.cnf file if using from a script.â danblack
Sep 4 at 7:33
you can put a password in a
~/.my.cnf file if using from a script.â danblack
Sep 4 at 7:33
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
A unix process can read from /dev/tty, and so circumvent redirection.
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created withforkinherit the controlling terminal from their parent process."1
â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
add a comment |Â
up vote
2
down vote
It uses getpass() from libc (where available) which is described in man pages as follows:
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
add a comment |Â
up vote
2
down vote
It can call the isatty unistd function.
NAME
isatty - test whether a file descriptor refers to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal.
Probably, as @Gerard H. Pille noted, mysql doesn't check at all but simply uses /dev/tty when you call it with -p.
Some additional detail
I checked into glibc sources the isatty implementation.
It simply uses the tcgetattr function to get the terminal capabilities of the file descriptor. If the function returns true, then it is a terminal.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
A unix process can read from /dev/tty, and so circumvent redirection.
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created withforkinherit the controlling terminal from their parent process."1
â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
add a comment |Â
up vote
3
down vote
accepted
A unix process can read from /dev/tty, and so circumvent redirection.
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created withforkinherit the controlling terminal from their parent process."1
â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
A unix process can read from /dev/tty, and so circumvent redirection.
A unix process can read from /dev/tty, and so circumvent redirection.
answered Aug 23 at 11:25
Gerard H. Pille
1,304212
1,304212
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created withforkinherit the controlling terminal from their parent process."1
â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
add a comment |Â
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created withforkinherit the controlling terminal from their parent process."1
â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created with
fork inherit the controlling terminal from their parent process."1â Karel Vlk
Aug 23 at 12:23
Thanks. Really simple. I forgot Unix processes have a notion of controlling terminals. "One of the attributes of a process is its controlling terminal. Child processes created with
fork inherit the controlling terminal from their parent process."1â Karel Vlk
Aug 23 at 12:23
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
And it may very well write to /dev/tty too, to disable the echo when the user enters the password.
â Gerard H. Pille
Aug 23 at 13:13
add a comment |Â
up vote
2
down vote
It uses getpass() from libc (where available) which is described in man pages as follows:
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
add a comment |Â
up vote
2
down vote
It uses getpass() from libc (where available) which is described in man pages as follows:
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It uses getpass() from libc (where available) which is described in man pages as follows:
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
It uses getpass() from libc (where available) which is described in man pages as follows:
The getpass() function opens /dev/tty (the controlling terminal of the process), outputs the string prompt, turns off echoing, reads one line (the "password"), restores the terminal state and closes /dev/tty again.
answered Aug 23 at 14:30
weirdan
48145
48145
add a comment |Â
add a comment |Â
up vote
2
down vote
It can call the isatty unistd function.
NAME
isatty - test whether a file descriptor refers to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal.
Probably, as @Gerard H. Pille noted, mysql doesn't check at all but simply uses /dev/tty when you call it with -p.
Some additional detail
I checked into glibc sources the isatty implementation.
It simply uses the tcgetattr function to get the terminal capabilities of the file descriptor. If the function returns true, then it is a terminal.
add a comment |Â
up vote
2
down vote
It can call the isatty unistd function.
NAME
isatty - test whether a file descriptor refers to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal.
Probably, as @Gerard H. Pille noted, mysql doesn't check at all but simply uses /dev/tty when you call it with -p.
Some additional detail
I checked into glibc sources the isatty implementation.
It simply uses the tcgetattr function to get the terminal capabilities of the file descriptor. If the function returns true, then it is a terminal.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It can call the isatty unistd function.
NAME
isatty - test whether a file descriptor refers to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal.
Probably, as @Gerard H. Pille noted, mysql doesn't check at all but simply uses /dev/tty when you call it with -p.
Some additional detail
I checked into glibc sources the isatty implementation.
It simply uses the tcgetattr function to get the terminal capabilities of the file descriptor. If the function returns true, then it is a terminal.
It can call the isatty unistd function.
NAME
isatty - test whether a file descriptor refers to a terminal
SYNOPSIS
#include <unistd.h>
int isatty(int fd);
DESCRIPTION
The isatty() function tests whether fd is an open file descriptor
referring to a terminal.
Probably, as @Gerard H. Pille noted, mysql doesn't check at all but simply uses /dev/tty when you call it with -p.
Some additional detail
I checked into glibc sources the isatty implementation.
It simply uses the tcgetattr function to get the terminal capabilities of the file descriptor. If the function returns true, then it is a terminal.
edited Aug 23 at 16:29
answered Aug 23 at 12:00
andcoz
11.9k32938
11.9k32938
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464367%2fhow-does-a-mysql-process-know-its-terminal-if-not-from-stdio%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
you can put a password in a
~/.my.cnffile if using from a script.â danblack
Sep 4 at 7:33