newlib/winsup/cygwin/external.cc
Christopher Faylor 8cb359d947 * child_info.h (child_info_spawn::hexec_proc): Eliminate.
* 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.
2004-11-26 04:15:10 +00:00

305 lines
6.6 KiB
C++

/* external.cc: Interface to Cygwin internals from external programs.
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
Written by Christopher Faylor <cgf@cygnus.com>
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 "security.h"
#include "sigproc.h"
#include "pinfo.h"
#include <exceptions.h>
#include "shared_info.h"
#include "cygwin_version.h"
#include "perprocess.h"
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "wincap.h"
#include "heap.h"
#include "pwdgrp.h"
#include "cygtls.h"
static external_pinfo *
fillout_pinfo (pid_t pid, int winpid)
{
BOOL nextpid;
static external_pinfo ep;
if ((nextpid = !!(pid & CW_NEXTPID)))
pid ^= CW_NEXTPID;
static winpids pids (0);
static unsigned int i;
if (!pids.npids || !nextpid)
{
pids.set (winpid);
i = 0;
}
if (!pid)
i = 0;
memset (&ep, 0, sizeof ep);
while (i < pids.npids)
{
DWORD thispid = pids.winpid (i);
_pinfo *p = pids[i];
i++;
if (!p)
{
if (!nextpid && thispid != (DWORD) pid)
continue;
ep.pid = cygwin_pid (thispid);
ep.dwProcessId = thispid;
ep.process_state = PID_IN_USE;
ep.ctty = -1;
break;
}
else if (nextpid || p->pid == pid || (winpid && thispid == (DWORD) pid))
{
ep.ctty = p->ctty;
ep.pid = p->pid;
ep.ppid = p->ppid;
ep.dwProcessId = p->dwProcessId;
ep.uid = p->uid;
ep.gid = p->gid;
ep.pgid = p->pgid;
ep.sid = p->sid;
ep.umask = 0;
ep.start_time = p->start_time;
ep.rusage_self = p->rusage_self;
ep.rusage_children = p->rusage_children;
strcpy (ep.progname, p->progname);
ep.strace_mask = 0;
ep.version = EXTERNAL_PINFO_VERSION;
ep.process_state = p->process_state;
ep.uid32 = p->uid;
ep.gid32 = p->gid;
break;
}
}
if (!ep.pid)
{
i = 0;
pids.reset ();
return 0;
}
return &ep;
}
static DWORD
get_cygdrive_info (char *user, char *system, char *user_flags,
char *system_flags)
{
int res = mount_table->get_cygdrive_info (user, system, user_flags,
system_flags);
return (res == ERROR_SUCCESS) ? 1 : 0;
}
static DWORD
get_cygdrive_prefixes (char *user, char *system)
{
char user_flags[CYG_MAX_PATH];
char system_flags[CYG_MAX_PATH];
DWORD res = get_cygdrive_info (user, system, user_flags, system_flags);
return res;
}
static DWORD
check_ntsec (const char *filename)
{
if (!filename)
return allow_ntsec;
path_conv pc (filename);
return allow_ntsec && pc.has_acls ();
}
extern "C" unsigned long
cygwin_internal (cygwin_getinfo_types t, ...)
{
va_list arg;
va_start (arg, t);
switch (t)
{
case CW_LOCK_PINFO:
return 1;
case CW_UNLOCK_PINFO:
return 1;
case CW_GETTHREADNAME:
return (DWORD) cygthread::name (va_arg (arg, DWORD));
case CW_SETTHREADNAME:
{
set_errno (ENOSYS);
return 0;
}
case CW_GETPINFO:
return (DWORD) fillout_pinfo (va_arg (arg, DWORD), 0);
case CW_GETVERSIONINFO:
return (DWORD) cygwin_version_strings;
case CW_READ_V1_MOUNT_TABLES:
set_errno (ENOSYS);
return 1;
case CW_USER_DATA:
return (DWORD) &__cygwin_user_data;
case CW_PERFILE:
perfile_table = va_arg (arg, struct __cygwin_perfile *);
return 0;
case CW_GET_CYGDRIVE_PREFIXES:
{
char *user = va_arg (arg, char *);
char *system = va_arg (arg, char *);
return get_cygdrive_prefixes (user, system);
}
case CW_GETPINFO_FULL:
return (DWORD) fillout_pinfo (va_arg (arg, pid_t), 1);
case CW_INIT_EXCEPTIONS:
init_exceptions (va_arg (arg, exception_list *));
return 0;
case CW_GET_CYGDRIVE_INFO:
{
char *user = va_arg (arg, char *);
char *system = va_arg (arg, char *);
char *user_flags = va_arg (arg, char *);
char *system_flags = va_arg (arg, char *);
return get_cygdrive_info (user, system, user_flags, system_flags);
}
case CW_SET_CYGWIN_REGISTRY_NAME:
{
const char *cr = va_arg (arg, char *);
if (check_null_empty_str_errno (cr))
return (DWORD) NULL;
cygheap->cygwin_regname = (char *) crealloc (cygheap->cygwin_regname,
strlen (cr) + 1);
strcpy (cygheap->cygwin_regname, cr);
}
case CW_GET_CYGWIN_REGISTRY_NAME:
return (DWORD) cygheap->cygwin_regname;
case CW_STRACE_TOGGLE:
{
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
if (p)
{
sig_send (p, __SIGSTRACE);
return 0;
}
else
{
set_errno (ESRCH);
return (DWORD) -1;
}
}
case CW_STRACE_ACTIVE:
{
return strace.active;
}
case CW_CYGWIN_PID_TO_WINPID:
{
pinfo p (va_arg (arg, pid_t));
return p ? p->dwProcessId : 0;
}
case CW_EXTRACT_DOMAIN_AND_USER:
{
struct passwd *pw = va_arg (arg, struct passwd *);
char *domain = va_arg (arg, char *);
char *user = va_arg (arg, char *);
extract_nt_dom_user (pw, domain, user);
return 0;
}
case CW_CMDLINE:
{
size_t n;
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
return (DWORD) p->cmdline (n);
}
case CW_CHECK_NTSEC:
{
char *filename = va_arg (arg, char *);
return check_ntsec (filename);
}
case CW_GET_ERRNO_FROM_WINERROR:
{
int error = va_arg (arg, int);
int deferrno = va_arg (arg, int);
return geterrno_from_win_error (error, deferrno);
}
case CW_GET_POSIX_SECURITY_ATTRIBUTE:
{
security_descriptor sd;
int attribute = va_arg (arg, int);
PSECURITY_ATTRIBUTES psa = va_arg (arg, PSECURITY_ATTRIBUTES);
void *sd_buf = va_arg (arg, void *);
DWORD sd_buf_size = va_arg (arg, DWORD);
set_security_attribute (attribute, psa, sd);
if (!psa->lpSecurityDescriptor || sd.size () > sd_buf_size)
return sd.size ();
memcpy (sd_buf, sd, sd.size ());
psa->lpSecurityDescriptor = sd_buf;
return 0;
}
case CW_GET_SHMLBA:
{
return getshmlba ();
}
case CW_GET_UID_FROM_SID:
{
PSID psid = va_arg (arg, PSID);
cygsid sid (psid);
struct passwd *pw = internal_getpwsid (sid);
return pw ? pw->pw_uid : (__uid32_t)-1;
}
case CW_GET_GID_FROM_SID:
{
PSID psid = va_arg (arg, PSID);
cygsid sid (psid);
struct __group32 *gr = internal_getgrsid (sid);
return gr ? gr->gr_gid : (__gid32_t)-1;
}
case CW_GET_BINMODE:
{
const char *path = va_arg (arg, const char *);
path_conv p (path, PC_SYM_FOLLOW | PC_FULL | PC_NULLEMPTY);
if (p.error)
{
set_errno (p.error);
return (unsigned long) -1;
}
return p.binmode ();
}
default:
return (DWORD) -1;
}
}