2000-02-18 03:38:33 +08:00
|
|
|
/* shared.cc: shared data area 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 <unistd.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.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"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "dtable.h"
|
2000-11-15 08:13:09 +08:00
|
|
|
#include "cygheap.h"
|
2001-01-29 08:46:25 +08:00
|
|
|
#include "heap.h"
|
2001-12-26 12:53:34 +08:00
|
|
|
#include "shared_info_magic.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "registry.h"
|
|
|
|
#include "cygwin_version.h"
|
2022-10-27 03:22:19 +08:00
|
|
|
#include "memory_layout.h"
|
2010-03-14 03:34:35 +08:00
|
|
|
#include "spinlock.h"
|
2008-04-19 04:13:37 +08:00
|
|
|
#include <alloca.h>
|
2008-07-25 02:25:52 +08:00
|
|
|
#include <wchar.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
shared_info NO_COPY *cygwin_shared;
|
2003-09-25 10:29:05 +08:00
|
|
|
user_info NO_COPY *user_shared;
|
2008-04-19 04:13:37 +08:00
|
|
|
HANDLE NO_COPY cygwin_shared_h;
|
2003-09-25 10:29:05 +08:00
|
|
|
HANDLE NO_COPY cygwin_user_h;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-04-19 04:13:37 +08:00
|
|
|
/* This function returns a handle to the top-level directory in the global
|
|
|
|
NT namespace used to implement global objects including shared memory. */
|
|
|
|
|
2011-11-19 01:38:04 +08:00
|
|
|
static HANDLE NO_COPY shared_parent_dir;
|
|
|
|
|
2008-04-19 04:13:37 +08:00
|
|
|
HANDLE
|
|
|
|
get_shared_parent_dir ()
|
|
|
|
{
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
2008-04-21 20:46:58 +08:00
|
|
|
|
2011-11-19 01:38:04 +08:00
|
|
|
if (!shared_parent_dir)
|
2008-04-19 04:13:37 +08:00
|
|
|
{
|
2008-04-21 20:46:58 +08:00
|
|
|
WCHAR bnoname[MAX_PATH];
|
2009-10-31 21:24:06 +08:00
|
|
|
__small_swprintf (bnoname, L"\\BaseNamedObjects\\%s%s-%S",
|
2008-04-21 20:46:58 +08:00
|
|
|
cygwin_version.shared_id,
|
2009-10-31 21:24:06 +08:00
|
|
|
_cygwin_testing ? cygwin_version.dll_build_date : "",
|
2012-02-14 19:27:43 +08:00
|
|
|
&cygheap->installation_key);
|
2008-04-21 20:46:58 +08:00
|
|
|
RtlInitUnicodeString (&uname, bnoname);
|
2011-11-19 01:38:04 +08:00
|
|
|
InitializeObjectAttributes (&attr, &uname, OBJ_OPENIF, NULL,
|
|
|
|
everyone_sd (CYG_SHARED_DIR_ACCESS));
|
|
|
|
status = NtCreateDirectoryObject (&shared_parent_dir,
|
|
|
|
CYG_SHARED_DIR_ACCESS, &attr);
|
2008-04-19 04:13:37 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("NtCreateDirectoryObject(%S): %y", &uname, status);
|
2008-04-21 20:46:58 +08:00
|
|
|
}
|
2011-11-19 01:38:04 +08:00
|
|
|
return shared_parent_dir;
|
2008-11-27 01:21:04 +08:00
|
|
|
}
|
2008-04-21 20:46:58 +08:00
|
|
|
|
2011-11-19 01:38:04 +08:00
|
|
|
static HANDLE NO_COPY session_parent_dir;
|
|
|
|
|
2008-04-21 20:46:58 +08:00
|
|
|
HANDLE
|
|
|
|
get_session_parent_dir ()
|
|
|
|
{
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2011-11-19 01:38:04 +08:00
|
|
|
if (!session_parent_dir)
|
2008-04-21 20:46:58 +08:00
|
|
|
{
|
|
|
|
PROCESS_SESSION_INFORMATION psi;
|
2009-12-19 04:32:04 +08:00
|
|
|
status = NtQueryInformationProcess (NtCurrentProcess (),
|
2008-04-21 20:46:58 +08:00
|
|
|
ProcessSessionInformation,
|
|
|
|
&psi, sizeof psi, NULL);
|
|
|
|
if (!NT_SUCCESS (status) || psi.SessionId == 0)
|
2011-11-19 01:38:04 +08:00
|
|
|
session_parent_dir = get_shared_parent_dir ();
|
2008-04-21 20:46:58 +08:00
|
|
|
else
|
2008-11-27 01:21:04 +08:00
|
|
|
{
|
2008-04-21 20:46:58 +08:00
|
|
|
WCHAR bnoname[MAX_PATH];
|
|
|
|
__small_swprintf (bnoname,
|
2009-10-31 21:24:06 +08:00
|
|
|
L"\\Sessions\\BNOLINKS\\%d\\%s%s-%S",
|
2008-04-21 20:46:58 +08:00
|
|
|
psi.SessionId, cygwin_version.shared_id,
|
2009-10-31 21:24:06 +08:00
|
|
|
_cygwin_testing ? cygwin_version.dll_build_date : "",
|
2012-02-14 19:27:43 +08:00
|
|
|
&cygheap->installation_key);
|
2008-04-21 20:46:58 +08:00
|
|
|
RtlInitUnicodeString (&uname, bnoname);
|
2011-11-19 01:38:04 +08:00
|
|
|
InitializeObjectAttributes (&attr, &uname, OBJ_OPENIF, NULL,
|
|
|
|
everyone_sd(CYG_SHARED_DIR_ACCESS));
|
|
|
|
status = NtCreateDirectoryObject (&session_parent_dir,
|
|
|
|
CYG_SHARED_DIR_ACCESS, &attr);
|
2008-04-21 20:46:58 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("NtCreateDirectoryObject(%S): %y", &uname, status);
|
2008-04-21 20:46:58 +08:00
|
|
|
}
|
2008-04-19 04:13:37 +08:00
|
|
|
}
|
2011-11-19 01:38:04 +08:00
|
|
|
return session_parent_dir;
|
2008-11-27 01:21:04 +08:00
|
|
|
}
|
2008-04-19 04:13:37 +08:00
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
char *
|
2008-04-19 04:13:37 +08:00
|
|
|
shared_name (char *ret_buf, const char *str, int num)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2008-04-21 20:46:58 +08:00
|
|
|
__small_sprintf (ret_buf, "%s.%d", str, num);
|
2003-05-21 16:01:57 +08:00
|
|
|
return ret_buf;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
WCHAR *
|
2009-01-26 21:42:37 +08:00
|
|
|
shared_name (WCHAR *ret_buf, const WCHAR *str, int num)
|
|
|
|
{
|
|
|
|
__small_swprintf (ret_buf, L"%W.%d", str, num);
|
|
|
|
return ret_buf;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#define page_const ((ptrdiff_t) 65535)
|
|
|
|
#define pround(n) ((ptrdiff_t)(((n) + page_const) & ~page_const))
|
2002-10-15 15:03:45 +08:00
|
|
|
|
2022-10-27 03:22:19 +08:00
|
|
|
/* FIXME: With ASLR, maybe we should ASLR the shared regions, too? */
|
|
|
|
static uintptr_t region_address[] =
|
2002-10-23 00:18:55 +08:00
|
|
|
{
|
2022-10-27 03:22:19 +08:00
|
|
|
CYGWIN_REGION_ADDRESS, /* SH_CYGWIN_SHARED */
|
|
|
|
USER_REGION_ADDRESS, /* SH_USER_SHARED */
|
|
|
|
PINFO_REGION_ADDRESS, /* SH_MYSELF */
|
|
|
|
SHARED_CONSOLE_REGION_ADDRESS, /* SH_SHARED_CONSOLE */
|
2006-07-27 05:02:01 +08:00
|
|
|
0
|
2002-10-23 00:18:55 +08:00
|
|
|
};
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
void *
|
2009-01-26 21:42:37 +08:00
|
|
|
open_shared (const WCHAR *name, int n, HANDLE& shared_h, DWORD size,
|
2010-03-10 05:26:55 +08:00
|
|
|
shared_locations m, PSECURITY_ATTRIBUTES psa, DWORD access)
|
|
|
|
{
|
|
|
|
return open_shared (name, n, shared_h, size, &m, psa, access);
|
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
void *
|
2010-03-10 05:26:55 +08:00
|
|
|
open_shared (const WCHAR *name, int n, HANDLE& shared_h, DWORD size,
|
|
|
|
shared_locations *m, PSECURITY_ATTRIBUTES psa, DWORD access)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
void *shared;
|
2002-10-15 15:03:45 +08:00
|
|
|
|
2002-10-16 01:04:20 +08:00
|
|
|
void *addr;
|
2010-03-10 05:26:55 +08:00
|
|
|
if (*m == SH_JUSTCREATE || *m == SH_JUSTOPEN)
|
2002-10-16 01:04:20 +08:00
|
|
|
addr = NULL;
|
|
|
|
else
|
2022-10-27 03:22:19 +08:00
|
|
|
addr = (void *) region_address[*m];
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-01-26 21:42:37 +08:00
|
|
|
WCHAR map_buf[MAX_PATH];
|
|
|
|
WCHAR *mapname = NULL;
|
2005-09-28 03:04:38 +08:00
|
|
|
|
2005-04-29 07:59:44 +08:00
|
|
|
if (shared_h)
|
2010-03-10 05:26:55 +08:00
|
|
|
*m = SH_JUSTOPEN;
|
2005-04-29 07:59:44 +08:00
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-09-28 03:04:38 +08:00
|
|
|
if (name)
|
2004-05-29 03:50:07 +08:00
|
|
|
mapname = shared_name (map_buf, name, n);
|
2010-03-10 05:26:55 +08:00
|
|
|
if (*m == SH_JUSTOPEN)
|
2009-01-26 21:42:37 +08:00
|
|
|
shared_h = OpenFileMappingW (access, FALSE, mapname);
|
2005-04-29 07:59:44 +08:00
|
|
|
else
|
|
|
|
{
|
2009-01-26 21:42:37 +08:00
|
|
|
shared_h = CreateFileMappingW (INVALID_HANDLE_VALUE, psa,
|
2008-11-27 01:21:04 +08:00
|
|
|
PAGE_READWRITE, 0, size, mapname);
|
2008-04-19 04:13:37 +08:00
|
|
|
if (GetLastError () == ERROR_ALREADY_EXISTS)
|
2010-03-10 05:26:55 +08:00
|
|
|
*m = SH_JUSTOPEN;
|
2005-04-29 07:59:44 +08:00
|
|
|
}
|
|
|
|
if (shared_h)
|
|
|
|
/* ok! */;
|
2010-03-10 05:26:55 +08:00
|
|
|
else if (*m != SH_JUSTOPEN)
|
2009-01-26 21:42:37 +08:00
|
|
|
api_fatal ("CreateFileMapping %W, %E. Terminating.", mapname);
|
2005-04-29 07:59:44 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-27 14:11:05 +08:00
|
|
|
shared = (shared_info *) MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (!shared)
|
2009-01-26 21:42:37 +08:00
|
|
|
api_fatal ("MapViewOfFileEx '%W'(%p), %E. Terminating.", mapname, shared_h);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-10-24 17:19:58 +08:00
|
|
|
debug_printf ("name %W, n %d, shared %p (wanted %p), h %p, *m %d",
|
|
|
|
mapname, n, shared, addr, shared_h, *m);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return shared;
|
|
|
|
}
|
|
|
|
|
2008-07-28 06:31:48 +08:00
|
|
|
/* Second half of user shared initialization: Initialize content. */
|
2008-07-25 23:23:56 +08:00
|
|
|
void
|
2010-03-16 05:29:15 +08:00
|
|
|
user_info::initialize ()
|
2008-07-25 23:23:56 +08:00
|
|
|
{
|
2008-07-28 06:31:48 +08:00
|
|
|
/* Wait for initialization of the Cygwin per-user shared, if necessary */
|
2010-03-16 05:29:15 +08:00
|
|
|
spinlock sversion (version, CURR_USER_MAGIC);
|
2008-07-28 06:31:48 +08:00
|
|
|
if (!sversion)
|
2008-07-25 23:23:56 +08:00
|
|
|
{
|
2014-02-07 04:38:35 +08:00
|
|
|
cb = sizeof (*user_shared);
|
2014-11-28 00:49:41 +08:00
|
|
|
/* Initialize mount table from system fstab prior to calling
|
|
|
|
internal_getpwsid. This allows to convert pw_dir and pw_shell
|
|
|
|
paths given in DOS notation to valid POSIX paths. */
|
|
|
|
mountinfo.init (false);
|
2008-07-25 23:23:56 +08:00
|
|
|
cygpsid sid (cygheap->user.sid ());
|
|
|
|
struct passwd *pw = internal_getpwsid (sid);
|
|
|
|
/* Correct the user name with what's defined in /etc/passwd before
|
|
|
|
loading the user fstab file. */
|
|
|
|
if (pw)
|
2008-11-27 01:21:04 +08:00
|
|
|
cygheap->user.set_name (pw->pw_name);
|
2014-11-28 00:49:41 +08:00
|
|
|
/* After fetching the user infos, add mount entries from user's fstab. */
|
|
|
|
mountinfo.init (true);
|
2008-07-28 06:31:48 +08:00
|
|
|
}
|
2010-03-16 05:29:15 +08:00
|
|
|
else if (sversion != CURR_USER_MAGIC)
|
|
|
|
sversion.multiple_cygwin_problem ("user shared memory version", version,
|
|
|
|
sversion);
|
|
|
|
else if (user_shared->cb != sizeof (*user_shared))
|
|
|
|
sversion.multiple_cygwin_problem ("user shared memory size", cb,
|
|
|
|
sizeof (*user_shared));
|
2008-07-25 23:23:56 +08:00
|
|
|
}
|
|
|
|
|
2008-07-28 06:31:48 +08:00
|
|
|
/* First half of user shared initialization: Create shared mem region. */
|
2003-09-11 05:01:40 +08:00
|
|
|
void
|
2010-03-16 05:29:15 +08:00
|
|
|
user_info::create (bool reinit)
|
2003-09-10 10:12:26 +08:00
|
|
|
{
|
2009-01-26 21:42:37 +08:00
|
|
|
WCHAR name[UNLEN + 1] = L""; /* Large enough for SID */
|
2003-09-11 05:01:40 +08:00
|
|
|
|
2003-09-25 10:29:05 +08:00
|
|
|
if (reinit)
|
2003-09-10 10:12:26 +08:00
|
|
|
{
|
2003-09-25 10:29:05 +08:00
|
|
|
if (!UnmapViewOfFile (user_shared))
|
|
|
|
debug_printf("UnmapViewOfFile %E");
|
|
|
|
if (!ForceCloseHandle (cygwin_user_h))
|
|
|
|
debug_printf("CloseHandle %E");
|
|
|
|
cygwin_user_h = NULL;
|
2003-09-10 10:12:26 +08:00
|
|
|
}
|
|
|
|
|
2003-09-25 10:29:05 +08:00
|
|
|
if (!cygwin_user_h)
|
2004-11-21 03:09:19 +08:00
|
|
|
cygheap->user.get_windows_id (name);
|
2003-09-11 05:01:40 +08:00
|
|
|
|
2003-09-25 10:29:05 +08:00
|
|
|
user_shared = (user_info *) open_shared (name, USER_VERSION,
|
2010-03-10 05:26:55 +08:00
|
|
|
cygwin_user_h, sizeof (user_info),
|
|
|
|
SH_USER_SHARED, &sec_none);
|
2009-01-26 21:42:37 +08:00
|
|
|
debug_printf ("opening user shared for '%W' at %p", name, user_shared);
|
2003-09-25 10:29:05 +08:00
|
|
|
ProtectHandleINH (cygwin_user_h);
|
|
|
|
debug_printf ("user shared version %x", user_shared->version);
|
2008-07-28 06:31:48 +08:00
|
|
|
if (reinit)
|
2010-03-16 05:29:15 +08:00
|
|
|
user_shared->initialize ();
|
2003-09-10 10:12:26 +08:00
|
|
|
}
|
|
|
|
|
2022-06-07 00:00:45 +08:00
|
|
|
void
|
2008-07-27 22:52:46 +08:00
|
|
|
shared_destroy ()
|
|
|
|
{
|
|
|
|
ForceCloseHandle (cygwin_shared_h);
|
|
|
|
UnmapViewOfFile (cygwin_shared);
|
|
|
|
ForceCloseHandle (cygwin_user_h);
|
|
|
|
UnmapViewOfFile (user_shared);
|
|
|
|
}
|
|
|
|
|
2012-02-22 01:03:51 +08:00
|
|
|
/* Initialize obcaseinsensitive.*/
|
2008-07-17 04:20:45 +08:00
|
|
|
void
|
|
|
|
shared_info::init_obcaseinsensitive ()
|
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Instead of reading the obcaseinsensitive registry value, test the
|
|
|
|
actual state of case sensitivity handling in the kernel. */
|
|
|
|
UNICODE_STRING sysroot;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
HANDLE h;
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&sysroot, L"\\SYSTEMROOT");
|
|
|
|
InitializeObjectAttributes (&attr, &sysroot, 0, NULL, NULL);
|
|
|
|
/* NtOpenSymbolicLinkObject returns STATUS_ACCESS_DENIED when called
|
|
|
|
with a 0 access mask. However, if the kernel is case sensitive,
|
|
|
|
it returns STATUS_OBJECT_NAME_NOT_FOUND because we used the incorrect
|
|
|
|
case for the filename (It's actually "\\SystemRoot"). */
|
|
|
|
obcaseinsensitive = NtOpenSymbolicLinkObject (&h, 0, &attr)
|
|
|
|
!= STATUS_OBJECT_NAME_NOT_FOUND;
|
2008-07-17 04:20:45 +08:00
|
|
|
}
|
|
|
|
|
2010-03-16 05:29:15 +08:00
|
|
|
void inline
|
|
|
|
shared_info::create ()
|
|
|
|
{
|
|
|
|
cygwin_shared = (shared_info *) open_shared (L"shared",
|
|
|
|
CYGWIN_VERSION_SHARED_DATA,
|
|
|
|
cygwin_shared_h,
|
|
|
|
sizeof (*cygwin_shared),
|
2011-11-18 06:08:14 +08:00
|
|
|
SH_CYGWIN_SHARED,
|
|
|
|
&sec_all_nih);
|
2010-03-16 05:29:15 +08:00
|
|
|
cygwin_shared->initialize ();
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2003-09-11 05:01:40 +08:00
|
|
|
shared_info::initialize ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2010-03-16 05:29:15 +08:00
|
|
|
spinlock sversion (version, CURR_SHARED_MAGIC);
|
2010-03-10 00:07:15 +08:00
|
|
|
if (!sversion)
|
|
|
|
{
|
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
|
|
|
LUID luid;
|
|
|
|
|
2010-03-10 05:26:55 +08:00
|
|
|
cb = sizeof (*this);
|
|
|
|
get_session_parent_dir (); /* Create session dir if first process. */
|
|
|
|
init_obcaseinsensitive (); /* Initialize obcaseinsensitive */
|
|
|
|
tty.init (); /* Initialize tty table */
|
|
|
|
mt.initialize (); /* Initialize shared tape information */
|
2017-03-22 03:17:42 +08:00
|
|
|
loadavg.initialize (); /* Initialize loadavg information */
|
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
|
|
|
NtAllocateLocallyUniqueId (&luid);/* Initialize pid_src to a low */
|
|
|
|
InterlockedExchange (&pid_src, /* random value to make start pid */
|
|
|
|
luid.LowPart % 2048);/* less predictably */
|
2017-03-01 17:19:37 +08:00
|
|
|
forkable_hardlink_support = 0; /* 0: Unknown, 1: Yes, -1: No */
|
2010-03-16 05:29:15 +08:00
|
|
|
/* Defer debug output printing the installation root and installation key
|
|
|
|
up to this point. Debug output except for system_printf requires
|
|
|
|
the global shared memory to exist. */
|
|
|
|
debug_printf ("Installation root: <%W> key: <%S>",
|
2012-02-14 19:27:43 +08:00
|
|
|
cygheap->installation_root, &cygheap->installation_key);
|
2010-03-10 00:07:15 +08:00
|
|
|
}
|
2013-05-23 00:55:55 +08:00
|
|
|
else if (sversion != CURR_SHARED_MAGIC)
|
2010-03-16 05:29:15 +08:00
|
|
|
sversion.multiple_cygwin_problem ("system shared memory version",
|
|
|
|
sversion, CURR_SHARED_MAGIC);
|
|
|
|
else if (cb != sizeof (*this))
|
2013-04-23 17:44:36 +08:00
|
|
|
system_printf ("size of shared memory region changed from %lu to %u",
|
2010-03-16 05:29:15 +08:00
|
|
|
sizeof (*this), cb);
|
2013-08-31 05:02:02 +08:00
|
|
|
/* FIXME? Shouldn't this be in memory_init? */
|
|
|
|
cygheap->user_heap.init ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
void
|
2014-10-17 22:36:59 +08:00
|
|
|
memory_init ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2010-03-16 05:29:15 +08:00
|
|
|
shared_info::create (); /* Initialize global shared memory */
|
|
|
|
user_info::create (false); /* Initialize per-user shared memory */
|
2012-07-30 03:18:05 +08:00
|
|
|
/* Initialize tty list session stuff. Doesn't really belong here but
|
|
|
|
this needs to be initialized before any tty or console manipulation
|
|
|
|
happens and it is a common location. */
|
|
|
|
tty_list::init_session ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|