Skip to content
Advertisement

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 %sn", cmd);
pipe_cmd = popen(cmd,"r");
if (pipe_cmd != NULL) {
    int ret = pclose(pipe_cmd);
    printf("pclose returned %d:%d,%dn", ret, WIFEXITED(ret), WEXITSTATUS(ret));
    return 0;
} else {
    printf("Error executing script : %sn", 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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement