4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-03 13:00:39 +08:00
Takashi Yano 10477d95ec Cygwin: console: Fix race issue on allocating console simultaneously.
Previously, if two or more processes request to allocate a console at
the same time, the same device number could be accidentally allocated.
This patch fixes the issue by creating a named mutex (input_mutex) and
checking if GetLastError() is ERROR_ALREADY_EXIST or not to ensure
the unit number will be allocated in an atomic way. Thanks to this,
EnumWindow() is not necessary anymore to scan console units.

This also makes minor device numbers unique and console devices visible
across sessions. This disallows duplicated device number for different
sessions, so the MAX_CONS_DEV has been increased from 64 to 128.

Additionally, the console allocation and scanning will be faster as
a side effect of eliminating EnumWindows().

Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from other terminals.")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-07-09 00:01:22 +09:00

99 lines
2.3 KiB
C++

/* shared_info.h: shared info for cygwin
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "tty.h"
#include "security.h"
#include "mtinfo.h"
#include "limits.h"
#include "mount.h"
#include "loadavg.h"
#define CURR_USER_MAGIC 0xab1fcce8U
class user_info
{
void initialize ();
public:
LONG version;
DWORD cb;
bool warned_msdos;
bool warned_notty;
bool warned_nonativesyms;
mount_info mountinfo;
friend void dll_crt0_1 (void *);
static void create (bool);
};
/******** Shared Info ********/
/* Data accessible to all tasks */
#define CURR_SHARED_MAGIC 0x205f4579U
#define USER_VERSION 1
/* NOTE: Do not make gratuitous changes to the names or organization of the
below class. The layout is checksummed to determine compatibility between
different cygwin versions. */
class shared_info
{
LONG version;
DWORD cb;
public:
tty_list tty;
HWND cons_hwnd[MAX_CONS_DEV];
LONG last_used_bindresvport;
DWORD obcaseinsensitive;
mtinfo mt;
loadavginfo loadavg;
LONG pid_src;
LONG forkable_hardlink_support;
void initialize ();
void init_obcaseinsensitive ();
unsigned heap_chunk_size ();
static void create ();
};
extern shared_info *cygwin_shared;
extern user_info *user_shared;
#define mount_table (&(user_shared->mountinfo))
extern HANDLE cygwin_user_h;
enum shared_locations
{
SH_CYGWIN_SHARED,
SH_USER_SHARED,
SH_MYSELF,
SH_SHARED_CONSOLE,
SH_TOTAL_SIZE,
SH_JUSTCREATE,
SH_JUSTOPEN
};
void memory_init ();
void shared_destroy ();
#define shared_align_past(p) \
((char *) (system_info.dwAllocationGranularity * \
(((DWORD) ((p) + 1) + system_info.dwAllocationGranularity - 1) / \
system_info.dwAllocationGranularity)))
HANDLE get_shared_parent_dir ();
HANDLE get_session_parent_dir ();
char *shared_name (char *, const char *, int);
WCHAR *shared_name (WCHAR *, const WCHAR *, int);
void *open_shared (const WCHAR *, int, HANDLE&, DWORD,
shared_locations, PSECURITY_ATTRIBUTES = &sec_all,
DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
void *open_shared (const WCHAR *, int, HANDLE&, DWORD,
shared_locations, bool &, PSECURITY_ATTRIBUTES = &sec_all,
DWORD = FILE_MAP_READ | FILE_MAP_WRITE);
extern void user_shared_create (bool reinit);