fork + syslog blocking

G

Gregory Starck

Hello,

I'm having what seems to be a deadlock in syslog with the code (wich
comes from a project I'm working on, but I've reduced the test case to
its minimum) I put at the end of this messsage.

It must be compiled with -lpthread. (so the fork could be also a
possible cause of what I'm mentionning).
The "bug" does not occur every times. So you should maybe launch the
prog a few times before the block occurs.

Is there someone knowing the true about this fact :
Is it from my code ? or is it a bug within syslog ???
or something else ?
And are they any technics (or patch for syslog ?) wich could resolve the
problem ??

Ho, btw, I'm using Linux 2.4.21, and it's a Mandrake 9.1.

Thanks for your responses.

Greg.



Here is the code :

##################################################################################

#include <sys/types.h>
#include <signal.h>
#include <syslog.h>
#include <stdio.h>
#include <errno.h>

void handler_sigchild (int signal_value);

void
set_father_signals (void)
{
struct sigaction
sigchild;
sigset_t
sigsblocked;

sigchild.sa_handler = handler_sigchild;
sigchild.sa_flags = 0;

if (sigfillset (&sigchild.sa_mask) == -1)
{
printf ("set_father_signals: a sigfillset() failed. err = %s
!\n", strerror (errno));
}

if ((sigfillset (&sigsblocked) == -1) /* and we respond
only to those */
|| (sigdelset (&sigsblocked, SIGCHLD) == -1))
{
printf ("set_father_signals: could not fill blocked signals.
err = %s !\n", strerror (errno));
}

if (sigprocmask (SIG_SETMASK, &sigsblocked, NULL) == -1)
{
printf ("set_father_signals: could not set blocked signals. err
= %s !\n", strerror (errno));
}

if (sigaction (SIGCHLD, &sigchild, NULL) == -1)
{
printf ("kern_set_father_signals: a sigaction() failed. err =
%s !\n", strerror (errno));
}
}

int main ( int argc, char * argv [] )
{
pid_t pid;

openlog ("testbug3", LOG_PID | LOG_CONS, LOG_DAEMON);

set_father_signals ();

switch (pid = fork ())
{
case -1:
printf ("Error while forking : %s\n", strerror (errno));
break;
case 0 : /* in child */
exit (0); /* just return immediately */
default : /* in father */
printf ("father: after fork, before syslog\n");
fflush (stdout);
syslog (LOG_INFO, "TRACE: child's pid = %lu", pid);
printf ("father: after fork, after syslog\n");
fflush (stdout);
}

closelog ();
wait (NULL); /* wait for the child */
printf ("exiting\n");
fflush (stdout);
}

void handler_sigchild (int sig)
{
printf ("In sighandle, before syslog\n");
fflush (stdout);
syslog (LOG_INFO, "received a SIGCHLD");
printf ("In sighandle, after syslog\n");
fflush (stdout);
}

#########################################################################
 
J

Jens.Toerring

Gregory Starck said:
I'm having what seems to be a deadlock in syslog with the code (wich
comes from a project I'm working on, but I've reduced the test case to
its minimum) I put at the end of this messsage.
It must be compiled with -lpthread. (so the fork could be also a
possible cause of what I'm mentionning).
The "bug" does not occur every times. So you should maybe launch the
prog a few times before the block occurs.

Sorry, but you really should post this e.g. on comp.unix.programmer
because everything your asking about is off-topic here in clc. The
topic of this group is programming in standard C and there's no
fork(), syslog() etc. in standard C - these are UNIX (or POSIX)
extensions. If you post in compunix.programmer you probably also
should explain in more detail what you mean by "deadlock" (and
did you notice that you may call closelog() before the SIGCHLD
has arrived?).
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top