Large amounts of unsafe code executed in child process after fork
By michael.... on December 09, 2011 18:29 (imported from Google Code)
Starting around PTYTask.m:592, a great deal of code is executed in the child process after a fork, which is highly dangerous. To quote Ye Olde Man Page:
There are limits to what you can do in the child process. To be totally safe you should restrict yourself to only executing async-signal safe operations until such time as one of the exec functions is called. All APIs, including global data symbols, in any framework or library should be assumed to be unsafe after a fork() unless explicitly documented to be safe or async-signal safe.
Basically, you can't safely call anything except the small set of functions listed in the 'sigaction' man page. The code mostly works, but it's purely due to luck, and could easily deadlock or crash in the right circumstances.
The setup code should be moved to before the fork, with a bare minimum of safe calls made in the child to properly set up the environment. The parent will have to clean up any data structures created, which is somewhat annoying, but unfortunately it's the only way to write this code correctly.