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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question























  • you can put a password in a ~/.my.cnf file if using from a script.
    – danblack
    Sep 4 at 7:33














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?










share|improve this question























  • you can put a password in a ~/.my.cnf file if using from a script.
    – danblack
    Sep 4 at 7:33












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 at 12:41

























asked Aug 23 at 11:15









Karel Vlk

686




686











  • 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















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










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










A unix process can read from /dev/tty, and so circumvent redirection.






share|improve this answer




















  • 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

















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.







share|improve this answer



























    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.






    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer




















      • 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














      up vote
      3
      down vote



      accepted










      A unix process can read from /dev/tty, and so circumvent redirection.






      share|improve this answer




















      • 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












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      A unix process can read from /dev/tty, and so circumvent redirection.






      share|improve this answer












      A unix process can read from /dev/tty, and so circumvent redirection.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      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 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
















      • 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















      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












      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.







      share|improve this answer
























        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.







        share|improve this answer






















          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.







          share|improve this answer












          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.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 23 at 14:30









          weirdan

          48145




          48145




















              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.






              share|improve this answer


























                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.






                share|improve this answer
























                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 23 at 16:29

























                  answered Aug 23 at 12:00









                  andcoz

                  11.9k32938




                  11.9k32938



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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