3

We have a multithreaded embedded application which, due to hardware constraints too mundane to discuss here, must remount its filesystem to be RW whenever it outputs to a file.

We are currently doing this via a system() call, and running the mount command. However, from time to time, this call blocks, and causes the application to go into deadlock.

During my debugging, I have placed system(NULL) prior to the original system() call, and this seems to sometimes block also.

Generally speaking, under what circumstances might system() block for all eternity?

3 Answers 3

8

Is this Linux? In glibc on Linux, system() blocks SIGCHLD, changes a couple of signal handlers, forks, waits for the child to die, then fixes what it did to the signal mask. In the child process, it undoes the signal mask changes and exec's the shell to run your command. This even happens when you call system(NULL) --- the only difference is that the called shell is called as sh -c exit 0.

In sum, you're spawning a process, loading the shell (and all its associated libraries), and waiting for the shell to die. You're probably getting bitten by loading the shell.

1
  • Although we still don't know what our problem is (it's probably caused by some subtlety of our platform), this answer contains some good info, so gets the tick this time.
    – Matt Dunn
    Dec 3, 2012 at 13:57
2

system() blocks until the called command has completed, so if the mount command never finishes then system() will never return.

1
  • That was my first thought, but system(NULL) never finishes, and I am able to perform the mount manually from the terminal whilst the application is in the blocked state.
    – Matt Dunn
    Nov 27, 2012 at 17:43
1

I guess if you run mount than you need root privileges, so maybe this is your case (excerpt from man system on Linux):

   Do not use system() from a program with set-user-ID or set-group-ID privileges, because strange values  for  some  environment
   variables  might  be  used  to  subvert  system  integrity.  Use the exec(3) family of functions instead, but not execlp(3) or
   execvp(3).  system() will not, in fact, work properly from programs with set-user-ID or set-group-ID privileges on systems  on
   which  /bin/sh  is  bash  version 2, since bash 2 drops privileges on startup.  (Debian uses a modified bash which does not do
   this when invoked as sh.)
1
  • 1
    It's a good answer, but not the issue here: I definitely have root privileges. And besides, this call works 70% of the time. The remaining 30%, something beyond my understanding happens, which causes all manner of pain.
    – Matt Dunn
    Nov 27, 2012 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.