libc/winsup/cygwin/external.cc
Christopher Faylor 5d97040501 *** cygwin DLL Changes:
* child_info.h (CURR_CHILD_INFO_MAGIC): Reset.
(child_info::dwProcessId): Delete.
(child_info::straced): New variable.
(child_info::handle_fork): New member function.
* dcrt0.cc (in_forkee): New global variable.
(__cygwin_user_data::forkee): Mark as obsolete.
(do_global_ctors): Use in_forkee rather than user_data->forkee.
(get_cygwin_startup_info): Ditto.  Deal with new straced field to allow strace
to deal with children of attached processes.
(initial_env): Accommodate changes to strace::hello.
(child_info_fork::handle_fork): Rename from plain old 'handle_fork'.  Move
alloc_stack() call elsewhere.
(dll_crt0_0): Fill out more of user_data.  Reference handle_fork via fork_info.
Add some debugging output.
(_dll_crt0): Don't wait for sync thread if sync_startup is invalid.  Zero
sync_startup here.  Call alloc_stack() here, if appropriate.
(dll_crt0_1): Use in_forkee rather than user_data->forkee.
(dll_crt0): Ditto.
* malloc_wrapper.cc (malloc_init): Ditto.
* dll_init.cc (in_forkee): Remove local static version of this variable.
(dll_list::load_after_fork): Don't set in_forkee here.
* external.cc (cygwin_internal): Use strace method rather than accessing field
directly.
* fhandler.cc (fhandler_base::read): Ditto.
* fhandler_tty.cc (fhandler_tty_common::__acquire_output_mutex): Ditto.
* fork.cc (frok::parent): Invoke strace write_childpid to communicate with
potential strace.
(child_copy): Add more detail to debugging output.
* init.cc (calibration_id): New static variable.
(prime_threads): Set sync_startup to invalid handle if we already know about
thread_func_ix.  Use static calibration_id to hold calibration thread id.
* munge_threadfunc (munge_threadfunc): Don't try to debug if we don't find
threadfunc_ix.
(dll_entry): Avoid calling munge_threadfunc and _cygtls::remove on non-cygwin
threads invoked during process startup.
* pinfo.cc (set_myself): Always call strace.hello here regardless of DEBUGGING.
* sigproc.cc (child_info::child_info): Remove spurious handling of dwProcessId.
Set straced as appropriate.
* spawn.cc (spawn_guts): Rename ciresrv to ch.  Invoke strace write_childpid to
communicate with potential strace.
* strace.cc: Include child_info.h.
(strace::hello): Remove inited test.  Use active() method to test if strace has
been activated.  Handle case where we are started before
(mypid): New function.
(strace::vsprntf): Try to deal more intelligently with case where progname may
not be filled out.  Put pid in parentheses if it is a windows pid rather than a
cygwin pid.  myself has been filled out.
(strace::write_childpid): New function for notifying strace about the creation
of children.
(strace::vprntf): Use strace method rather than accessing field directly.
(strace_printf): Ditto.
(strace::wm): Ditto.
* winsup.h (in_forkee): Declare.
* include/sys/strace.h (strace::write_childpid): Declare new function.
(strace::attached): Define new function.
(strace::active): Ditto.
(strace::active_val): Ditto.
(_STRACE_ON): Delete.
(_STRACE_OFF): Ditto.
(define_strace0): Use strace method rather than accessing field directly.
(strace_printf_wrap): Ditto.
(strace_printf_wrap1): Ditto.

*** cygwin utils changes:
* strace.cc (nprocesses): Make static global.
(quiet): New variable.
(strace_active): Ditto.
(add_child): Increment nprocesses here.  Don't add a child if it is already
added (windows bug?).  Report on child if not quiet.
(get_child): Just return NULL if child not found.
(remove_child): Report on child if not quiet.
(attach_process): Don't complain if given a windows process.  Use windows pid
in error.
(handle_output_debug_string): Issue error if trying to manipulate a process
that we don't know about.  Handle _STRACE_CHILD_PID - attach to reported child
when we get this.
(proc_child): Move nprocesses to file scope.  Report on exceptions.
(longopts): Implement "--quiet".
(opts): Implement "-q".
(main): Manipulate quiet flag.
* utils.sgml (strace): Add words describing '-q'.
2005-12-29 20:46:34 +00:00

320 lines
6.9 KiB
C++

/* external.cc: Interface to Cygwin internals from external programs.
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 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"
#include "child_info.h"
#include <unistd.h>
child_info *get_cygwin_startup_info ();
static winpids pids;
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 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:
/* noop */ /* 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:
case CW_GET_CYGWIN_REGISTRY_NAME:
return 0;
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 getpagesize ();
}
case CW_GET_UID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
return psid.get_id (false, NULL);
}
case CW_GET_GID_FROM_SID:
{
cygpsid psid = va_arg (arg, PSID);
return psid.get_id (true, NULL);
}
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);
return (unsigned long) -1;
}
return p.binmode ();
}
case CW_HOOK:
{
const char *name = va_arg (arg, const char *);
const void *hookfn = va_arg (arg, const void *);
WORD subsys;
return (unsigned long) hook_or_detect_cygwin (name, hookfn, subsys);
}
case CW_ARGV:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
return (unsigned long) (ci ? ci->moreinfo->argv : NULL);
}
case CW_ENVP:
{
child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
return (unsigned long) (ci ? ci->moreinfo->envp : NULL);
}
case CW_DEBUG_SELF:
error_start_init (va_arg (arg, const char *));
try_to_debug ();
break;
default:
break;
}
return (unsigned long) -1;
}