here's my effort. I've redirected stdout of the child to a pipe. The resulting output is on below.I still have some problems..
1- Can anyone explain why the parent process only reads from the pipe after the child has terminated?
2- Also, why "Connected PLAYSTATION(R)3 Controller (00:23:06:A4:E6:AA)" isn't sent into the pipe? this message is sent with syslog() instead of printf().
Code:
int fd[2];
if( pipe(fd) !=0 )
{
perror("Failed to create pipe");
exit(EXIT_FAILURE);
}
switch ( fork() )
{
case -1:
perror("Failed to fork");
exit(EXIT_FAILURE);
break;
case 0:
if( dup2(fd[1],STDOUT_FILENO) ==-1 )
{
perror("Fail redirecting stdout");
exit(EXIT_FAILURE);
}
close(fd[0]);
if(execl("/usr/bin/sixad","sixad","-s",NULL)==-1)
perror("Child unable to execute sixad");
exit(EXIT_FAILURE);
break;
default:
printf("parent\n");
close(fd[1]);
char buf[500];
while(1)
{
printf("while\n");
if ( read(fd[0], buf, sizeof(buf)) <= 0)
{
perror("Fail reading from pipe:\n ");
exit(EXIT_FAILURE);
}
printf("Read from pipe:\n %s",buf);
}
}
Output:
(sixad source code: syslog(LOG_INFO, "Connected PLAYSTATION(R)3 Controller (%s)", bda); )
Output Continuation:
sixad[7953]: sixad started, press the PS button now
sixad[7969]: Connected PLAYSTATION(R)3 Controller (00:23:06:A4:E6:AA)
(I pressed ctrl+c in the console)
Output Continuation
^C
sixad[7953]: Exit
Read from pipe:
sixad settings:
Enable LEDs: 1
js# as LED #: 1
Start LED #: 1
LED # increase: 1
LED animation: 1
Buttons: 1
Sens. buttons: 1
Axis: 1
Accelerometers: 1
Acceleration: 0
Speed: 0
Position: 0
Rumble: 0
Debug: 0
while
Fail reading from pipe:
: Success
Note: sixad settings are printed with printf