diff --git a/winsup/cygwin/fhandler_timerfd.cc b/winsup/cygwin/fhandler_timerfd.cc index 8ce91af86..cbcbefdcb 100644 --- a/winsup/cygwin/fhandler_timerfd.cc +++ b/winsup/cygwin/fhandler_timerfd.cc @@ -161,10 +161,15 @@ fhandler_timerfd::dup (fhandler_base *child, int flags) void fhandler_timerfd::fixup_after_exec () { - if (!close_on_exec ()) + if (close_on_exec ()) + return; + __try { - /* TODO after exec */ + timer_tracker *tt = (timer_tracker *) timerid; + tt->fixup_after_exec (); } + __except (EFAULT) {} + __endtry } int diff --git a/winsup/cygwin/timer.cc b/winsup/cygwin/timer.cc index bd412f0e2..be3fcf7c5 100644 --- a/winsup/cygwin/timer.cc +++ b/winsup/cygwin/timer.cc @@ -108,16 +108,6 @@ timer_tracker::timer_tracker (clockid_t c, const sigevent *e, bool fd) } } -void timer_tracker::increment_instances () -{ - InterlockedIncrement (&instance_count); -} - -LONG timer_tracker::decrement_instances () -{ - return InterlockedDecrement (&instance_count); -} - static inline int64_t timespec_to_us (const timespec& ts) { @@ -152,7 +142,6 @@ timer_tracker::_disarm_event () yield (); if (ret == EVENT_ARMED) { - InterlockedExchange64 (&overrun_count_curr, overrun_count); ret = overrun_count_curr; InterlockedExchange64 (&overrun_count, 0); @@ -461,6 +450,23 @@ timer_tracker::restart () } } +/* Only called from fhandler_timerfd::fixup_after_exec. Note that + we don't touch the instance count. This is handled by closing + the timer from fhandler_timerfd::close on O_CLOEXEC. Ultimately + the instance count should be correct after execve. */ +void +timer_tracker::fixup_after_exec () +{ + lock_timer_tracker here; + /* Check if timer is already in the list. If so, skip it. */ + for (timer_tracker *tt = &ttstart; tt->next != NULL; tt = tt->next) + if (tt->next == this) + return; + next = ttstart.next; + ttstart.next = this; + restart (); +} + void timer_tracker::fixup_after_fork () { diff --git a/winsup/cygwin/timer.h b/winsup/cygwin/timer.h index f4c89bc6b..3b426a308 100644 --- a/winsup/cygwin/timer.h +++ b/winsup/cygwin/timer.h @@ -30,7 +30,7 @@ class timer_tracker LONG64 overrun_count; bool cancel (); - LONG decrement_instances (); + LONG decrement_instances () { return InterlockedDecrement (&instance_count); } int clean_and_unhook (); LONG64 _disarm_event (); void restart (); @@ -45,7 +45,8 @@ class timer_tracker ~timer_tracker (); inline bool is_timer_tracker () const { return magic == TT_MAGIC; } - void increment_instances (); + + void increment_instances () { InterlockedIncrement (&instance_count); } LONG64 wait (bool nonblocking); HANDLE get_timerfd_handle () const { return timerfd_event; } @@ -58,6 +59,7 @@ class timer_tracker unsigned int disarm_event (); DWORD thread_func (); + void fixup_after_exec (); static void fixup_after_fork (); static int close (timer_tracker *tt); };