This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug nptl/18138] New: sem_timedwait should not convert absolute timeout to relative
- From: "jsm28 at gcc dot gnu.org" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sourceware dot org
- Date: Tue, 17 Mar 2015 23:57:02 +0000
- Subject: [Bug nptl/18138] New: sem_timedwait should not convert absolute timeout to relative
- Auto-submitted: auto-generated
https://sourceware.org/bugzilla/show_bug.cgi?id=18138
Bug ID: 18138
Summary: sem_timedwait should not convert absolute timeout to
relative
Product: glibc
Version: 2.21
Status: NEW
Severity: normal
Priority: P2
Component: nptl
Assignee: unassigned at sourceware dot org
Reporter: jsm28 at gcc dot gnu.org
CC: drepper.fsp at gmail dot com
sem_timedwait converts absolute timeouts to relative to pass them to the futex
syscall. (Before the recent reimplementation, on x86_64 it used
FUTEX_CLOCK_REALTIME, but not on other architectures.)
Correctly implementing POSIX requirements, however, requires use of
FUTEX_CLOCK_REALTIME; passing a relative timeout to the kernel does not conform
to POSIX. The POSIX specification for sem_timedwait says "The timeout shall be
based on the CLOCK_REALTIME clock.". The POSIX specification for clock_settime
says "If the value of the CLOCK_REALTIME clock is set via clock_settime(), the
new value of the clock shall be used to determine the time of expiration for
absolute time services based upon the CLOCK_REALTIME clock. This applies to the
time at which armed absolute timers expire. If the absolute time requested at
the invocation of such a time service is before the new value of the clock, the
time service shall expire immediately as if the clock had reached the requested
time normally.". If a relative timeout is passed to the kernel, it is
interpreted according to the CLOCK_MONOTONIC clock, and so fails to meet that
POSIX requirement in the event of clock changes.
Testcase below (requires root, changes the system clock and leaves the clock
changed). Testing a patch.
#include <errno.h>
#include <inttypes.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int
main (void)
{
struct timespec old_time, new_time, timeout, final_time;
if (clock_gettime (CLOCK_REALTIME, &old_time) != 0)
{
perror ("clock_gettime");
exit (EXIT_FAILURE);
}
new_time = old_time;
timeout = old_time;
new_time.tv_sec += 30;
timeout.tv_sec += 20;
pid_t pid = fork ();
if (pid == -1)
{
perror ("fork");
exit (EXIT_FAILURE);
}
if (pid == 0)
{
/* Child. */
if (sleep (1) != 0)
{
perror ("sleep");
exit (EXIT_FAILURE);
}
if (clock_settime (CLOCK_REALTIME, &new_time) != 0)
{
perror ("clock_settime");
exit (EXIT_FAILURE);
}
exit (EXIT_SUCCESS);
}
/* Parent. */
sem_t sem;
if (sem_init (&sem, 0, 0) != 0)
{
perror ("sem_init");
exit (EXIT_FAILURE);
}
int ret = sem_timedwait (&sem, &timeout);
if (ret != -1 || errno != ETIMEDOUT)
{
fprintf (stderr, "sem_timedout returned %d (errno = %d: %m)\n",
ret, errno);
exit (EXIT_FAILURE);
}
if (clock_gettime (CLOCK_REALTIME, &final_time) != 0)
{
perror ("clock_gettime");
exit (EXIT_FAILURE);
}
int64_t time_diff
= (INT64_C(1000000000) * (final_time.tv_sec - new_time.tv_sec)
+ (final_time.tv_nsec - new_time.tv_nsec));
fprintf (stderr, "time difference %"PRId64"ns\n", time_diff);
if (time_diff < 0 || time_diff > 1000000000)
{
fprintf (stderr, "bad time difference\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "time difference OK\n");
exit (EXIT_SUCCESS);
}
--
You are receiving this mail because:
You are on the CC list for the bug.