8cb359d947
* dcrt0.cc (dll_crt0_0): Remove hexec_proc stuff. * fork.cc (fork_child): Remove call to pinfo_fixup_after_fork. * pinfo.cc (set_myself): Close and zero pid_handle if set. (pinfo_fixup_after_fork): Delete. (proc_waiter): Don't close vchild.hProcess here. Do that when we are remove the vchild from procs. Save hProcess as pid_handle only on first reparent operation. (pinfo::wait): Don't set pid_handle here. (pinfo::alert_parent): Always try to send signal. If unsuccessful then close and zero wr_proc_pipe. * pinfo.h (pinfo::pinfo): Make sure that appropriate parts of the class are zeroed on construction. (pinfo::alert_parent): Take char argument. (pinfo_fixup_after_fork): Delete declaration. (hexec_proc): Ditto. * sigproc.cc (remove_proc): Close pid_handle and hProcess if appropriate. * spawn.cc (spawn_guts): Set cygheap->pid_handle on first exec. * cygheap.h (init_cygheap::pid_handle): New element. * pinfo.cc (set_myself): Clear previously existing cygheap->pid_handle when a new process has been started. (pinfo::wait): Make sure that a handle to the newly forked/spawned process is kept around so that the pid will not be reused. * pinfo.h (_pinfo::pid_handle): Move. (pinfo::pid_handle): to here. * spawn.cc (spawn_guts): Create a pid_handle in cygheap prior to spawning to ensure that the pid does not get reused during the lifetime of the "cygwin pid". * pinfo.h (pinfo::alert_parent): New function. * exceptions.cc (sig_handle_tty_stop): Use alert_parent to send "signals" to parent. * fork.cc (fork_parent): Don't close pi.hProcess. Let the waiter thread do that. * pinfo.cc (proc_waiter): Detect case where process exits without setting the exit code and use value from GetExitCodeProcess. Reluctantly implement __SIGREPARENT. (pinfo::alert_parent): Define. * sigproc.h (__SIGREPARENT): New enum. * spawn.cc (spawn_guts): Send reparent signal to parent on exec. Always create process in suspended state to avoid races. Remove cygthread.h in favor of cygtls.h throughout since cygtls now includes cygthread.h. Eliminate ppid_handle usage throughout. * child_info.h: Regenerate magic number (child_info): Remove pppid_handle. * cygthread.h (cygthread::release): New method. Frees thread without waiting. * cygthread.cc (cygthread::stub): Set _ctinfo in _mytls to point to information for executing thread. Don't call SetEvent if thread is no longer in use. (cygthread::simplestub): Ditto. * cygtls.h (_cygtls::_ctinfo): New element contains pointer to information about executing cygthread, if any. * dcrt0.cc: Remove last vestiges of per_thread stuff. (dll_crt0_0): Ditto. Remove accommodation for ppid_handle. (do_exit): Remove obsolete reparenting test. (_exit): Exit with a more SUSv3-like exit value. * dtable.cc (dtable::stdio_init): Check for myself->cygstarted rather than myself->ppid_handle to see if we were started by a cygwin process. * exceptions.cc (open_stackdumpfile): Ditto. (handle_exceptions): Ditto. (ctrl_c_handler): Ditto. (sig_handle_tty_stop): Ditto. Let parent send signal to itself on STOP. (sigpacket::process): Comment out vfork test. (signal_exit): Use more SUSv3-like exit value on signal. * external.cc (fillout_pinfo): Don't set hProcess. * fork.cc: Remove VFORK cruft. (per_thread::set): Delete. (fork_child): Remove perthread stuff. (fork_parent): Remove obsolete subproc_init. Accommodate new method for tracking subprocesses. * pinfo.cc (set_myself): Accommodate new pinfo/_pinfo layout. Set some things here that used to be set in wait_sig. (_pinfo::exit): Set exitcode here. Close process pipe. (_pinfo::commune_send): Accommodeate new pinfo/_pinfo layout. (proc_waiter): New function. Waits, in a thread for subprocess to go away. (pinfo::wait): New function. Initialization for proc_waiter. * pinfo.h (_pinfo::exitcode): New element. (_pinfo::cygstarted): Ditto. (_pinfo::wr_proc_pipe): Ditto. (_pinfo::ppid_handle): Delete. (_pinfo::hProcess): Delete. (_pinfo::lock): Delete. (pinfo::hProcess): New element. (pinfo::lock): Ditto. (pinfo::wait): Declare new function. (pinfo::preserve): Define new function. * sigproc.cc: Remove old stuff from wait_subproc thread based method. (zombies): Remove. (procs): New. (my_parent_is_alive): Just check that the parent pid exists. (mychild): Just use pinfo methods to determine if child is mine. (proc_subproc): Revamp PROC_ADDCHILD to use pinfo::wait. Remove PROC_CHILDTERMINATED logic. Use different method to remove processes from list when SIGCHLD == SIG_IGN. (proc_terminate): Gut. (subproc_init): Delete. (init_child_info): Remove setting of pppid_handle. (checkstate): Revamp to only scan procs array. (remove_proc): Rename from remove_zombie. Don't close hProcess or pid_handle. Don't release memory if it's myself. (stopped_or_terminated): Change logic to handle new consolidated proc/zombie array. (wait_subproc): Delete. * sigproc.h: Remove obsolete EXIT_* defines. (subproc_init): Remove declaration. * spawn.cc (spawn_guts): Remove reparenting stuff. Use standard wait logic to wait for child if started from a non-cygwin process. * tlsoffsets.h: Regenerate. * tty.cc (tty_init): Check for myself->cygstarted rather than myself->ppid_handle to see if we were started by a cygwin process. * include/sys/signal.h (external_pinfo::exitcode): Replace hProcess. * include/sys/wait.h (WCOREDUMP): Define. * fhandler_tty.cc (fhandler_tty_slave::read): Add debugging output for timeout case. * signal.cc (abort): Flag that we are exiting with the ABORT signal.
275 lines
6.0 KiB
C++
275 lines
6.0 KiB
C++
/* timer.cc
|
|
|
|
Copyright 2004 Red Hat, Inc.
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#include "winsup.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "hires.h"
|
|
#include "thread.h"
|
|
#include "cygtls.h"
|
|
#include "sigproc.h"
|
|
#include "sync.h"
|
|
|
|
#define TT_MAGIC 0x513e4a1c
|
|
struct timer_tracker
|
|
{
|
|
static muto *protect;
|
|
unsigned magic;
|
|
clockid_t clock_id;
|
|
sigevent evp;
|
|
itimerspec it;
|
|
HANDLE cancel;
|
|
int flags;
|
|
cygthread *th;
|
|
struct timer_tracker *next;
|
|
int settime (int, const itimerspec *, itimerspec *);
|
|
timer_tracker (clockid_t, const sigevent *);
|
|
timer_tracker ();
|
|
};
|
|
|
|
timer_tracker ttstart;
|
|
|
|
muto *timer_tracker::protect;
|
|
|
|
timer_tracker::timer_tracker ()
|
|
{
|
|
new_muto (protect);
|
|
}
|
|
|
|
timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
|
|
{
|
|
if (e != NULL)
|
|
evp = *e;
|
|
else
|
|
{
|
|
evp.sigev_notify = SIGEV_SIGNAL;
|
|
evp.sigev_signo = SIGALRM;
|
|
evp.sigev_value.sival_ptr = this;
|
|
}
|
|
clock_id = c;
|
|
cancel = NULL;
|
|
flags = 0;
|
|
memset (&it, 0, sizeof (it));
|
|
protect->acquire ();
|
|
next = ttstart.next;
|
|
ttstart.next = this;
|
|
protect->release ();
|
|
magic = TT_MAGIC;
|
|
}
|
|
|
|
static long long
|
|
to_us (timespec& ts)
|
|
{
|
|
long long res = ts.tv_sec;
|
|
res *= 1000000;
|
|
res += ts.tv_nsec / 1000 + ((ts.tv_nsec % 1000) >= 500 ? 1 : 0);
|
|
return res;
|
|
}
|
|
|
|
static NO_COPY itimerspec itzero;
|
|
static NO_COPY timespec tzero;
|
|
|
|
static DWORD WINAPI
|
|
timer_thread (VOID *x)
|
|
{
|
|
timer_tracker *tp = ((timer_tracker *) x);
|
|
timer_tracker tt = *tp;
|
|
for (bool first = true; ; first = false)
|
|
{
|
|
long long sleep_us = to_us (first ? tt.it.it_value : tt.it.it_interval);
|
|
long long sleep_to = sleep_us;
|
|
long long now = gtod.usecs (false);
|
|
if (tt.flags & TIMER_ABSTIME)
|
|
sleep_us -= now;
|
|
else
|
|
sleep_to += now;
|
|
|
|
DWORD sleep_ms = (sleep_us < 0) ? 0 : (sleep_us / 1000);
|
|
debug_printf ("%p waiting for %u ms, first %d", x, sleep_ms, first);
|
|
tp->it.it_value = tzero;
|
|
switch (WaitForSingleObject (tt.cancel, sleep_ms))
|
|
{
|
|
case WAIT_TIMEOUT:
|
|
debug_printf ("timed out");
|
|
break;
|
|
case WAIT_OBJECT_0:
|
|
now = gtod.usecs (false);
|
|
sleep_us = sleep_to - now;
|
|
if (sleep_us < 0)
|
|
sleep_us = 0;
|
|
tp->it.it_value.tv_sec = sleep_us / 1000000;
|
|
tp->it.it_value.tv_nsec = (sleep_us % 1000000) * 1000;
|
|
debug_printf ("%p cancelled, elapsed %D", x, sleep_us);
|
|
goto out;
|
|
default:
|
|
debug_printf ("%p timer wait failed, %E", x);
|
|
goto out;
|
|
}
|
|
|
|
switch (tt.evp.sigev_notify)
|
|
{
|
|
case SIGEV_SIGNAL:
|
|
{
|
|
siginfo_t si;
|
|
memset (&si, 0, sizeof (si));
|
|
si.si_signo = tt.evp.sigev_signo;
|
|
si.si_sigval.sival_ptr = tt.evp.sigev_value.sival_ptr;
|
|
debug_printf ("%p sending sig %d", x, tt.evp.sigev_signo);
|
|
sig_send (NULL, si);
|
|
break;
|
|
}
|
|
case SIGEV_THREAD:
|
|
{
|
|
pthread_t notify_thread;
|
|
debug_printf ("%p starting thread", x);
|
|
int rc = pthread_create (¬ify_thread, tt.evp.sigev_notify_attributes,
|
|
(void * (*) (void *)) tt.evp.sigev_notify_function,
|
|
&tt.evp.sigev_value);
|
|
if (rc)
|
|
{
|
|
debug_printf ("thread creation failed, %E");
|
|
return 0;
|
|
}
|
|
// FIXME: pthread_join?
|
|
break;
|
|
}
|
|
}
|
|
if (!tt.it.it_interval.tv_sec && !tt.it.it_interval.tv_nsec)
|
|
break;
|
|
tt.flags = 0;
|
|
debug_printf ("looping");
|
|
}
|
|
|
|
out:
|
|
CloseHandle (tt.cancel);
|
|
// FIXME: race here but is it inevitable?
|
|
if (tt.cancel == tp->cancel)
|
|
tp->cancel = NULL;
|
|
return 0;
|
|
}
|
|
|
|
static bool
|
|
it_bad (const timespec& t)
|
|
{
|
|
if (t.tv_nsec < 0 || t.tv_nsec >= 1000000000 || t.tv_sec < 0)
|
|
{
|
|
set_errno (EINVAL);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int
|
|
timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalue)
|
|
{
|
|
if (!value)
|
|
{
|
|
set_errno (EINVAL);
|
|
return -1;
|
|
}
|
|
|
|
if (__check_invalid_read_ptr_errno (value, sizeof (*value)))
|
|
return -1;
|
|
|
|
if (ovalue && check_null_invalid_struct_errno (ovalue))
|
|
return -1;
|
|
|
|
itimerspec *elapsed;
|
|
if (!cancel)
|
|
elapsed = &itzero;
|
|
else
|
|
{
|
|
SetEvent (cancel); // should be closed when the thread exits
|
|
th->detach ();
|
|
elapsed = ⁢
|
|
}
|
|
|
|
if (ovalue)
|
|
*ovalue = *elapsed;
|
|
|
|
if (value->it_value.tv_sec || value->it_value.tv_nsec)
|
|
{
|
|
if (it_bad (value->it_value))
|
|
return -1;
|
|
if (it_bad (value->it_interval))
|
|
return -1;
|
|
flags = in_flags;
|
|
cancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
|
it = *value;
|
|
th = new cygthread (timer_thread, this, "itimer");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
extern "C" int
|
|
timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
|
|
{
|
|
if (evp && check_null_invalid_struct_errno (evp))
|
|
return -1;
|
|
if (check_null_invalid_struct_errno (timerid))
|
|
return -1;
|
|
|
|
if (clock_id != CLOCK_REALTIME)
|
|
{
|
|
set_errno (EINVAL);
|
|
return -1;
|
|
}
|
|
|
|
*timerid = (timer_t) new timer_tracker (clock_id, evp);
|
|
return 0;
|
|
}
|
|
|
|
extern "C" int
|
|
timer_settime (timer_t timerid, int flags, const struct itimerspec *value,
|
|
struct itimerspec *ovalue)
|
|
{
|
|
timer_tracker *tt = (timer_tracker *) timerid;
|
|
if (check_null_invalid_struct_errno (tt) || tt->magic != TT_MAGIC)
|
|
return -1;
|
|
return tt->settime (flags, value, ovalue);
|
|
}
|
|
|
|
extern "C" int
|
|
timer_delete (timer_t timerid)
|
|
{
|
|
timer_tracker *in_tt = (timer_tracker *) timerid;
|
|
if (check_null_invalid_struct_errno (in_tt) || in_tt->magic != TT_MAGIC)
|
|
return -1;
|
|
|
|
timer_tracker::protect->acquire ();
|
|
for (timer_tracker *tt = &ttstart; tt->next != NULL; tt = tt->next)
|
|
if (tt->next == in_tt)
|
|
{
|
|
timer_tracker *deleteme = tt->next;
|
|
tt->next = deleteme->next;
|
|
delete deleteme;
|
|
timer_tracker::protect->release ();
|
|
return 0;
|
|
}
|
|
timer_tracker::protect->release ();
|
|
|
|
set_errno (EINVAL);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
fixup_timers_after_fork ()
|
|
{
|
|
for (timer_tracker *tt = &ttstart; tt->next != NULL; /* nothing */)
|
|
{
|
|
timer_tracker *deleteme = tt->next;
|
|
tt->next = deleteme->next;
|
|
delete deleteme;
|
|
}
|
|
}
|