|
Home > Archive > Unix Programming > March 2005 > question about execl(), fork(), stdin, stdout, stderr
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
question about execl(), fork(), stdin, stdout, stderr
|
|
|
| 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.
| |
|
| 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.
| |
| Barry Margolin 2005-03-23, 2: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 ***
| |
|
| 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.
|
|
|
|
|