Calling ksh script from system() causing segfault-Collection of common programming errors

I’ve got a fairly well tested bit of an application I’m trying to get working on Ubuntu Linux, and having a lot of problems, seemingly with the ksh implementation. The problem seems to manifest when it is being called by the system build command.

There is a ksh script which auto-generates a C header file from a C source file. When I try to run it via a system() call in a C application, the ksh crashes with a segfault.

At the command line, I can run the command without problems, and it works correctly. It also works in the same circumstances on all other platforms I’ve used (including SLES and Fedora Linux). I can also make it work by changing the shebang line in the script to #! /bin/bash (most of the work it does is really in a sed script so no exotic commands are done in the shell script).

But we’ve standardised on ksh, mainly as it was the best shell available on older AIX versions that we still support, and it is easy to add to Linux distros if it isn’t included by default.

I’m not too sure where to look on this – has anyone seen anything similar?

  1. For info, I finally worked out the specifics causing the problem. The most basic way is simply to open 100 file pointers to the same file, then call system() e.g.

    for( i = 0; i < 100; i++ )
      fopen( "file", "r" );
    system( "script" );
    

    As long as the script shebang is the AT&T ksh shell, that reliably crashes on this one box. I found a bit of the system build leaves lots of hanging file pointers to one file. Not enough to hit ulimit problems, but enough for this weirdness.

  2. See if you can fix the problem by putting the full path in the system call. If its not finding on the path you anticipate when using ksh this could be part of the problem.