I'm not understanding what exactly happens with stdin, stdout and
stderr when I fork and execl from the child. Here's what I want
to do:
After NNTP server receives a POST, I save the article to a text
file on disk, then I want to (right after saving) fork() so I can
call an external program to do something about the saved file.
The problem is that this external program prints stuff to the
STDOUT/STDERR and this is confusing my NNTP server. Whatever this
program prints, my NNTP server thinks it's a client talking to
it, so it causes problems and the client thinks the NNTP is not
understanding its commands.
I tried executing the external program with > /dev/null 2&>1:
i = fork();
if(i == -1) { /* fork failed */ }
if (i == 0) {
n = execl(SPOOLDIR "/upload", SPOOLDIR "/upload",
name, ">/dev/null", "2>&1", NULL);
if( n == -1) /* execl failed */
} else return; /* parents continues operations */
But that did not seem to shut up ``upload'' program. Am I doing
this wrong or should I do something else? Thank you.
|
|
0
|
|
|
|
Reply
|
kevin499 (31)
|
3/22/2005 11:19:50 PM |
|
On Tue, 22 Mar 2005 23:19:50 +0000,
Kevin <kevin@hotmail.com> wrote:
[...]
> The problem is that this external program prints stuff to the
> STDOUT/STDERR and this is confusing my NNTP server. Whatever
> this program prints, my NNTP server thinks it's a client
> talking to it, so it causes problems and the client thinks the
> NNTP is not understanding its commands.
>
> I tried executing the external program with > /dev/null 2&>1:
Here's how I solved it: the upload program was a PERL program and
I had access to change it (which I didn't think of mentioning
here...), so I simply redirected STDOUT, STDERR to /dev/null from
within the program.
open( STDOUT, ">/dev/null") || die $!;
open( STDERR, ">/dev/null") || die $!;
However, if someone would like to explain me how I could've done
from withing the NNTP server, I'd appreciate. Thanks.
|
|
0
|
|
|
|
Reply
|
Kevin
|
3/23/2005 12:28:28 AM
|
|
On Wed, 23 Mar 2005 00:37:11 -0500,
Barry Margolin <barmar@alum.mit.edu> wrote:
> In article <20050322231950.339e3199.kevin@hotmail.com>,
> Kevin <kevin@hotmail.com> wrote:
>
> > I tried executing the external program with > /dev/null 2&>1:
> >
> > i = fork();
> > if(i == -1) { /* fork failed */ }
> > if (i == 0) {
> > n = execl(SPOOLDIR "/upload", SPOOLDIR "/upload",
> > name, ">/dev/null", "2>&1", NULL);
> >
> > if( n == -1) /* execl failed */
> > } else return; /* parents continues operations */
>
> I/O redirection operators like > and < are implemented by the
> shell, not by execl(). If you want to redirect, you should use
> open() and dup2() before calling execl():
>
> devnull = open("/dev/null", O_RDWR);
> dup2(STDIN_FILENO, devnull);
> dup2(STDOUT_FILENO, devnull);
> dup2(STDERR_FILENO, devnull);
> close(devnull);
> n = execl(SPOOLDIR "/upload", SPOOLDIR "/upload", name,
> (char*)NULL);
Mmmm I see. Thanks, Barry.
|
|
0
|
|
|
|
Reply
|
Kevin
|
3/23/2005 1:15:10 AM
|
|
In article <20050322231950.339e3199.kevin@hotmail.com>,
Kevin <kevin@hotmail.com> wrote:
> I tried executing the external program with > /dev/null 2&>1:
>
> i = fork();
> if(i == -1) { /* fork failed */ }
> if (i == 0) {
> n = execl(SPOOLDIR "/upload", SPOOLDIR "/upload",
> name, ">/dev/null", "2>&1", NULL);
>
> if( n == -1) /* execl failed */
> } else return; /* parents continues operations */
I/O redirection operators like > and < are implemented by the shell, not
by execl(). If you want to redirect, you should use open() and dup2()
before calling execl():
devnull = open("/dev/null", O_RDWR);
dup2(STDIN_FILENO, devnull);
dup2(STDOUT_FILENO, devnull);
dup2(STDERR_FILENO, devnull);
close(devnull);
n = execl(SPOOLDIR "/upload", SPOOLDIR "/upload", name, (char*)NULL);
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
0
|
|
|
|
Reply
|
Barry
|
3/23/2005 5:37:11 AM
|
|
|
3 Replies
941 Views
(page loaded in 0.067 seconds)
Similiar Articles: question about execl(), fork(), stdin, stdout, stderr - comp.unix ...I'm not understanding what exactly happens with stdin, stdout and stderr when I fork and execl from the child. Here's what I want to do: After NNTP ... Getting stdout and stdin streams of child process where child is ...question about execl(), fork(), stdin, stdout, stderr - comp.unix ... Getting stdout and stdin streams of child process where child is ... question about execl ... parents ... How to test if stdout == stderr? - comp.unix.programmerquestion about execl(), fork(), stdin, stdout, stderr - comp.unix ... I'm not understanding what exactly happens with stdin, stdout and stderr when I fork and execl from ... Write to stderr? - comp.lang.rexxquestion about execl(), fork(), stdin, stdout, stderr - comp.unix ... « It ..... asking this particular question ... to the parent either of stdin or stdout, but not both ... Windows file redirection and pipe operations - comp.lang.idl ...question about execl(), fork(), stdin, stdout, stderr - comp.unix ..... POST, I save the article to a text file on ... else return; /* parents continues operations ... fork (UDP) server - comp.unix.programmerSo my first question I guess is: Do you *REALLY* need a concurrent ... back short after: it has a wildcard UDP ... question about execl(), fork(), stdin, stdout, stderr ... ssh remote execution - comp.unix.programmer... using the following call after I fork another process execl ... Redirect stdout, stderr and stdin to/from somewhere other ... Question about setting environment variables ... Mysterious file descriptor behavior - comp.unix.programmer ...My question is why then do I get "Hello World"? ... the shell takes a shortcut when pre-allocating stdin, stdout and stderr for processes that it forks off. comp.unix.programmer - page 32... Post Question ... 09:34 AM) Hi, I use fork()+pty+exec() to invoke "gnuplot" and in child process, I call dup2() to redirect stdin, stdout and stderr to ... pipe error: "too many open files" - comp.unix.programmer ...... writing a program that makes a call to pipe and fork ... dup2(proci[0], 0); /* copy reading end to stdin */ dup2(proco[1], 1); /* copy reading end to stdout ... question about execl(), fork(), stdin, stdout, stderr - comp.unix ...I'm not understanding what exactly happens with stdin, stdout and stderr when I fork and execl from the child. Here's what I want to do: After NNTP ... question about execl(), fork(), stdin, stdout, stderrI'm not understanding what exactly happens with stdin, stdout and stderr when I fork and execl from the child. Here's what I want to do: After NNTP server receives a ... 7/21/2012 3:00:37 AM
|