2000-02-17 20:38:33 +01:00
|
|
|
/* pinfo.cc: process table support
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2008-04-07 18:15:45 +02:00
|
|
|
#include "miscfuncs.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdlib.h>
|
2005-01-12 23:40:46 +01:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "dtable.h"
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "environ.h"
|
2000-11-02 06:25:56 +01:00
|
|
|
#include "ntdll.h"
|
2002-10-15 09:03:45 +02:00
|
|
|
#include "shared_info.h"
|
2003-09-16 05:39:55 +02:00
|
|
|
#include "cygheap.h"
|
2004-02-12 04:01:58 +01:00
|
|
|
#include "cygmalloc.h"
|
2004-02-13 20:34:32 +01:00
|
|
|
#include "cygtls.h"
|
2008-03-07 12:24:51 +01:00
|
|
|
#include "tls_pbuf.h"
|
2004-12-03 05:46:00 +01:00
|
|
|
#include "child_info.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2008-12-19 20:09:51 +01:00
|
|
|
class pinfo_basic: public _pinfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
pinfo_basic();
|
|
|
|
};
|
|
|
|
|
2011-12-23 18:12:10 +01:00
|
|
|
pinfo_basic::pinfo_basic ()
|
2008-12-19 20:09:51 +01:00
|
|
|
{
|
|
|
|
pid = dwProcessId = GetCurrentProcessId ();
|
2015-07-19 22:38:30 +02:00
|
|
|
PWCHAR pend = wcpncpy (progname, global_progname,
|
|
|
|
sizeof (progname) / sizeof (WCHAR) - 1);
|
|
|
|
*pend = L'\0';
|
2011-12-23 18:12:10 +01:00
|
|
|
/* Default uid/gid are needed very early to initialize shared user info. */
|
|
|
|
uid = ILLEGAL_UID;
|
2014-02-09 20:44:56 +01:00
|
|
|
gid = ILLEGAL_GID;
|
2008-12-19 20:09:51 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2008-12-19 20:09:51 +01:00
|
|
|
pinfo_basic myself_initial NO_COPY;
|
|
|
|
|
|
|
|
pinfo NO_COPY myself (static_cast<_pinfo *> (&myself_initial)); // Avoid myself != NULL checks
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-05-22 06:50:54 +02:00
|
|
|
/* Setup the pinfo structure for this process. There may already be a
|
|
|
|
_pinfo for this "pid" if h != NULL. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2008-12-31 22:33:34 +01:00
|
|
|
void
|
2008-12-20 18:32:31 +01:00
|
|
|
pinfo::thisproc (HANDLE h)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2008-12-20 18:32:31 +01:00
|
|
|
procinfo = NULL;
|
|
|
|
|
2013-12-18 04:58:11 +01:00
|
|
|
DWORD flags = PID_IN_USE | PID_ACTIVE;
|
2004-09-12 05:47:57 +02:00
|
|
|
if (!h)
|
2013-12-18 04:58:11 +01:00
|
|
|
{
|
|
|
|
h = INVALID_HANDLE_VALUE;
|
|
|
|
cygheap->pid = cygwin_pid (myself_initial.pid);
|
|
|
|
flags |= PID_NEW;
|
|
|
|
}
|
2008-12-19 20:09:51 +01:00
|
|
|
|
2013-12-18 04:58:11 +01:00
|
|
|
init (cygheap->pid, flags, h);
|
2008-12-20 18:32:31 +01:00
|
|
|
procinfo->process_state |= PID_IN_USE;
|
|
|
|
procinfo->dwProcessId = myself_initial.pid;
|
2010-09-01 23:06:36 +02:00
|
|
|
procinfo->sendsig = myself_initial.sendsig;
|
2010-05-18 16:30:51 +02:00
|
|
|
wcscpy (procinfo->progname, myself_initial.progname);
|
2011-11-24 22:36:53 +01:00
|
|
|
debug_printf ("myself dwProcessId %u", procinfo->dwProcessId);
|
2013-12-18 04:58:11 +01:00
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
2004-11-26 05:15:10 +01:00
|
|
|
{
|
|
|
|
/* here if execed */
|
|
|
|
static pinfo NO_COPY myself_identity;
|
2008-12-20 18:32:31 +01:00
|
|
|
myself_identity.init (cygwin_pid (procinfo->dwProcessId), PID_EXECED, NULL);
|
|
|
|
procinfo->exec_sendsig = NULL;
|
|
|
|
procinfo->exec_dwProcessId = 0;
|
2013-12-18 05:28:46 +01:00
|
|
|
myself_identity->ppid = procinfo->pid;
|
2004-11-26 05:15:10 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the process table entry for the current task.
|
2002-01-29 03:02:03 +01:00
|
|
|
This is not called for forked tasks, only execed ones. */
|
2000-02-17 20:38:33 +01:00
|
|
|
void __stdcall
|
2000-10-17 01:55:58 +02:00
|
|
|
pinfo_init (char **envp, int envc)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-09-03 06:16:35 +02:00
|
|
|
if (envp)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2000-10-17 01:55:58 +02:00
|
|
|
environ_init (envp, envc);
|
2000-02-17 20:38:33 +01:00
|
|
|
/* spawn has already set up a pid structure for us so we'll use that */
|
|
|
|
myself->process_state |= PID_CYGPARENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Invent our own pid. */
|
|
|
|
|
2008-12-20 18:32:31 +01:00
|
|
|
myself.thisproc (NULL);
|
2000-07-29 18:24:59 +02:00
|
|
|
myself->pgid = myself->sid = myself->pid;
|
2000-02-17 20:38:33 +01:00
|
|
|
myself->ctty = -1;
|
* (child_info.h, cygheap.h, dcrt0.cc, dir.cc, fhandler.cc, fhandler.h,
fhandler_clipboard.cc, fhandler_disk_file.cc, fhandler_dsp.cc,
fhandler_floppy.cc, fhandler_mem.cc, fhandler_random.cc,
fhandler_tape.cc, fhandler_zero.cc, grp.cc, mmap.cc, passwd.cc,
pinfo.cc, pinfo.h, pipe.cc, sec_acl.cc, sec_helper.cc, security.cc,
security.h, spawn.cc, syscalls.cc, thread.h, uinfo.cc, winsup.h):
Change usage of uid_t to __uid16_t, gid_t to __gid16_t and
off_t to __off32_t throughout. Use INVALID_UID, INVALID_GID and
INVALID_SEEK instead casting -1 to the appropriate type.
* winsup.h: Define INVALID_UID, INVALID_GID and INVALID_SEEK.
* include/cygwin/acl.h: Define internal __aclent16_t and __aclent32_t
types. Don't declare acl functions when compiling Cygwin.
* include/cygwin/grp.h: Declare getgrgid() and getgrnam() with
correct types for internal usage.
2002-02-10 14:38:51 +01:00
|
|
|
myself->uid = ILLEGAL_UID;
|
2014-02-09 20:44:56 +01:00
|
|
|
myself->gid = ILLEGAL_GID;
|
2000-10-17 01:55:58 +02:00
|
|
|
environ_init (NULL, 0); /* call after myself has been set up */
|
2009-12-18 21:32:04 +01:00
|
|
|
myself->nice = winprio_to_nice (GetPriorityClass (GetCurrentProcess ()));
|
2013-12-18 04:58:11 +01:00
|
|
|
myself->ppid = 1; /* always set last */
|
2005-01-25 23:45:11 +01:00
|
|
|
debug_printf ("Set nice to %d", myself->nice);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2010-09-01 23:06:36 +02:00
|
|
|
myself->process_state |= PID_ACTIVE;
|
2011-05-05 17:03:54 +02:00
|
|
|
myself->process_state &= ~(PID_INITIALIZING | PID_EXITED | PID_REAPED);
|
2012-03-20 03:08:14 +01:00
|
|
|
myself.preserve ();
|
2013-12-18 04:58:11 +01:00
|
|
|
debug_printf ("pid %d, pgid %d, process_state %y", myself->pid, myself->pgid, myself->process_state);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2013-03-29 17:19:11 +01:00
|
|
|
DWORD
|
|
|
|
pinfo::status_exit (DWORD x)
|
2008-03-27 02:50:40 +01:00
|
|
|
{
|
|
|
|
switch (x)
|
|
|
|
{
|
|
|
|
case STATUS_DLL_NOT_FOUND:
|
|
|
|
{
|
2013-03-29 17:19:11 +01:00
|
|
|
path_conv pc;
|
|
|
|
if (!procinfo)
|
2015-07-19 22:38:30 +02:00
|
|
|
pc.check ("/dev/null", PC_NOWARN | PC_POSIX);
|
2013-03-29 17:19:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
UNICODE_STRING uc;
|
|
|
|
RtlInitUnicodeString(&uc, procinfo->progname);
|
2015-07-19 22:38:30 +02:00
|
|
|
pc.check (&uc, PC_NOWARN | PC_POSIX);
|
2013-03-29 17:19:11 +01:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
small_printf ("%s: error while loading shared libraries: %s: cannot "
|
|
|
|
"open shared object file: No such file or directory\n",
|
2015-07-19 22:38:30 +02:00
|
|
|
pc.get_posix (), find_first_notloaded_dll (pc));
|
2011-11-05 19:29:51 +01:00
|
|
|
x = 127 << 8;
|
2008-03-27 02:50:40 +01:00
|
|
|
}
|
|
|
|
break;
|
2009-10-07 17:47:38 +02:00
|
|
|
case STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION: /* custom error value */
|
|
|
|
/* We've already printed the error message in pseudo-reloc.c */
|
2011-11-05 19:29:51 +01:00
|
|
|
x = 127 << 8;
|
|
|
|
break;
|
|
|
|
case STATUS_ACCESS_VIOLATION:
|
|
|
|
x = SIGSEGV;
|
2009-10-07 17:47:38 +02:00
|
|
|
break;
|
2011-11-05 19:47:39 +01:00
|
|
|
case STATUS_ILLEGAL_INSTRUCTION:
|
|
|
|
x = SIGILL;
|
|
|
|
break;
|
2013-05-24 15:32:10 +02:00
|
|
|
case STATUS_NO_MEMORY:
|
|
|
|
/* If the PATH environment variable is longer than about 30K and the full
|
|
|
|
Windows environment is > 32K, startup of an exec'ed process fails with
|
|
|
|
STATUS_NO_MEMORY. This happens with all Cygwin executables, as well
|
|
|
|
as, for instance, notepad, but it does not happen with CMD for some
|
2013-05-24 15:43:26 +02:00
|
|
|
reason (but note, the environment *in* CMD is broken and shortened).
|
|
|
|
This occurs at a point where there's no return to the exec'ing parent
|
|
|
|
process, so we have to find some way to inform the user what happened.
|
2013-05-24 15:32:10 +02:00
|
|
|
|
|
|
|
FIXME: For now, just return with SIGBUS set. Maybe it's better to add
|
|
|
|
a lengthy small_printf instead. */
|
|
|
|
x = SIGBUS;
|
|
|
|
break;
|
2008-03-27 02:50:40 +01:00
|
|
|
default:
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("*** STATUS_%y\n", x);
|
2011-11-05 19:29:51 +01:00
|
|
|
x = 127 << 8;
|
2008-03-27 02:50:40 +01:00
|
|
|
}
|
2011-11-05 19:29:51 +01:00
|
|
|
return EXITCODE_SET | x;
|
2008-03-27 02:50:40 +01:00
|
|
|
}
|
|
|
|
|
2005-01-11 16:31:04 +01:00
|
|
|
# define self (*this)
|
2009-10-06 23:51:17 +02:00
|
|
|
void
|
|
|
|
pinfo::set_exit_code (DWORD x)
|
|
|
|
{
|
|
|
|
if (x >= 0xc0000000UL)
|
2011-11-05 19:29:51 +01:00
|
|
|
self->exitcode = status_exit (x);
|
|
|
|
else
|
|
|
|
self->exitcode = EXITCODE_SET | (sigExeced ?: (x & 0xff) << 8);
|
2009-10-06 23:51:17 +02:00
|
|
|
}
|
|
|
|
|
2000-10-15 03:37:07 +02:00
|
|
|
void
|
2005-01-17 05:12:08 +01:00
|
|
|
pinfo::maybe_set_exit_code_from_windows ()
|
2005-01-11 16:31:04 +01:00
|
|
|
{
|
|
|
|
DWORD x = 0xdeadbeef;
|
|
|
|
DWORD oexitcode = self->exitcode;
|
2005-09-20 04:06:57 +02:00
|
|
|
|
2005-01-16 18:00:27 +01:00
|
|
|
if (hProcess && !(self->exitcode & EXITCODE_SET))
|
2005-01-11 16:31:04 +01:00
|
|
|
{
|
2008-03-27 02:50:40 +01:00
|
|
|
WaitForSingleObject (hProcess, INFINITE); /* just to be safe, in case
|
|
|
|
process hasn't quite exited
|
|
|
|
after closing pipe */
|
2005-01-11 16:31:04 +01:00
|
|
|
GetExitCodeProcess (hProcess, &x);
|
2009-10-06 23:51:17 +02:00
|
|
|
set_exit_code (x);
|
2005-01-11 16:31:04 +01:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
sigproc_printf ("pid %d, exit value - old %y, windows %y, cygwin %y",
|
2005-01-22 22:17:53 +01:00
|
|
|
self->pid, oexitcode, x, self->exitcode);
|
2005-01-11 16:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pinfo::exit (DWORD n)
|
2000-10-15 03:37:07 +02:00
|
|
|
{
|
2013-01-14 07:03:59 +01:00
|
|
|
debug_only_printf ("winpid %d, exit %d", GetCurrentProcessId (), n);
|
|
|
|
proc_terminate ();
|
2011-11-26 03:35:49 +01:00
|
|
|
lock_process until_exit (true);
|
2004-02-13 20:34:32 +01:00
|
|
|
cygthread::terminate ();
|
2005-10-18 01:27:00 +02:00
|
|
|
|
2005-01-16 18:00:27 +01:00
|
|
|
if (n != EXITCODE_NOSET)
|
2005-09-13 19:08:54 +02:00
|
|
|
self->exitcode = EXITCODE_SET | n;/* We're really exiting. Record the UNIX exit code. */
|
2005-09-20 04:06:57 +02:00
|
|
|
else
|
2013-01-14 07:03:59 +01:00
|
|
|
maybe_set_exit_code_from_windows (); /* may block */
|
|
|
|
exit_state = ES_FINAL;
|
2005-09-20 04:06:57 +02:00
|
|
|
|
2011-05-28 20:17:09 +02:00
|
|
|
if (myself->ctty > 0 && !iscons_dev (myself->ctty))
|
2010-10-23 20:55:01 +02:00
|
|
|
{
|
2010-10-23 20:57:43 +02:00
|
|
|
lock_ttys here;
|
2011-05-28 20:17:09 +02:00
|
|
|
tty *t = cygwin_shared->tty[device::minor(myself->ctty)];
|
2010-10-23 20:55:01 +02:00
|
|
|
if (!t->slave_alive ())
|
|
|
|
t->setpgid (0);
|
|
|
|
}
|
2005-01-11 16:31:04 +01:00
|
|
|
|
|
|
|
/* FIXME: There is a potential race between an execed process and its
|
2005-09-23 05:35:41 +02:00
|
|
|
parent here. I hated to add a mutex just for that, though. */
|
2005-01-11 16:31:04 +01:00
|
|
|
struct rusage r;
|
2009-12-18 21:32:04 +01:00
|
|
|
fill_rusage (&r, GetCurrentProcess ());
|
2005-01-11 16:31:04 +01:00
|
|
|
add_rusage (&self->rusage_self, &r);
|
2005-03-08 15:31:21 +01:00
|
|
|
int exitcode = self->exitcode & 0xffff;
|
|
|
|
if (!self->cygstarted)
|
2006-01-02 04:14:56 +01:00
|
|
|
exitcode = ((exitcode & 0xff) << 8) | ((exitcode >> 8) & 0xff);
|
2013-04-23 11:44:36 +02:00
|
|
|
sigproc_printf ("Calling ExitProcess n %y, exitcode %y", n, exitcode);
|
2011-11-28 17:00:07 +01:00
|
|
|
if (!TerminateProcess (GetCurrentProcess (), exitcode))
|
|
|
|
system_printf ("TerminateProcess failed, %E");
|
2005-09-24 01:37:52 +02:00
|
|
|
ExitProcess (exitcode);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2005-01-11 16:31:04 +01:00
|
|
|
# undef self
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
inline void
|
|
|
|
pinfo::_pinfo_release ()
|
|
|
|
{
|
|
|
|
if (procinfo)
|
|
|
|
{
|
|
|
|
void *unmap_procinfo = procinfo;
|
|
|
|
procinfo = NULL;
|
|
|
|
UnmapViewOfFile (unmap_procinfo);
|
|
|
|
}
|
|
|
|
HANDLE close_h;
|
|
|
|
if (h)
|
|
|
|
{
|
|
|
|
close_h = h;
|
|
|
|
h = NULL;
|
|
|
|
ForceCloseHandle1 (close_h, pinfo_shared_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
void
|
2005-01-22 22:17:53 +01:00
|
|
|
pinfo::init (pid_t n, DWORD flag, HANDLE h0)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2005-04-29 01:59:44 +02:00
|
|
|
shared_locations shloc;
|
2005-01-26 01:15:11 +01:00
|
|
|
h = NULL;
|
2005-03-08 06:05:02 +01:00
|
|
|
if (myself && !(flag & PID_EXECED)
|
|
|
|
&& (n == myself->pid || (DWORD) n == myself->dwProcessId))
|
2000-07-29 18:24:59 +02:00
|
|
|
{
|
2000-08-26 05:48:37 +02:00
|
|
|
procinfo = myself;
|
2000-07-29 18:24:59 +02:00
|
|
|
destroy = 0;
|
|
|
|
return;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-02-26 21:50:25 +01:00
|
|
|
int createit = flag & (PID_IN_USE | PID_EXECED);
|
2005-01-26 01:15:11 +01:00
|
|
|
DWORD access = FILE_MAP_READ
|
|
|
|
| (flag & (PID_IN_USE | PID_EXECED | PID_MAP_RW)
|
|
|
|
? FILE_MAP_WRITE : 0);
|
2011-11-14 02:29:49 +01:00
|
|
|
if (!h0 || myself.h)
|
2005-04-29 01:59:44 +02:00
|
|
|
shloc = (flag & (PID_IN_USE | PID_EXECED)) ? SH_JUSTCREATE : SH_JUSTOPEN;
|
2002-10-14 22:25:52 +02:00
|
|
|
else
|
|
|
|
{
|
2005-04-29 01:59:44 +02:00
|
|
|
shloc = SH_MYSELF;
|
2005-01-26 01:15:11 +01:00
|
|
|
if (h0 == INVALID_HANDLE_VALUE)
|
|
|
|
h0 = NULL;
|
2002-10-14 22:25:52 +02:00
|
|
|
}
|
|
|
|
|
2005-01-26 01:15:11 +01:00
|
|
|
procinfo = NULL;
|
2005-06-07 21:31:42 +02:00
|
|
|
PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
|
2005-04-29 01:59:44 +02:00
|
|
|
PSECURITY_ATTRIBUTES sec_attribs = sec_user_nih (sa_buf, cygheap->user.sid(),
|
|
|
|
well_known_world_sid,
|
|
|
|
FILE_MAP_READ);
|
|
|
|
|
2005-01-26 01:15:11 +01:00
|
|
|
for (int i = 0; i < 20; i++)
|
2000-10-15 03:37:07 +02:00
|
|
|
{
|
2005-04-29 01:59:44 +02:00
|
|
|
DWORD mapsize;
|
|
|
|
if (flag & PID_EXECED)
|
|
|
|
mapsize = PINFO_REDIR_SIZE;
|
|
|
|
else
|
|
|
|
mapsize = sizeof (_pinfo);
|
|
|
|
|
2010-03-09 22:26:55 +01:00
|
|
|
procinfo = (_pinfo *) open_shared (L"cygpid", n, h0, mapsize, &shloc,
|
2005-04-29 01:59:44 +02:00
|
|
|
sec_attribs, access);
|
2005-01-26 01:15:11 +01:00
|
|
|
if (!h0)
|
|
|
|
{
|
2005-04-29 01:59:44 +02:00
|
|
|
if (createit)
|
|
|
|
__seterrno ();
|
|
|
|
return;
|
2000-10-15 03:37:07 +02:00
|
|
|
}
|
2005-01-22 22:17:53 +01:00
|
|
|
|
|
|
|
if (!procinfo)
|
2004-07-24 21:18:58 +02:00
|
|
|
{
|
2005-01-22 22:17:53 +01:00
|
|
|
if (exit_state)
|
2004-07-24 21:18:58 +02:00
|
|
|
return;
|
|
|
|
|
2012-11-11 11:56:01 +01:00
|
|
|
if (GetLastError () == ERROR_INVALID_HANDLE)
|
|
|
|
api_fatal ("MapViewOfFileEx h0 %p, i %d failed, %E", h0, i);
|
|
|
|
|
2005-07-29 16:33:50 +02:00
|
|
|
debug_printf ("MapViewOfFileEx h0 %p, i %d failed, %E", h0, i);
|
2010-03-13 00:13:48 +01:00
|
|
|
yield ();
|
2005-01-22 22:17:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-04-29 01:59:44 +02:00
|
|
|
bool created = shloc != SH_JUSTOPEN;
|
|
|
|
|
2012-09-17 22:07:49 +02:00
|
|
|
/* Detect situation where a transitional memory block is being retrieved.
|
|
|
|
If the block has been allocated with PINFO_REDIR_SIZE but not yet
|
|
|
|
updated with a PID_EXECED state then we'll retry. */
|
2017-04-24 17:34:31 +02:00
|
|
|
if (!created && !(flag & PID_NEW) && !procinfo->ppid)
|
|
|
|
{
|
|
|
|
/* Fetching process info for /proc or ps? just ignore this one. */
|
|
|
|
if (flag & PID_NOREDIR)
|
|
|
|
break;
|
2017-04-24 17:35:06 +02:00
|
|
|
/* FIXME: Do we ever hit this case? And if so, in what situation? */
|
|
|
|
system_printf ("This shouldn't happen:\n"
|
|
|
|
" me: (%d, %d, %d, %W)\n"
|
|
|
|
" pid %d\n"
|
|
|
|
" process_state %y\n"
|
|
|
|
" cygstarted %d\n"
|
|
|
|
" dwProcessId %d\n"
|
|
|
|
" name %W",
|
|
|
|
myself->pid, myself->dwProcessId, myself->cygstarted,
|
|
|
|
myself->progname,
|
|
|
|
procinfo->pid, procinfo->process_state,
|
|
|
|
procinfo->cygstarted, procinfo->dwProcessId,
|
|
|
|
procinfo->progname);
|
2017-04-24 17:34:31 +02:00
|
|
|
/* If not populated, wait 2 seconds for procinfo to become populated.
|
|
|
|
Would like to wait with finer granularity but that is not easily
|
|
|
|
doable. */
|
|
|
|
for (int i = 0; i < 200 && !procinfo->ppid; i++)
|
|
|
|
Sleep (10);
|
|
|
|
}
|
2012-09-17 22:07:49 +02:00
|
|
|
|
2012-05-08 17:06:43 +02:00
|
|
|
if (!created && createit && (procinfo->process_state & PID_REAPED))
|
|
|
|
{
|
|
|
|
memset (procinfo, 0, sizeof (*procinfo));
|
|
|
|
created = true; /* Lie that we created this - just reuse old
|
|
|
|
shared memory */
|
2012-08-17 01:34:45 +02:00
|
|
|
}
|
2012-05-08 17:06:43 +02:00
|
|
|
|
2012-05-07 17:05:56 +02:00
|
|
|
if ((procinfo->process_state & PID_REAPED)
|
|
|
|
|| ((procinfo->process_state & PID_INITIALIZING) && (flag & PID_NOREDIR)
|
|
|
|
&& cygwin_pid (procinfo->dwProcessId) != procinfo->pid))
|
2000-10-20 06:20:21 +02:00
|
|
|
{
|
2005-01-22 22:17:53 +01:00
|
|
|
set_errno (ESRCH);
|
|
|
|
break;
|
2000-10-20 06:20:21 +02:00
|
|
|
}
|
|
|
|
|
2000-10-15 03:37:07 +02:00
|
|
|
if (procinfo->process_state & PID_EXECED)
|
|
|
|
{
|
|
|
|
pid_t realpid = procinfo->pid;
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("execed process windows pid %u, cygwin pid %d", n, realpid);
|
2000-10-15 03:37:07 +02:00
|
|
|
if (realpid == n)
|
|
|
|
api_fatal ("retrieval of execed process info for pid %d failed due to recursion.", n);
|
2005-01-22 22:17:53 +01:00
|
|
|
|
2005-01-26 01:15:11 +01:00
|
|
|
n = realpid;
|
|
|
|
CloseHandle (h0);
|
|
|
|
h0 = NULL;
|
2005-01-22 22:17:53 +01:00
|
|
|
goto loop;
|
2000-10-15 03:37:07 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2005-12-22 04:16:36 +01:00
|
|
|
/* In certain pathological cases, it is possible for the shared memory
|
|
|
|
region to exist for a while after a process has exited. This should
|
|
|
|
only be a brief occurrence, so rather than introduce some kind of
|
|
|
|
locking mechanism, just loop. */
|
2011-05-05 17:03:54 +02:00
|
|
|
if (!created && createit && (procinfo->process_state & (PID_EXITED | PID_REAPED)))
|
2005-09-22 23:10:07 +02:00
|
|
|
{
|
2005-12-22 04:16:36 +01:00
|
|
|
debug_printf ("looping because pid %d, procinfo->pid %d, "
|
2011-05-05 17:03:54 +02:00
|
|
|
"procinfo->dwProcessid %u has PID_EXITED|PID_REAPED set",
|
2005-09-22 23:10:07 +02:00
|
|
|
n, procinfo->pid, procinfo->dwProcessId);
|
|
|
|
goto loop;
|
|
|
|
}
|
2000-10-15 03:37:07 +02:00
|
|
|
|
2013-12-18 04:58:11 +01:00
|
|
|
if (flag & PID_NEW)
|
|
|
|
procinfo->start_time = time (NULL);
|
2000-10-15 03:37:07 +02:00
|
|
|
if (!created)
|
|
|
|
/* nothing */;
|
2000-10-21 06:53:49 +02:00
|
|
|
else if (!(flag & PID_EXECED))
|
2005-01-16 18:00:27 +01:00
|
|
|
procinfo->pid = n;
|
2000-07-29 18:24:59 +02:00
|
|
|
else
|
|
|
|
{
|
2000-08-26 05:48:37 +02:00
|
|
|
procinfo->process_state |= PID_IN_USE | PID_EXECED;
|
2000-11-03 05:27:03 +01:00
|
|
|
procinfo->pid = myself->pid;
|
2000-07-29 18:24:59 +02:00
|
|
|
}
|
2004-11-26 05:15:10 +01:00
|
|
|
|
2005-01-22 22:17:53 +01:00
|
|
|
h = h0; /* Success! */
|
2000-10-15 03:37:07 +02:00
|
|
|
break;
|
2005-01-22 22:17:53 +01:00
|
|
|
|
|
|
|
loop:
|
2011-11-14 02:29:49 +01:00
|
|
|
_pinfo_release ();
|
2005-02-26 21:50:25 +01:00
|
|
|
if (h0)
|
2010-03-13 00:13:48 +01:00
|
|
|
yield ();
|
2005-01-22 22:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h)
|
2005-01-26 01:15:11 +01:00
|
|
|
{
|
|
|
|
destroy = 1;
|
|
|
|
ProtectHandle1 (h, pinfo_shared_handle);
|
|
|
|
}
|
2005-01-22 22:17:53 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
h = h0;
|
2011-11-14 02:29:49 +01:00
|
|
|
_pinfo_release ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
}
|
2000-10-09 04:53:44 +02:00
|
|
|
|
2003-09-27 03:58:23 +02:00
|
|
|
void
|
|
|
|
pinfo::set_acl()
|
|
|
|
{
|
2005-06-07 21:31:42 +02:00
|
|
|
PACL acl_buf = (PACL) alloca (1024);
|
2003-09-27 03:58:23 +02:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
2007-07-19 10:36:32 +02:00
|
|
|
NTSTATUS status;
|
2003-11-28 21:55:59 +01:00
|
|
|
|
2005-06-07 21:31:42 +02:00
|
|
|
sec_acl (acl_buf, true, true, cygheap->user.sid (),
|
2003-09-27 03:58:23 +02:00
|
|
|
well_known_world_sid, FILE_MAP_READ);
|
2011-04-28 11:30:36 +02:00
|
|
|
RtlCreateSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
|
2011-04-28 17:54:47 +02:00
|
|
|
status = RtlSetDaclSecurityDescriptor (&sd, TRUE, acl_buf, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("RtlSetDaclSecurityDescriptor %y", status);
|
2007-07-19 10:36:32 +02:00
|
|
|
else if ((status = NtSetSecurityObject (h, DACL_SECURITY_INFORMATION, &sd)))
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("NtSetSecurityObject %y", status);
|
2003-09-27 03:58:23 +02:00
|
|
|
}
|
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
pinfo::pinfo (HANDLE parent, pinfo_minimal& from, pid_t pid):
|
|
|
|
pinfo_minimal (), destroy (false), procinfo (NULL), waiter_ready (false),
|
2011-12-18 00:39:47 +01:00
|
|
|
wait_thread (NULL)
|
2011-11-14 02:29:49 +01:00
|
|
|
{
|
|
|
|
HANDLE herr;
|
|
|
|
const char *duperr = NULL;
|
|
|
|
if (!DuplicateHandle (parent, herr = from.rd_proc_pipe, GetCurrentProcess (),
|
|
|
|
&rd_proc_pipe, 0, false, DUPLICATE_SAME_ACCESS))
|
|
|
|
duperr = "couldn't duplicate parent rd_proc_pipe handle %p for forked child %d after exec, %E";
|
|
|
|
else if (!DuplicateHandle (parent, herr = from.hProcess, GetCurrentProcess (),
|
|
|
|
&hProcess, 0, false, DUPLICATE_SAME_ACCESS))
|
|
|
|
duperr = "couldn't duplicate parent process handle %p for forked child %d after exec, %E";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h = NULL;
|
|
|
|
DuplicateHandle (parent, from.h, GetCurrentProcess (), &h, 0, false,
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
|
|
|
init (pid, PID_MAP_RW, h);
|
|
|
|
if (*this)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duperr)
|
|
|
|
debug_printf (duperr, herr, pid);
|
|
|
|
|
|
|
|
/* Returning with procinfo == NULL. Any open handles will be closed by the
|
|
|
|
destructor. */
|
|
|
|
}
|
|
|
|
|
2005-11-14 05:28:45 +01:00
|
|
|
const char *
|
|
|
|
_pinfo::_ctty (char *buf)
|
|
|
|
{
|
2011-05-28 20:17:09 +02:00
|
|
|
if (ctty <= 0)
|
2005-11-14 05:28:45 +01:00
|
|
|
strcpy (buf, "no ctty");
|
|
|
|
else
|
2011-05-28 20:17:09 +02:00
|
|
|
{
|
|
|
|
device d;
|
|
|
|
d.parse (ctty);
|
2016-06-23 16:56:41 +02:00
|
|
|
__small_sprintf (buf, "ctty %s", d.name ());
|
2011-05-28 20:17:09 +02:00
|
|
|
}
|
2005-11-14 05:28:45 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-10-16 00:37:30 +02:00
|
|
|
bool
|
|
|
|
_pinfo::set_ctty (fhandler_termios *fh, int flags)
|
2003-12-07 23:37:12 +01:00
|
|
|
{
|
2011-10-16 00:37:30 +02:00
|
|
|
tty_min& tc = *fh->tc ();
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("old %s, ctty device number %y, tc.ntty device number %y flags & O_NOCTTY %y", __ctty (), ctty, tc.ntty, flags & O_NOCTTY);
|
2016-03-19 18:45:53 +01:00
|
|
|
if (fh && (ctty <= 0 || ctty == tc.ntty) && !(flags & O_NOCTTY))
|
2003-12-07 23:37:12 +01:00
|
|
|
{
|
2011-10-16 00:37:30 +02:00
|
|
|
ctty = tc.ntty;
|
2011-05-06 00:30:53 +02:00
|
|
|
if (cygheap->ctty != fh->archetype)
|
|
|
|
{
|
2011-05-28 20:17:09 +02:00
|
|
|
debug_printf ("cygheap->ctty %p, archetype %p", cygheap->ctty, fh->archetype);
|
2011-05-06 00:30:53 +02:00
|
|
|
if (!cygheap->ctty)
|
|
|
|
syscall_printf ("ctty was NULL");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syscall_printf ("ctty %p, usecount %d", cygheap->ctty,
|
|
|
|
cygheap->ctty->archetype_usecount (0));
|
|
|
|
cygheap->ctty->close ();
|
|
|
|
}
|
|
|
|
cygheap->ctty = (fhandler_termios *) fh->archetype;
|
|
|
|
if (cygheap->ctty)
|
|
|
|
{
|
|
|
|
fh->archetype_usecount (1);
|
|
|
|
/* guard ctty fh */
|
|
|
|
report_tty_counts (cygheap->ctty, "ctty", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-03 22:32:07 +02:00
|
|
|
lock_ttys here;
|
|
|
|
syscall_printf ("attaching %s sid %d, pid %d, pgid %d, tty->pgid %d, tty->sid %d",
|
2011-10-16 00:37:30 +02:00
|
|
|
__ctty (), sid, pid, pgid, tc.getpgid (), tc.getsid ());
|
2011-06-04 02:12:29 +02:00
|
|
|
if (!cygwin_finished_initializing && !myself->cygstarted
|
2012-01-08 07:24:17 +01:00
|
|
|
&& pgid == pid && tc.getpgid () && tc.getsid ())
|
2012-02-14 20:08:20 +01:00
|
|
|
pgid = tc.getpgid ();
|
2003-12-07 23:37:12 +01:00
|
|
|
|
2012-01-08 07:24:17 +01:00
|
|
|
/* May actually need to do this:
|
|
|
|
|
|
|
|
if (sid == pid && !tc.getsid () || !procinfo (tc.getsid ())->exists)
|
|
|
|
|
|
|
|
but testing for process existence is expensive so we avoid it until
|
|
|
|
an obvious bug surfaces. */
|
|
|
|
if (sid == pid && !tc.getsid ())
|
|
|
|
tc.setsid (sid);
|
|
|
|
sid = tc.getsid ();
|
|
|
|
/* See above */
|
|
|
|
if (!tc.getpgid () && pgid == pid)
|
|
|
|
tc.setpgid (pgid);
|
2003-12-07 23:37:12 +01:00
|
|
|
}
|
2016-11-24 15:06:51 +01:00
|
|
|
debug_printf ("cygheap->ctty now %p, archetype %p", cygheap->ctty, fh ? fh->archetype : NULL);
|
2011-10-16 00:37:30 +02:00
|
|
|
return ctty > 0;
|
2003-12-07 23:37:12 +01:00
|
|
|
}
|
|
|
|
|
2005-02-27 05:30:08 +01:00
|
|
|
/* Test to determine if a process really exists and is processing signals.
|
|
|
|
*/
|
2013-05-01 03:20:37 +02:00
|
|
|
bool __reg1
|
2005-02-27 05:30:08 +01:00
|
|
|
_pinfo::exists ()
|
|
|
|
{
|
2016-04-05 10:26:06 +02:00
|
|
|
return this && process_state && !(process_state & (PID_EXITED | PID_REAPED | PID_EXECED));
|
2005-02-27 05:30:08 +01:00
|
|
|
}
|
|
|
|
|
2002-10-30 22:05:18 +01:00
|
|
|
bool
|
|
|
|
_pinfo::alive ()
|
|
|
|
{
|
2016-06-27 17:56:16 +02:00
|
|
|
HANDLE h = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, false,
|
|
|
|
dwProcessId);
|
2002-10-30 22:05:18 +01:00
|
|
|
if (h)
|
|
|
|
CloseHandle (h);
|
|
|
|
return !!h;
|
|
|
|
}
|
|
|
|
|
2005-10-18 01:27:00 +02:00
|
|
|
DWORD WINAPI
|
|
|
|
commune_process (void *arg)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
2005-10-18 01:27:00 +02:00
|
|
|
siginfo_t& si = *((siginfo_t *) arg);
|
2008-03-07 12:24:51 +01:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *path = tp.c_get ();
|
2002-10-30 22:05:18 +01:00
|
|
|
DWORD nr;
|
2005-09-29 00:56:47 +02:00
|
|
|
HANDLE& tothem = si._si_commune._si_write_handle;
|
|
|
|
HANDLE process_sync =
|
|
|
|
OpenSemaphore (SYNCHRONIZE, false, shared_name (path, "commune", si.si_pid));
|
|
|
|
if (process_sync) // FIXME: this test shouldn't be necessary
|
|
|
|
ProtectHandle (process_sync);
|
2002-10-30 22:05:18 +01:00
|
|
|
|
2012-12-21 19:52:00 +01:00
|
|
|
lock_process now;
|
2006-03-20 02:59:23 +01:00
|
|
|
if (si._si_commune._si_code & PICOM_EXTRASTR)
|
|
|
|
si._si_commune._si_str = (char *) (&si + 1);
|
2005-10-03 19:23:54 +02:00
|
|
|
|
2005-09-29 00:56:47 +02:00
|
|
|
switch (si._si_commune._si_code)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
|
|
|
case PICOM_CMDLINE:
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
sigproc_printf ("processing PICOM_CMDLINE");
|
2006-07-19 18:00:28 +02:00
|
|
|
unsigned n = 0;
|
2003-09-05 03:55:01 +02:00
|
|
|
const char *argv[__argc_safe + 1];
|
2004-06-07 06:26:32 +02:00
|
|
|
|
2003-09-05 03:55:01 +02:00
|
|
|
for (int i = 0; i < __argc_safe; i++)
|
|
|
|
{
|
2003-12-03 17:35:52 +01:00
|
|
|
if (IsBadStringPtr (__argv[i], INT32_MAX))
|
2003-09-05 03:55:01 +02:00
|
|
|
argv[i] = "";
|
|
|
|
else
|
|
|
|
argv[i] = __argv[i];
|
|
|
|
n += strlen (argv[i]) + 1;
|
|
|
|
}
|
|
|
|
argv[__argc_safe] = NULL;
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
|
|
|
/*__seterrno ();*/ // this is run from the signal thread, so don't set errno
|
2011-08-13 12:28:15 +02:00
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
|
2002-10-30 22:05:18 +01:00
|
|
|
}
|
|
|
|
else
|
2003-09-05 03:55:01 +02:00
|
|
|
for (const char **a = argv; *a; a++)
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, *a, strlen (*a) + 1, &nr, 1000L))
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
2011-08-13 12:28:15 +02:00
|
|
|
sigproc_printf ("WritePipeOverlapped arg %d failed, %E",
|
|
|
|
a - argv);
|
2002-10-30 22:05:18 +01:00
|
|
|
break;
|
|
|
|
}
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-01-29 12:23:07 +01:00
|
|
|
case PICOM_CWD:
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
sigproc_printf ("processing PICOM_CWD");
|
2008-02-14 17:47:11 +01:00
|
|
|
unsigned int n = strlen (cygheap->cwd.get (path, 1, 1, NT_MAX_PATH)) + 1;
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof cwd failed, %E");
|
|
|
|
else if (!WritePipeOverlapped (tothem, path, n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped cwd failed, %E");
|
2005-01-29 12:23:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PICOM_ROOT:
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
sigproc_printf ("processing PICOM_ROOT");
|
2005-09-29 00:56:47 +02:00
|
|
|
unsigned n;
|
2005-01-29 12:23:07 +01:00
|
|
|
if (cygheap->root.exists ())
|
2005-01-31 11:28:55 +01:00
|
|
|
n = strlen (strcpy (path, cygheap->root.posix_path ())) + 1;
|
2005-01-29 12:23:07 +01:00
|
|
|
else
|
2005-01-31 11:28:55 +01:00
|
|
|
n = strlen (strcpy (path, "/")) + 1;
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof root failed, %E");
|
|
|
|
else if (!WritePipeOverlapped (tothem, path, n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped root failed, %E");
|
2005-01-31 11:28:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PICOM_FDS:
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
sigproc_printf ("processing PICOM_FDS");
|
2005-01-31 11:28:55 +01:00
|
|
|
unsigned int n = 0;
|
|
|
|
int fd;
|
|
|
|
cygheap_fdenum cfd;
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
|
|
|
n += sizeof (int);
|
|
|
|
cfd.rewind ();
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof fds failed, %E");
|
2005-01-31 11:28:55 +01:00
|
|
|
else
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &fd, sizeof fd, &nr, 1000L))
|
2005-01-31 11:28:55 +01:00
|
|
|
{
|
2011-08-13 12:28:15 +02:00
|
|
|
sigproc_printf ("WritePipeOverlapped fd %d failed, %E", fd);
|
2005-01-31 11:28:55 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-05-02 05:50:11 +02:00
|
|
|
break;
|
2005-01-31 11:28:55 +01:00
|
|
|
}
|
2005-02-06 12:15:29 +01:00
|
|
|
case PICOM_PIPE_FHANDLER:
|
2005-10-02 02:13:41 +02:00
|
|
|
{
|
|
|
|
sigproc_printf ("processing PICOM_FDS");
|
2016-01-11 12:35:41 +01:00
|
|
|
int64_t unique_id = si._si_commune._si_pipe_unique_id;
|
2005-10-02 02:13:41 +02:00
|
|
|
unsigned int n = 0;
|
|
|
|
cygheap_fdenum cfd;
|
|
|
|
while (cfd.next () >= 0)
|
2016-01-11 12:35:41 +01:00
|
|
|
if (cfd->get_unique_id () == unique_id)
|
2005-10-02 02:13:41 +02:00
|
|
|
{
|
|
|
|
fhandler_pipe *fh = cfd;
|
|
|
|
n = sizeof *fh;
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof hdl failed, %E");
|
|
|
|
else if (!WritePipeOverlapped (tothem, fh, n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped hdl failed, %E");
|
2005-10-02 02:13:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!n && !WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof hdl failed, %E");
|
2005-10-02 02:13:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-01-31 11:28:55 +01:00
|
|
|
case PICOM_FD:
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
sigproc_printf ("processing PICOM_FD");
|
2005-09-29 00:56:47 +02:00
|
|
|
int fd = si._si_commune._si_fd;
|
2005-02-06 12:15:29 +01:00
|
|
|
unsigned int n = 0;
|
2005-01-31 11:28:55 +01:00
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
2005-02-01 16:11:47 +01:00
|
|
|
n = strlen (strcpy (path, "")) + 1;
|
2005-01-31 11:28:55 +01:00
|
|
|
else
|
|
|
|
n = strlen (cfd->get_proc_fd_name (path)) + 1;
|
2011-08-13 12:28:15 +02:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof fd failed, %E");
|
|
|
|
else if (!WritePipeOverlapped (tothem, path, n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped fd failed, %E");
|
2005-05-02 05:50:11 +02:00
|
|
|
break;
|
2017-01-10 16:38:01 +01:00
|
|
|
}
|
|
|
|
case PICOM_ENVIRON:
|
|
|
|
{
|
2017-01-10 16:03:09 +01:00
|
|
|
sigproc_printf ("processing PICOM_ENVIRON");
|
|
|
|
unsigned n = 0;
|
|
|
|
char **env = cur_environ ();
|
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
n += strlen (*e) + 1;
|
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
|
|
|
|
else
|
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
if (!WritePipeOverlapped (tothem, *e, strlen (*e) + 1, &nr, 1000L))
|
|
|
|
{
|
2017-01-10 16:38:01 +01:00
|
|
|
sigproc_printf ("WritePipeOverlapped arg %d failed, %E",
|
|
|
|
e - env);
|
2017-01-10 16:03:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-01-10 16:38:01 +01:00
|
|
|
}
|
|
|
|
}
|
2005-09-29 00:56:47 +02:00
|
|
|
if (process_sync)
|
|
|
|
{
|
2005-10-02 02:13:41 +02:00
|
|
|
DWORD res = WaitForSingleObject (process_sync, 5000);
|
|
|
|
if (res != WAIT_OBJECT_0)
|
2013-04-23 11:44:36 +02:00
|
|
|
sigproc_printf ("WFSO failed - %u, %E", res);
|
2005-10-02 02:13:41 +02:00
|
|
|
else
|
|
|
|
sigproc_printf ("synchronized with pid %d", si.si_pid);
|
2005-09-29 00:56:47 +02:00
|
|
|
ForceCloseHandle (process_sync);
|
|
|
|
}
|
|
|
|
CloseHandle (tothem);
|
2005-10-18 01:27:00 +02:00
|
|
|
_my_tls._ctinfo->auto_release ();
|
|
|
|
return 0;
|
2002-10-30 22:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
commune_result
|
2005-09-29 00:56:47 +02:00
|
|
|
_pinfo::commune_request (__uint32_t code, ...)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
|
|
|
DWORD nr;
|
|
|
|
commune_result res;
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
va_list args;
|
2005-09-29 00:56:47 +02:00
|
|
|
siginfo_t si = {0};
|
|
|
|
HANDLE& hp = si._si_commune._si_process_handle;
|
|
|
|
HANDLE& fromthem = si._si_commune._si_read_handle;
|
|
|
|
HANDLE request_sync = NULL;
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
|
2002-11-05 04:39:25 +01:00
|
|
|
res.s = NULL;
|
|
|
|
res.n = 0;
|
|
|
|
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-06-08 14:38:30 +02:00
|
|
|
if (ISSTATE (this, PID_NOTCYGWIN))
|
|
|
|
{
|
|
|
|
set_errno (ENOTSUP);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-10-30 22:05:18 +01:00
|
|
|
|
2009-10-14 06:17:05 +02:00
|
|
|
va_start (args, code);
|
2005-09-29 00:56:47 +02:00
|
|
|
si._si_commune._si_code = code;
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case PICOM_PIPE_FHANDLER:
|
2016-01-11 12:35:41 +01:00
|
|
|
si._si_commune._si_pipe_unique_id = va_arg (args, int64_t);
|
2005-09-28 21:22:25 +02:00
|
|
|
break;
|
2002-10-30 22:05:18 +01:00
|
|
|
|
2005-09-29 00:56:47 +02:00
|
|
|
case PICOM_FD:
|
|
|
|
si._si_commune._si_fd = va_arg (args, int);
|
|
|
|
break;
|
2005-09-28 21:22:25 +02:00
|
|
|
|
2005-09-29 00:56:47 +02:00
|
|
|
break;
|
2005-09-28 21:22:25 +02:00
|
|
|
}
|
2009-10-14 06:17:05 +02:00
|
|
|
va_end (args);
|
2005-09-28 21:22:25 +02:00
|
|
|
|
2007-12-06 19:24:06 +01:00
|
|
|
char name_buf[MAX_PATH];
|
2013-04-23 11:44:36 +02:00
|
|
|
request_sync = CreateSemaphore (&sec_none_nih, 0, INT32_MAX,
|
2005-09-29 00:56:47 +02:00
|
|
|
shared_name (name_buf, "commune", myself->pid));
|
|
|
|
if (!request_sync)
|
|
|
|
goto err;
|
|
|
|
ProtectHandle (request_sync);
|
|
|
|
|
|
|
|
si.si_signo = __SIGCOMMUNE;
|
|
|
|
if (sig_send (this, si))
|
2005-10-02 02:13:41 +02:00
|
|
|
{
|
|
|
|
ForceCloseHandle (request_sync); /* don't signal semaphore since there was apparently no receiving process */
|
|
|
|
request_sync = NULL;
|
|
|
|
goto err;
|
|
|
|
}
|
2002-10-30 22:05:18 +01:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
DWORD n;
|
2002-10-30 22:05:18 +01:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case PICOM_CMDLINE:
|
2005-01-29 12:23:07 +01:00
|
|
|
case PICOM_CWD:
|
2017-01-10 16:03:09 +01:00
|
|
|
case PICOM_ENVIRON:
|
2005-01-29 12:23:07 +01:00
|
|
|
case PICOM_ROOT:
|
2005-01-31 11:28:55 +01:00
|
|
|
case PICOM_FDS:
|
2005-09-29 00:56:47 +02:00
|
|
|
case PICOM_FD:
|
|
|
|
case PICOM_PIPE_FHANDLER:
|
2013-04-23 11:44:36 +02:00
|
|
|
if (!ReadPipeOverlapped (fromthem, &n, sizeof n, &nr, 1000L)
|
2011-08-13 12:28:15 +02:00
|
|
|
|| nr != sizeof n)
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
2005-02-06 12:15:29 +01:00
|
|
|
if (!n)
|
2005-05-02 05:50:11 +02:00
|
|
|
res.s = NULL;
|
2005-02-06 12:15:29 +01:00
|
|
|
else
|
2005-05-02 05:50:11 +02:00
|
|
|
{
|
2007-11-26 22:30:49 +01:00
|
|
|
res.s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2005-02-06 12:15:29 +01:00
|
|
|
char *p;
|
2011-08-13 12:28:15 +02:00
|
|
|
for (p = res.s;
|
2013-04-23 11:44:36 +02:00
|
|
|
n && ReadPipeOverlapped (fromthem, p, n, &nr, 1000L);
|
2011-08-13 12:28:15 +02:00
|
|
|
p += nr, n -= nr)
|
2005-02-06 12:15:29 +01:00
|
|
|
continue;
|
2005-08-24 19:45:11 +02:00
|
|
|
if (n)
|
2005-02-06 12:15:29 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
2005-08-24 19:45:11 +02:00
|
|
|
res.n = p - res.s;
|
2005-05-02 05:50:11 +02:00
|
|
|
}
|
2002-10-30 22:05:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err:
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
memset (&res, 0, sizeof (res));
|
|
|
|
|
2002-10-30 22:05:18 +01:00
|
|
|
out:
|
2005-09-29 00:56:47 +02:00
|
|
|
if (request_sync)
|
|
|
|
{
|
|
|
|
LONG res;
|
|
|
|
ReleaseSemaphore (request_sync, 1, &res);
|
|
|
|
ForceCloseHandle (request_sync);
|
|
|
|
}
|
|
|
|
if (hp)
|
|
|
|
CloseHandle (hp);
|
|
|
|
if (fromthem)
|
|
|
|
CloseHandle (fromthem);
|
2002-10-30 22:05:18 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-02-06 12:15:29 +01:00
|
|
|
fhandler_pipe *
|
2016-01-11 12:35:41 +01:00
|
|
|
_pinfo::pipe_fhandler (int64_t unique_id, size_t &n)
|
2005-02-06 12:15:29 +01:00
|
|
|
{
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2005-02-06 12:15:29 +01:00
|
|
|
return NULL;
|
|
|
|
if (pid == myself->pid)
|
|
|
|
return NULL;
|
2016-01-11 12:35:41 +01:00
|
|
|
commune_result cr = commune_request (PICOM_PIPE_FHANDLER, unique_id);
|
2005-02-06 12:15:29 +01:00
|
|
|
n = cr.n;
|
|
|
|
return (fhandler_pipe *) cr.s;
|
|
|
|
}
|
|
|
|
|
2005-01-31 11:28:55 +01:00
|
|
|
char *
|
|
|
|
_pinfo::fd (int fd, size_t &n)
|
|
|
|
{
|
|
|
|
char *s;
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2005-01-31 11:28:55 +01:00
|
|
|
return NULL;
|
|
|
|
if (pid != myself->pid)
|
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
commune_result cr = commune_request (PICOM_FD, fd);
|
2005-01-31 11:28:55 +01:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
2005-08-24 06:38:39 +02:00
|
|
|
s = cstrdup ("");
|
2005-01-31 11:28:55 +01:00
|
|
|
else
|
2008-02-14 17:47:11 +01:00
|
|
|
s = cfd->get_proc_fd_name ((char *) cmalloc_abort (HEAP_COMMUNE, NT_MAX_PATH));
|
2005-01-31 11:28:55 +01:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
_pinfo::fds (size_t &n)
|
|
|
|
{
|
|
|
|
char *s;
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2005-01-31 11:28:55 +01:00
|
|
|
return NULL;
|
|
|
|
if (pid != myself->pid)
|
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
commune_result cr = commune_request (PICOM_FDS);
|
2005-01-31 11:28:55 +01:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
int fd;
|
2005-10-24 17:42:14 +02:00
|
|
|
cygheap_fdenum cfd (true);
|
2005-01-31 11:28:55 +01:00
|
|
|
while ((fd = cfd.next ()) >= 0)
|
|
|
|
n += sizeof (int);
|
|
|
|
cfd.rewind ();
|
2007-11-26 22:30:49 +01:00
|
|
|
s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2005-01-31 11:28:55 +01:00
|
|
|
int *p = (int *) s;
|
|
|
|
while ((fd = cfd.next ()) >= 0 && (char *) p - s < (int) n)
|
|
|
|
*p++ = fd;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-01-29 12:23:07 +01:00
|
|
|
char *
|
|
|
|
_pinfo::root (size_t& n)
|
|
|
|
{
|
|
|
|
char *s;
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2005-01-29 12:23:07 +01:00
|
|
|
return NULL;
|
2015-06-08 21:48:13 +02:00
|
|
|
if (pid != myself->pid && !ISSTATE (this, PID_NOTCYGWIN))
|
2005-01-29 12:23:07 +01:00
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
commune_result cr = commune_request (PICOM_ROOT);
|
2005-01-29 12:23:07 +01:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cygheap->root.exists ())
|
2005-08-24 06:38:39 +02:00
|
|
|
s = cstrdup (cygheap->root.posix_path ());
|
2005-01-29 12:23:07 +01:00
|
|
|
else
|
2005-08-24 06:38:39 +02:00
|
|
|
s = cstrdup ("/");
|
2005-01-29 12:23:07 +01:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-06-08 21:48:13 +02:00
|
|
|
static HANDLE
|
|
|
|
open_commune_proc_parms (DWORD pid, PRTL_USER_PROCESS_PARAMETERS prupp)
|
|
|
|
{
|
|
|
|
HANDLE proc;
|
|
|
|
NTSTATUS status;
|
|
|
|
PROCESS_BASIC_INFORMATION pbi;
|
|
|
|
PEB lpeb;
|
|
|
|
|
2016-06-27 17:56:16 +02:00
|
|
|
proc = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ,
|
|
|
|
FALSE, pid);
|
2015-06-08 21:48:13 +02:00
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
status = NtQueryInformationProcess (proc, ProcessBasicInformation,
|
|
|
|
&pbi, sizeof pbi, NULL);
|
|
|
|
if (NT_SUCCESS (status)
|
|
|
|
&& ReadProcessMemory (proc, pbi.PebBaseAddress, &lpeb, sizeof lpeb, NULL)
|
|
|
|
&& ReadProcessMemory (proc, lpeb.ProcessParameters, prupp, sizeof *prupp,
|
|
|
|
NULL))
|
|
|
|
return proc;
|
|
|
|
NtClose (proc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-29 12:23:07 +01:00
|
|
|
char *
|
|
|
|
_pinfo::cwd (size_t& n)
|
|
|
|
{
|
2015-06-08 21:55:11 +02:00
|
|
|
char *s = NULL;
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2005-01-29 12:23:07 +01:00
|
|
|
return NULL;
|
2015-06-08 21:48:13 +02:00
|
|
|
if (ISSTATE (this, PID_NOTCYGWIN))
|
|
|
|
{
|
|
|
|
RTL_USER_PROCESS_PARAMETERS rupp;
|
|
|
|
HANDLE proc = open_commune_proc_parms (dwProcessId, &rupp);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR cwd = tp.w_get ();
|
|
|
|
|
|
|
|
if (ReadProcessMemory (proc, rupp.CurrentDirectoryName.Buffer,
|
|
|
|
cwd, rupp.CurrentDirectoryName.Length,
|
|
|
|
NULL))
|
|
|
|
{
|
|
|
|
/* Drop trailing backslash, add trailing \0 in passing. */
|
|
|
|
cwd[rupp.CurrentDirectoryName.Length / sizeof (WCHAR) - 1]
|
|
|
|
= L'\0';
|
|
|
|
s = (char *) cmalloc_abort (HEAP_COMMUNE, NT_MAX_PATH);
|
|
|
|
mount_table->conv_to_posix_path (cwd, s, 0);
|
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
NtClose (proc);
|
|
|
|
}
|
|
|
|
else if (pid != myself->pid)
|
2005-01-29 12:23:07 +01:00
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
commune_result cr = commune_request (PICOM_CWD);
|
2005-01-29 12:23:07 +01:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-14 17:47:11 +01:00
|
|
|
s = (char *) cmalloc_abort (HEAP_COMMUNE, NT_MAX_PATH);
|
|
|
|
cygheap->cwd.get (s, 1, 1, NT_MAX_PATH);
|
2005-01-29 12:23:07 +01:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-10-30 22:05:18 +01:00
|
|
|
char *
|
2003-03-08 20:22:08 +01:00
|
|
|
_pinfo::cmdline (size_t& n)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
2015-06-08 21:55:11 +02:00
|
|
|
char *s = NULL;
|
2016-04-05 10:26:06 +02:00
|
|
|
if (!this || !pid)
|
2002-10-30 22:05:18 +01:00
|
|
|
return NULL;
|
2015-06-08 21:48:13 +02:00
|
|
|
if (ISSTATE (this, PID_NOTCYGWIN))
|
|
|
|
{
|
|
|
|
RTL_USER_PROCESS_PARAMETERS rupp;
|
|
|
|
HANDLE proc = open_commune_proc_parms (dwProcessId, &rupp);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR cmdline = tp.w_get ();
|
|
|
|
|
|
|
|
if (ReadProcessMemory (proc, rupp.CommandLine.Buffer, cmdline,
|
|
|
|
rupp.CommandLine.Length, NULL))
|
|
|
|
{
|
|
|
|
/* Add trailing \0. */
|
|
|
|
cmdline[rupp.CommandLine.Length / sizeof (WCHAR)]
|
|
|
|
= L'\0';
|
|
|
|
n = sys_wcstombs_alloc (&s, HEAP_COMMUNE, cmdline,
|
|
|
|
rupp.CommandLine.Length
|
|
|
|
/ sizeof (WCHAR));
|
|
|
|
/* Quotes & Spaces post-processing. */
|
|
|
|
bool in_quote = false;
|
|
|
|
for (char *c = s; *c; ++c)
|
|
|
|
if (*c == '"')
|
|
|
|
in_quote = !in_quote;
|
|
|
|
else if (*c == ' ' && !in_quote)
|
|
|
|
*c = '\0';
|
|
|
|
}
|
|
|
|
NtClose (proc);
|
|
|
|
}
|
|
|
|
else if (pid != myself->pid)
|
2002-10-30 22:05:18 +01:00
|
|
|
{
|
2005-09-29 00:56:47 +02:00
|
|
|
commune_result cr = commune_request (PICOM_CMDLINE);
|
2002-10-30 22:05:18 +01:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-19 18:00:28 +02:00
|
|
|
n = 0;
|
2002-10-30 22:05:18 +01:00
|
|
|
for (char **a = __argv; *a; a++)
|
2002-12-16 19:56:33 +01:00
|
|
|
n += strlen (*a) + 1;
|
2002-10-30 22:05:18 +01:00
|
|
|
char *p;
|
2007-11-26 22:30:49 +01:00
|
|
|
p = s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2002-10-30 22:05:18 +01:00
|
|
|
for (char **a = __argv; *a; a++)
|
|
|
|
{
|
|
|
|
strcpy (p, *a);
|
|
|
|
p = strchr (p, '\0') + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:03:09 +01:00
|
|
|
|
|
|
|
char *
|
|
|
|
_pinfo::environ (size_t& n)
|
|
|
|
{
|
|
|
|
char **env = NULL;
|
|
|
|
if (!this || !pid)
|
|
|
|
return NULL;
|
|
|
|
if (ISSTATE (this, PID_NOTCYGWIN))
|
|
|
|
{
|
|
|
|
RTL_USER_PROCESS_PARAMETERS rupp;
|
|
|
|
HANDLE proc = open_commune_proc_parms (dwProcessId, &rupp);
|
|
|
|
|
|
|
|
if (!proc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
SIZE_T envsize;
|
|
|
|
PWCHAR envblock;
|
|
|
|
if (!VirtualQueryEx (proc, rupp.Environment, &mbi, sizeof(mbi)))
|
|
|
|
{
|
|
|
|
NtClose (proc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIZE_T read;
|
|
|
|
envsize = (ptrdiff_t) mbi.RegionSize
|
|
|
|
- ((ptrdiff_t) rupp.Environment - (ptrdiff_t) mbi.BaseAddress);
|
|
|
|
envblock = (PWCHAR) cmalloc_abort (HEAP_COMMUNE, envsize);
|
|
|
|
|
|
|
|
if (ReadProcessMemory (proc, rupp.Environment, envblock, envsize, &read))
|
|
|
|
env = win32env_to_cygenv (envblock, false);
|
|
|
|
|
|
|
|
NtClose (proc);
|
|
|
|
}
|
|
|
|
else if (pid != myself->pid)
|
|
|
|
{
|
|
|
|
commune_result cr = commune_request (PICOM_ENVIRON);
|
|
|
|
n = cr.n;
|
|
|
|
return cr.s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
env = cur_environ ();
|
|
|
|
|
|
|
|
if (env == NULL)
|
2017-01-10 16:38:01 +01:00
|
|
|
return NULL;
|
2017-01-10 16:03:09 +01:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
n += strlen (*e) + 1;
|
|
|
|
|
|
|
|
char *p, *s;
|
|
|
|
p = s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
{
|
|
|
|
strcpy (p, *e);
|
|
|
|
p = strchr (p, '\0') + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-11-26 05:15:10 +01:00
|
|
|
/* This is the workhorse which waits for the write end of the pipe
|
2004-12-24 19:31:23 +01:00
|
|
|
created during new process creation. If the pipe is closed or a zero
|
|
|
|
is received on the pipe, it is assumed that the cygwin pid has exited.
|
|
|
|
Otherwise, various "signals" can be sent to the parent to inform the
|
|
|
|
parent to perform a certain action. */
|
2004-11-26 05:15:10 +01:00
|
|
|
static DWORD WINAPI
|
|
|
|
proc_waiter (void *arg)
|
|
|
|
{
|
2005-01-17 05:12:08 +01:00
|
|
|
pinfo vchild = *(pinfo *) arg;
|
|
|
|
((pinfo *) arg)->waiter_ready = true;
|
2004-11-26 05:15:10 +01:00
|
|
|
|
2006-02-06 19:24:11 +01:00
|
|
|
siginfo_t si = {0};
|
2004-11-26 05:15:10 +01:00
|
|
|
si.si_signo = SIGCHLD;
|
2005-09-26 15:47:27 +02:00
|
|
|
si.si_code = CLD_EXITED;
|
2004-11-26 05:15:10 +01:00
|
|
|
si.si_pid = vchild->pid;
|
|
|
|
#if 0 // FIXME: This is tricky to get right
|
|
|
|
si.si_utime = pchildren[rc]->rusage_self.ru_utime;
|
|
|
|
si.si_stime = pchildren[rc].rusage_self.ru_stime;
|
|
|
|
#endif
|
|
|
|
pid_t pid = vchild->pid;
|
2011-11-14 02:29:49 +01:00
|
|
|
bool its_me = vchild == myself;
|
2004-11-26 05:15:10 +01:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
DWORD nb;
|
|
|
|
char buf = '\0';
|
2005-01-11 16:31:04 +01:00
|
|
|
|
2004-11-26 05:15:10 +01:00
|
|
|
if (!ReadFile (vchild.rd_proc_pipe, &buf, 1, &nb, NULL)
|
|
|
|
&& GetLastError () != ERROR_BROKEN_PIPE)
|
|
|
|
{
|
|
|
|
system_printf ("error on read of child wait pipe %p, %E", vchild.rd_proc_pipe);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
if (!its_me && have_execed_cygwin)
|
|
|
|
break;
|
|
|
|
|
2004-11-26 05:15:10 +01:00
|
|
|
si.si_uid = vchild->uid;
|
|
|
|
|
|
|
|
switch (buf)
|
|
|
|
{
|
2004-12-03 05:46:00 +01:00
|
|
|
case __ALERT_ALIVE:
|
|
|
|
continue;
|
2004-11-26 05:15:10 +01:00
|
|
|
case 0:
|
|
|
|
/* Child exited. Do some cleanup and signal myself. */
|
2005-01-17 05:12:08 +01:00
|
|
|
vchild.maybe_set_exit_code_from_windows ();
|
2004-11-26 05:15:10 +01:00
|
|
|
if (WIFEXITED (vchild->exitcode))
|
2005-09-26 15:47:27 +02:00
|
|
|
si.si_code = CLD_EXITED;
|
2004-11-26 05:15:10 +01:00
|
|
|
else if (WCOREDUMP (vchild->exitcode))
|
2005-09-26 15:47:27 +02:00
|
|
|
si.si_code = CLD_DUMPED;
|
2004-11-26 05:15:10 +01:00
|
|
|
else
|
2005-09-26 15:47:27 +02:00
|
|
|
si.si_code = CLD_KILLED;
|
2004-11-26 05:15:10 +01:00
|
|
|
si.si_status = vchild->exitcode;
|
2005-01-17 05:12:08 +01:00
|
|
|
vchild->process_state = PID_EXITED;
|
|
|
|
/* This should always be last. Do not use vchild-> beyond this point */
|
2004-11-26 05:15:10 +01:00
|
|
|
break;
|
|
|
|
case SIGTTIN:
|
|
|
|
case SIGTTOU:
|
|
|
|
case SIGTSTP:
|
|
|
|
case SIGSTOP:
|
2004-12-05 20:41:26 +01:00
|
|
|
if (ISSTATE (myself, PID_NOCLDSTOP)) // FIXME: No need for this flag to be in _pinfo any longer
|
|
|
|
continue;
|
2004-11-26 05:15:10 +01:00
|
|
|
/* Child stopped. Signal myself. */
|
2005-09-26 15:47:27 +02:00
|
|
|
si.si_code = CLD_STOPPED;
|
2004-11-26 05:15:10 +01:00
|
|
|
break;
|
|
|
|
case SIGCONT:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
system_printf ("unknown value %d on proc pipe", buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-14 02:29:49 +01:00
|
|
|
if (its_me && ch_spawn.signal_myself_exited ())
|
|
|
|
break;
|
2004-11-26 05:15:10 +01:00
|
|
|
|
|
|
|
/* Send a SIGCHLD to myself. We do this here, rather than in proc_subproc
|
|
|
|
to avoid the proc_subproc lock since the signal thread will eventually
|
|
|
|
be calling proc_subproc and could unnecessarily block. */
|
|
|
|
sig_send (myself_nowait, si);
|
|
|
|
|
|
|
|
/* If we're just stopped or got a continue signal, keep looping.
|
|
|
|
Otherwise, return this thread to the pool. */
|
|
|
|
if (buf != '\0')
|
|
|
|
sigproc_printf ("looping");
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigproc_printf ("exiting wait thread for pid %d", pid);
|
2004-12-22 17:59:03 +01:00
|
|
|
vchild.wait_thread = NULL;
|
2004-12-23 15:57:08 +01:00
|
|
|
_my_tls._ctinfo->auto_release (); /* automatically return the cygthread to the cygthread pool */
|
2004-11-26 05:15:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function to set up the process pipe and kick off proc_waiter */
|
2011-11-07 21:05:49 +01:00
|
|
|
bool
|
2004-11-26 05:15:10 +01:00
|
|
|
pinfo::wait ()
|
|
|
|
{
|
|
|
|
preserve (); /* Preserve the shared memory associated with the pinfo */
|
|
|
|
|
2005-01-17 05:12:08 +01:00
|
|
|
waiter_ready = false;
|
2004-11-26 05:15:10 +01:00
|
|
|
/* Fire up a new thread to track the subprocess */
|
2011-11-14 02:29:49 +01:00
|
|
|
cygthread *h = new cygthread (proc_waiter, this, "waitproc");
|
2004-11-26 05:15:10 +01:00
|
|
|
if (!h)
|
|
|
|
sigproc_printf ("tracking thread creation failed for pid %d", (*this)->pid);
|
|
|
|
else
|
|
|
|
{
|
2004-12-05 22:29:37 +01:00
|
|
|
wait_thread = h;
|
2013-04-23 11:44:36 +02:00
|
|
|
sigproc_printf ("created tracking thread for pid %d, winpid %y, rd_proc_pipe %p",
|
2004-11-26 05:15:10 +01:00
|
|
|
(*this)->pid, (*this)->dwProcessId, rd_proc_pipe);
|
|
|
|
}
|
|
|
|
|
2011-11-07 21:05:49 +01:00
|
|
|
return true;
|
2004-11-26 05:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* function to send a "signal" to the parent when something interesting happens
|
|
|
|
in the child. */
|
2004-12-03 05:46:00 +01:00
|
|
|
bool
|
2004-12-24 19:31:23 +01:00
|
|
|
_pinfo::alert_parent (char sig)
|
2004-11-26 05:15:10 +01:00
|
|
|
{
|
2004-12-03 05:46:00 +01:00
|
|
|
DWORD nb = 0;
|
2005-10-19 16:19:37 +02:00
|
|
|
|
|
|
|
/* Send something to our parent. If the parent has gone away, close the pipe.
|
|
|
|
Don't send if this is an exec stub.
|
|
|
|
|
|
|
|
FIXME: Is there a race here if we run this while another thread is attempting
|
|
|
|
to exec()? */
|
2012-03-20 16:07:30 +01:00
|
|
|
if (my_wr_proc_pipe)
|
2004-11-26 05:15:10 +01:00
|
|
|
{
|
2012-03-20 16:07:30 +01:00
|
|
|
if (WriteFile (my_wr_proc_pipe, &sig, 1, &nb, NULL))
|
2004-12-24 19:31:23 +01:00
|
|
|
/* all is well */;
|
|
|
|
else if (GetLastError () != ERROR_BROKEN_PIPE)
|
|
|
|
debug_printf ("sending %d notification to parent failed, %E", sig);
|
|
|
|
else
|
|
|
|
{
|
2005-01-17 05:12:08 +01:00
|
|
|
ppid = 1;
|
2012-03-20 16:07:30 +01:00
|
|
|
HANDLE closeit = my_wr_proc_pipe;
|
|
|
|
my_wr_proc_pipe = NULL;
|
2012-03-20 03:08:14 +01:00
|
|
|
ForceCloseHandle1 (closeit, wr_proc_pipe);
|
2004-12-24 19:31:23 +01:00
|
|
|
}
|
2004-11-26 05:15:10 +01:00
|
|
|
}
|
2004-12-03 05:46:00 +01:00
|
|
|
return (bool) nb;
|
2004-11-26 05:15:10 +01:00
|
|
|
}
|
|
|
|
|
2000-08-26 05:48:37 +02:00
|
|
|
void
|
|
|
|
pinfo::release ()
|
|
|
|
{
|
2011-11-14 02:29:49 +01:00
|
|
|
_pinfo_release ();
|
|
|
|
HANDLE close_h;
|
|
|
|
if (rd_proc_pipe)
|
2000-08-26 05:48:37 +02:00
|
|
|
{
|
2011-11-14 02:29:49 +01:00
|
|
|
close_h = rd_proc_pipe;
|
|
|
|
rd_proc_pipe = NULL;
|
|
|
|
ForceCloseHandle1 (close_h, rd_proc_pipe);
|
2005-01-22 22:17:53 +01:00
|
|
|
}
|
2011-11-14 02:29:49 +01:00
|
|
|
if (hProcess)
|
2005-01-22 22:17:53 +01:00
|
|
|
{
|
2011-11-14 02:29:49 +01:00
|
|
|
close_h = hProcess;
|
|
|
|
hProcess = NULL;
|
|
|
|
ForceCloseHandle1 (close_h, childhProc);
|
2000-08-26 05:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* DOCTOOL-START
|
|
|
|
|
|
|
|
<sect1 id="func-cygwin-winpid-to-pid">
|
|
|
|
<title>cygwin_winpid_to_pid</title>
|
|
|
|
|
2004-02-23 00:21:20 +01:00
|
|
|
<funcsynopsis><funcprototype>
|
2000-02-17 20:38:33 +01:00
|
|
|
<funcdef>extern "C" pid_t
|
|
|
|
<function>cygwin_winpid_to_pid</function>
|
|
|
|
</funcdef>
|
|
|
|
<paramdef>int <parameter>winpid</parameter></paramdef>
|
2004-02-23 00:21:20 +01:00
|
|
|
</funcprototype></funcsynopsis>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
<para>Given a windows pid, converts to the corresponding Cygwin
|
|
|
|
pid, if any. Returns -1 if windows pid does not correspond to
|
|
|
|
a cygwin pid.</para>
|
|
|
|
<example>
|
|
|
|
<title>Example use of cygwin_winpid_to_pid</title>
|
|
|
|
<programlisting>
|
|
|
|
extern "C" cygwin_winpid_to_pid (int winpid);
|
|
|
|
pid_t mypid;
|
|
|
|
mypid = cygwin_winpid_to_pid (windows_pid);
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
DOCTOOL-END */
|
|
|
|
|
|
|
|
extern "C" pid_t
|
|
|
|
cygwin_winpid_to_pid (int winpid)
|
|
|
|
{
|
2002-01-29 03:02:03 +01:00
|
|
|
pinfo p (cygwin_pid (winpid));
|
2000-07-29 18:24:59 +02:00
|
|
|
if (p)
|
|
|
|
return p->pid;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-07-29 18:24:59 +02:00
|
|
|
set_errno (ESRCH);
|
|
|
|
return (pid_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-02 06:25:56 +01:00
|
|
|
#define slop_pidlist 200
|
|
|
|
#define size_pidlist(i) (sizeof (pidlist[0]) * ((i) + 1))
|
2000-11-05 07:42:23 +01:00
|
|
|
#define size_pinfolist(i) (sizeof (pinfolist[0]) * ((i) + 1))
|
2005-12-22 04:16:36 +01:00
|
|
|
class _onreturn
|
|
|
|
{
|
2013-12-18 04:58:11 +01:00
|
|
|
HANDLE h;
|
2005-12-22 04:16:36 +01:00
|
|
|
public:
|
|
|
|
~_onreturn ()
|
|
|
|
{
|
2013-12-18 04:58:11 +01:00
|
|
|
if (h)
|
2005-12-22 04:16:36 +01:00
|
|
|
{
|
2013-12-18 04:58:11 +01:00
|
|
|
CloseHandle (h);
|
2005-12-22 04:16:36 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 04:58:11 +01:00
|
|
|
void no_close_handle (pinfo& p)
|
|
|
|
{
|
|
|
|
p.hProcess = h;
|
|
|
|
h = NULL;
|
|
|
|
}
|
|
|
|
_onreturn (): h (NULL) {}
|
|
|
|
void operator = (HANDLE h0) {h = h0;}
|
|
|
|
operator HANDLE () const {return h;}
|
2005-12-22 04:16:36 +01:00
|
|
|
};
|
2000-11-02 06:25:56 +01:00
|
|
|
|
2000-11-05 07:42:23 +01:00
|
|
|
inline void
|
|
|
|
winpids::add (DWORD& nelem, bool winpid, DWORD pid)
|
|
|
|
{
|
|
|
|
pid_t cygpid = cygwin_pid (pid);
|
2005-12-22 04:16:36 +01:00
|
|
|
|
2000-11-05 07:42:23 +01:00
|
|
|
if (nelem >= npidlist)
|
|
|
|
{
|
|
|
|
npidlist += slop_pidlist;
|
2001-10-13 19:23:35 +02:00
|
|
|
pidlist = (DWORD *) realloc (pidlist, size_pidlist (npidlist + 1));
|
|
|
|
pinfolist = (pinfo *) realloc (pinfolist, size_pinfolist (npidlist + 1));
|
2000-11-05 07:42:23 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 04:58:11 +01:00
|
|
|
_onreturn onreturn;
|
2005-12-12 19:43:31 +01:00
|
|
|
pinfo& p = pinfolist[nelem];
|
2011-11-14 02:29:49 +01:00
|
|
|
memset (&p, 0, sizeof (p));
|
2005-12-12 19:43:31 +01:00
|
|
|
|
2005-12-23 23:50:20 +01:00
|
|
|
bool perform_copy;
|
2013-12-18 04:58:11 +01:00
|
|
|
if (cygpid == myself->pid)
|
|
|
|
{
|
|
|
|
p = myself;
|
|
|
|
perform_copy = false;
|
|
|
|
}
|
2005-12-22 04:16:36 +01:00
|
|
|
else
|
2013-12-18 04:58:11 +01:00
|
|
|
{
|
|
|
|
/* Open a process to prevent a subsequent exit from invalidating the
|
|
|
|
shared memory region. */
|
2016-06-27 17:56:16 +02:00
|
|
|
onreturn = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
|
2005-12-23 23:50:20 +01:00
|
|
|
|
2013-12-18 04:58:11 +01:00
|
|
|
/* If we couldn't open the process then we don't have rights to it and should
|
|
|
|
make a copy of the shared memory area when it exists (it may not). */
|
|
|
|
perform_copy = onreturn ? make_copy : true;
|
|
|
|
|
|
|
|
p.init (cygpid, PID_NOREDIR | pinfo_access, NULL);
|
|
|
|
}
|
2005-12-22 04:16:36 +01:00
|
|
|
|
|
|
|
/* If we're just looking for winpids then don't do any special cygwin "stuff* */
|
2000-11-05 07:42:23 +01:00
|
|
|
if (winpid)
|
2014-02-15 05:17:27 +01:00
|
|
|
{
|
|
|
|
perform_copy = true;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-11-05 07:42:23 +01:00
|
|
|
|
2005-12-22 04:16:36 +01:00
|
|
|
/* !p means that we couldn't find shared memory for this pid. Probably means
|
|
|
|
that it isn't a cygwin process. */
|
2005-12-12 19:43:31 +01:00
|
|
|
if (!p)
|
2003-09-17 03:15:56 +02:00
|
|
|
{
|
|
|
|
if (!pinfo_access)
|
|
|
|
return;
|
2005-12-12 19:43:31 +01:00
|
|
|
p.init (cygpid, PID_NOREDIR, NULL);
|
|
|
|
if (!p)
|
2003-09-17 03:15:56 +02:00
|
|
|
return;
|
2013-12-18 04:58:11 +01:00
|
|
|
}
|
2003-09-17 03:15:56 +02:00
|
|
|
|
|
|
|
/* Scan list of previously recorded pids to make sure that this pid hasn't
|
|
|
|
shown up before. This can happen when a process execs. */
|
|
|
|
for (unsigned i = 0; i < nelem; i++)
|
2005-12-12 19:43:31 +01:00
|
|
|
if (pinfolist[i]->pid == p->pid)
|
2003-09-17 03:15:56 +02:00
|
|
|
{
|
2005-12-12 19:43:31 +01:00
|
|
|
if ((_pinfo *) p != (_pinfo *) myself)
|
|
|
|
p.release ();
|
2003-09-17 03:15:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2005-12-22 04:16:36 +01:00
|
|
|
/* Exit here.
|
|
|
|
|
|
|
|
If p is "false" then, eventually any opened process handle will be closed and
|
|
|
|
the function will exit without adding anything to the pid list.
|
|
|
|
|
|
|
|
If p is "true" then we've discovered a cygwin process.
|
2005-12-22 06:57:54 +01:00
|
|
|
|
2005-12-22 04:16:36 +01:00
|
|
|
Handle "myself" differently. Don't copy it and close/zero the handle we
|
|
|
|
just opened to it.
|
|
|
|
If not performing a copy, then keep the process handle open for the duration
|
|
|
|
of the life of the procinfo region to potential races when a new process uses
|
|
|
|
this pid.
|
|
|
|
Otherwise, malloc some memory for a copy of the shared memory.
|
2005-12-22 06:57:54 +01:00
|
|
|
|
2005-12-22 04:16:36 +01:00
|
|
|
If the malloc failed, then "oh well". Just keep the shared memory around
|
|
|
|
and eventually close the handle when the winpids goes out of scope.
|
|
|
|
|
|
|
|
If malloc succeeds, copy the procinfo we just grabbed into the new region,
|
|
|
|
release the shared memory and allow the handle to be closed when this
|
|
|
|
function returns.
|
|
|
|
|
|
|
|
Oh, and add the pid to the list and bump the number of elements. */
|
|
|
|
|
|
|
|
if (p)
|
2005-12-12 19:43:31 +01:00
|
|
|
{
|
2005-12-22 04:16:36 +01:00
|
|
|
if (p == (_pinfo *) myself)
|
|
|
|
/* handle specially. Close the handle but (eventually) don't
|
|
|
|
deallocate procinfo in release call */;
|
|
|
|
else if (!perform_copy)
|
2013-12-18 04:58:11 +01:00
|
|
|
onreturn.no_close_handle (p); /* Don't close the handle until release */
|
2005-12-22 04:16:36 +01:00
|
|
|
else
|
2005-12-12 19:43:31 +01:00
|
|
|
{
|
2005-12-22 04:16:36 +01:00
|
|
|
_pinfo *pnew = (_pinfo *) malloc (sizeof (*p.procinfo));
|
|
|
|
if (!pnew)
|
2013-12-18 04:58:11 +01:00
|
|
|
onreturn.no_close_handle (p);
|
2005-12-22 04:16:36 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pnew = *p.procinfo;
|
2011-11-14 02:29:49 +01:00
|
|
|
p.release ();
|
2005-12-22 04:16:36 +01:00
|
|
|
p.procinfo = pnew;
|
|
|
|
p.destroy = false;
|
2014-02-15 05:17:27 +01:00
|
|
|
if (winpid)
|
|
|
|
p->dwProcessId = pid;
|
2005-12-22 04:16:36 +01:00
|
|
|
}
|
2005-12-12 19:43:31 +01:00
|
|
|
}
|
|
|
|
}
|
2005-12-22 06:26:14 +01:00
|
|
|
if (p || winpid)
|
2011-08-11 09:28:42 +02:00
|
|
|
pidlist[nelem++] = !p ? pid : p->dwProcessId;
|
2000-11-05 07:42:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD
|
2007-02-22 11:54:47 +01:00
|
|
|
winpids::enum_processes (bool winpid)
|
2000-11-02 06:25:56 +01:00
|
|
|
{
|
|
|
|
DWORD nelem = 0;
|
2014-02-15 05:17:27 +01:00
|
|
|
|
|
|
|
if (!winpid)
|
2011-08-11 09:28:42 +02:00
|
|
|
{
|
2014-02-15 05:17:27 +01:00
|
|
|
HANDLE dir = get_shared_parent_dir ();
|
|
|
|
BOOLEAN restart = TRUE;
|
|
|
|
ULONG context;
|
|
|
|
struct fdbi
|
2000-11-02 06:25:56 +01:00
|
|
|
{
|
2014-02-15 05:17:27 +01:00
|
|
|
DIRECTORY_BASIC_INFORMATION dbi;
|
|
|
|
WCHAR buf[2][NAME_MAX + 1];
|
|
|
|
} f;
|
|
|
|
while (NT_SUCCESS (NtQueryDirectoryObject (dir, &f, sizeof f, TRUE,
|
|
|
|
restart, &context, NULL)))
|
|
|
|
{
|
|
|
|
restart = FALSE;
|
|
|
|
f.dbi.ObjectName.Buffer[f.dbi.ObjectName.Length / sizeof (WCHAR)] = L'\0';
|
|
|
|
if (wcsncmp (f.dbi.ObjectName.Buffer, L"cygpid.", 7) == 0)
|
|
|
|
{
|
|
|
|
DWORD pid = wcstoul (f.dbi.ObjectName.Buffer + 7, NULL, 10);
|
|
|
|
add (nelem, false, pid);
|
|
|
|
}
|
2000-11-02 06:25:56 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 05:17:27 +01:00
|
|
|
else
|
2000-11-02 06:25:56 +01:00
|
|
|
{
|
2011-08-11 09:28:42 +02:00
|
|
|
static DWORD szprocs;
|
2013-04-23 11:44:36 +02:00
|
|
|
static PSYSTEM_PROCESS_INFORMATION procs;
|
2011-08-11 09:28:42 +02:00
|
|
|
|
2014-02-15 05:17:27 +01:00
|
|
|
while (1)
|
2011-12-22 12:02:36 +01:00
|
|
|
{
|
2014-02-15 05:17:27 +01:00
|
|
|
PSYSTEM_PROCESS_INFORMATION new_p = (PSYSTEM_PROCESS_INFORMATION)
|
|
|
|
realloc (procs, szprocs += 200 * sizeof (*procs));
|
|
|
|
if (!new_p)
|
2011-12-22 12:02:36 +01:00
|
|
|
{
|
|
|
|
system_printf ("out of memory reading system process "
|
|
|
|
"information");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-15 05:17:27 +01:00
|
|
|
procs = new_p;
|
|
|
|
NTSTATUS status = NtQuerySystemInformation (SystemProcessInformation,
|
|
|
|
procs, szprocs, NULL);
|
2011-12-22 12:02:36 +01:00
|
|
|
if (NT_SUCCESS (status))
|
2011-08-11 09:28:42 +02:00
|
|
|
break;
|
|
|
|
|
2014-02-15 05:17:27 +01:00
|
|
|
if (status != STATUS_INFO_LENGTH_MISMATCH)
|
2011-08-11 09:28:42 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
system_printf ("error %y reading system process information",
|
2011-12-22 12:02:36 +01:00
|
|
|
status);
|
2011-08-11 09:28:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
PSYSTEM_PROCESS_INFORMATION px = procs;
|
2014-02-15 05:17:27 +01:00
|
|
|
while (1)
|
2011-08-11 09:28:42 +02:00
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
if (px->UniqueProcessId)
|
2014-02-15 05:17:27 +01:00
|
|
|
add (nelem, true, (DWORD) (uintptr_t) px->UniqueProcessId);
|
2013-04-23 11:44:36 +02:00
|
|
|
if (!px->NextEntryOffset)
|
2011-08-11 09:28:42 +02:00
|
|
|
break;
|
2016-03-19 18:45:56 +01:00
|
|
|
px = (PSYSTEM_PROCESS_INFORMATION) ((char *) px + px->NextEntryOffset);
|
2011-08-11 09:28:42 +02:00
|
|
|
}
|
2000-11-02 06:25:56 +01:00
|
|
|
}
|
|
|
|
return nelem;
|
|
|
|
}
|
|
|
|
|
2000-07-29 18:24:59 +02:00
|
|
|
void
|
2002-11-29 08:05:26 +01:00
|
|
|
winpids::set (bool winpid)
|
2000-07-29 18:24:59 +02:00
|
|
|
{
|
2007-02-22 11:54:47 +01:00
|
|
|
npids = enum_processes (winpid);
|
2001-10-13 19:23:35 +02:00
|
|
|
if (pidlist)
|
|
|
|
pidlist[npids] = 0;
|
2000-07-29 18:24:59 +02:00
|
|
|
}
|
|
|
|
|
2000-11-05 07:42:23 +01:00
|
|
|
DWORD
|
|
|
|
winpids::enum_init (bool winpid)
|
2000-07-29 18:24:59 +02:00
|
|
|
{
|
2007-02-22 11:54:47 +01:00
|
|
|
return enum_processes (winpid);
|
2000-11-05 07:42:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winpids::release ()
|
|
|
|
{
|
2005-12-12 19:43:31 +01:00
|
|
|
_pinfo *p;
|
2000-11-05 07:42:23 +01:00
|
|
|
for (unsigned i = 0; i < npids; i++)
|
2005-12-22 04:16:36 +01:00
|
|
|
if (pinfolist[i] == (_pinfo *) myself)
|
|
|
|
continue;
|
2005-12-22 06:11:44 +01:00
|
|
|
else if (pinfolist[i].hProcess)
|
2011-11-14 02:29:49 +01:00
|
|
|
pinfolist[i].release ();
|
2005-12-22 04:16:36 +01:00
|
|
|
else if ((p = pinfolist[i]))
|
|
|
|
{
|
|
|
|
pinfolist[i].procinfo = NULL;
|
|
|
|
free (p);
|
|
|
|
}
|
2000-11-05 07:42:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
winpids::~winpids ()
|
|
|
|
{
|
|
|
|
if (npidlist)
|
|
|
|
{
|
|
|
|
release ();
|
|
|
|
free (pidlist);
|
|
|
|
free (pinfolist);
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|