The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-23-2007
ku@ntum ku@ntum is offline
Registered User
 

Join Date: May 2007
Posts: 4
terminal device control through a C/C++ program

Hi,

I want to fork a new process from a daemon that needs terminal attachment to some ttyN or pts/N device. Here is the code


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>


int main(int argc, char **args)
{
char *con = args[1];

printf("\nStart demonizing ...\n");

int i = fork();
if (i == 0)
{
close (0);
close (1);
close (2);
setsid();

int j = fork();
if (j == 0)
{
setsid();
//int in = open(con, O_RDWR);
int in = open(con, O_RDONLY);
int ou = open(con, O_WRONLY);
dup2(in, fileno(stdin));
dup2(ou, fileno(stdout));
dup2(ou, fileno(stderr));

printf("\n input :");

stdin = fdopen(0, "r");
stdout = fdopen(1, "w");
stderr = fdopen(2, "w");

char tmp[100];
strcpy(tmp, "");
//gets(tmp);
fread(tmp, sizeof(tmp), 1, stdin);
printf("\n output : %s\n", tmp);
}
exit(0);
}
}


The program takes a single command line argument - the name of the terminal device special file, in my case that will be /dev/"pts/1". The problem is that, the inner most spawned child does the output on the pts/1 terminal successfully, but it fails to take input from it.

Anyone can help here.

Last edited by ku@ntum; 05-23-2007 at 04:28 AM..
Reply With Quote
Remove advertisements
!!
Forum Sponsor