public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "jsm28 at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sourceware.org Subject: [Bug nptl/18138] New: sem_timedwait should not convert absolute timeout to relative Date: Tue, 17 Mar 2015 23:57:00 -0000 [thread overview] Message-ID: <bug-18138-131@http.sourceware.org/bugzilla/> (raw) 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.
next reply other threads:[~2015-03-17 23:57 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2015-03-17 23:57 jsm28 at gcc dot gnu.org [this message] 2015-03-18 17:06 ` [Bug nptl/18138] " cvs-commit at gcc dot gnu.org 2015-03-18 17:07 ` jsm28 at gcc dot gnu.org 2015-03-25 15:19 ` cvs-commit at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-18138-131@http.sourceware.org/bugzilla/ \ --to=sourceware-bugzilla@sourceware.org \ --cc=glibc-bugs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).