This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: pthreads, fork+exec, and deadlocks
* Roland McGrath <roland@redhat.com> [030910 14:15]:
> > After fork(), I want to close all file descriptors, endpwent(),
> > closelog(), manage stdio/stderr/stdin etc. properly before doing the
> > exec().
>
> Probably all these descriptors should have FD_CLOEXEC set so the right
> thing happens automatically at exec.
The problem is not specifically with closing file descriptors.
The particular problem I'm seeing is with syslog_lock.
If another thread is writing to syslog when the fork happens, the
syslog_lock mutex is cloned in a locked state.
A child can never reliably call any syslog functions. Is this
intended? I can certainly work around this.
Attached is test.c. It's a simple program which starts a bunch of threads,
which just do syslog(...) fork().
The fork()ed child simply does syslog(...); _exit(0);
pstree -p 22305
test(22305)---test(22307)-+-test(22308)---test(22318)
|-test(22309)---test(22325)
|-test(22310)---test(22319)
|-test(22311)---test(22321)
`-test(22312)---test(22326)
All the children are "stuck" on the syslog_lock mutex.
gdb attached to a fork()ed child (22318):
0x42028d69 in sigsuspend () from /lib/i686/libc.so.6
(gdb) bt
#0 0x42028d69 in sigsuspend () from /lib/i686/libc.so.6
#1 0x40037108 in __pthread_wait_for_restart_signal () from /lib/i686/libpthread.so.0
#2 0x40039480 in __pthread_alt_lock () from /lib/i686/libpthread.so.0
#3 0x40035f87 in pthread_mutex_lock () from /lib/i686/libpthread.so.0
#4 0x420d660f in vsyslog () from /lib/i686/libc.so.6
#5 0x420d617f in syslog () from /lib/i686/libc.so.6
#6 0x08048781 in slave (unused=0x8049b98) at test.c:41
#7 0x40035941 in pthread_start_thread () from /lib/i686/libpthread.so.0