4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-01-28 10:00:23 +08:00
Christopher Faylor 44d2fc0a45 * autoload.cc: Call _api_fatal in asm.
* child_info.h: Redefine CURR_CHILD_INFO_MAGIC.
(child_info_fork::abort): Rename from handle_failure.  Change arguments.
* cygtls.h (_local_storage::ttybuf): New field.
* dcrt0.cc (vapi_fatal): Split api_fatal.  Add "in forked process" to message
when appropriate.
(api_fatal): Use vapi_fatal.
* devices.h: Make multiple inclusion safe.
(fh_devices): Add FH_CONS* stuff.  Reorder slightly.
(device): Eliminate anonymous union.  Add more ways to access minor/major.
(device::setunit): Accommodate no-longer-anonymous union.
(device::is_fs): Ditto.
(device::is_fs_special): Ditto.
(device::major): New function.
(device::minor): Ditto.
(device::is_device): New function.
(device::not_device): Ditto.
(device::operator int): New operator.
(device::operator fh_devices): Ditto.
(device::operator bool): Ditto.
(device::operator DWORD): Ditto.
(device::operator =): Ditto.
(isproc_dev): New function.
(isprocsys_dev): Ditto.
(iscons_dev): Ditto.
(istty_slave_dev): Ditto.
* devices.in: Add new "/dev/cons*" strings.  Accommodate no-longer-anonymous
union throughout.
(BRACK): Use more precise method for initialization.
* devices.cc: Regenerate.
* dtable.cc (dtable::stdio_init): Use get_cttyp instead of get_tty.
(dtable::find_archetype): Use new DWORD operator in device to test archetypes.
(dtable::init_std_file_from_handle): Use different method to initialize 'dev'.
Adapt to different ctty handling and accommodate /dev/cons*.
(fh_alloc): Accommodate no-longer-anonymous union.  Adapt to new /dev/cons*.
(build_fh_pc): Make debugging output more useful.
* exceptions.cc (ctrl_c_handler): Use get_cttyp instead of get_tty.
* external.cc (fillout_pinfo): Accommodate new cons* stuff.
* fhandler.cc (fhandler_base::read): Eliminate is_slow() test.
* fhandler.h (fhandler_base::*): Adapt to changes in device.h.
(fhandler_*::is_slow): Delete.
( fhandler_proc::get_proc_fhandler): Return fh_devices type.
* fhandler_console.cc (open_shared_console): New function.
(console_unit): New class.
(console_unit::console_unit): New constructor.
(enum_windows): New function.  Declare as friend to console_unit.
(fhandler_console::set_unit): New function.
(fhandler_console::get_tty_stuff): Call set_unit to set the unit number and
determine if initialization is needed.  Eliminate flags parameter.
(tty_list::get_cttyp): Rename (sorta) from get_tty.  Return pointer to correct
tty_min.
(fhandler_console::open): Adapt to elimination of argument to get_tty_stuff.
(fhandler_console::output_tcsetattr): Properly detect error condition.
(fhandler_console::fixup_after_fork_exec): Adapt to get_tty_stuff() setting tc
automatically.
* fhandler_proc.cc: Use FH_BAD rather than 0 throughout where using fh_devices
enum.
(fhandler_proc::get_proc_fhandler): Return fh_devices.  Adapt to devices.h
changes.
* fhandler_process.cc: Adapt to devices.h changes.  Use FH_BAD rather than 0
throughout where using fh_devices enum.
* fhandler_procnet.cc: Ditto.
* fhandler_procsys.cc: Ditto.
* fhandler_procsysvipc.cc: Ditto.
* fhandler_tape.cc (fhandler_dev_tape::fhandler_dev_tape): Ditto.
* fhandler_termios.cc (handler_termios::bg_check): Use tc->ttyname() rather
than assuming that we can construct a tty.
* fhandler_tty.cc (fhandler_tty_master::fhandler_tty_master): Just return
get_minor() of dev.
(fhandler_pty_master::process_slave_output): Add slightly more debugging info.
(fhandler_tty_slave::fhandler_tty_slave): Change name from ntty to unit.
(fhandler_pty_master::open): Ditto.
(fhandler_tty_slave::ioctl): Adapt to change which causes ctty to represent a
complete device.
(fhandler_tty_master::init_console): Add debugging for failure path.
(fhandler_pty_master::setup): Use get_unit() to retrieve unit number rather
than relying on raw ntty.
(fhandler_pty_master::setup): Ditto.
* fhandler_virtual.h (virt_tab_t): Redefine fhandler as fh_devices.
* fork.cc: Remove obsolete vfork stuff.
(frok::child): Don't assume that a ctty == 0 is valid.
* mount.cc (mount_info::conv_to_win32_path): Adapt to device struct changes.
(mount_info::conv_to_win32_path): Ditto.
* path.cc (path_conv::check): Retrive major/minor numbers via a method rather
than accessing them directly from device.  Rely on dev operators to
set/retrieve device information as required by device struct change.
* path.h (isproc_dev): Move to devices.h.
(isprocsys_dev): Ditto.
(isvirtual_dev): Ditto.
(path_conv:{isdevice,isfifo,isspecial,iscygdrive,issocket,get_devn,get_unitn}):
Use device methods to access/manipulate devices.
* pinfo.cc (pinfo::exit): Don't assume that ctty == 0 is valid.  Use iscons_dev
to determine if a device is a console.
(_pinfo::_ctty): Use device::parse to generate tty/cons name.
(_pinfo::set_ctty): Don't assume that ctty == 0 is valid.  Remove redundant
info from debugging.
* shared.cc (offsets): Remove console offset.
* shared_info.h (shared_locations): Ditto.
* syscalls.cc (umask): Use device methods to manipulate device information.
(ctermid): Use device::parse to generate term device name.
* tlsoffsets.h: Regenerate.
* tty.cc (ttyslot): Return minor number of ctty since ctty now represents a
full device.
(tty::create_master): Set ctty to a complete device.
(tty_list::attach): Rework to detect new /dev/cons* stuff.
(tty_list::terminate): Adapt to changes to ctty.
(tty_list::init): Adapt to change to setntty - pass in device major number.
(tty::exists): Use get_unit() to retrive tty unit number.
(tty::open_mutex): Ditto.
(tty::open_inuse): Ditto.
(tty::create_inuse): Ditto.
(tty::get_event): Ditto.
(tty_min::ttyname): Define new function.
* tty.h (tty_min::ntty): Redefine as fh_devices.
(tty::exists): Use get_unit() to retrive tty unit number.
(tty::open_mutex): Ditto.
(tty::open_inuse): Ditto.
(tty::create_inuse): Ditto.
(tty::get_event): Ditto.
(tty_min::ttyname): Declare new function.
(tty::getntty): Declare as const.
(tty_list::operator []): Assure that only minor part of argument is used.
* dll_init.cc (dll_list::alloc): Detect mismatch of data segments early issuing
an explicit error message if necessary.
* heap.cc (heap_init): Adapt to changes from fork->handle_failure to
fork->abort.
* pinfo.h (EXITCODE_FORK_FAILED): New enum.  (from Ryan Johnson)
* sigproc.cc (child_info_fork::abort): Rename from handle_failure.  Change
arguments to allow passing in a printf-like message.
* winsup.h (api_fatal): Delete macro definition.
(api_fatal): Redefine from __api_fatal.
(vapi_fatal): Declare new function.
* include/sys/strace.h (strace_vprintf): Define new macro.
* ntdll.h (_SYSTEM_INFORMATION_CLASS): Add SystemHandleInformation.
2011-05-28 18:17:09 +00:00

525 lines
12 KiB
C++

/* external.cc: Interface to Cygwin internals from external programs.
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011 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 "sigproc.h"
#include "pinfo.h"
#include "shared_info.h"
#include "cygwin_version.h"
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "heap.h"
#include "cygtls.h"
#include "child_info.h"
#include "environ.h"
#include "cygserver_setpwd.h"
#include <unistd.h>
#include <stdlib.h>
#include <wchar.h>
#include <iptypes.h>
child_info *get_cygwin_startup_info ();
static void exit_process (UINT, bool) __attribute__((noreturn));
static winpids pids;
static external_pinfo *
fillout_pinfo (pid_t pid, int winpid)
{
BOOL nextpid;
static external_pinfo ep;
static char ep_progname_long_buf[NT_MAX_PATH];
if ((nextpid = !!(pid & CW_NEXTPID)))
pid ^= CW_NEXTPID;
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 = iscons_dev (p->ctty) ? p->ctty : device::minor (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;
ep.progname[0] = '\0';
sys_wcstombs(ep.progname, MAX_PATH, 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;
ep.progname_long = ep_progname_long_buf;
sys_wcstombs(ep.progname_long, NT_MAX_PATH, p->progname);
break;
}
}
if (!ep.pid)
{
i = 0;
pids.reset ();
return 0;
}
return &ep;
}
static inline 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
check_ntsec (const char *filename)
{
if (!filename)
return true;
path_conv pc (filename);
return pc.has_acls ();
}
/* Copy cygwin environment variables to the Windows environment. */
static void
sync_winenv ()
{
int unused_envc;
PWCHAR envblock = NULL;
char **envp = build_env (cur_environ (), envblock, unused_envc, false);
PWCHAR p = envblock;
if (envp)
{
for (char **e = envp; *e; e++)
cfree (*e);
cfree (envp);
}
if (!p)
return;
while (*p)
{
PWCHAR eq = wcschr (p, L'=');
if (eq)
{
*eq = L'\0';
SetEnvironmentVariableW (p, ++eq);
p = eq;
}
p = wcschr (p, L'\0') + 1;
}
free (envblock);
}
/*
* Cygwin-specific wrapper for win32 ExitProcess and TerminateProcess.
* It ensures that the correct exit code, derived from the specified
* status value, will be made available to this process's parent (if
* that parent is also a cygwin process). If useTerminateProcess is
* true, then TerminateProcess(GetCurrentProcess(),...) will be used;
* otherwise, ExitProcess(...) is called.
*
* Used by startup code for cygwin processes which is linked statically
* into applications, and is not part of the cygwin DLL -- which is why
* this interface is exposed. "Normal" programs should use ANSI exit(),
* ANSI abort(), or POSIX _exit(), rather than this function -- because
* calling ExitProcess or TerminateProcess, even through this wrapper,
* skips much of the cygwin process cleanup code.
*/
static void
exit_process (UINT status, bool useTerminateProcess)
{
pid_t pid = getpid ();
external_pinfo * ep = fillout_pinfo (pid, 1);
DWORD dwpid = ep ? ep->dwProcessId : pid;
pinfo p (pid, PID_MAP_RW);
if ((dwpid == GetCurrentProcessId()) && (p->pid == ep->pid))
p.set_exit_code ((DWORD)status);
if (useTerminateProcess)
TerminateProcess (GetCurrentProcess(), status);
/* avoid 'else' clause to silence warning */
ExitProcess (status);
}
extern "C" unsigned long
cygwin_internal (cygwin_getinfo_types t, ...)
{
va_list arg;
uintptr_t res = (uintptr_t) -1;
va_start (arg, t);
switch (t)
{
case CW_LOCK_PINFO:
res = 1;
break;
case CW_UNLOCK_PINFO:
res = 1;
break;
case CW_GETTHREADNAME:
res = (DWORD) cygthread::name (va_arg (arg, DWORD));
break;
case CW_SETTHREADNAME:
{
set_errno (ENOSYS);
res = 0;
}
break;
case CW_GETPINFO:
res = (DWORD) fillout_pinfo (va_arg (arg, DWORD), 0);
break;
case CW_GETVERSIONINFO:
res = (DWORD) cygwin_version_strings;
break;
case CW_READ_V1_MOUNT_TABLES:
set_errno (ENOSYS);
res = 1;
break;
case CW_USER_DATA:
/* This is a kludge to work around a version of _cygwin_common_crt0
which overwrote the cxx_malloc field with the local DLL copy.
Hilarity ensues if the DLL is not loaded like while the process
is forking. */
__cygwin_user_data.cxx_malloc = &default_cygwin_cxx_malloc;
res = (DWORD) &__cygwin_user_data;
break;
case CW_PERFILE:
perfile_table = va_arg (arg, struct __cygwin_perfile *);
res = 0;
break;
case CW_GET_CYGDRIVE_PREFIXES:
{
char *user = va_arg (arg, char *);
char *system = va_arg (arg, char *);
res = get_cygdrive_info (user, system, NULL, NULL);
}
break;
case CW_GETPINFO_FULL:
res = (DWORD) fillout_pinfo (va_arg (arg, pid_t), 1);
break;
case CW_INIT_EXCEPTIONS:
/* noop */ /* init_exceptions (va_arg (arg, exception_list *)); */
res = 0;
break;
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 *);
res = get_cygdrive_info (user, system, user_flags, system_flags);
}
break;
case CW_SET_CYGWIN_REGISTRY_NAME:
case CW_GET_CYGWIN_REGISTRY_NAME:
res = 0;
break;
case CW_STRACE_TOGGLE:
{
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
if (p)
{
sig_send (p, __SIGSTRACE);
res = 0;
}
else
{
set_errno (ESRCH);
res = (DWORD) -1;
}
}
break;
case CW_STRACE_ACTIVE:
{
res = strace.active ();
}
break;
case CW_CYGWIN_PID_TO_WINPID:
{
pinfo p (va_arg (arg, pid_t));
res = p ? p->dwProcessId : 0;
}
break;
case CW_EXTRACT_DOMAIN_AND_USER:
{
WCHAR nt_domain[MAX_DOMAIN_NAME_LEN + 1];
WCHAR nt_user[UNLEN + 1];
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, nt_domain, nt_user);
if (domain)
sys_wcstombs (domain, MAX_DOMAIN_NAME_LEN + 1, nt_domain);
if (user)
sys_wcstombs (user, UNLEN + 1, nt_user);
res = 0;
}
break;
case CW_CMDLINE:
{
size_t n;
pid_t pid = va_arg (arg, pid_t);
pinfo p (pid);
res = (DWORD) p->cmdline (n);
}
break;
case CW_CHECK_NTSEC:
{
char *filename = va_arg (arg, char *);
res = check_ntsec (filename);
}
break;
case CW_GET_ERRNO_FROM_WINERROR:
{
int error = va_arg (arg, int);
int deferrno = va_arg (arg, int);
res = geterrno_from_win_error (error, deferrno);
}
break;
case CW_GET_POSIX_SECURITY_ATTRIBUTE:
{
path_conv dummy;
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 (dummy, attribute, psa, sd);
if (!psa->lpSecurityDescriptor)
res = sd.size ();
else
{
psa->lpSecurityDescriptor = sd_buf;
res = sd.copy (sd_buf, sd_buf_size);
}
}
break;
case CW_GET_SHMLBA:
{
res = getpagesize ();
}
break;
case CW_GET_UID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
res = psid.get_id (false, NULL);
}
break;
case CW_GET_GID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
res = psid.get_id (true, NULL);
}
break;
case CW_GET_BINMODE:
{
const char *path = va_arg (arg, const char *);
path_conv p (path, PC_SYM_FOLLOW | PC_NULLEMPTY);
if (p.error)
{
set_errno (p.error);
res = (unsigned long) -1;
}
else
res = p.binmode ();
}
break;
case CW_HOOK:
{
const char *name = va_arg (arg, const char *);
const void *hookfn = va_arg (arg, const void *);
WORD subsys;
res = (unsigned long) hook_or_detect_cygwin (name, hookfn, subsys);
}
break;
case CW_ARGV:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
res = (unsigned long) (ci ? ci->moreinfo->argv : NULL);
}
break;
case CW_ENVP:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
res = (unsigned long) (ci ? ci->moreinfo->envp : NULL);
}
break;
case CW_DEBUG_SELF:
error_start_init (va_arg (arg, const char *));
try_to_debug ();
break;
case CW_SYNC_WINENV:
sync_winenv ();
res = 0;
break;
case CW_CYGTLS_PADSIZE:
res = CYGTLS_PADSIZE;
break;
case CW_SET_DOS_FILE_WARNING:
{
extern bool dos_file_warning;
dos_file_warning = va_arg (arg, int);
res = 0;
}
break;
case CW_SET_PRIV_KEY:
{
const char *passwd = va_arg (arg, const char *);
const char *username = va_arg (arg, const char *);
res = setlsapwd (passwd, username);
}
break;
case CW_SETERRNO:
{
const char *file = va_arg (arg, const char *);
int line = va_arg (arg, int);
seterrno(file, line);
res = 0;
}
break;
case CW_EXIT_PROCESS:
{
UINT status = va_arg (arg, UINT);
int useTerminateProcess = va_arg (arg, int);
exit_process (status, !!useTerminateProcess); /* no return */
}
case CW_SET_EXTERNAL_TOKEN:
{
HANDLE token = va_arg (arg, HANDLE);
int type = va_arg (arg, int);
set_imp_token (token, type);
res = 0;
}
break;
case CW_GET_INSTKEY:
{
extern WCHAR installation_key_buf[18];
PWCHAR dest = va_arg (arg, PWCHAR);
wcscpy (dest, installation_key_buf);
res = 0;
}
break;
case CW_INT_SETLOCALE:
{
extern void internal_setlocale ();
internal_setlocale ();
res = 0;
}
break;
case CW_CVT_MNT_OPTS:
{
extern bool fstab_read_flags (char **, unsigned &, bool);
char **option_string = va_arg (arg, char **);
if (!option_string || !*option_string)
set_errno (EINVAL);
else
{
unsigned *pflags = va_arg (arg, unsigned *);
unsigned flags = pflags ? *pflags : 0;
if (fstab_read_flags (option_string, flags, true))
{
if (pflags)
*pflags = flags;
res = 0;
}
}
}
break;
case CW_LST_MNT_OPTS:
{
extern char *fstab_list_flags ();
char **option_string = va_arg (arg, char **);
if (!option_string)
set_errno (EINVAL);
else
{
*option_string = fstab_list_flags ();
if (*option_string)
res = 0;
}
}
break;
case CW_STRERROR:
{
int err = va_arg (arg, int);
res = (uintptr_t) strerror (err);
}
break;
default:
set_errno (ENOSYS);
}
va_end (arg);
return res;
}