using su with pseudo terminal

This is a discussion on using su with pseudo terminal within the Linux Programming forums, part of the Platform Specific Boards category; Hello- I am trying to automate a call to su. Unfortunately, su requires input from a terminal. I have seen ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,893
    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, 08:59 PM
  3. Pseudo TTY not reading singal character
    By MrUmunhum in forum Linux Programming
    Replies: 0
    Last Post: 11-18-2008, 06: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, 05:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21