mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* nanosleep truncated on 64 bit Linux by 292 billion years
       [not found]   ` <alpine.DEB.2.10.1408060017090.3323@nanos>
@ 2014-10-27  4:01     ` Pádraig Brady
  2014-10-28 15:45       ` Thomas Gleixner
  0 siblings, 1 reply; 2+ messages in thread
From: Pádraig Brady @ 2014-10-27  4:01 UTC (permalink / raw)
  To: Thomas Gleixner, Linux Kernel Mailing List; +Cc: Paul Eggert, bug-gnulib

[-- Attachment #1: Type: text/plain, Size: 867 bytes --]

I noticed that nanosleep() on 64 bit, "only" supports 292 years,
rather than the full potential 292 billion years with 64 bit time_t, due to:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/time.h?id=refs/tags/v3.16#n87

Attached is a program from Paul Eggert that illustrates the bug.
Running this program on a buggy host outputs something like this:

  Setting alarm for 1 second from now ...
  Sleeping for 9223372036854775807.999999999 seconds...
  After alarm sent off, remaining time is 9223357678.462306617 seconds;
  i.e., nanosleep claimed that it slept for about 293079448610.606445 years.

Gnulib-using applications have a workaround for this bug, but a workaround
shouldn't be necessary.  For what it's worth, the bug is fixed in Solaris 11 (x86-64),
though it's present in Solaris 10 (64-bit sparc).

thanks,
Pádraig.

[-- Attachment #2: nanosleep-bug.c --]
[-- Type: text/plain, Size: 1175 bytes --]

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

static void
check_for_SIGALRM (int sig)
{
  if (sig != SIGALRM)
    _exit (1);
}

int
main (void)
{
  static struct sigaction act;
  struct timespec forever, remaining;
  time_t time_t_max = (1ull << (sizeof time_t_max * CHAR_BIT - 1)) - 1;
  act.sa_handler = check_for_SIGALRM;
  sigemptyset (&act.sa_mask);
  sigaction (SIGALRM, &act, NULL);
  forever.tv_sec = time_t_max;
  forever.tv_nsec = 999999999;
  printf ("Setting alarm for 1 second from now ...\n");
  alarm (1);
  printf ("Sleeping for %lld.%09ld seconds...\n",
	  (long long) forever.tv_sec, forever.tv_nsec);
  if (nanosleep (&forever, &remaining) == 0)
    return 2;
  if (errno != EINTR)
    return 3;
  if (remaining.tv_sec < time_t_max - 10)
    {
      printf ("After alarm sent off, remaining time is %lld.%09ld seconds;\n",
	      (long long) remaining.tv_sec, remaining.tv_nsec);
      printf ("i.e., nanosleep claimed that it slept for about %f years.\n",
	      (forever.tv_sec - remaining.tv_sec) / (24 * 60 * 60 * 364.2425));
      return 4;
    }
  printf ("ok\n");
  return 0;
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: nanosleep truncated on 64 bit Linux by 292 billion years
  2014-10-27  4:01     ` nanosleep truncated on 64 bit Linux by 292 billion years Pádraig Brady
@ 2014-10-28 15:45       ` Thomas Gleixner
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Gleixner @ 2014-10-28 15:45 UTC (permalink / raw)
  To: Pádraig Brady; +Cc: Linux Kernel Mailing List, Paul Eggert, bug-gnulib

[-- Attachment #1: Type: TEXT/PLAIN, Size: 775 bytes --]

On Mon, 27 Oct 2014, Pádraig Brady wrote:
> I noticed that nanosleep() on 64 bit, "only" supports 292 years,
> rather than the full potential 292 billion years with 64 bit time_t, due to:
> https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/time.h?id=refs/tags/v3.16#n87
> 
> Attached is a program from Paul Eggert that illustrates the bug.
> Running this program on a buggy host outputs something like this:
> 
>   Setting alarm for 1 second from now ...
>   Sleeping for 9223372036854775807.999999999 seconds...
>   After alarm sent off, remaining time is 9223357678.462306617 seconds;
>   i.e., nanosleep claimed that it slept for about 293079448610.606445 years.

I'm aware of the issue. It's on my ever growing todo list.

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-10-28 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <53E0FF15.4090808@draigBrady.com>
     [not found] ` <53E13F04.70609@cs.ucla.edu>
     [not found]   ` <alpine.DEB.2.10.1408060017090.3323@nanos>
2014-10-27  4:01     ` nanosleep truncated on 64 bit Linux by 292 billion years Pádraig Brady
2014-10-28 15:45       ` Thomas Gleixner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome