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.