Technical article 100835

Has the behavior of system(S) changed in SCO OpenServer 5.0?

OpenServer 5.0.0

Written · Last revised

Problem

I am writing a program which calls the library routine system(S) and attempts to catch the signal SIGCHLD (see signal(S)) upon the termination of the child process which is running the specified command. When this signal is received, a wait(S) system call is made from within the program to obtain this child process's return status.

The following program works as expected when built and run on SCO UNIX System V/386 Release 3.2 Operating System Version 4.2. However, when built and run on SCO OpenServer Release 5.0.0, this program behaves differently with the call to wait(S) returning -1 and errno (see perror(S)) set to ECHILD, indicating "No child processes".

----------------------<cut-here>-----------------------------
/*
 * cc -o errno errno.c
 *
 ****************************************
 *  Example output from SCO UNIX 3.2v4.2:
 ****************************************
 *
 *  UID   PID   PPID   C    STIME  TTY      TIME COMMAND
 *  ...   ...   ....   .    .....  ...      .... .......
 *  root  19673 17290  2 08:35:10  p21      0:00 errno
 *  root  19675 19673  3 08:35:10  p21      0:00 sh -c ps -ef
 *  root  19676 19675 22 08:35:10  p21      0:00 ps -ef
 *  ...   ...   ....   .    .....  ...      .... .......
 *  my_func() entered...
 *  death of PID 19675 with retval 0 (errno = 0)
 *  system() returns -1
 *
 *********************************************
 *  Example output from SCO OpenServer R5.0.0:
 *********************************************
 *
 *  UID   PID  PPID   C    STIME  TTY      TIME     COMMAND
 *  ...   ...  ....   .    .....  ...      ....     .......
 *  root 23787 23735  1 08:33:49  ttyp0    00:00:00 errno
 *  root 23788 23787  1 08:33:49  ttyp0    00:00:00 sh -c ps -ef
 *  root 23789 23788 10 08:33:49  ttyp0    00:00:00 ps -ef
 *  ...   ...  ....   .    .....  ...      ....     .......
 *  my_func() entered...
 *  death of PID -1 with retval 0 (errno = 10)
 *  system() returns 0
 *
 */
#include <sys/signal.h>
#include <errno.h>
extern void my_func();

main ( argc, argv) int argc; char *argv[]; { int x;

signal (SIGCLD, my_func);
x = system ("ps -ef");
        printf("system() returns %d\n", x);
        sleep(5);
        exit(0);
}
void my_func()
{
int pid, retval;

printf("my_func() entered...\n"); pid = wait(&retval); printf("death of PID %d with retval %d (errno = %d)\n", pid, retval,errno); signal(SIGCLD, my_func); return; }

----------------------<cut-here>-----------------------------

Has the behavior of system(S) changed in SCO OpenServer Release 5.0?

Cause

In SCO UNIX Operating System Version 4.2, system(S) was implemented simply using fork(S), exec(S) and waitpid(S), with the parent process executing waitpid(S) in order to wait for the child process which was executing the command to terminate.

In the above program, this results in my_func() being called when this child process dies and the subsequent waitpid(S) in the system(S) library routine failing since there are no existing unwaited-for child processes. Hence, system(S) returns -1 with errno set to ECHILD, as demonstrated in the above example output.

In SCO OpenServer Release 5.0, the implementation of system(S) has been reworked so that the signal SIGCHLD is held (or blocked) within the parent process (see sighold(S)) until the child process has terminated and the parent process has issued a waitpid(S) to obtain the return status for this child process, that is, for the duration of the system(S) routine. Only then is the SIGCHLD signal released (see sigrelse(S)).

In addition, if the user-defined action for SIGCHLD has been set to SIG_IGN prior to calling system(S), in other words, wait(S) and waitpid(S) will block until *all* of the calling process's child processes terminate, then within the system(S) function this action is redefined to SIG_DFL and subsequently reset to SIG_IGN upon exiting the system(S) function.

In the above program, this change in behavior of system(S) results in my_func() being called when the signal is released. However, at this time there are no unwaited-for child processes and thus the wait(S) call within this function fails with a return value of -1 with errno set to ECHILD.

Solution

The use of system(S) is not always suitable in all cases (system(S) is *not* a system call, but a library routine). For this reason, it may be advisable to directly use fork(S), exec(S), and waitpid(S) as required.

From the Xinuos technical article database, record 100835. Reproduced with permission. The original is at www.sco.com/ta/100835, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.