Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Oct 2017 08:34:26 +0200
From:      Matthias Apitz <guru@unixarea.de>
To:        freebsd-questions@freebsd.org
Subject:   no dead-lock when signal handler calls localtime_r() on FreeBSD, but on Linux
Message-ID:  <20171002063425.GA21552@sh4-5.1blu.de>

next in thread | raw e-mail | index | archive | help


Hello,

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?

Thanks

	matthias

/*
 * 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");
  fflush(stdout);
  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);
#endif
  }

  return 0;
}


-- 
Matthias Apitz               |  /"\   ASCII Ribbon Campaign:
E-mail: guru@unixarea.de     |  \ /   - No HTML/RTF in E-mail
WWW: http://www.unixarea.de/ |   X    - No proprietary attachments
phone: +49-176-38902045      |  / \   - Respect for open standards
                             | en.wikipedia.org/wiki/ASCII_Ribbon_Campaign



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20171002063425.GA21552>