We have an application that creates several threads using the pthread library. As we want only the main process to handle certain signals all the pthreads have SIGTERM, SIGINT and SIGHUP blocked. In one of our pthreads we restart the network using
system("/etc/init.d/networking restart");
It looks like the system call will fork another child process that inherits the signal mask of the pthread. The problem is that it restarts the ssh daemon and the newly created ssh daemon has inherited the signal mask from our pthread (SIGTERM, SIGINT and SIGHUP being blocked). The result being future network restarts taking a long time as it fails to send the SIGTERM and waits for a timeout before sending SIGKILL.
My question is what is the best way of restarting the network from a multi-threaded application like ours. Is there a way of telling init to restart a service?
I thought of unblocking signals before calling system, but then if someone terminates our application during this time our main signal handler may not get called.
We are running a debian linux distribution running on an embedded ARM board. Here’s the output from uname -a
Linux ts 2.6.35.3-433-g0fae922 #6 PREEMPT Thu Jul 28 09:24:24 MST 2011 armv7l GNU/Linux
Any help appreciated,
Martin.
This is exceedingly unsafe, and will cause your application to hang or crash. See this question and discussion.
Fork a child before you have created any threads. Have that child wait on a socket, or for a signal (e.g.
SIGUSR1). When woken up, child will dosystem(...)and go back to sleep.