 |
|
 |
|
|
 |
question about execl(), fork(), stdin, stdout, stderr |
 |
 |
|
|
03-23-05 07:52 AM
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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: question about execl(), fork(), stdin, stdout, stderr |
 |
 |
|
|
03-23-05 07:52 AM
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.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: question about execl(), fork(), stdin, stdout, stderr |
 |
 |
|
|
03-23-05 07:52 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 ***
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: question about execl(), fork(), stdin, stdout, stderr |
 |
 |
|
|
03-23-05 07:52 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/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.
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 04:16 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|