Skip to contentSkip to conversation

popen/pclose problem

parisc-linux Paul J.Y. Lahaie 1 participant 1 message Mar 14, 2001

lore
Share & tools
lore
#0 lore raw

There seems to be a bug with popen/pclose on parisc. In dbootstrap, the Debian bootstrap install program, there is a function that will run an application with its output redirected to syslog with the use of popen/pclose. In this application, pclose always returns -1 which is (according to the manpage):

"The pclose function returns -1 if wait4 returns an error, or some other error is detected."

The function with the error is execlog in utilities/dbootstrap/util.c.

Interestingly, if I take out that function and wrap a main around it, pclose returns the right values. Here follows the standalone program I wrote using the function.

--- cut here ---
#include <stdio.h>
#include <syslog.h>
#define MAXLINE 1024
int execlog (const char *incmd, int priority)
{
FILE *output;
char line[MAXLINE];
char cmd[strlen(incmd) + 6];
strcpy(cmd, incmd);

    openlog("sysrun", LOG_PID, LOG_USER);

/* FIXME: this can cause the shell command if there's redirection
already in the passed string */
strcat(cmd, " 2>&1");
output = popen(cmd, "r");

  while (fgets(line, MAXLINE, output) != NULL ) {
    syslog(priority, line);

}
closelog();
return(pclose(output));
}

int main(int argc,char **argv)

{
    int ret;
    if( argc < 2 )
    {
        fprintf( stderr, "Missing arguments.\n" );
        exit(1);
    }

    ret = execlog( argv[1], LOG_INFO);
    if( ret != 0 )
    {
        fprintf( stderr, "Bad return value %d\n", ret );
        return ret;
    }
    return 0;
}

--- cut here ---