question about execl(), fork(), stdin, stdout, stderr

  • Follow


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:













7/21/2012 3:00:37 AM


Reply: