no dead-lock when signal handler calls localtime_r() on FreeBSD, but on Linux

Matthias Apitz guru at
Mon Oct 2 06:34:36 UTC 2017


I'm on the way clarifying a deadlock-issue we face on Linux when localtime_r()
is called in a signal-handler funtion. If you compile the code attached
below with gcc on Linux, the code gives what one deserves: a dead-lock
when Ctrl-C is pressed.

Interestingly this does not give a dead-lock on my FreeBSD (12-CURRENT)
system. I checked our man page sigaction(2) but the function is not in
the list of functions which may be called safe in signal-handlern. 

Any idea, while this does not give a dead-lock on FreeBSD too?



 * compile with: gcc -Wall -DUNSAFE -o deadlock deadlock.c
 * to get a dead-lock

#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

void handler(int signum)
  char result[100];
  time_t now;
  struct tm time1;

  printf("in handler() now calling localtime_r() ...\n");
  now = time(NULL);
  localtime_r(&now, &time1);
  strftime(result, 100, "%T", &time1);
  printf("... at %s, user pressed Ctrl-C\n", result);

int main (void)
  time_t now;
  struct tm ltime;

  if (signal(SIGINT, handler) == SIG_IGN)
    signal(SIGINT, SIG_IGN);

  now = time(NULL);
  while(1) {
#ifdef UNSAFE
    localtime_r(&now, &ltime);

  return 0;

Matthias Apitz               |  /"\   ASCII Ribbon Campaign:
E-mail: guru at     |  \ /   - No HTML/RTF in E-mail
WWW: |   X    - No proprietary attachments
phone: +49-176-38902045      |  / \   - Respect for open standards

More information about the freebsd-questions mailing list