fralbo
fralbo

Reputation: 2664

how to interpret pclose() status when popen() fails?

My application running on embedded linux (busybox) tries to execute a script via popen(cmd, "r").
cmd = "sh /tmp/DF1_05/update.sh DF1_05"

I can execute this script without problem launching it by hand from sh, but it fails when it's launched by the application.
The first lines of update.sh script are:

#!/bin/sh
echo "Starting update script"
....

I cannot even see the echo ouput.

My application's code is:

sprintf(cmd,"sh /tmp/%s/update.sh %s",version_ihm_usb,version_ihm_usb);
printf("Executing script %s\n", cmd);
pipe_cmd = popen(cmd,"r");
if (pipe_cmd != NULL) {
    int ret = pclose(pipe_cmd);
    printf("pclose returned %d:%d,%d\n", ret, WIFEXITED(ret), WEXITSTATUS(ret));
    return 0;
} else {
    printf("Error executing script : %s\n", strerror(errno));
}

The application output is:

Executing script sh /tmp/DF1_05/update.sh DF1_05
pclose returned 36096:1,141

So according to popen man page, pclose outputs a status similar to what wait4 outputs and as WIFEXITED(ret) is true, WEXITSTATUS(ret) is the output status of the child .......
Ok but after that it's a treasure hunt for me and in fact, I cannot interpret what code 141 is.

Does anyone have more precise info?

Upvotes: 1

Views: 614

Answers (2)

KamilCuk
KamilCuk

Reputation: 140980

I cannot interpret what code 141 is.

From man popen emphasis mine:

The popen() function opens a process by creating a pipe, forking, and invoking the shell.

From bash manual, which is a common convention:

When a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status.

From man signal:

   Signal        x86/ARM     Alpha/   MIPS   PARISC   Notes
               most others   SPARC
   ─────────────────────────────────────────────────────────────────
   SIGHUP           1           1       1       1
   .... other signals ....
   SIGPIPE         13          13      13      13

The process was terminated by SIGPIPE. SIGPIPE is 13 on your system. So your shell returned the exit status as 128 + 13 = 141.

Upvotes: 4

craig65535
craig65535

Reputation: 3582

Check WIFSIGNALED(ret) and WTERMSIG(ret). You may find that the child was terminated by a SIGPIPE.

Also, pclose is not likely failing here, but I would check its return value before passing that to the wait macros. If pclose returns < 0, log an error.

Upvotes: 3

Related Questions