* 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.
		
			
				
	
	
		
			292 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			292 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/* window.cc: hidden windows for signals/itimer support
 | 
						|
 | 
						|
   Copyright 1997, 1998, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
 | 
						|
 | 
						|
   Written by Sergey Okhapkin <sos@prospect.com.ru>
 | 
						|
 | 
						|
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 <sys/time.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <signal.h>
 | 
						|
#include <limits.h>
 | 
						|
#include <wingdi.h>
 | 
						|
#include <winuser.h>
 | 
						|
#define USE_SYS_TYPES_FD_SET
 | 
						|
#include <winsock2.h>
 | 
						|
#include <unistd.h>
 | 
						|
#include "cygerrno.h"
 | 
						|
#include "perprocess.h"
 | 
						|
#include "security.h"
 | 
						|
#include "thread.h"
 | 
						|
#include "cygtls.h"
 | 
						|
#include "sync.h"
 | 
						|
#include "wininfo.h"
 | 
						|
 | 
						|
wininfo NO_COPY winmsg;
 | 
						|
 | 
						|
muto NO_COPY *wininfo::lock;
 | 
						|
 | 
						|
wininfo::wininfo ()
 | 
						|
{
 | 
						|
  new_muto_name (lock, "!winlock");
 | 
						|
}
 | 
						|
 | 
						|
int __stdcall
 | 
						|
wininfo::process (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 | 
						|
{
 | 
						|
#ifndef NOSTRACE
 | 
						|
  strace.wm (uMsg, wParam, lParam);
 | 
						|
#endif
 | 
						|
  switch (uMsg)
 | 
						|
    {
 | 
						|
    case WM_PAINT:
 | 
						|
      return 0;
 | 
						|
    case WM_DESTROY:
 | 
						|
      PostQuitMessage (0);
 | 
						|
      return 0;
 | 
						|
    case WM_TIMER:
 | 
						|
      if (wParam == timer_active)
 | 
						|
	{
 | 
						|
	  UINT elapse = itv.it_interval.tv_sec * 1000 +
 | 
						|
			itv.it_interval.tv_usec / 1000;
 | 
						|
	  KillTimer (hwnd, timer_active);
 | 
						|
	  if (!elapse)
 | 
						|
	    timer_active = 0;
 | 
						|
	  else
 | 
						|
	    {
 | 
						|
	      timer_active = SetTimer (hwnd, 1, elapse, NULL);
 | 
						|
	      start_time = GetTickCount ();
 | 
						|
	      itv.it_value = itv.it_interval;
 | 
						|
	    }
 | 
						|
	  raise (SIGALRM);
 | 
						|
	}
 | 
						|
      return 0;
 | 
						|
    case WM_ASYNCIO:
 | 
						|
      if (WSAGETSELECTEVENT (lParam) == FD_OOB)
 | 
						|
	raise (SIGURG);
 | 
						|
      else
 | 
						|
	raise (SIGIO);
 | 
						|
      return 0;
 | 
						|
    default:
 | 
						|
      return DefWindowProc (hwnd, uMsg, wParam, lParam);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
static LRESULT CALLBACK
 | 
						|
process_window_events (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 | 
						|
{
 | 
						|
  return winmsg.process (hwnd, uMsg, wParam, lParam);
 | 
						|
}
 | 
						|
 | 
						|
/* Handle windows events.  Inherits ownership of the wininfo lock */
 | 
						|
DWORD WINAPI
 | 
						|
wininfo::winthread ()
 | 
						|
{
 | 
						|
  MSG msg;
 | 
						|
  WNDCLASS wc;
 | 
						|
  static NO_COPY char classname[] = "CygwinWndClass";
 | 
						|
 | 
						|
  lock->grab ();
 | 
						|
  /* Register the window class for the main window. */
 | 
						|
 | 
						|
  wc.style = 0;
 | 
						|
  wc.lpfnWndProc = (WNDPROC) process_window_events;
 | 
						|
  wc.cbClsExtra = 0;
 | 
						|
  wc.cbWndExtra = 0;
 | 
						|
  wc.hInstance = user_data->hmodule;
 | 
						|
  wc.hIcon = NULL;
 | 
						|
  wc.hCursor = NULL;
 | 
						|
  wc.hbrBackground = NULL;
 | 
						|
  wc.lpszMenuName = NULL;
 | 
						|
  wc.lpszClassName = classname;
 | 
						|
 | 
						|
  if (!RegisterClass (&wc))
 | 
						|
    api_fatal ("cannot register window class, %E");
 | 
						|
 | 
						|
  /* Create hidden window. */
 | 
						|
  hwnd = CreateWindow (classname, classname, WS_POPUP, CW_USEDEFAULT,
 | 
						|
			   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
 | 
						|
			   (HWND) NULL, (HMENU) NULL, user_data->hmodule,
 | 
						|
			   (LPVOID) NULL);
 | 
						|
  if (!hwnd)
 | 
						|
    api_fatal ("couldn't create window, %E");
 | 
						|
  lock->release ();
 | 
						|
 | 
						|
  while (GetMessage (&msg, hwnd, 0, 0) == TRUE)
 | 
						|
    DispatchMessage (&msg);
 | 
						|
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
static DWORD WINAPI
 | 
						|
winthread (VOID *arg)
 | 
						|
{
 | 
						|
  return  ((wininfo *) arg)->winthread ();
 | 
						|
}
 | 
						|
 | 
						|
wininfo::operator
 | 
						|
HWND ()
 | 
						|
{
 | 
						|
  if (hwnd)
 | 
						|
    return hwnd;
 | 
						|
 | 
						|
  lock->acquire ();
 | 
						|
  if (!hwnd)
 | 
						|
    {
 | 
						|
      lock->upforgrabs ();
 | 
						|
      cygthread *h = new cygthread (::winthread, this, "win");
 | 
						|
      h->SetThreadPriority (THREAD_PRIORITY_HIGHEST);
 | 
						|
      h->zap_h ();
 | 
						|
      lock->acquire ();
 | 
						|
    }
 | 
						|
  lock->release ();
 | 
						|
  return hwnd;
 | 
						|
}
 | 
						|
 | 
						|
extern "C" int
 | 
						|
setitimer (int which, const struct itimerval *value, struct itimerval *oldvalue)
 | 
						|
{
 | 
						|
  if (which != ITIMER_REAL)
 | 
						|
    {
 | 
						|
      set_errno (ENOSYS);
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  return winmsg.setitimer (value, oldvalue);
 | 
						|
}
 | 
						|
 | 
						|
/* FIXME: Very racy */
 | 
						|
int __stdcall
 | 
						|
wininfo::setitimer (const struct itimerval *value, struct itimerval *oldvalue)
 | 
						|
{
 | 
						|
  /* Check if we will wrap */
 | 
						|
  if (itv.it_value.tv_sec >= (long) (UINT_MAX / 1000))
 | 
						|
    {
 | 
						|
      set_errno (EINVAL);
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  if (timer_active)
 | 
						|
    {
 | 
						|
      KillTimer (winmsg, timer_active);
 | 
						|
      timer_active = 0;
 | 
						|
    }
 | 
						|
  if (oldvalue)
 | 
						|
    *oldvalue = itv;
 | 
						|
  if (value == NULL)
 | 
						|
    {
 | 
						|
      set_errno (EFAULT);
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  itv = *value;
 | 
						|
  UINT elapse = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
 | 
						|
  if (elapse == 0)
 | 
						|
    if (itv.it_value.tv_usec)
 | 
						|
      elapse = 1;
 | 
						|
    else
 | 
						|
      return 0;
 | 
						|
  if (!(timer_active = SetTimer (winmsg, 1, elapse, NULL)))
 | 
						|
    {
 | 
						|
      __seterrno ();
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  start_time = GetTickCount ();
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
extern "C" int
 | 
						|
getitimer (int which, struct itimerval *value)
 | 
						|
{
 | 
						|
  if (which != ITIMER_REAL)
 | 
						|
    {
 | 
						|
      set_errno (EINVAL);
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  if (value == NULL)
 | 
						|
    {
 | 
						|
      set_errno (EFAULT);
 | 
						|
      return -1;
 | 
						|
    }
 | 
						|
  return winmsg.getitimer (value);
 | 
						|
}
 | 
						|
 | 
						|
/* FIXME: racy */
 | 
						|
int __stdcall
 | 
						|
wininfo::getitimer (struct itimerval *value)
 | 
						|
{
 | 
						|
  *value = itv;
 | 
						|
  if (!timer_active)
 | 
						|
    {
 | 
						|
      value->it_value.tv_sec = 0;
 | 
						|
      value->it_value.tv_usec = 0;
 | 
						|
      return 0;
 | 
						|
    }
 | 
						|
 | 
						|
  UINT elapse, val;
 | 
						|
 | 
						|
  elapse = GetTickCount () - start_time;
 | 
						|
  val = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
 | 
						|
  val -= elapse;
 | 
						|
  value->it_value.tv_sec = val / 1000;
 | 
						|
  value->it_value.tv_usec = val % 1000;
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
extern "C" unsigned int
 | 
						|
alarm (unsigned int seconds)
 | 
						|
{
 | 
						|
  int ret;
 | 
						|
  struct itimerval newt, oldt;
 | 
						|
 | 
						|
  newt.it_value.tv_sec = seconds;
 | 
						|
  newt.it_value.tv_usec = 0;
 | 
						|
  newt.it_interval.tv_sec = 0;
 | 
						|
  newt.it_interval.tv_usec = 0;
 | 
						|
  setitimer (ITIMER_REAL, &newt, &oldt);
 | 
						|
  ret = oldt.it_value.tv_sec;
 | 
						|
  if (ret == 0 && oldt.it_value.tv_usec)
 | 
						|
    ret = 1;
 | 
						|
  return ret;
 | 
						|
}
 | 
						|
 | 
						|
extern "C" useconds_t
 | 
						|
ualarm (useconds_t value, useconds_t interval)
 | 
						|
{
 | 
						|
  struct itimerval timer, otimer;
 | 
						|
 | 
						|
  timer.it_value.tv_sec = 0;
 | 
						|
  timer.it_value.tv_usec = value;
 | 
						|
  timer.it_interval.tv_sec = 0;
 | 
						|
  timer.it_interval.tv_usec = interval;
 | 
						|
 | 
						|
  if (setitimer (ITIMER_REAL, &timer, &otimer) < 0)
 | 
						|
    return (u_int)-1;
 | 
						|
 | 
						|
  return (otimer.it_value.tv_sec * 1000000) + otimer.it_value.tv_usec;
 | 
						|
}
 | 
						|
 | 
						|
bool
 | 
						|
has_visible_window_station (void)
 | 
						|
{
 | 
						|
  HWINSTA station_hdl;
 | 
						|
  USEROBJECTFLAGS uof;
 | 
						|
  DWORD len;
 | 
						|
 | 
						|
  /* Check if the process is associated with a visible window station.
 | 
						|
     These are processes running on the local desktop as well as processes
 | 
						|
     running in terminal server sessions.
 | 
						|
     Processes running in a service session not explicitely associated
 | 
						|
     with the desktop (using the "Allow service to interact with desktop"
 | 
						|
     property) are running in an invisible window station. */
 | 
						|
  if ((station_hdl = GetProcessWindowStation ())
 | 
						|
      && GetUserObjectInformationA (station_hdl, UOI_FLAGS, &uof,
 | 
						|
				    sizeof uof, &len)
 | 
						|
      && (uof.dwFlags & WSF_VISIBLE))
 | 
						|
    return true;
 | 
						|
  return false;
 | 
						|
}
 |