Thread: using su with pseudo terminal

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    using su with pseudo terminal

    Hello-

    I am trying to automate a call to su. Unfortunately, su requires input from a terminal. I have seen examples of how to create a pseudo terminal and I understand that su will accept input from these:

    Code:
      int    fdm, fds;
      char   *slavename;
      extern char *ptsname();
    
      fdm = open("/dev/ptmx", O_RDWR);
      grantpt(fdm);
      unlockpt(fdm);
      slavename = ptsname(fdm);
      fds = open(slavename, O_RDWR);
    (This is just from a man page I found)

    From the man page of ptmx:
    Pseudo-terminals can also be used to send input to programs that normally refuse to read input from pipes (such as su(8), and
    passwd(8)).
    Does anyone have a good example of how to create and use the pseudo terminals for automating a call to su?

    Thanks.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You are 99% of the way there already. Now, you just need to fork(), then dup() the slave fd to descriptors 0 and 1 (stdin and stdout), and possibly stderr as well, then exec() the su program. If you choose not to dup descriptor 2, you should close it anyway.

    Code:
    pid = fork();
    if(pid == 0)
    {
        close(0);
        dup(fds);
        close(1);
        dup(fds);
        close(2);
        dup(fds); /* stderr also goes here */
        exec("su" etc etc);
    }
    /* Master read/write code goes in the parent */

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Linux question, nothing to do with networking. Moved.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Clearing Terminal
    By mrginger in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 11:58 AM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  3. Pseudo TTY not reading singal character
    By MrUmunhum in forum Linux Programming
    Replies: 0
    Last Post: 11-18-2008, 07:02 PM
  4. Reading terminal arguments
    By DenKain in forum C++ Programming
    Replies: 24
    Last Post: 10-07-2008, 04:37 PM
  5. virtual terminal won't scroll
    By cerin in forum Tech Board
    Replies: 2
    Last Post: 02-26-2008, 06:43 PM