2000-02-18 03:38:33 +08: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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2008-04-08 00:15:45 +08:00
|
|
|
#include "miscfuncs.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-10-01 12:10:07 +08: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 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sigproc.h"
|
|
|
|
#include "pinfo.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "environ.h"
|
2000-11-02 13:25:56 +08:00
|
|
|
#include "ntdll.h"
|
2002-10-15 15:03:45 +08:00
|
|
|
#include "shared_info.h"
|
2003-09-16 11:39:55 +08:00
|
|
|
#include "cygheap.h"
|
2004-02-12 11:01:58 +08:00
|
|
|
#include "cygmalloc.h"
|
2004-02-14 03:34:32 +08:00
|
|
|
#include "cygtls.h"
|
2008-03-07 19:24:51 +08:00
|
|
|
#include "tls_pbuf.h"
|
2004-12-03 12:46:00 +08:00
|
|
|
#include "child_info.h"
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
#include "dll_init.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-12-20 03:09:51 +08:00
|
|
|
class pinfo_basic: public _pinfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
pinfo_basic();
|
|
|
|
};
|
|
|
|
|
2011-12-24 01:12:10 +08:00
|
|
|
pinfo_basic::pinfo_basic ()
|
2008-12-20 03:09:51 +08:00
|
|
|
{
|
2019-02-08 22:49:47 +08:00
|
|
|
dwProcessId = GetCurrentProcessId ();
|
2015-07-20 04:38:30 +08:00
|
|
|
PWCHAR pend = wcpncpy (progname, global_progname,
|
|
|
|
sizeof (progname) / sizeof (WCHAR) - 1);
|
|
|
|
*pend = L'\0';
|
2011-12-24 01:12:10 +08:00
|
|
|
/* Default uid/gid are needed very early to initialize shared user info. */
|
|
|
|
uid = ILLEGAL_UID;
|
2014-02-10 03:44:56 +08:00
|
|
|
gid = ILLEGAL_GID;
|
2008-12-20 03:09:51 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-12-20 03:09:51 +08:00
|
|
|
pinfo_basic myself_initial NO_COPY;
|
|
|
|
|
|
|
|
pinfo NO_COPY myself (static_cast<_pinfo *> (&myself_initial)); // Avoid myself != NULL checks
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-05-22 12:50:54 +08:00
|
|
|
/* Setup the pinfo structure for this process. There may already be a
|
|
|
|
_pinfo for this "pid" if h != NULL. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-01-01 05:33:34 +08:00
|
|
|
void
|
2008-12-21 01:32:31 +08:00
|
|
|
pinfo::thisproc (HANDLE h)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2008-12-21 01:32:31 +08:00
|
|
|
procinfo = NULL;
|
Cygwin: make sure exec'ed process exists early in process list
killpg(pgid, 0) (or kill_pgrp(pgid, si_signo=0), in signal.cc)
fails (returns -1) even when there is a process in the process
group pgid, if the process is in the middle of spawnve(), see
https://cygwin.com/pipermail/cygwin/2022-May/251479.html
When exec'ing a process the assumption is that the exec'ed process creates its
own symlink (in pinfo::thisproc() in pinfo.cc). If the exec'ing process
calls NtClose on it's own winpid symlink, but the exec'ed process didn't
progress enough into initialization, there's a slim chance that neither
the exec'ing process, nor the exec'ed process has a winpid symlink
attached.
Always create the winpid symlink in spawn.cc, even for exec'ed Cygwin
processes. Make sure to dup the handle into the new process, and stop
creating the winpid symlink in exec'ed processes.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-05-19 16:46:33 +08:00
|
|
|
bool execed = !!h;
|
2008-12-21 01:32:31 +08:00
|
|
|
|
2013-12-18 11:58:11 +08:00
|
|
|
DWORD flags = PID_IN_USE | PID_ACTIVE;
|
2019-02-08 22:49:47 +08:00
|
|
|
/* Forked process or process started from non-Cygwin parent needs a pid. */
|
Cygwin: make sure exec'ed process exists early in process list
killpg(pgid, 0) (or kill_pgrp(pgid, si_signo=0), in signal.cc)
fails (returns -1) even when there is a process in the process
group pgid, if the process is in the middle of spawnve(), see
https://cygwin.com/pipermail/cygwin/2022-May/251479.html
When exec'ing a process the assumption is that the exec'ed process creates its
own symlink (in pinfo::thisproc() in pinfo.cc). If the exec'ing process
calls NtClose on it's own winpid symlink, but the exec'ed process didn't
progress enough into initialization, there's a slim chance that neither
the exec'ing process, nor the exec'ed process has a winpid symlink
attached.
Always create the winpid symlink in spawn.cc, even for exec'ed Cygwin
processes. Make sure to dup the handle into the new process, and stop
creating the winpid symlink in exec'ed processes.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-05-19 16:46:33 +08:00
|
|
|
if (!execed)
|
2013-12-18 11:58:11 +08:00
|
|
|
{
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
cygheap->pid = create_cygwin_pid ();
|
2013-12-18 11:58:11 +08:00
|
|
|
flags |= PID_NEW;
|
2019-07-24 23:48:57 +08:00
|
|
|
h = INVALID_HANDLE_VALUE;
|
2013-12-18 11:58:11 +08:00
|
|
|
}
|
2019-02-08 22:49:47 +08:00
|
|
|
/* spawnve'd process got pid in parent, cygheap->pid has been set in
|
|
|
|
child_info_spawn::handle_spawn. */
|
2008-12-20 03:09:51 +08:00
|
|
|
|
2013-12-18 11:58:11 +08:00
|
|
|
init (cygheap->pid, flags, h);
|
2008-12-21 01:32:31 +08:00
|
|
|
procinfo->process_state |= PID_IN_USE;
|
2019-02-08 22:49:47 +08:00
|
|
|
procinfo->dwProcessId = myself_initial.dwProcessId;
|
2010-09-02 05:06:36 +08:00
|
|
|
procinfo->sendsig = myself_initial.sendsig;
|
2010-05-18 22:30:51 +08:00
|
|
|
wcscpy (procinfo->progname, myself_initial.progname);
|
Cygwin: make sure exec'ed process exists early in process list
killpg(pgid, 0) (or kill_pgrp(pgid, si_signo=0), in signal.cc)
fails (returns -1) even when there is a process in the process
group pgid, if the process is in the middle of spawnve(), see
https://cygwin.com/pipermail/cygwin/2022-May/251479.html
When exec'ing a process the assumption is that the exec'ed process creates its
own symlink (in pinfo::thisproc() in pinfo.cc). If the exec'ing process
calls NtClose on it's own winpid symlink, but the exec'ed process didn't
progress enough into initialization, there's a slim chance that neither
the exec'ing process, nor the exec'ed process has a winpid symlink
attached.
Always create the winpid symlink in spawn.cc, even for exec'ed Cygwin
processes. Make sure to dup the handle into the new process, and stop
creating the winpid symlink in exec'ed processes.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-05-19 16:46:33 +08:00
|
|
|
if (!execed)
|
|
|
|
create_winpid_symlink ();
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
procinfo->exec_sendsig = NULL;
|
|
|
|
procinfo->exec_dwProcessId = 0;
|
2011-11-25 05:36:53 +08:00
|
|
|
debug_printf ("myself dwProcessId %u", procinfo->dwProcessId);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the process table entry for the current task.
|
2002-01-29 10:02:03 +08:00
|
|
|
This is not called for forked tasks, only execed ones. */
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2000-10-17 07:55:58 +08:00
|
|
|
pinfo_init (char **envp, int envc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
if (envp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-17 07:55:58 +08:00
|
|
|
environ_init (envp, envc);
|
2000-02-18 03:38:33 +08: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-21 01:32:31 +08:00
|
|
|
myself.thisproc (NULL);
|
2000-07-30 00:24:59 +08:00
|
|
|
myself->pgid = myself->sid = myself->pid;
|
2023-03-05 18:02:13 +08:00
|
|
|
myself->ctty = CTTY_UNINITIALIZED;
|
* (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 21:38:51 +08:00
|
|
|
myself->uid = ILLEGAL_UID;
|
2014-02-10 03:44:56 +08:00
|
|
|
myself->gid = ILLEGAL_GID;
|
2000-10-17 07:55:58 +08:00
|
|
|
environ_init (NULL, 0); /* call after myself has been set up */
|
2009-12-19 04:32:04 +08:00
|
|
|
myself->nice = winprio_to_nice (GetPriorityClass (GetCurrentProcess ()));
|
2013-12-18 11:58:11 +08:00
|
|
|
myself->ppid = 1; /* always set last */
|
2005-01-26 06:45:11 +08:00
|
|
|
debug_printf ("Set nice to %d", myself->nice);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2010-09-02 05:06:36 +08:00
|
|
|
myself->process_state |= PID_ACTIVE;
|
2011-05-05 23:03:54 +08:00
|
|
|
myself->process_state &= ~(PID_INITIALIZING | PID_EXITED | PID_REAPED);
|
2022-02-28 06:24:45 +08:00
|
|
|
if (being_debugged ())
|
|
|
|
myself->process_state |= PID_DEBUGGED;
|
2012-03-20 10:08:14 +08:00
|
|
|
myself.preserve ();
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
debug_printf ("pid %d, pgid %d, process_state %y",
|
|
|
|
myself->pid, myself->pgid, myself->process_state);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 00:19:11 +08:00
|
|
|
DWORD
|
|
|
|
pinfo::status_exit (DWORD x)
|
2008-03-27 09:50:40 +08:00
|
|
|
{
|
|
|
|
switch (x)
|
|
|
|
{
|
|
|
|
case STATUS_DLL_NOT_FOUND:
|
|
|
|
{
|
2013-03-30 00:19:11 +08:00
|
|
|
path_conv pc;
|
|
|
|
if (!procinfo)
|
2020-01-29 04:59:22 +08:00
|
|
|
pc.check ("/dev/null", PC_POSIX);
|
2013-03-30 00:19:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
UNICODE_STRING uc;
|
|
|
|
RtlInitUnicodeString(&uc, procinfo->progname);
|
2020-01-29 04:59:22 +08:00
|
|
|
pc.check (&uc, PC_POSIX);
|
2013-03-30 00:19:11 +08:00
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
small_printf ("%s: error while loading shared libraries: %s: cannot "
|
|
|
|
"open shared object file: No such file or directory\n",
|
2015-07-20 04:38:30 +08:00
|
|
|
pc.get_posix (), find_first_notloaded_dll (pc));
|
2011-11-06 02:29:51 +08:00
|
|
|
x = 127 << 8;
|
2008-03-27 09:50:40 +08:00
|
|
|
}
|
|
|
|
break;
|
2009-10-07 23:47:38 +08:00
|
|
|
case STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION: /* custom error value */
|
|
|
|
/* We've already printed the error message in pseudo-reloc.c */
|
2011-11-06 02:29:51 +08:00
|
|
|
x = 127 << 8;
|
|
|
|
break;
|
|
|
|
case STATUS_ACCESS_VIOLATION:
|
|
|
|
x = SIGSEGV;
|
2009-10-07 23:47:38 +08:00
|
|
|
break;
|
2011-11-06 02:47:39 +08:00
|
|
|
case STATUS_ILLEGAL_INSTRUCTION:
|
|
|
|
x = SIGILL;
|
|
|
|
break;
|
2013-05-24 21:32:10 +08: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 21:43:26 +08: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.
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
|
2013-05-24 21:32:10 +08:00
|
|
|
FIXME: For now, just return with SIGBUS set. Maybe it's better to add
|
|
|
|
a lengthy small_printf instead. */
|
|
|
|
x = SIGBUS;
|
|
|
|
break;
|
2022-02-24 20:04:49 +08:00
|
|
|
case STATUS_CONTROL_C_EXIT:
|
|
|
|
x = SIGINT;
|
|
|
|
break;
|
2008-03-27 09:50:40 +08:00
|
|
|
default:
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("*** STATUS_%y\n", x);
|
2011-11-06 02:29:51 +08:00
|
|
|
x = 127 << 8;
|
2008-03-27 09:50:40 +08:00
|
|
|
}
|
2011-11-06 02:29:51 +08:00
|
|
|
return EXITCODE_SET | x;
|
2008-03-27 09:50:40 +08:00
|
|
|
}
|
|
|
|
|
2005-01-11 23:31:04 +08:00
|
|
|
# define self (*this)
|
2009-10-07 05:51:17 +08:00
|
|
|
void
|
|
|
|
pinfo::set_exit_code (DWORD x)
|
|
|
|
{
|
|
|
|
if (x >= 0xc0000000UL)
|
2011-11-06 02:29:51 +08:00
|
|
|
self->exitcode = status_exit (x);
|
|
|
|
else
|
|
|
|
self->exitcode = EXITCODE_SET | (sigExeced ?: (x & 0xff) << 8);
|
2009-10-07 05:51:17 +08:00
|
|
|
}
|
|
|
|
|
2000-10-15 09:37:07 +08:00
|
|
|
void
|
2005-01-17 12:12:08 +08:00
|
|
|
pinfo::maybe_set_exit_code_from_windows ()
|
2005-01-11 23:31:04 +08:00
|
|
|
{
|
|
|
|
DWORD x = 0xdeadbeef;
|
|
|
|
DWORD oexitcode = self->exitcode;
|
2005-09-20 10:06:57 +08:00
|
|
|
|
2005-01-17 01:00:27 +08:00
|
|
|
if (hProcess && !(self->exitcode & EXITCODE_SET))
|
2005-01-11 23:31:04 +08:00
|
|
|
{
|
2008-03-27 09:50:40 +08:00
|
|
|
WaitForSingleObject (hProcess, INFINITE); /* just to be safe, in case
|
|
|
|
process hasn't quite exited
|
|
|
|
after closing pipe */
|
2005-01-11 23:31:04 +08:00
|
|
|
GetExitCodeProcess (hProcess, &x);
|
2009-10-07 05:51:17 +08:00
|
|
|
set_exit_code (x);
|
2005-01-11 23:31:04 +08:00
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("pid %d, exit value - old %y, windows %y, cygwin %y",
|
2005-01-23 05:17:53 +08:00
|
|
|
self->pid, oexitcode, x, self->exitcode);
|
2005-01-11 23:31:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pinfo::exit (DWORD n)
|
2000-10-15 09:37:07 +08:00
|
|
|
{
|
2013-01-14 14:03:59 +08:00
|
|
|
debug_only_printf ("winpid %d, exit %d", GetCurrentProcessId (), n);
|
|
|
|
proc_terminate ();
|
2011-11-26 10:35:49 +08:00
|
|
|
lock_process until_exit (true);
|
2004-02-14 03:34:32 +08:00
|
|
|
cygthread::terminate ();
|
2005-10-18 07:27:00 +08:00
|
|
|
|
2005-01-17 01:00:27 +08:00
|
|
|
if (n != EXITCODE_NOSET)
|
2005-09-14 01:08:54 +08:00
|
|
|
self->exitcode = EXITCODE_SET | n;/* We're really exiting. Record the UNIX exit code. */
|
2005-09-20 10:06:57 +08:00
|
|
|
else
|
2013-01-14 14:03:59 +08:00
|
|
|
maybe_set_exit_code_from_windows (); /* may block */
|
|
|
|
exit_state = ES_FINAL;
|
2005-09-20 10:06:57 +08:00
|
|
|
|
2023-03-05 18:02:13 +08:00
|
|
|
if (CTTY_IS_VALID (myself->ctty) && !iscons_dev (myself->ctty))
|
2010-10-24 02:55:01 +08:00
|
|
|
{
|
2010-10-24 02:57:43 +08:00
|
|
|
lock_ttys here;
|
2011-05-29 02:17:09 +08:00
|
|
|
tty *t = cygwin_shared->tty[device::minor(myself->ctty)];
|
2010-10-24 02:55:01 +08:00
|
|
|
if (!t->slave_alive ())
|
|
|
|
t->setpgid (0);
|
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
|
|
|
|
/* FIXME: There is a potential race between an execed process and its
|
2005-09-23 11:35:41 +08:00
|
|
|
parent here. I hated to add a mutex just for that, though. */
|
2005-01-11 23:31:04 +08:00
|
|
|
struct rusage r;
|
2009-12-19 04:32:04 +08:00
|
|
|
fill_rusage (&r, GetCurrentProcess ());
|
2005-01-11 23:31:04 +08:00
|
|
|
add_rusage (&self->rusage_self, &r);
|
2005-03-08 22:31:21 +08:00
|
|
|
int exitcode = self->exitcode & 0xffff;
|
|
|
|
if (!self->cygstarted)
|
2006-01-02 11:14:56 +08:00
|
|
|
exitcode = ((exitcode & 0xff) << 8) | ((exitcode >> 8) & 0xff);
|
forkables: Create forkable hardlinks, yet unused.
In preparation to protect fork() against dll- and exe-updates, create
hardlinks to the main executable and each loaded dll in subdirectories
of /var/run/cygfork/, if that one exists on the NTFS file system.
The directory names consist of the user sid, the main executable's NTFS
IndexNumber, and the most recent LastWriteTime of all involved binaries
(dlls and main executable). Next to the main.exe hardlink we create the
empty file main.exe.local to enable dll redirection.
The name of the mutex to synchronize hardlink creation/cleanup also is
assembled from these directory names, to allow for synchronized cleanup
of even orphaned hardlink directories.
The hardlink to each dynamically loaded dll goes into another directory,
named using the NTFS IndexNumber of the dll's original directory.
* Makefile.in (DLL_OFILES): Add forkable.o.
* dll_init.h (struct dll): Declare member variables fbi, fii,
forkable_ntname. Declare methods nominate_forkable,
create_forkable.
(struct dll_list): Declare enum forkables_needs. Declare member
variables forkables_dirx_size, forkables_dirx_ntname,
forkables_mutex_name, forkables_mutex. Declare private methods
forkable_ntnamesize, prepare_forkables_nomination,
update_forkables_needs, update_forkables, create_forkables,
denominate_forkables, close_mutex, try_remove_forkables,
set_forkables_inheritance, request_forkables. Declare public
static methods ntopenfile, read_fii, read_fbi. Declare public
methods release_forkables, cleanup_forkables. Define public
inline method setup_forkables.
* dll_init.cc (dll_list::alloc): Allocate memory to hold the
name of the hardlink in struct dll member forkable_ntname.
Initialize struct dll members fbi, fii.
(dll_list::load_after_fork): Call release_forkables method.
* fork.cc: Rename public fork function to static dofork, add
with_forkables as bool pointer parameter. Add new fork function
calling dofork. (struct frok): Add bool pointer member
with_forkables, add as constructor parameter.
(frok::parent): Call dlls.setup_forkables before CreateProcessW,
dlls.release_forkables afterwards.
* pinfo.cc (pinfo::exit): Call dlls.cleanup_forkables.
* syscalls.cc (_unlink_nt): Rename public unlink_nt function to
static _unlink_nt, with 'shareable' as additional argument.
(unlink_nt): New, wrap _unlink_nt for original behaviour.
(unlink_nt_shareable): New, wrap _unlink_nt to keep a binary
file still loadable while removing one of its hardlinks.
* forkable.cc: New file.
Implement static functions mkdirs, rmdirs, rmdirs_synchronized,
stat_real_file_once, format_IndexNumber, rootname, sidname,
exename, lwtimename. Define static array forkable_nameparts.
(struct dll): Implement nominate_forkable, create_forkable.
(struct dll_list): Implement static methods ntopenfile,
read_fii, read_fbi. Implement forkable_ntnamesize,
2016-12-07 18:58:27 +08:00
|
|
|
sigproc_printf ("Calling dlls.cleanup_forkables n %y, exitcode %y", n, exitcode);
|
|
|
|
dlls.cleanup_forkables ();
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("Calling ExitProcess n %y, exitcode %y", n, exitcode);
|
2011-11-29 00:00:07 +08:00
|
|
|
if (!TerminateProcess (GetCurrentProcess (), exitcode))
|
|
|
|
system_printf ("TerminateProcess failed, %E");
|
2005-09-24 07:37:52 +08:00
|
|
|
ExitProcess (exitcode);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2005-01-11 23:31:04 +08:00
|
|
|
# undef self
|
2000-02-18 03:38:33 +08:00
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
/* Return next free Cygwin PID between 2 and 65535, round-robin. Each new
|
|
|
|
PID is checked that it doesn't collide with an existing PID. For that,
|
|
|
|
just check if the "cygpid.PID" section exists. */
|
|
|
|
pid_t
|
|
|
|
create_cygwin_pid ()
|
|
|
|
{
|
|
|
|
pid_t pid = 0;
|
|
|
|
WCHAR sym_name[24];
|
|
|
|
UNICODE_STRING sym_str;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
HANDLE sym_hdl;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
pid = ((uint32_t) InterlockedIncrement (&cygwin_shared->pid_src))
|
2019-02-02 19:23:39 +08:00
|
|
|
% MAX_PID;
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
}
|
|
|
|
while (pid < 2);
|
|
|
|
__small_swprintf (sym_name, L"cygpid.%u", pid);
|
|
|
|
RtlInitUnicodeString (&sym_str, sym_name);
|
|
|
|
InitializeObjectAttributes (&attr, &sym_str, OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (), NULL);
|
|
|
|
/* We just want to know if the section (and thus the process) still
|
|
|
|
exists. Instead of actually opening the section, try to open
|
|
|
|
it as symlink. NtOpenSymbolicLinkObject will always returns an
|
|
|
|
error:
|
|
|
|
- STATUS_OBJECT_NAME_NOT_FOUND if the section doesn't exist,
|
|
|
|
so the slot is free and we can use this pid.
|
|
|
|
- STATUS_OBJECT_TYPE_MISMATCH if the section exists, so we have
|
|
|
|
to skip this pid and loop to try the next one.
|
|
|
|
As side-effect we never have to close the section handle and thus
|
|
|
|
we don't influence the lifetime of the section. */
|
|
|
|
status = NtOpenSymbolicLinkObject (&sym_hdl, SYMBOLIC_LINK_QUERY, &attr);
|
|
|
|
}
|
|
|
|
while (status == STATUS_OBJECT_TYPE_MISMATCH);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert Windows WINPID into Cygwin PID. Utilize the "winpid.WINPID"
|
|
|
|
symlinks created for each process. The symlink contains the Cygwin
|
|
|
|
PID as target. Return 0 if no "winpid.WINPID" symlink exists for
|
|
|
|
this WINPID. */
|
|
|
|
pid_t
|
|
|
|
cygwin_pid (DWORD dwProcessId)
|
|
|
|
{
|
|
|
|
WCHAR sym_name[24];
|
|
|
|
WCHAR pid_name[12];
|
|
|
|
UNICODE_STRING sym_str;
|
|
|
|
UNICODE_STRING pid_str;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
HANDLE sym_hdl;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
__small_swprintf (sym_name, L"winpid.%u", dwProcessId);
|
|
|
|
RtlInitUnicodeString (&sym_str, sym_name);
|
|
|
|
InitializeObjectAttributes (&attr, &sym_str, OBJ_CASE_INSENSITIVE,
|
|
|
|
get_shared_parent_dir (), NULL);
|
|
|
|
status = NtOpenSymbolicLinkObject (&sym_hdl, SYMBOLIC_LINK_QUERY, &attr);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
return 0;
|
|
|
|
RtlInitEmptyUnicodeString (&pid_str, pid_name,
|
|
|
|
sizeof pid_name - sizeof (WCHAR));
|
|
|
|
status = NtQuerySymbolicLinkObject (sym_hdl, &pid_str, NULL);
|
|
|
|
NtClose (sym_hdl);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
system_printf ("NtOpenSymbolicLinkObject: %y, PID %u, ret 0",
|
|
|
|
status, dwProcessId);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pid_str.Buffer[pid_str.Length / sizeof (WCHAR)] = L'\0';
|
|
|
|
pid_t ret = (pid_t) wcstoul (pid_str.Buffer, NULL, 10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create "winpid.WINPID" symlinks with the Cygwin PID of that process as
|
|
|
|
target. This is used to find the Cygwin PID for a given Windows WINPID. */
|
2019-02-03 01:11:42 +08:00
|
|
|
void
|
|
|
|
pinfo::create_winpid_symlink ()
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
{
|
|
|
|
WCHAR sym_name[24];
|
|
|
|
WCHAR pid_name[24];
|
|
|
|
UNICODE_STRING sym_str;
|
|
|
|
UNICODE_STRING pid_str;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
|
2019-02-03 01:11:42 +08:00
|
|
|
__small_swprintf (sym_name, L"winpid.%u", procinfo->dwProcessId);
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
RtlInitUnicodeString (&sym_str, sym_name);
|
|
|
|
__small_swprintf (pid_name, L"%u", procinfo->pid);
|
|
|
|
RtlInitUnicodeString (&pid_str, pid_name);
|
|
|
|
InitializeObjectAttributes (&attr, &sym_str, OBJ_CASE_INSENSITIVE,
|
2019-03-12 04:40:04 +08:00
|
|
|
get_shared_parent_dir (),
|
|
|
|
everyone_sd (SYMBOLIC_LINK_QUERY));
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
NtCreateSymbolicLinkObject (&winpid_hdl, SYMBOLIC_LINK_ALL_ACCESS,
|
|
|
|
&attr, &pid_str);
|
|
|
|
}
|
|
|
|
|
2011-11-14 09:29:49 +08: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-18 03:38:33 +08:00
|
|
|
void
|
2005-01-23 05:17:53 +08:00
|
|
|
pinfo::init (pid_t n, DWORD flag, HANDLE h0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-04-29 07:59:44 +08:00
|
|
|
shared_locations shloc;
|
2005-01-26 08:15:11 +08:00
|
|
|
h = NULL;
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (myself && n == myself->pid)
|
2000-07-30 00:24:59 +08:00
|
|
|
{
|
2000-08-26 11:48:37 +08:00
|
|
|
procinfo = myself;
|
2000-07-30 00:24:59 +08:00
|
|
|
destroy = 0;
|
|
|
|
return;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
int createit = (flag & PID_IN_USE);
|
2005-01-26 08:15:11 +08:00
|
|
|
DWORD access = FILE_MAP_READ
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
| (flag & (PID_IN_USE | PID_MAP_RW) ? FILE_MAP_WRITE : 0);
|
2011-11-14 09:29:49 +08:00
|
|
|
if (!h0 || myself.h)
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
shloc = (flag & PID_IN_USE) ? SH_JUSTCREATE : SH_JUSTOPEN;
|
2002-10-15 04:25:52 +08:00
|
|
|
else
|
|
|
|
{
|
2005-04-29 07:59:44 +08:00
|
|
|
shloc = SH_MYSELF;
|
2005-01-26 08:15:11 +08:00
|
|
|
if (h0 == INVALID_HANDLE_VALUE)
|
|
|
|
h0 = NULL;
|
2002-10-15 04:25:52 +08:00
|
|
|
}
|
|
|
|
|
2005-01-26 08:15:11 +08:00
|
|
|
procinfo = NULL;
|
2005-06-08 03:31:42 +08:00
|
|
|
PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
|
2005-04-29 07:59:44 +08:00
|
|
|
PSECURITY_ATTRIBUTES sec_attribs = sec_user_nih (sa_buf, cygheap->user.sid(),
|
|
|
|
well_known_world_sid,
|
|
|
|
FILE_MAP_READ);
|
|
|
|
|
2005-01-26 08:15:11 +08:00
|
|
|
for (int i = 0; i < 20; i++)
|
2000-10-15 09:37:07 +08:00
|
|
|
{
|
2023-01-17 05:25:42 +08:00
|
|
|
bool created;
|
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
procinfo = (_pinfo *) open_shared (L"cygpid", n, h0, sizeof (_pinfo),
|
2023-01-17 05:25:42 +08:00
|
|
|
shloc, created, sec_attribs, access);
|
2005-01-26 08:15:11 +08:00
|
|
|
if (!h0)
|
|
|
|
{
|
2005-04-29 07:59:44 +08:00
|
|
|
if (createit)
|
|
|
|
__seterrno ();
|
|
|
|
return;
|
2000-10-15 09:37:07 +08:00
|
|
|
}
|
2005-01-23 05:17:53 +08:00
|
|
|
|
|
|
|
if (!procinfo)
|
2004-07-25 03:18:58 +08:00
|
|
|
{
|
2005-01-23 05:17:53 +08:00
|
|
|
if (exit_state)
|
2004-07-25 03:18:58 +08:00
|
|
|
return;
|
|
|
|
|
2012-11-11 18:56:01 +08:00
|
|
|
if (GetLastError () == ERROR_INVALID_HANDLE)
|
|
|
|
api_fatal ("MapViewOfFileEx h0 %p, i %d failed, %E", h0, i);
|
|
|
|
|
2005-07-29 22:33:50 +08:00
|
|
|
debug_printf ("MapViewOfFileEx h0 %p, i %d failed, %E", h0, i);
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2005-01-23 05:17:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
/* Just fetching info for ps or /proc, don't do anything rash. */
|
|
|
|
if (!created && !(flag & PID_NEW) && !procinfo->ppid
|
|
|
|
&& (flag & PID_PROCINFO))
|
|
|
|
break;
|
2012-09-18 04:07:49 +08:00
|
|
|
|
2012-05-08 23:06:43 +08: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 07:34:45 +08:00
|
|
|
}
|
2012-05-08 23:06:43 +08:00
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (procinfo->process_state & PID_REAPED)
|
2000-10-20 12:20:21 +08:00
|
|
|
{
|
2005-01-23 05:17:53 +08:00
|
|
|
set_errno (ESRCH);
|
|
|
|
break;
|
2000-10-20 12:20:21 +08:00
|
|
|
}
|
|
|
|
|
2005-12-22 11:16:36 +08: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. */
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (!created && createit
|
|
|
|
&& (procinfo->process_state & (PID_EXITED | PID_REAPED)))
|
2005-09-23 05:10:07 +08:00
|
|
|
{
|
2005-12-22 11:16:36 +08:00
|
|
|
debug_printf ("looping because pid %d, procinfo->pid %d, "
|
2011-05-05 23:03:54 +08:00
|
|
|
"procinfo->dwProcessid %u has PID_EXITED|PID_REAPED set",
|
2005-09-23 05:10:07 +08:00
|
|
|
n, procinfo->pid, procinfo->dwProcessId);
|
|
|
|
goto loop;
|
|
|
|
}
|
2000-10-15 09:37:07 +08:00
|
|
|
|
2013-12-18 11:58:11 +08:00
|
|
|
if (flag & PID_NEW)
|
|
|
|
procinfo->start_time = time (NULL);
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (created)
|
2005-01-17 01:00:27 +08:00
|
|
|
procinfo->pid = n;
|
2004-11-26 12:15:10 +08:00
|
|
|
|
2005-01-23 05:17:53 +08:00
|
|
|
h = h0; /* Success! */
|
2000-10-15 09:37:07 +08:00
|
|
|
break;
|
2005-01-23 05:17:53 +08:00
|
|
|
|
|
|
|
loop:
|
2011-11-14 09:29:49 +08:00
|
|
|
_pinfo_release ();
|
2005-02-27 04:50:25 +08:00
|
|
|
if (h0)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2005-01-23 05:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (h)
|
2005-01-26 08:15:11 +08:00
|
|
|
{
|
|
|
|
destroy = 1;
|
|
|
|
ProtectHandle1 (h, pinfo_shared_handle);
|
|
|
|
}
|
2005-01-23 05:17:53 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
h = h0;
|
2011-11-14 09:29:49 +08:00
|
|
|
_pinfo_release ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2023-01-18 04:58:06 +08:00
|
|
|
if (shloc == SH_MYSELF)
|
|
|
|
cygheap->shared_regions.myself_shared_addr = procinfo;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-10-09 10:53:44 +08:00
|
|
|
|
2003-09-27 09:58:23 +08:00
|
|
|
void
|
|
|
|
pinfo::set_acl()
|
|
|
|
{
|
2005-06-08 03:31:42 +08:00
|
|
|
PACL acl_buf = (PACL) alloca (1024);
|
2003-09-27 09:58:23 +08:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
2007-07-19 16:36:32 +08:00
|
|
|
NTSTATUS status;
|
2003-11-29 04:55:59 +08:00
|
|
|
|
2005-06-08 03:31:42 +08:00
|
|
|
sec_acl (acl_buf, true, true, cygheap->user.sid (),
|
2003-09-27 09:58:23 +08:00
|
|
|
well_known_world_sid, FILE_MAP_READ);
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
|
2011-04-28 23:54:47 +08:00
|
|
|
status = RtlSetDaclSecurityDescriptor (&sd, TRUE, acl_buf, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("RtlSetDaclSecurityDescriptor %y", status);
|
2007-07-19 16:36:32 +08:00
|
|
|
else if ((status = NtSetSecurityObject (h, DACL_SECURITY_INFORMATION, &sd)))
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtSetSecurityObject %y", status);
|
2003-09-27 09:58:23 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 19:49:15 +08:00
|
|
|
void
|
|
|
|
pinfo_minimal::set_inheritance (bool inherit)
|
|
|
|
{
|
|
|
|
DWORD i_flag = inherit ? HANDLE_FLAG_INHERIT : 0;
|
|
|
|
|
|
|
|
SetHandleInformation (rd_proc_pipe, HANDLE_FLAG_INHERIT, i_flag);
|
|
|
|
SetHandleInformation (hProcess, HANDLE_FLAG_INHERIT, i_flag);
|
|
|
|
SetHandleInformation (h, HANDLE_FLAG_INHERIT, i_flag);
|
|
|
|
}
|
|
|
|
|
2011-11-14 09:29:49 +08:00
|
|
|
pinfo::pinfo (HANDLE parent, pinfo_minimal& from, pid_t pid):
|
|
|
|
pinfo_minimal (), destroy (false), procinfo (NULL), waiter_ready (false),
|
2011-12-18 07:39:47 +08:00
|
|
|
wait_thread (NULL)
|
2011-11-14 09:29:49 +08:00
|
|
|
{
|
2019-11-02 19:49:15 +08:00
|
|
|
/* cygheap_exec_info::record_children set the inheritance of the required
|
|
|
|
child handles so just copy them over... */
|
|
|
|
rd_proc_pipe = from.rd_proc_pipe;
|
|
|
|
hProcess = from.hProcess;
|
|
|
|
h = from.h;
|
|
|
|
/* ...and reset their inheritance. */
|
|
|
|
set_inheritance (false);
|
|
|
|
init (pid, PID_MAP_RW, h);
|
2011-11-14 09:29:49 +08:00
|
|
|
}
|
|
|
|
|
2005-11-14 12:28:45 +08:00
|
|
|
const char *
|
|
|
|
_pinfo::_ctty (char *buf)
|
|
|
|
{
|
2023-03-05 18:02:13 +08:00
|
|
|
if (!CTTY_IS_VALID (ctty))
|
2024-01-31 03:42:37 +08:00
|
|
|
stpcpy (buf, "no ctty");
|
2005-11-14 12:28:45 +08:00
|
|
|
else
|
2011-05-29 02:17:09 +08:00
|
|
|
{
|
|
|
|
device d;
|
|
|
|
d.parse (ctty);
|
2016-06-23 22:56:41 +08:00
|
|
|
__small_sprintf (buf, "ctty %s", d.name ());
|
2011-05-29 02:17:09 +08:00
|
|
|
}
|
2005-11-14 12:28:45 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-10-16 06:37:30 +08:00
|
|
|
bool
|
|
|
|
_pinfo::set_ctty (fhandler_termios *fh, int flags)
|
2003-12-08 06:37:12 +08:00
|
|
|
{
|
2011-10-16 06:37:30 +08:00
|
|
|
tty_min& tc = *fh->tc ();
|
2013-04-23 17:44:36 +08: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);
|
2023-03-05 18:02:13 +08:00
|
|
|
if (fh && (!CTTY_IS_VALID (ctty) || ctty == tc.ntty) && !(flags & O_NOCTTY))
|
2003-12-08 06:37:12 +08:00
|
|
|
{
|
2023-03-05 18:02:13 +08:00
|
|
|
if (tc.getsid () && tc.getsid () != sid && ctty == CTTY_RELEASED)
|
2022-12-20 19:26:36 +08:00
|
|
|
; /* Do nothing if another session is associated with the TTY. */
|
|
|
|
else
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
2022-12-20 19:26:36 +08:00
|
|
|
ctty = tc.ntty;
|
|
|
|
if (cygheap->ctty != fh->archetype)
|
2011-05-06 06:30:53 +08:00
|
|
|
{
|
2022-12-20 19:26:36 +08:00
|
|
|
debug_printf ("cygheap->ctty %p, archetype %p",
|
|
|
|
cygheap->ctty, fh->archetype);
|
|
|
|
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", "");
|
|
|
|
}
|
2011-05-06 06:30:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-04 04:32:07 +08:00
|
|
|
lock_ttys here;
|
|
|
|
syscall_printf ("attaching %s sid %d, pid %d, pgid %d, tty->pgid %d, tty->sid %d",
|
2011-10-16 06:37:30 +08:00
|
|
|
__ctty (), sid, pid, pgid, tc.getpgid (), tc.getsid ());
|
2011-06-04 08:12:29 +08:00
|
|
|
if (!cygwin_finished_initializing && !myself->cygstarted
|
2019-07-25 16:40:03 +08:00
|
|
|
&& pgid == pid && tc.getpgid () && tc.getsid ()
|
|
|
|
/* Even GDB starts app via CreateProcess which changes cygstarted.
|
|
|
|
This results in setting the wrong pgid here, so just skip this
|
|
|
|
under debugger. */
|
|
|
|
&& !being_debugged ())
|
2012-02-15 03:08:20 +08:00
|
|
|
pgid = tc.getpgid ();
|
2003-12-08 06:37:12 +08:00
|
|
|
|
2012-01-08 14:24:17 +08: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);
|
2023-03-05 18:02:13 +08:00
|
|
|
if (CTTY_IS_VALID (ctty))
|
2022-12-26 19:50:15 +08:00
|
|
|
sid = tc.getsid ();
|
2012-01-08 14:24:17 +08:00
|
|
|
/* See above */
|
2019-09-21 05:10:35 +08:00
|
|
|
if ((!tc.getpgid () || being_debugged ()) && pgid == pid)
|
2012-01-08 14:24:17 +08:00
|
|
|
tc.setpgid (pgid);
|
2003-12-08 06:37:12 +08:00
|
|
|
}
|
2016-11-24 22:06:51 +08:00
|
|
|
debug_printf ("cygheap->ctty now %p, archetype %p", cygheap->ctty, fh ? fh->archetype : NULL);
|
2023-03-05 18:02:13 +08:00
|
|
|
return CTTY_IS_VALID (ctty);
|
2003-12-08 06:37:12 +08:00
|
|
|
}
|
|
|
|
|
2005-02-27 12:30:08 +08:00
|
|
|
/* Test to determine if a process really exists and is processing signals.
|
|
|
|
*/
|
2022-05-24 03:52:52 +08:00
|
|
|
bool
|
2005-02-27 12:30:08 +08:00
|
|
|
_pinfo::exists ()
|
|
|
|
{
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
return process_state && !(process_state & (PID_EXITED | PID_REAPED));
|
2005-02-27 12:30:08 +08:00
|
|
|
}
|
|
|
|
|
2002-10-31 05:05:18 +08:00
|
|
|
bool
|
|
|
|
_pinfo::alive ()
|
|
|
|
{
|
2016-06-27 23:56:16 +08:00
|
|
|
HANDLE h = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, false,
|
|
|
|
dwProcessId);
|
2002-10-31 05:05:18 +08:00
|
|
|
if (h)
|
|
|
|
CloseHandle (h);
|
|
|
|
return !!h;
|
|
|
|
}
|
|
|
|
|
2023-01-10 01:02:14 +08:00
|
|
|
static commune_result
|
|
|
|
commune_process_siginfo ()
|
|
|
|
{
|
|
|
|
commune_result res = { 0 };
|
|
|
|
|
|
|
|
res.pnd = sig_send (myself, __SIGPENDINGALL, NULL);
|
|
|
|
res.blk = cygheap->compute_sigblkmask ();
|
|
|
|
for (int sig = 1; sig < NSIG; ++sig)
|
|
|
|
if (global_sigs[sig].sa_handler == SIG_IGN)
|
|
|
|
res.ign |= SIGTOMASK (sig);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-08-05 03:16:32 +08:00
|
|
|
DWORD
|
2005-10-18 07:27:00 +08:00
|
|
|
commune_process (void *arg)
|
2002-10-31 05:05:18 +08:00
|
|
|
{
|
2005-10-18 07:27:00 +08:00
|
|
|
siginfo_t& si = *((siginfo_t *) arg);
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *path = tp.c_get ();
|
2002-10-31 05:05:18 +08:00
|
|
|
DWORD nr;
|
2005-09-29 06:56:47 +08: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-31 05:05:18 +08:00
|
|
|
|
2012-12-22 02:52:00 +08:00
|
|
|
lock_process now;
|
2006-03-20 09:59:23 +08:00
|
|
|
if (si._si_commune._si_code & PICOM_EXTRASTR)
|
|
|
|
si._si_commune._si_str = (char *) (&si + 1);
|
2005-10-04 01:23:54 +08:00
|
|
|
|
2005-09-29 06:56:47 +08:00
|
|
|
switch (si._si_commune._si_code)
|
2002-10-31 05:05:18 +08:00
|
|
|
{
|
|
|
|
case PICOM_CMDLINE:
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
sigproc_printf ("processing PICOM_CMDLINE");
|
2006-07-20 00:00:28 +08:00
|
|
|
unsigned n = 0;
|
2024-02-01 02:54:13 +08:00
|
|
|
int argc = __argv0_orig ? 1 : __argc_safe;
|
|
|
|
const char *argv[argc + 1];
|
2004-06-07 12:26:32 +08:00
|
|
|
|
2024-02-01 02:54:13 +08:00
|
|
|
for (int i = 0; i < argc; i++)
|
2003-09-05 09:55:01 +08:00
|
|
|
{
|
2024-02-01 02:41:25 +08:00
|
|
|
argv[i] = __argv[i] ?: "";
|
2003-09-05 09:55:01 +08:00
|
|
|
n += strlen (argv[i]) + 1;
|
|
|
|
}
|
2024-02-01 02:54:13 +08:00
|
|
|
argv[argc] = NULL;
|
2011-08-13 18:28:15 +08:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
2024-02-01 02:54:13 +08:00
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
|
|
|
|
else for (int i = 0; i < argc; i++)
|
|
|
|
if (!WritePipeOverlapped (tothem, __argv[i],
|
|
|
|
strlen (__argv[i]) + 1, &nr, 1000L))
|
|
|
|
{
|
|
|
|
sigproc_printf ("WritePipeOverlapped arg %d failed, %E", i);
|
|
|
|
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 08:37:18 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-01-29 19:23:07 +08:00
|
|
|
case PICOM_CWD:
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
sigproc_printf ("processing PICOM_CWD");
|
2008-02-15 00:47:11 +08:00
|
|
|
unsigned int n = strlen (cygheap->cwd.get (path, 1, 1, NT_MAX_PATH)) + 1;
|
2011-08-13 18:28:15 +08: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 19:23:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PICOM_ROOT:
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
sigproc_printf ("processing PICOM_ROOT");
|
2005-09-29 06:56:47 +08:00
|
|
|
unsigned n;
|
2005-01-29 19:23:07 +08:00
|
|
|
if (cygheap->root.exists ())
|
2005-01-31 18:28:55 +08:00
|
|
|
n = strlen (strcpy (path, cygheap->root.posix_path ())) + 1;
|
2005-01-29 19:23:07 +08:00
|
|
|
else
|
2005-01-31 18:28:55 +08:00
|
|
|
n = strlen (strcpy (path, "/")) + 1;
|
2011-08-13 18:28:15 +08: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 18:28:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-03-01 23:19:41 +08:00
|
|
|
case PICOM_SIGINFO:
|
|
|
|
{
|
|
|
|
sigproc_printf ("processing PICOM_SIGINFO");
|
2023-01-10 01:02:14 +08:00
|
|
|
commune_result cr = commune_process_siginfo ();
|
2022-03-01 23:19:41 +08:00
|
|
|
if (!WritePipeOverlapped (tothem, &cr, sizeof cr, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped siginfo failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2005-01-31 18:28:55 +08:00
|
|
|
case PICOM_FDS:
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
sigproc_printf ("processing PICOM_FDS");
|
2005-01-31 18:28:55 +08:00
|
|
|
unsigned int n = 0;
|
|
|
|
int fd;
|
|
|
|
cygheap_fdenum cfd;
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
|
|
|
n += sizeof (int);
|
|
|
|
cfd.rewind ();
|
2011-08-13 18:28:15 +08:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof fds failed, %E");
|
2005-01-31 18:28:55 +08:00
|
|
|
else
|
|
|
|
while ((fd = cfd.next ()) >= 0)
|
2011-08-13 18:28:15 +08:00
|
|
|
if (!WritePipeOverlapped (tothem, &fd, sizeof fd, &nr, 1000L))
|
2005-01-31 18:28:55 +08:00
|
|
|
{
|
2011-08-13 18:28:15 +08:00
|
|
|
sigproc_printf ("WritePipeOverlapped fd %d failed, %E", fd);
|
2005-01-31 18:28:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-05-02 11:50:11 +08:00
|
|
|
break;
|
2005-01-31 18:28:55 +08:00
|
|
|
}
|
2005-02-06 19:15:29 +08:00
|
|
|
case PICOM_PIPE_FHANDLER:
|
2005-10-02 08:13:41 +08:00
|
|
|
{
|
2019-01-06 04:57:03 +08:00
|
|
|
sigproc_printf ("processing PICOM_PIPE_FHANDLER");
|
2016-01-11 19:35:41 +08:00
|
|
|
int64_t unique_id = si._si_commune._si_pipe_unique_id;
|
2005-10-02 08:13:41 +08:00
|
|
|
unsigned int n = 0;
|
|
|
|
cygheap_fdenum cfd;
|
|
|
|
while (cfd.next () >= 0)
|
2016-01-11 19:35:41 +08:00
|
|
|
if (cfd->get_unique_id () == unique_id)
|
2005-10-02 08:13:41 +08:00
|
|
|
{
|
|
|
|
fhandler_pipe *fh = cfd;
|
|
|
|
n = sizeof *fh;
|
2011-08-13 18:28:15 +08: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 08:13:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-08-13 18:28:15 +08:00
|
|
|
if (!n && !WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof hdl failed, %E");
|
2005-10-02 08:13:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-01-06 04:57:03 +08:00
|
|
|
case PICOM_FILE_PATHCONV:
|
|
|
|
{
|
|
|
|
sigproc_printf ("processing PICOM_FILE_PATHCONV");
|
|
|
|
int fd = si._si_commune._si_fd;
|
2019-01-07 05:39:45 +08:00
|
|
|
uint32_t flags = si._si_commune._si_flags;
|
2019-01-06 04:57:03 +08:00
|
|
|
unsigned int n = 0;
|
|
|
|
cygheap_fdget cfd (fd);
|
2019-01-07 05:39:45 +08:00
|
|
|
if (cfd >= 0
|
|
|
|
&& (!(flags & FFH_LINKAT)
|
|
|
|
|| (cfd->get_flags () & (O_TMPFILE | O_EXCL))
|
|
|
|
!= (O_TMPFILE | O_EXCL)))
|
2019-01-06 04:57:03 +08:00
|
|
|
{
|
|
|
|
fhandler_base *fh = cfd;
|
|
|
|
void *ser_buf = fh->pc.serialize (fh->get_handle (), n);
|
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof hdl failed, %E");
|
|
|
|
else if (!WritePipeOverlapped (tothem, ser_buf, n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped hdl failed, %E");
|
|
|
|
cfree (ser_buf);
|
|
|
|
}
|
|
|
|
else if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof hdl failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2005-01-31 18:28:55 +08:00
|
|
|
case PICOM_FD:
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
sigproc_printf ("processing PICOM_FD");
|
2005-09-29 06:56:47 +08:00
|
|
|
int fd = si._si_commune._si_fd;
|
2005-02-06 19:15:29 +08:00
|
|
|
unsigned int n = 0;
|
2005-01-31 18:28:55 +08:00
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
2005-02-01 23:11:47 +08:00
|
|
|
n = strlen (strcpy (path, "")) + 1;
|
2005-01-31 18:28:55 +08:00
|
|
|
else
|
|
|
|
n = strlen (cfd->get_proc_fd_name (path)) + 1;
|
2011-08-13 18:28:15 +08: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 11:50:11 +08:00
|
|
|
break;
|
2017-01-10 23:38:01 +08:00
|
|
|
}
|
|
|
|
case PICOM_ENVIRON:
|
|
|
|
{
|
2017-01-10 23:03:09 +08:00
|
|
|
sigproc_printf ("processing PICOM_ENVIRON");
|
|
|
|
unsigned n = 0;
|
2022-07-29 04:00:40 +08:00
|
|
|
char **env = environ;
|
2018-06-06 23:45:55 +08:00
|
|
|
if (env)
|
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
n += strlen (*e) + 1;
|
2017-01-10 23:03:09 +08:00
|
|
|
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
|
|
|
|
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
|
2018-06-06 23:45:55 +08:00
|
|
|
else if (env)
|
2017-01-10 23:03:09 +08:00
|
|
|
for (char **e = env; *e; e++)
|
|
|
|
if (!WritePipeOverlapped (tothem, *e, strlen (*e) + 1, &nr, 1000L))
|
|
|
|
{
|
2017-01-10 23:38:01 +08:00
|
|
|
sigproc_printf ("WritePipeOverlapped arg %d failed, %E",
|
|
|
|
e - env);
|
2017-01-10 23:03:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-01-10 23:38:01 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-29 06:56:47 +08:00
|
|
|
if (process_sync)
|
|
|
|
{
|
2005-10-02 08:13:41 +08:00
|
|
|
DWORD res = WaitForSingleObject (process_sync, 5000);
|
|
|
|
if (res != WAIT_OBJECT_0)
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("WFSO failed - %u, %E", res);
|
2005-10-02 08:13:41 +08:00
|
|
|
else
|
|
|
|
sigproc_printf ("synchronized with pid %d", si.si_pid);
|
2005-09-29 06:56:47 +08:00
|
|
|
ForceCloseHandle (process_sync);
|
|
|
|
}
|
|
|
|
CloseHandle (tothem);
|
2005-10-18 07:27:00 +08:00
|
|
|
_my_tls._ctinfo->auto_release ();
|
|
|
|
return 0;
|
2002-10-31 05:05:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
commune_result
|
2005-09-29 06:56:47 +08:00
|
|
|
_pinfo::commune_request (__uint32_t code, ...)
|
2002-10-31 05:05:18 +08:00
|
|
|
{
|
|
|
|
DWORD nr;
|
2022-03-01 23:19:41 +08:00
|
|
|
commune_result res = { 0 };
|
* 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 08:37:18 +08:00
|
|
|
va_list args;
|
2005-09-29 06:56:47 +08: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 08:37:18 +08:00
|
|
|
|
2017-09-17 10:04:14 +08:00
|
|
|
if (!pid)
|
2002-10-31 05:05:18 +08:00
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-06-08 20:38:30 +08:00
|
|
|
if (ISSTATE (this, PID_NOTCYGWIN))
|
|
|
|
{
|
|
|
|
set_errno (ENOTSUP);
|
|
|
|
goto err;
|
|
|
|
}
|
2002-10-31 05:05:18 +08:00
|
|
|
|
2009-10-14 12:17:05 +08:00
|
|
|
va_start (args, code);
|
2005-09-29 06:56:47 +08:00
|
|
|
si._si_commune._si_code = code;
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case PICOM_PIPE_FHANDLER:
|
2016-01-11 19:35:41 +08:00
|
|
|
si._si_commune._si_pipe_unique_id = va_arg (args, int64_t);
|
2005-09-29 03:22:25 +08:00
|
|
|
break;
|
2002-10-31 05:05:18 +08:00
|
|
|
|
2005-09-29 06:56:47 +08:00
|
|
|
case PICOM_FD:
|
2019-01-06 04:57:03 +08:00
|
|
|
case PICOM_FILE_PATHCONV:
|
2005-09-29 06:56:47 +08:00
|
|
|
si._si_commune._si_fd = va_arg (args, int);
|
2019-01-07 05:39:45 +08:00
|
|
|
si._si_commune._si_flags = va_arg (args, uint32_t);
|
2005-09-29 06:56:47 +08:00
|
|
|
break;
|
2005-09-29 03:22:25 +08:00
|
|
|
|
2005-09-29 06:56:47 +08:00
|
|
|
break;
|
2005-09-29 03:22:25 +08:00
|
|
|
}
|
2009-10-14 12:17:05 +08:00
|
|
|
va_end (args);
|
2005-09-29 03:22:25 +08:00
|
|
|
|
2007-12-07 02:24:06 +08:00
|
|
|
char name_buf[MAX_PATH];
|
2013-04-23 17:44:36 +08:00
|
|
|
request_sync = CreateSemaphore (&sec_none_nih, 0, INT32_MAX,
|
2005-09-29 06:56:47 +08: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 08:13:41 +08:00
|
|
|
{
|
|
|
|
ForceCloseHandle (request_sync); /* don't signal semaphore since there was apparently no receiving process */
|
|
|
|
request_sync = NULL;
|
|
|
|
goto err;
|
|
|
|
}
|
2002-10-31 05:05:18 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
DWORD n;
|
2002-10-31 05:05:18 +08:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case PICOM_CMDLINE:
|
2005-01-29 19:23:07 +08:00
|
|
|
case PICOM_CWD:
|
2017-01-10 23:03:09 +08:00
|
|
|
case PICOM_ENVIRON:
|
2005-01-29 19:23:07 +08:00
|
|
|
case PICOM_ROOT:
|
2005-01-31 18:28:55 +08:00
|
|
|
case PICOM_FDS:
|
2005-09-29 06:56:47 +08:00
|
|
|
case PICOM_FD:
|
|
|
|
case PICOM_PIPE_FHANDLER:
|
2019-01-06 04:57:03 +08:00
|
|
|
case PICOM_FILE_PATHCONV:
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!ReadPipeOverlapped (fromthem, &n, sizeof n, &nr, 1000L)
|
2011-08-13 18:28:15 +08: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 08:37:18 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
2005-02-06 19:15:29 +08:00
|
|
|
if (!n)
|
2005-05-02 11:50:11 +08:00
|
|
|
res.s = NULL;
|
2005-02-06 19:15:29 +08:00
|
|
|
else
|
2005-05-02 11:50:11 +08:00
|
|
|
{
|
2007-11-27 05:30:49 +08:00
|
|
|
res.s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2005-02-06 19:15:29 +08:00
|
|
|
char *p;
|
2011-08-13 18:28:15 +08:00
|
|
|
for (p = res.s;
|
2013-04-23 17:44:36 +08:00
|
|
|
n && ReadPipeOverlapped (fromthem, p, n, &nr, 1000L);
|
2011-08-13 18:28:15 +08:00
|
|
|
p += nr, n -= nr)
|
2005-02-06 19:15:29 +08:00
|
|
|
continue;
|
2005-08-25 01:45:11 +08:00
|
|
|
if (n)
|
2005-02-06 19:15:29 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
2005-08-25 01:45:11 +08:00
|
|
|
res.n = p - res.s;
|
2005-05-02 11:50:11 +08:00
|
|
|
}
|
2002-10-31 05:05:18 +08:00
|
|
|
break;
|
2022-03-01 23:19:41 +08:00
|
|
|
case PICOM_SIGINFO:
|
|
|
|
if (!ReadPipeOverlapped (fromthem, &res, sizeof res, &nr, 1000L)
|
|
|
|
|| nr != sizeof res)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2002-10-31 05:05:18 +08:00
|
|
|
}
|
|
|
|
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 08:37:18 +08:00
|
|
|
memset (&res, 0, sizeof (res));
|
|
|
|
|
2002-10-31 05:05:18 +08:00
|
|
|
out:
|
2005-09-29 06:56:47 +08:00
|
|
|
if (request_sync)
|
|
|
|
{
|
|
|
|
LONG res;
|
|
|
|
ReleaseSemaphore (request_sync, 1, &res);
|
|
|
|
ForceCloseHandle (request_sync);
|
|
|
|
}
|
|
|
|
if (hp)
|
|
|
|
CloseHandle (hp);
|
|
|
|
if (fromthem)
|
|
|
|
CloseHandle (fromthem);
|
2002-10-31 05:05:18 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-02-06 19:15:29 +08:00
|
|
|
fhandler_pipe *
|
2016-01-11 19:35:41 +08:00
|
|
|
_pinfo::pipe_fhandler (int64_t unique_id, size_t &n)
|
2005-02-06 19:15:29 +08:00
|
|
|
{
|
2017-09-17 10:04:15 +08:00
|
|
|
if (!pid)
|
2005-02-06 19:15:29 +08:00
|
|
|
return NULL;
|
|
|
|
if (pid == myself->pid)
|
|
|
|
return NULL;
|
2016-01-11 19:35:41 +08:00
|
|
|
commune_result cr = commune_request (PICOM_PIPE_FHANDLER, unique_id);
|
2005-02-06 19:15:29 +08:00
|
|
|
n = cr.n;
|
|
|
|
return (fhandler_pipe *) cr.s;
|
|
|
|
}
|
|
|
|
|
2019-01-06 04:57:03 +08:00
|
|
|
void *
|
2019-01-07 05:39:45 +08:00
|
|
|
_pinfo::file_pathconv (int fd, uint32_t flags, size_t &n)
|
2019-01-06 04:57:03 +08:00
|
|
|
{
|
|
|
|
if (!pid)
|
|
|
|
return NULL;
|
|
|
|
if (pid == myself->pid)
|
|
|
|
return NULL;
|
2019-01-07 05:39:45 +08:00
|
|
|
commune_result cr = commune_request (PICOM_FILE_PATHCONV, fd, flags);
|
2019-01-06 04:57:03 +08:00
|
|
|
n = cr.n;
|
|
|
|
return (void *) cr.s;
|
|
|
|
}
|
|
|
|
|
2005-01-31 18:28:55 +08:00
|
|
|
char *
|
|
|
|
_pinfo::fd (int fd, size_t &n)
|
|
|
|
{
|
|
|
|
char *s;
|
2017-09-17 10:04:16 +08:00
|
|
|
if (!pid)
|
2005-01-31 18:28:55 +08:00
|
|
|
return NULL;
|
|
|
|
if (pid != myself->pid)
|
|
|
|
{
|
2005-09-29 06:56:47 +08:00
|
|
|
commune_result cr = commune_request (PICOM_FD, fd);
|
2005-01-31 18:28:55 +08:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cygheap_fdget cfd (fd);
|
|
|
|
if (cfd < 0)
|
2005-08-24 12:38:39 +08:00
|
|
|
s = cstrdup ("");
|
2005-01-31 18:28:55 +08:00
|
|
|
else
|
2008-02-15 00:47:11 +08:00
|
|
|
s = cfd->get_proc_fd_name ((char *) cmalloc_abort (HEAP_COMMUNE, NT_MAX_PATH));
|
2005-01-31 18:28:55 +08:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
_pinfo::fds (size_t &n)
|
|
|
|
{
|
|
|
|
char *s;
|
2017-09-17 10:04:10 +08:00
|
|
|
if (!pid)
|
2005-01-31 18:28:55 +08:00
|
|
|
return NULL;
|
|
|
|
if (pid != myself->pid)
|
|
|
|
{
|
2005-09-29 06:56:47 +08:00
|
|
|
commune_result cr = commune_request (PICOM_FDS);
|
2005-01-31 18:28:55 +08:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
int fd;
|
2005-10-24 23:42:14 +08:00
|
|
|
cygheap_fdenum cfd (true);
|
2005-01-31 18:28:55 +08:00
|
|
|
while ((fd = cfd.next ()) >= 0)
|
|
|
|
n += sizeof (int);
|
|
|
|
cfd.rewind ();
|
2007-11-27 05:30:49 +08:00
|
|
|
s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2005-01-31 18:28:55 +08:00
|
|
|
int *p = (int *) s;
|
|
|
|
while ((fd = cfd.next ()) >= 0 && (char *) p - s < (int) n)
|
|
|
|
*p++ = fd;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:23:07 +08:00
|
|
|
char *
|
|
|
|
_pinfo::root (size_t& n)
|
|
|
|
{
|
|
|
|
char *s;
|
2017-09-17 10:04:11 +08:00
|
|
|
if (!pid)
|
2005-01-29 19:23:07 +08:00
|
|
|
return NULL;
|
2015-06-09 03:48:13 +08:00
|
|
|
if (pid != myself->pid && !ISSTATE (this, PID_NOTCYGWIN))
|
2005-01-29 19:23:07 +08:00
|
|
|
{
|
2005-09-29 06:56:47 +08:00
|
|
|
commune_result cr = commune_request (PICOM_ROOT);
|
2005-01-29 19:23:07 +08:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cygheap->root.exists ())
|
2005-08-24 12:38:39 +08:00
|
|
|
s = cstrdup (cygheap->root.posix_path ());
|
2005-01-29 19:23:07 +08:00
|
|
|
else
|
2005-08-24 12:38:39 +08:00
|
|
|
s = cstrdup ("/");
|
2005-01-29 19:23:07 +08:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:19:41 +08:00
|
|
|
int
|
|
|
|
_pinfo::siginfo (sigset_t &pnd, sigset_t &blk, sigset_t &ign)
|
|
|
|
{
|
2023-01-10 01:02:14 +08:00
|
|
|
commune_result cr;
|
|
|
|
|
2022-03-01 23:19:41 +08:00
|
|
|
if (!pid)
|
|
|
|
return -1;
|
|
|
|
if (pid != myself->pid && !ISSTATE (this, PID_NOTCYGWIN))
|
2023-01-10 01:02:14 +08:00
|
|
|
cr = commune_request (PICOM_SIGINFO);
|
2022-03-01 23:19:41 +08:00
|
|
|
else
|
2023-01-10 01:02:14 +08:00
|
|
|
cr = commune_process_siginfo ();
|
|
|
|
pnd = cr.pnd;
|
|
|
|
blk = cr.blk;
|
|
|
|
ign = cr.ign;
|
2022-03-01 23:19:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-09 03:48:13 +08: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 23:56:16 +08:00
|
|
|
proc = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ,
|
|
|
|
FALSE, pid);
|
2015-06-09 03:48:13 +08: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 19:23:07 +08:00
|
|
|
char *
|
|
|
|
_pinfo::cwd (size_t& n)
|
|
|
|
{
|
2015-06-09 03:55:11 +08:00
|
|
|
char *s = NULL;
|
2017-09-17 10:04:12 +08:00
|
|
|
if (!pid)
|
2005-01-29 19:23:07 +08:00
|
|
|
return NULL;
|
2015-06-09 03:48:13 +08: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 19:23:07 +08:00
|
|
|
{
|
2005-09-29 06:56:47 +08:00
|
|
|
commune_result cr = commune_request (PICOM_CWD);
|
2005-01-29 19:23:07 +08:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
s = (char *) cmalloc_abort (HEAP_COMMUNE, NT_MAX_PATH);
|
|
|
|
cygheap->cwd.get (s, 1, 1, NT_MAX_PATH);
|
2005-01-29 19:23:07 +08:00
|
|
|
n = strlen (s) + 1;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-10-31 05:05:18 +08:00
|
|
|
char *
|
2003-03-09 03:22:08 +08:00
|
|
|
_pinfo::cmdline (size_t& n)
|
2002-10-31 05:05:18 +08:00
|
|
|
{
|
2015-06-09 03:55:11 +08:00
|
|
|
char *s = NULL;
|
2017-09-17 10:04:13 +08:00
|
|
|
if (!pid)
|
2002-10-31 05:05:18 +08:00
|
|
|
return NULL;
|
2015-06-09 03:48:13 +08: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-31 05:05:18 +08:00
|
|
|
{
|
2005-09-29 06:56:47 +08:00
|
|
|
commune_result cr = commune_request (PICOM_CMDLINE);
|
2002-10-31 05:05:18 +08:00
|
|
|
s = cr.s;
|
|
|
|
n = cr.n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-20 00:00:28 +08:00
|
|
|
n = 0;
|
2024-02-01 02:54:13 +08:00
|
|
|
int argc = __argv0_orig ? 1 : __argc_safe;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
n += strlen (__argv[i]) + 1;
|
2002-10-31 05:05:18 +08:00
|
|
|
char *p;
|
2007-11-27 05:30:49 +08:00
|
|
|
p = s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
|
2024-02-01 02:54:13 +08:00
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
p = stpcpy (p, __argv[i]) + 1;
|
2002-10-31 05:05:18 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-01-10 23:03:09 +08:00
|
|
|
|
|
|
|
char *
|
|
|
|
_pinfo::environ (size_t& n)
|
|
|
|
{
|
|
|
|
char **env = NULL;
|
2017-09-17 10:04:17 +08:00
|
|
|
if (!pid)
|
2017-01-10 23:03:09 +08:00
|
|
|
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
|
2022-07-29 04:00:40 +08:00
|
|
|
env = ::environ;
|
2017-01-10 23:03:09 +08:00
|
|
|
|
|
|
|
if (env == NULL)
|
2017-01-10 23:38:01 +08:00
|
|
|
return NULL;
|
2017-01-10 23:03:09 +08: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++)
|
2024-01-31 03:42:37 +08:00
|
|
|
p = stpcpy (p, *e) + 1;
|
2017-01-10 23:03:09 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-11-26 12:15:10 +08:00
|
|
|
/* This is the workhorse which waits for the write end of the pipe
|
2004-12-25 02:31:23 +08: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. */
|
2022-08-05 03:16:32 +08:00
|
|
|
static DWORD
|
2004-11-26 12:15:10 +08:00
|
|
|
proc_waiter (void *arg)
|
|
|
|
{
|
2005-01-17 12:12:08 +08:00
|
|
|
pinfo vchild = *(pinfo *) arg;
|
|
|
|
((pinfo *) arg)->waiter_ready = true;
|
2004-11-26 12:15:10 +08:00
|
|
|
|
2006-02-07 02:24:11 +08:00
|
|
|
siginfo_t si = {0};
|
2004-11-26 12:15:10 +08:00
|
|
|
si.si_signo = SIGCHLD;
|
2005-09-26 21:47:27 +08:00
|
|
|
si.si_code = CLD_EXITED;
|
2004-11-26 12:15:10 +08: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 09:29:49 +08:00
|
|
|
bool its_me = vchild == myself;
|
2004-11-26 12:15:10 +08:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
DWORD nb;
|
|
|
|
char buf = '\0';
|
2005-01-11 23:31:04 +08:00
|
|
|
|
2004-11-26 12:15:10 +08: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 09:29:49 +08:00
|
|
|
if (!its_me && have_execed_cygwin)
|
|
|
|
break;
|
|
|
|
|
2004-11-26 12:15:10 +08:00
|
|
|
si.si_uid = vchild->uid;
|
|
|
|
|
|
|
|
switch (buf)
|
|
|
|
{
|
2004-12-03 12:46:00 +08:00
|
|
|
case __ALERT_ALIVE:
|
|
|
|
continue;
|
2004-11-26 12:15:10 +08:00
|
|
|
case 0:
|
|
|
|
/* Child exited. Do some cleanup and signal myself. */
|
2005-01-17 12:12:08 +08:00
|
|
|
vchild.maybe_set_exit_code_from_windows ();
|
2004-11-26 12:15:10 +08:00
|
|
|
if (WIFEXITED (vchild->exitcode))
|
2005-09-26 21:47:27 +08:00
|
|
|
si.si_code = CLD_EXITED;
|
2004-11-26 12:15:10 +08:00
|
|
|
else if (WCOREDUMP (vchild->exitcode))
|
2005-09-26 21:47:27 +08:00
|
|
|
si.si_code = CLD_DUMPED;
|
2004-11-26 12:15:10 +08:00
|
|
|
else
|
2005-09-26 21:47:27 +08:00
|
|
|
si.si_code = CLD_KILLED;
|
2004-11-26 12:15:10 +08:00
|
|
|
si.si_status = vchild->exitcode;
|
2005-01-17 12:12:08 +08:00
|
|
|
vchild->process_state = PID_EXITED;
|
|
|
|
/* This should always be last. Do not use vchild-> beyond this point */
|
2004-11-26 12:15:10 +08:00
|
|
|
break;
|
|
|
|
case SIGTTIN:
|
|
|
|
case SIGTTOU:
|
|
|
|
case SIGTSTP:
|
|
|
|
case SIGSTOP:
|
2004-12-06 03:41:26 +08:00
|
|
|
if (ISSTATE (myself, PID_NOCLDSTOP)) // FIXME: No need for this flag to be in _pinfo any longer
|
|
|
|
continue;
|
2004-11-26 12:15:10 +08:00
|
|
|
/* Child stopped. Signal myself. */
|
2005-09-26 21:47:27 +08:00
|
|
|
si.si_code = CLD_STOPPED;
|
2004-11-26 12:15:10 +08:00
|
|
|
break;
|
|
|
|
case SIGCONT:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
system_printf ("unknown value %d on proc pipe", buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-11-14 09:29:49 +08:00
|
|
|
if (its_me && ch_spawn.signal_myself_exited ())
|
|
|
|
break;
|
2004-11-26 12:15:10 +08: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-23 00:59:03 +08:00
|
|
|
vchild.wait_thread = NULL;
|
2004-12-23 22:57:08 +08:00
|
|
|
_my_tls._ctinfo->auto_release (); /* automatically return the cygthread to the cygthread pool */
|
2004-11-26 12:15:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function to set up the process pipe and kick off proc_waiter */
|
2011-11-08 04:05:49 +08:00
|
|
|
bool
|
2004-11-26 12:15:10 +08:00
|
|
|
pinfo::wait ()
|
|
|
|
{
|
|
|
|
preserve (); /* Preserve the shared memory associated with the pinfo */
|
|
|
|
|
2005-01-17 12:12:08 +08:00
|
|
|
waiter_ready = false;
|
2004-11-26 12:15:10 +08:00
|
|
|
/* Fire up a new thread to track the subprocess */
|
2011-11-14 09:29:49 +08:00
|
|
|
cygthread *h = new cygthread (proc_waiter, this, "waitproc");
|
2004-11-26 12:15:10 +08:00
|
|
|
if (!h)
|
|
|
|
sigproc_printf ("tracking thread creation failed for pid %d", (*this)->pid);
|
|
|
|
else
|
|
|
|
{
|
2004-12-06 05:29:37 +08:00
|
|
|
wait_thread = h;
|
2013-04-23 17:44:36 +08:00
|
|
|
sigproc_printf ("created tracking thread for pid %d, winpid %y, rd_proc_pipe %p",
|
2004-11-26 12:15:10 +08:00
|
|
|
(*this)->pid, (*this)->dwProcessId, rd_proc_pipe);
|
|
|
|
}
|
|
|
|
|
2011-11-08 04:05:49 +08:00
|
|
|
return true;
|
2004-11-26 12:15:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* function to send a "signal" to the parent when something interesting happens
|
|
|
|
in the child. */
|
2004-12-03 12:46:00 +08:00
|
|
|
bool
|
2004-12-25 02:31:23 +08:00
|
|
|
_pinfo::alert_parent (char sig)
|
2004-11-26 12:15:10 +08:00
|
|
|
{
|
2004-12-03 12:46:00 +08:00
|
|
|
DWORD nb = 0;
|
2005-10-19 22:19:37 +08: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 23:07:30 +08:00
|
|
|
if (my_wr_proc_pipe)
|
2004-11-26 12:15:10 +08:00
|
|
|
{
|
2012-03-20 23:07:30 +08:00
|
|
|
if (WriteFile (my_wr_proc_pipe, &sig, 1, &nb, NULL))
|
2004-12-25 02:31:23 +08:00
|
|
|
/* all is well */;
|
|
|
|
else if (GetLastError () != ERROR_BROKEN_PIPE)
|
|
|
|
debug_printf ("sending %d notification to parent failed, %E", sig);
|
|
|
|
else
|
|
|
|
{
|
2005-01-17 12:12:08 +08:00
|
|
|
ppid = 1;
|
2012-03-20 23:07:30 +08:00
|
|
|
HANDLE closeit = my_wr_proc_pipe;
|
|
|
|
my_wr_proc_pipe = NULL;
|
2012-03-20 10:08:14 +08:00
|
|
|
ForceCloseHandle1 (closeit, wr_proc_pipe);
|
2004-12-25 02:31:23 +08:00
|
|
|
}
|
2004-11-26 12:15:10 +08:00
|
|
|
}
|
2004-12-03 12:46:00 +08:00
|
|
|
return (bool) nb;
|
2004-11-26 12:15:10 +08:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:48:37 +08:00
|
|
|
void
|
|
|
|
pinfo::release ()
|
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
_pinfo_release ();
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (winpid_hdl)
|
|
|
|
NtClose (winpid_hdl);
|
2011-11-14 09:29:49 +08:00
|
|
|
HANDLE close_h;
|
|
|
|
if (rd_proc_pipe)
|
2000-08-26 11:48:37 +08:00
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
close_h = rd_proc_pipe;
|
|
|
|
rd_proc_pipe = NULL;
|
|
|
|
ForceCloseHandle1 (close_h, rd_proc_pipe);
|
2005-01-23 05:17:53 +08:00
|
|
|
}
|
2011-11-14 09:29:49 +08:00
|
|
|
if (hProcess)
|
2005-01-23 05:17:53 +08:00
|
|
|
{
|
2011-11-14 09:29:49 +08:00
|
|
|
close_h = hProcess;
|
|
|
|
hProcess = NULL;
|
|
|
|
ForceCloseHandle1 (close_h, childhProc);
|
2000-08-26 11:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* DOCTOOL-START
|
|
|
|
|
|
|
|
<sect1 id="func-cygwin-winpid-to-pid">
|
|
|
|
<title>cygwin_winpid_to_pid</title>
|
|
|
|
|
2004-02-23 07:21:20 +08:00
|
|
|
<funcsynopsis><funcprototype>
|
2000-02-18 03:38:33 +08:00
|
|
|
<funcdef>extern "C" pid_t
|
|
|
|
<function>cygwin_winpid_to_pid</function>
|
|
|
|
</funcdef>
|
|
|
|
<paramdef>int <parameter>winpid</parameter></paramdef>
|
2004-02-23 07:21:20 +08:00
|
|
|
</funcprototype></funcsynopsis>
|
2000-02-18 03:38:33 +08: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 10:02:03 +08:00
|
|
|
pinfo p (cygwin_pid (winpid));
|
2000-07-30 00:24:59 +08:00
|
|
|
if (p)
|
|
|
|
return p->pid;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
set_errno (ESRCH);
|
|
|
|
return (pid_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-02 13:25:56 +08:00
|
|
|
#define slop_pidlist 200
|
|
|
|
#define size_pidlist(i) (sizeof (pidlist[0]) * ((i) + 1))
|
2000-11-05 14:42:23 +08:00
|
|
|
#define size_pinfolist(i) (sizeof (pinfolist[0]) * ((i) + 1))
|
2005-12-22 11:16:36 +08:00
|
|
|
class _onreturn
|
|
|
|
{
|
2013-12-18 11:58:11 +08:00
|
|
|
HANDLE h;
|
2005-12-22 11:16:36 +08:00
|
|
|
public:
|
|
|
|
~_onreturn ()
|
|
|
|
{
|
2013-12-18 11:58:11 +08:00
|
|
|
if (h)
|
2005-12-22 11:16:36 +08:00
|
|
|
{
|
2013-12-18 11:58:11 +08:00
|
|
|
CloseHandle (h);
|
2005-12-22 11:16:36 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 11:58:11 +08: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 11:16:36 +08:00
|
|
|
};
|
2000-11-02 13:25:56 +08:00
|
|
|
|
2000-11-05 14:42:23 +08:00
|
|
|
inline void
|
|
|
|
winpids::add (DWORD& nelem, bool winpid, DWORD pid)
|
|
|
|
{
|
|
|
|
pid_t cygpid = cygwin_pid (pid);
|
2005-12-22 11:16:36 +08:00
|
|
|
|
2000-11-05 14:42:23 +08:00
|
|
|
if (nelem >= npidlist)
|
|
|
|
{
|
|
|
|
npidlist += slop_pidlist;
|
2001-10-14 01:23:35 +08:00
|
|
|
pidlist = (DWORD *) realloc (pidlist, size_pidlist (npidlist + 1));
|
2019-07-16 02:22:07 +08:00
|
|
|
pinfolist = (pinfo *) realloc ((void *) pinfolist, size_pinfolist (npidlist + 1));
|
2000-11-05 14:42:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 11:58:11 +08:00
|
|
|
_onreturn onreturn;
|
2005-12-13 02:43:31 +08:00
|
|
|
pinfo& p = pinfolist[nelem];
|
2019-07-16 02:22:07 +08:00
|
|
|
memset ((void *) &p, 0, sizeof (p));
|
2005-12-13 02:43:31 +08:00
|
|
|
|
2005-12-24 06:50:20 +08:00
|
|
|
bool perform_copy;
|
2013-12-18 11:58:11 +08:00
|
|
|
if (cygpid == myself->pid)
|
|
|
|
{
|
|
|
|
p = myself;
|
|
|
|
perform_copy = false;
|
|
|
|
}
|
2005-12-22 11:16:36 +08:00
|
|
|
else
|
2013-12-18 11:58:11 +08:00
|
|
|
{
|
|
|
|
/* Open a process to prevent a subsequent exit from invalidating the
|
|
|
|
shared memory region. */
|
2016-06-27 23:56:16 +08:00
|
|
|
onreturn = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
|
2005-12-24 06:50:20 +08:00
|
|
|
|
2019-03-27 20:53:32 +08: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). */
|
2013-12-18 11:58:11 +08:00
|
|
|
perform_copy = onreturn ? make_copy : true;
|
|
|
|
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
p.init (cygpid, PID_PROCINFO | pinfo_access, NULL);
|
2013-12-18 11:58:11 +08:00
|
|
|
}
|
2019-04-02 19:00:22 +08:00
|
|
|
/* Did we catch the process during exec? Try to fix. */
|
|
|
|
if (p && p->dwProcessId != pid)
|
|
|
|
pid = p->dwProcessId;
|
2005-12-22 11:16:36 +08:00
|
|
|
|
|
|
|
/* If we're just looking for winpids then don't do any special cygwin "stuff* */
|
2000-11-05 14:42:23 +08:00
|
|
|
if (winpid)
|
2014-02-15 12:17:27 +08:00
|
|
|
{
|
|
|
|
perform_copy = true;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-11-05 14:42:23 +08:00
|
|
|
|
2005-12-22 11:16:36 +08:00
|
|
|
/* !p means that we couldn't find shared memory for this pid. Probably means
|
|
|
|
that it isn't a cygwin process. */
|
2005-12-13 02:43:31 +08:00
|
|
|
if (!p)
|
2003-09-17 09:15:56 +08:00
|
|
|
{
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
if (!pinfo_access || !cygpid)
|
2003-09-17 09:15:56 +08:00
|
|
|
return;
|
Cygwin: processes: use dedicated Cygwin PID rather than Windows PID
Using the Windows PID as Cygwin PID has a few drawbacks:
- the PIDs on Windows get reused quickly. Some POSIX applications choke
on that, so we need extra code to avoid too quick PID reuse.
- The code to avoid PID reuse keeps parent process handles and
(depending on a build option) child processes open unnecessarily.
- After an execve, the process has a split personality: Its Windows PID
is a new PID, while its Cygwin PID is the PID of the execve caller
process. This requires to keep two procinfo shared sections open, the
second just to redirect process info requests to the first, correct
one.
This patch changes the way Cygwin PIDs are generated:
- Cygwin PIDs are generated independently of the Windows PID, in a way
expected by POSIX processes. The PIDs are created incrementally in
the range between 2 and 65535, round-robin.
- On startup of the first Cygwin process, choose a semi-random start PID
for the first process in the lower PID range to make the PIDs slightly
unpredictable. This may not be necessary but it seems kind of inviting
to know that the first Cygwin process always starts with PID 2.
- Every process not only creates the shared procinfo section, but also a
symlink in the NT namespace, symlinking the Windows PID to the Cygwin
PID. This drops the need for the extra procinfo section after execve.
- Don't keep other process handles around unnecessarily.
- Simplify the code creating/opening the shared procinfo section and
make a clear distinction between interfaces getting a Cygwin PID and
interfaces getting a Windows PID.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-02-01 04:19:03 +08:00
|
|
|
p.init (cygpid, PID_PROCINFO, NULL);
|
2005-12-13 02:43:31 +08:00
|
|
|
if (!p)
|
2003-09-17 09:15:56 +08:00
|
|
|
return;
|
2013-12-18 11:58:11 +08:00
|
|
|
}
|
2003-09-17 09:15:56 +08:00
|
|
|
|
2019-03-27 20:53:32 +08:00
|
|
|
out:
|
2003-09-17 09:15:56 +08: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++)
|
2019-03-27 20:53:32 +08:00
|
|
|
if (pidlist[i] == pid)
|
2003-09-17 09:15:56 +08:00
|
|
|
{
|
2019-03-27 20:53:32 +08:00
|
|
|
if (p && (_pinfo *) p != (_pinfo *) myself)
|
2005-12-13 02:43:31 +08:00
|
|
|
p.release ();
|
2003-09-17 09:15:56 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-27 20:53:32 +08:00
|
|
|
/* 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.
|
2005-12-22 11:16:36 +08:00
|
|
|
|
|
|
|
If p is "true" then we've discovered a cygwin process.
|
2005-12-22 13:57:54 +08:00
|
|
|
|
2005-12-22 11:16:36 +08:00
|
|
|
Handle "myself" differently. Don't copy it and close/zero the handle we
|
2019-03-27 20:53:32 +08:00
|
|
|
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 13:57:54 +08:00
|
|
|
|
2019-03-27 20:53:32 +08:00
|
|
|
If malloc failed, then "oh well". Just keep the shared memory around
|
2005-12-22 11:16:36 +08:00
|
|
|
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
|
2019-03-27 20:53:32 +08:00
|
|
|
function returns. */
|
2005-12-22 11:16:36 +08:00
|
|
|
if (p)
|
2005-12-13 02:43:31 +08:00
|
|
|
{
|
2005-12-22 11:16:36 +08: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 11:58:11 +08:00
|
|
|
onreturn.no_close_handle (p); /* Don't close the handle until release */
|
2005-12-22 11:16:36 +08:00
|
|
|
else
|
2005-12-13 02:43:31 +08:00
|
|
|
{
|
2005-12-22 11:16:36 +08:00
|
|
|
_pinfo *pnew = (_pinfo *) malloc (sizeof (*p.procinfo));
|
|
|
|
if (!pnew)
|
2013-12-18 11:58:11 +08:00
|
|
|
onreturn.no_close_handle (p);
|
2005-12-22 11:16:36 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*pnew = *p.procinfo;
|
2011-11-14 09:29:49 +08:00
|
|
|
p.release ();
|
2005-12-22 11:16:36 +08:00
|
|
|
p.procinfo = pnew;
|
|
|
|
p.destroy = false;
|
2014-02-15 12:17:27 +08:00
|
|
|
if (winpid)
|
|
|
|
p->dwProcessId = pid;
|
2005-12-22 11:16:36 +08:00
|
|
|
}
|
2005-12-13 02:43:31 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 20:53:32 +08:00
|
|
|
/* Add pid to the list and bump the number of elements. */
|
2005-12-22 13:26:14 +08:00
|
|
|
if (p || winpid)
|
2019-03-27 20:53:32 +08:00
|
|
|
pidlist[nelem++] = pid;
|
2000-11-05 14:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD
|
2007-02-22 18:54:47 +08:00
|
|
|
winpids::enum_processes (bool winpid)
|
2000-11-02 13:25:56 +08:00
|
|
|
{
|
|
|
|
DWORD nelem = 0;
|
2014-02-15 12:17:27 +08:00
|
|
|
|
|
|
|
if (!winpid)
|
2011-08-11 15:28:42 +08:00
|
|
|
{
|
2021-08-19 22:42:23 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
NTSTATUS status;
|
2014-02-15 12:17:27 +08:00
|
|
|
HANDLE dir = get_shared_parent_dir ();
|
|
|
|
BOOLEAN restart = TRUE;
|
2021-08-19 22:42:23 +08:00
|
|
|
bool last_run = false;
|
|
|
|
ULONG context = 0;
|
|
|
|
PDIRECTORY_BASIC_INFORMATION dbi_buf = (PDIRECTORY_BASIC_INFORMATION)
|
|
|
|
tp.w_get ();
|
|
|
|
while (!last_run)
|
2014-02-15 12:17:27 +08:00
|
|
|
{
|
2021-08-19 22:42:23 +08:00
|
|
|
status = NtQueryDirectoryObject (dir, dbi_buf, 65536, FALSE, restart,
|
|
|
|
&context, NULL);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("NtQueryDirectoryObject, status %y", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status != STATUS_MORE_ENTRIES)
|
|
|
|
last_run = true;
|
2014-02-15 12:17:27 +08:00
|
|
|
restart = FALSE;
|
2021-08-19 22:42:23 +08:00
|
|
|
for (PDIRECTORY_BASIC_INFORMATION dbi = dbi_buf;
|
|
|
|
dbi->ObjectName.Length > 0;
|
|
|
|
dbi++)
|
2014-02-15 12:17:27 +08:00
|
|
|
{
|
2021-08-19 22:42:23 +08:00
|
|
|
if (wcsncmp (dbi->ObjectName.Buffer, L"winpid.", 7) == 0)
|
|
|
|
{
|
|
|
|
DWORD pid = wcstoul (dbi->ObjectName.Buffer + 7, NULL, 10);
|
|
|
|
add (nelem, false, pid);
|
|
|
|
}
|
2019-03-27 20:53:32 +08:00
|
|
|
}
|
2000-11-02 13:25:56 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 12:17:27 +08:00
|
|
|
else
|
2000-11-02 13:25:56 +08:00
|
|
|
{
|
2011-08-11 15:28:42 +08:00
|
|
|
static DWORD szprocs;
|
2013-04-23 17:44:36 +08:00
|
|
|
static PSYSTEM_PROCESS_INFORMATION procs;
|
2011-08-11 15:28:42 +08:00
|
|
|
|
2014-02-15 12:17:27 +08:00
|
|
|
while (1)
|
2011-12-22 19:02:36 +08:00
|
|
|
{
|
2014-02-15 12:17:27 +08:00
|
|
|
PSYSTEM_PROCESS_INFORMATION new_p = (PSYSTEM_PROCESS_INFORMATION)
|
|
|
|
realloc (procs, szprocs += 200 * sizeof (*procs));
|
|
|
|
if (!new_p)
|
2011-12-22 19:02:36 +08:00
|
|
|
{
|
|
|
|
system_printf ("out of memory reading system process "
|
|
|
|
"information");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-15 12:17:27 +08:00
|
|
|
procs = new_p;
|
|
|
|
NTSTATUS status = NtQuerySystemInformation (SystemProcessInformation,
|
|
|
|
procs, szprocs, NULL);
|
2011-12-22 19:02:36 +08:00
|
|
|
if (NT_SUCCESS (status))
|
2011-08-11 15:28:42 +08:00
|
|
|
break;
|
|
|
|
|
2014-02-15 12:17:27 +08:00
|
|
|
if (status != STATUS_INFO_LENGTH_MISMATCH)
|
2011-08-11 15:28:42 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
system_printf ("error %y reading system process information",
|
2011-12-22 19:02:36 +08:00
|
|
|
status);
|
2011-08-11 15:28:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
PSYSTEM_PROCESS_INFORMATION px = procs;
|
2014-02-15 12:17:27 +08:00
|
|
|
while (1)
|
2011-08-11 15:28:42 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
if (px->UniqueProcessId)
|
2014-02-15 12:17:27 +08:00
|
|
|
add (nelem, true, (DWORD) (uintptr_t) px->UniqueProcessId);
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!px->NextEntryOffset)
|
2011-08-11 15:28:42 +08:00
|
|
|
break;
|
2016-03-20 01:45:56 +08:00
|
|
|
px = (PSYSTEM_PROCESS_INFORMATION) ((char *) px + px->NextEntryOffset);
|
2011-08-11 15:28:42 +08:00
|
|
|
}
|
2000-11-02 13:25:56 +08:00
|
|
|
}
|
|
|
|
return nelem;
|
|
|
|
}
|
|
|
|
|
2000-07-30 00:24:59 +08:00
|
|
|
void
|
2002-11-29 15:05:26 +08:00
|
|
|
winpids::set (bool winpid)
|
2000-07-30 00:24:59 +08:00
|
|
|
{
|
2007-02-22 18:54:47 +08:00
|
|
|
npids = enum_processes (winpid);
|
2001-10-14 01:23:35 +08:00
|
|
|
if (pidlist)
|
|
|
|
pidlist[npids] = 0;
|
2000-07-30 00:24:59 +08:00
|
|
|
}
|
|
|
|
|
2000-11-05 14:42:23 +08:00
|
|
|
DWORD
|
|
|
|
winpids::enum_init (bool winpid)
|
2000-07-30 00:24:59 +08:00
|
|
|
{
|
2007-02-22 18:54:47 +08:00
|
|
|
return enum_processes (winpid);
|
2000-11-05 14:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winpids::release ()
|
|
|
|
{
|
2005-12-13 02:43:31 +08:00
|
|
|
_pinfo *p;
|
2000-11-05 14:42:23 +08:00
|
|
|
for (unsigned i = 0; i < npids; i++)
|
2005-12-22 11:16:36 +08:00
|
|
|
if (pinfolist[i] == (_pinfo *) myself)
|
|
|
|
continue;
|
2005-12-22 13:11:44 +08:00
|
|
|
else if (pinfolist[i].hProcess)
|
2011-11-14 09:29:49 +08:00
|
|
|
pinfolist[i].release ();
|
2005-12-22 11:16:36 +08:00
|
|
|
else if ((p = pinfolist[i]))
|
|
|
|
{
|
|
|
|
pinfolist[i].procinfo = NULL;
|
|
|
|
free (p);
|
|
|
|
}
|
2000-11-05 14:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
winpids::~winpids ()
|
|
|
|
{
|
|
|
|
if (npidlist)
|
|
|
|
{
|
|
|
|
release ();
|
|
|
|
free (pidlist);
|
|
|
|
free (pinfolist);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|