	Fork safety.

Between fork and exec we must be careful to not cause
a deadlock, or outputting things twice, or other bad things.


Things to look for:

* Buffered stdio output. After fork, it may end up sent twice
  (once from parent and once from child).
  Call fflush(NULL) before fork.

Things to look for if we might be called from multi-threaded program:

* malloc() can deadlock. After fork, malloc mutex may be locked.
  Do not use malloc. (Wow, this is hard).

* stdio can deadlock.
  Use write(fd) insted of printf's.

* exit() can deadlock (!) - it calls stdio to flush buffers.


Things which are ok:

* _exit(n)
* fd operations (open, close, dup, read/write).
* setting signal handlers and masks.
* [p]error_msg[_and_die]. (We make sure it doesn't do bad things)


	Standards reference:

http://pubs.opengroup.org/onlinepubs/007904975/functions/fork.html

"A process shall be created with a single thread. If a multi-threaded process
calls fork(), the new process shall contain a replica of the calling thread
and its entire address space, possibly including the states of mutexes and
other resources. Consequently, to avoid errors, the child process may only
execute async-signal-safe operations until such time as one of the exec
functions is called."

http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_04.html

"The following table defines a set of functions that shall be either reentrant
 or non-interruptible by signals and shall be async-signal-safe.
Therefore applications may invoke them, without restriction,
from signal-catching functions:

_Exit()
_exit()
abort()
accept()
access()
aio_error()
aio_return()
aio_suspend()
alarm()
bind()
cfgetispeed()
cfgetospeed()
cfsetispeed()
cfsetospeed()
chdir()
chmod()
chown()
clock_gettime()
close()
connect()
creat()
dup()
dup2()
execle()
execve()
fchmod()
fchown()
fcntl()
fdatasync()
fork()
fpathconf()
fstat()
fsync()
ftruncate()
getegid()
geteuid()
getgid()
getgroups()
getpeername()
getpgrp()
getpid()
getppid()
getsockname()
getsockopt()
getuid()
kill()
link()
listen()
lseek()
lstat()
mkdir()
mkfifo()
open()
pathconf()
pause()
pipe()
poll()
posix_trace_event()
pselect()
raise()
read()
readlink()
recv()
recvfrom()
recvmsg()
rename()
rmdir()
select()
sem_post()
send()
sendmsg()
sendto()
setgid()
setpgid()
setsid()
setsockopt()
setuid()
shutdown()
sigaction()
sigaddset()
sigdelset()
sigemptyset()
sigfillset()
sigismember()
sleep()
signal()
sigpause()
sigpending()
sigprocmask()
sigqueue()
sigset()
sigsuspend()
sockatmark()
socket()
socketpair()
stat()
symlink()
sysconf()
tcdrain()
tcflow()
tcflush()
tcgetattr()
tcgetpgrp()
tcsendbreak()
tcsetattr()
tcsetpgrp()
time()
timer_getoverrun()
timer_gettime()
timer_settime()
times()
umask()
uname()
unlink()
utime()
wait()
waitpid()
write()
"

