system() function in Linux returns -1 (92 Views)
Reply
kpj
Occasional Visitor
kpj
Posts: 1
Registered: ‎11-12-2012
Message 1 of 3 (92 Views)

system() function in Linux returns -1

 

Hello,

 

We have installed our product in the Linux RHEL 5.5 machine. From our product we are using the system() function to spawn a different process which is passed as a command argument. The result is returned as -1 . It is currently happening only in one machine.  The same code looks to be working correctly in other machines and the system() function returns the command status properly.

 

Is there any specific setting at the shell or kernel level because of this it can happen ?  Any help on this would be appreciated.

 

Regards
kpj

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,389
Registered: ‎03-06-2006
Message 2 of 3 (85 Views)

Re: system() function in Linux returns -1

The -1 seems to imply it can't fork.  Do you have the errno value?

Please use plain text.
Honored Contributor
Matti_Kurkela
Posts: 6,249
Registered: ‎12-02-2001
Message 3 of 3 (84 Views)

Re: system() function in Linux returns -1

Is /bin/sh available on the system that has the problem?

 

Is your application running setuid or setgid on the system that has the problem?

 

Maybe something in your application is causing the argument of the system() function to be NULL in that particular system?

 

Or maybe the system is so out of memory that it cannot create new processes at the moment?

 

Is SELinux enabled? If it is, is the product running in a security context that disallows creation of child processes?

(Run "ps -Zp <application_PID>" to find the SELinux security context label associated with the process.)

 

Back when I was on a "Programming in C 101" course at an University, the professor recommended against using the system() function in major applications. His rationale was that it had rather limited facilities for programmatic error detection, making it hard or impossible to tell exactly what went wrong. Using fork()/vfork(), execve() (or one of the other functions of the exec() family) and wait() separately would allow for better error detection and reporting.

In his opinion, system() was good for small quick-and-dirty programs, but not much else. I think your issue shows he knew what he was talking about.

MK
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation