Thread: How come I can't re-acquire the controlling terminal here

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How come I can't re-acquire the controlling terminal here

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    void reterm(void)
    {
      pid_t pid;
      if((pid = fork()) < 0) {
        fprintf(stderr, "can't fork\n");
        exit(1);
      }
    
      if(pid !=0)
        exit(1);
    
      if(setsid() < 0) {
        fprintf(stderr, "setsid error\n");
      }
    
    }
    
    int main(void)
    {
      int fd;
    
      reterm();
    
      if((fd = open("/dev/null", O_RDONLY | O_NOCTTY)) < 0) {
        fprintf(stderr, "can't open tty\n");
        exit(1);
      }
    
      sleep(5000);
      close(fd);
    
      return 0;
    }
    [cd@localhost oakland]$ gcc -Wall reterm.c -o reterm
    [cd@localhost oakland]$ ./reterm
    [cd@localhost oakland]$ ps waux | grep reterm
    cd 25021 0.0 0.0 1484 156 ? Ss 17:36 0:00 ./
    reterm
    cd 25031 0.0 0.1 3880 676 pts/3 R+ 17:36 0:00 grep
    reterm
    [cd@localhost oakland]$

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since when was /dev/null a tty?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Well, if I change it to /dev/tty like in the following

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <errno.h>
    
    void reterm(void)
    {
      pid_t pid;
    
      if((pid = fork()) < 0) {
        fprintf(stderr, "can't fork\n");
        exit(1);
      }
    
      if(pid !=0)
        exit(1);
    
      if(setsid() < 0) 
        fprintf(stderr, "setsid error\n");
    }
    
    int main(void) 
    {
    
      int fd;
    
      reterm();
    
      if((fd = open("/dev/tty", O_RDONLY | O_NOCTTY)) < 0) {
        perror("Can't open /dev/tty: ");
        exit(1);
      }
    
      sleep(5000);
      close(fd);
    
      return 0;
    }
    I get

    $ gcc -Wall reterm.c -o reterm
    $ ./reterm
    Can't open /dev/tty: : No such device or address
    $ ls -al /dev/tty
    crw-rw-rw- 1 root tty 5, 0 Aug 9 08:35 /dev/tty
    Last edited by Overworked_PhD; 08-12-2008 at 07:22 AM.

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. controlling terminal question
    By Overworked_PhD in forum Linux Programming
    Replies: 3
    Last Post: 10-01-2008, 09:20 PM
  4. using su with pseudo terminal
    By oncemyway in forum Linux Programming
    Replies: 2
    Last Post: 04-30-2007, 10:52 AM
  5. Controlling terminal tty
    By pdstatha in forum C Programming
    Replies: 0
    Last Post: 04-02-2002, 03:57 AM