spawning a shell script

Hi-

I’m having trouble using spawn to start a shell script. For some reason, the following error message is printed:

invalid file descriptor to bufgets: 20

The spawn function call does not return an error, so my C program thinks everything is fine. The script’s process turns into a zombie. Spawning an equivalent C program in place of the script works fine.


Here’s some sample code from the caller:

qnx_spawn_options.priority= 10;
if ( (pid= spawnl( P_NOWAIT, “/home/peted/bin/sleep.sh”,
“/home/peted/bin/sleep.sh”, “0”, NULL )) == ERROR )
{
printf( “start_tasks: couldn’t spawn /home/peted/bin/sleep.sh 0, error= %s.”,
strerror(errno) );
}


The script is very simple. It’s set to be world executable; the “shebang” line is in the upper left corner of the file:

#!/bin/sh

while [ 1 ]
do
sleep 60
done


The same problem happens with a call to spawnv().

Any thoughts?

TIA,

  • Pete

Pete DiMarco <peted@ifspurity.nospam.com> wrote:

Hi-

I’m having trouble using spawn to start a shell script. For some reason, the following error message is printed:

invalid file descriptor to bufgets: 20

The spawn function call does not return an error, so my C program thinks everything is fine. The script’s process turns into a zombie. Spawning an equivalent C program in place of the script works fine.



Here’s some sample code from the caller:

qnx_spawn_options.priority= 10;
if ( (pid= spawnl( P_NOWAIT, “/home/peted/bin/sleep.sh”,
“/home/peted/bin/sleep.sh”, “0”, NULL )) == ERROR )
{
printf( “start_tasks: couldn’t spawn /home/peted/bin/sleep.sh 0, error= %s.”,
strerror(errno) );
}



The script is very simple. It’s set to be world executable; the “shebang” line is in the upper left corner of the file:

#!/bin/sh

while [ 1 ]
do
sleep 60
done



The same problem happens with a call to spawnv().

Any thoughts?

You should always check the help file :slight_smile:

Function spawn(), struct inherit, member short flags, the first
flag is:

SPAWN_CHECK_SCRIPT - let spawn() start a shell, passing path
as a script (not implemented)


So you have a mistake that didn’t set that flag, but, even you
did set it, it won’t work, and I think a ENOSYS is supposed to
return back.

Your option is spawn("/bin/sh", “-c”, “your script”… )

-xtang

Pete DiMarco <peted@ifspurity.nospam.com> wrote:

Hi-

I’m having trouble using spawn to start a shell script.
For some reason, the following error message is printed:

invalid file descriptor to bufgets: 20

Its a bug/limitation in our implementation of the pdksh code.
We use qnx_spawn() (instead of fork&exec) for re-directing fds
in pipes etc in the shell, and it takes an array of 10 fds –
so it saves away the original 10 fds before doing this – but
if it has more open fds than 10 passed to it, it will go past
the number it was coded to handle – up to 20 fds.

The work-around is to set the FD_CLOEXEC flag for fds that you
open in the parent process, so that they aren’t inheritted by
the shell – unless you really want them to be. (Mostly, you
don’t want them inheritted by the shell.) Look at the fcntl()
function docs for this flag.

-David

QNX Training Services
dagibbs@qnx.com

Xiaodan Tang <xtang@qnx.com> wrote:

Pete DiMarco <> peted@ifspurity.nospam.com> > wrote:

You should always check the help file > :slight_smile:

Function spawn(), struct inherit, member short flags, the first
flag is:

SPAWN_CHECK_SCRIPT - let spawn() start a shell, passing path
as a script (not implemented)



So you have a mistake that didn’t set that flag, but, even you
did set it, it won’t work, and I think a ENOSYS is supposed to
return back.

Your option is spawn("/bin/sh", “-c”, “your script”… )

This would more likely be the problem under QNX NTO – Pete didn’t actually
mention which OS he was using, but the particular error is one
that I’ve seen a lot under QNX4.

(Sometimes it helps to tell us which OS you are using. :slight_smile:

-David

QNX Training Services
dagibbs@qnx.com

Thanks for all the feedback. I’ve been struggling with this problem
for a few days.

Part of what is confusing me is that an earlier version of my application
works as I would expect. It reads a pathname from a file and runs the
program that the path points to, regardless of whether the program is a
script or not. The new version of my application won’t spawn scripts, not
even when I use system() in place of spawnv().

The main difference between my 2 applications is that the new one opens
16 message queues (the old application just opened 10 message queues).

David (dagibbs@qnx.com) wrote:

Its a bug/limitation in our implementation of the pdksh code.
We use qnx_spawn() (instead of fork&exec) for re-directing fds
in pipes etc in the shell, and it takes an array of 10 fds –
so it saves away the original 10 fds before doing this – but
if it has more open fds than 10 passed to it, it will go past
the number it was coded to handle – up to 20 fds.

So my new app has 16 msg queue fd’s plus 3 standard IO fd’s… this
makes sense now.

The work-around is to set the FD_CLOEXEC flag for fds that you
open in the parent process, so that they aren’t inheritted by
the shell – unless you really want them to be. (Mostly, you
don’t want them inheritted by the shell.) Look at the fcntl()
function docs for this flag.

This would more likely be the problem under QNX NTO – Pete didn’t actually
mention which OS he was using, but the particular error is one
that I’ve seen a lot under QNX4.

(Sometimes it helps to tell us which OS you are using. > :slight_smile:

Oops, sorry. I’m using QNX 4.

  • Pete