Good routing of app's stderr.
--- a/main.c Fri Sep 14 23:37:11 2007 +0200
+++ b/main.c Fri Sep 14 23:47:40 2007 +0200
@@ -21,17 +21,20 @@
return b;
}
-static int fork_app(int *opipe, char * const command[])
+static int fork_app(int *opipe /*[3]*/, char * const command[])
{
int p_input[2]; /* from mpg321 to us */
int p_output[2]; /* from us to mpg321 */
+ int p_error[2]; /* from mpg321 to us, its stderr */
int pid;
int res;
pipe(p_input);
pipe(p_output);
+ pipe(p_error);
opipe[0] = p_input[0]; /* For us to read */
opipe[1] = p_output[1]; /* For us to write */
+ opipe[2] = p_error[0]; /* For us to read */
pid = fork();
@@ -41,9 +44,12 @@
close(p_input[0]);
res = dup2(p_input[1], 1);
if (res == -1) perror("Dup2 1");
- res = dup2(p_input[1], 2);
+ close(p_error[0]);
+ res = dup2(p_error[1], 2);
if (res == -1) perror("Dup2 2");
close(p_input[1]);
+ close(p_error[1]);
+
close(p_output[1]);
res = dup2(p_output[0], 0);
if (res == -1) perror("Dup2 3");
@@ -56,6 +62,7 @@
error("Failed fork");
default: /* parent */
close(p_input[1]);
+ close(p_error[1]);
close(p_output[0]);
}
@@ -78,25 +85,41 @@
{
char buf[100];
fd_set read_set;
- int child_read, child_write;
+ int child_read, child_write, child_read_error;
int maxfd;
int opened_socket;
int stdin_opened;
+ int child_read_opened, child_read_error_opened;
int res;
child_read = child_pipe[0];
+ child_read_error = child_pipe[2];
child_write = child_pipe[1];
stdin_opened = 1;
opened_socket = -1; /* no socket opened */
+ child_read_opened = 1;
+ child_read_error_opened = 1;
do
{
FD_ZERO(&read_set);
+ maxfd = 0;
if (stdin_opened)
FD_SET(0, &read_set);
- FD_SET(child_read, &read_set);
- maxfd = child_read;
+
+ if (child_read_opened)
+ {
+ FD_SET(child_read, &read_set);
+ maxfd = max(maxfd, child_read);
+ }
+
+ if (child_read_error_opened)
+ {
+ FD_SET(child_read_error, &read_set);
+ maxfd = max(maxfd, child_read_error);
+ }
+
if (opened_socket >= 0)
{
FD_SET(opened_socket, &read_set);
@@ -120,11 +143,27 @@
error("Error in select()");
}
- if (FD_ISSET(child_read, &read_set))
+ if (child_read_opened && FD_ISSET(child_read, &read_set))
{
res = forward_app_data(child_read, 1);
if (res == 0)
- break;
+ {
+ close(1);
+ child_read_opened = 0;
+ if (child_read_error_opened == 0)
+ break;
+ }
+ }
+ if (child_read_error_opened && FD_ISSET(child_read_error, &read_set))
+ {
+ res = forward_app_data(child_read_error, 2);
+ if (res == 0)
+ {
+ close(2);
+ child_read_error_opened = 0;
+ if (child_read_opened == 0)
+ break;
+ }
}
if (FD_ISSET(0, &read_set))
{
@@ -153,7 +192,7 @@
static int server(int argn, char * const argv[])
{
- int p[2];
+ int p[3];
int lsocket;
int child;