2000-09-03 06:16:35 +02:00
|
|
|
/* cygheap.h: Cygwin heap manager.
|
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
|
|
|
2011, 2012, 2013 Red Hat, Inc.
|
2000-09-03 06:16:35 +02:00
|
|
|
|
|
|
|
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. */
|
|
|
|
|
2005-11-04 21:45:56 +01:00
|
|
|
#include "hires.h"
|
2009-08-01 21:52:46 +02:00
|
|
|
#include "cygheap_malloc.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-02 04:26:04 +02:00
|
|
|
#define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap_max)))
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-11-14 06:53:32 +01:00
|
|
|
struct _cmalloc_entry
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
unsigned b;
|
2000-11-14 06:53:32 +01:00
|
|
|
char *ptr;
|
|
|
|
};
|
|
|
|
struct _cmalloc_entry *prev;
|
|
|
|
char data[0];
|
|
|
|
};
|
|
|
|
|
2001-06-03 04:31:16 +02:00
|
|
|
struct cygheap_root_mount_info
|
|
|
|
{
|
2003-11-15 00:40:06 +01:00
|
|
|
char posix_path[CYG_MAX_PATH];
|
2001-06-03 04:31:16 +02:00
|
|
|
unsigned posix_pathlen;
|
2003-11-15 00:40:06 +01:00
|
|
|
char native_path[CYG_MAX_PATH];
|
2001-06-03 04:31:16 +02:00
|
|
|
unsigned native_pathlen;
|
2008-07-16 22:20:45 +02:00
|
|
|
bool caseinsensitive;
|
2001-06-03 04:31:16 +02:00
|
|
|
};
|
|
|
|
|
2001-06-04 03:28:09 +02:00
|
|
|
/* CGF: FIXME This doesn't belong here */
|
|
|
|
|
2000-11-15 01:13:09 +01:00
|
|
|
class cygheap_root
|
|
|
|
{
|
|
|
|
/* Root directory information.
|
|
|
|
This is used after a chroot is called. */
|
2001-06-03 04:31:16 +02:00
|
|
|
struct cygheap_root_mount_info *m;
|
|
|
|
|
2000-11-15 01:13:09 +01:00
|
|
|
public:
|
2001-06-03 04:31:16 +02:00
|
|
|
bool posix_ok (const char *path)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return 1;
|
2008-07-16 22:20:45 +02:00
|
|
|
return path_prefix_p (m->posix_path, path, m->posix_pathlen,
|
|
|
|
m->caseinsensitive);
|
2001-06-03 04:31:16 +02:00
|
|
|
}
|
|
|
|
bool ischroot_native (const char *path)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return 1;
|
|
|
|
return strncasematch (m->native_path, path, m->native_pathlen)
|
2001-09-07 23:32:07 +02:00
|
|
|
&& (path[m->native_pathlen] == '\\' || !path[m->native_pathlen]);
|
2001-06-03 04:31:16 +02:00
|
|
|
}
|
|
|
|
const char *unchroot (const char *path)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return path;
|
|
|
|
const char *p = path + m->posix_pathlen;
|
|
|
|
if (!*p)
|
|
|
|
p = "/";
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
bool exists () {return !!m;}
|
2008-07-16 22:20:45 +02:00
|
|
|
void set (const char *, const char *, bool);
|
2001-06-03 04:31:16 +02:00
|
|
|
size_t posix_length () const { return m->posix_pathlen; }
|
|
|
|
const char *posix_path () const { return m->posix_path; }
|
|
|
|
size_t native_length () const { return m->native_pathlen; }
|
|
|
|
const char *native_path () const { return m->native_path; }
|
2000-11-15 01:13:09 +01:00
|
|
|
};
|
|
|
|
|
2002-06-12 07:13:54 +02:00
|
|
|
enum homebodies
|
|
|
|
{
|
|
|
|
CH_HOMEDRIVE,
|
|
|
|
CH_HOMEPATH,
|
|
|
|
CH_HOME
|
|
|
|
};
|
|
|
|
|
2001-01-17 15:57:09 +01:00
|
|
|
class cygheap_user
|
|
|
|
{
|
2000-11-15 01:13:09 +01:00
|
|
|
/* Extendend user information.
|
|
|
|
The information is derived from the internal_getlogin call
|
|
|
|
when on a NT system. */
|
2009-06-30 23:18:44 +02:00
|
|
|
char *pname; /* user's name */
|
2000-11-15 22:04:02 +01:00
|
|
|
char *plogsrv; /* Logon server, may be FQDN */
|
|
|
|
char *pdomain; /* Logon domain of the user */
|
2002-06-12 07:13:54 +02:00
|
|
|
char *homedrive; /* User's home drive */
|
|
|
|
char *homepath; /* User's home path */
|
2004-10-07 23:28:57 +02:00
|
|
|
char *psystemroot; /* Value of SYSTEMROOT */
|
2002-06-29 04:36:08 +02:00
|
|
|
char *pwinname; /* User's name as far as Windows knows it */
|
2002-06-27 22:44:27 +02:00
|
|
|
char *puserprof; /* User profile */
|
2003-09-27 03:56:36 +02:00
|
|
|
cygsid effec_cygsid; /* buffer for user's SID */
|
|
|
|
cygsid saved_cygsid; /* Remains intact even after impersonation */
|
2000-11-15 01:13:09 +01:00
|
|
|
public:
|
2013-04-23 11:44:36 +02:00
|
|
|
uid_t saved_uid; /* Remains intact even after impersonation */
|
|
|
|
gid_t saved_gid; /* Ditto */
|
|
|
|
uid_t real_uid; /* Remains intact on seteuid, replaced by setuid */
|
|
|
|
gid_t real_gid; /* Ditto */
|
|
|
|
user_groups groups; /* Primary and supp SIDs */
|
2000-11-15 01:13:09 +01:00
|
|
|
|
2000-11-15 22:04:02 +01:00
|
|
|
/* token is needed if set(e)uid should be called. It can be set by a call
|
|
|
|
to `set_impersonation_token()'. */
|
2003-06-30 15:07:36 +02:00
|
|
|
HANDLE external_token;
|
|
|
|
HANDLE internal_token;
|
2005-04-03 15:06:43 +02:00
|
|
|
HANDLE curr_primary_token;
|
2007-07-16 22:01:15 +02:00
|
|
|
HANDLE curr_imp_token;
|
2009-10-13 12:23:31 +02:00
|
|
|
bool ext_token_is_restricted; /* external_token is restricted token */
|
|
|
|
bool curr_token_is_restricted; /* curr_primary_token is restricted token */
|
|
|
|
bool setuid_to_restricted; /* switch to restricted token by setuid () */
|
2000-11-15 22:04:02 +01:00
|
|
|
|
2002-06-27 22:44:27 +02:00
|
|
|
/* CGF 2002-06-27. I removed the initializaton from this constructor
|
|
|
|
since this class is always allocated statically. That means that everything
|
|
|
|
is zero anyway so there is no need to initialize it to zero. Since the
|
|
|
|
token initialization is always handled during process startup as well,
|
2003-12-07 23:37:12 +01:00
|
|
|
I've removed the constructor entirely. Please reinstate this if this
|
2002-06-27 22:44:27 +02:00
|
|
|
situation ever changes.
|
2001-06-04 16:29:54 +02:00
|
|
|
cygheap_user () : pname (NULL), plogsrv (NULL), pdomain (NULL),
|
2003-09-16 11:24:52 +02:00
|
|
|
homedrive (NULL), homepath (NULL),
|
2002-06-27 22:44:27 +02:00
|
|
|
token (INVALID_HANDLE_VALUE) {}
|
|
|
|
*/
|
|
|
|
|
2000-11-15 01:13:09 +01:00
|
|
|
~cygheap_user ();
|
|
|
|
|
2003-09-10 23:01:40 +02:00
|
|
|
void init ();
|
2000-11-15 01:13:09 +01:00
|
|
|
void set_name (const char *new_name);
|
|
|
|
const char *name () const { return pname; }
|
|
|
|
|
2002-06-29 04:36:08 +02:00
|
|
|
const char *env_logsrv (const char *, size_t);
|
|
|
|
const char *env_homepath (const char *, size_t);
|
|
|
|
const char *env_homedrive (const char *, size_t);
|
|
|
|
const char *env_userprofile (const char *, size_t);
|
|
|
|
const char *env_domain (const char *, size_t);
|
|
|
|
const char *env_name (const char *, size_t);
|
2004-10-07 23:28:57 +02:00
|
|
|
const char *env_systemroot (const char *, size_t);
|
2000-11-15 01:13:09 +01:00
|
|
|
|
2002-06-29 04:36:08 +02:00
|
|
|
const char *logsrv ()
|
|
|
|
{
|
2002-06-30 00:05:30 +02:00
|
|
|
const char *p = env_logsrv ("LOGONSERVER=", sizeof ("LOGONSERVER=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 04:36:08 +02:00
|
|
|
}
|
|
|
|
const char *winname ()
|
|
|
|
{
|
2002-06-30 00:05:30 +02:00
|
|
|
const char *p = env_name ("USERNAME=", sizeof ("USERNAME=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 04:36:08 +02:00
|
|
|
}
|
|
|
|
const char *domain ()
|
|
|
|
{
|
2002-06-30 00:05:30 +02:00
|
|
|
const char *p = env_domain ("USERDOMAIN=", sizeof ("USERDOMAIN=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 04:36:08 +02:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
void set_sid (PSID new_sid) { effec_cygsid = new_sid;}
|
|
|
|
void set_saved_sid () { saved_cygsid = effec_cygsid; }
|
2003-09-27 03:56:36 +02:00
|
|
|
PSID sid () { return effec_cygsid; }
|
|
|
|
PSID saved_sid () { return saved_cygsid; }
|
2002-06-12 07:13:54 +02:00
|
|
|
const char *ontherange (homebodies what, struct passwd * = NULL);
|
2004-10-28 03:46:01 +02:00
|
|
|
#define NO_IMPERSONATION NULL
|
2007-07-16 22:01:15 +02:00
|
|
|
bool issetuid () const { return curr_imp_token != NO_IMPERSONATION; }
|
2005-04-03 15:06:43 +02:00
|
|
|
HANDLE primary_token () { return curr_primary_token; }
|
2007-07-16 22:01:15 +02:00
|
|
|
HANDLE imp_token () { return curr_imp_token; }
|
2003-06-30 15:07:36 +02:00
|
|
|
void deimpersonate ()
|
|
|
|
{
|
2007-01-24 11:50:47 +01:00
|
|
|
RevertToSelf ();
|
2003-06-30 15:07:36 +02:00
|
|
|
}
|
2005-04-03 15:06:43 +02:00
|
|
|
bool reimpersonate ()
|
2003-06-30 15:07:36 +02:00
|
|
|
{
|
2006-12-12 17:27:32 +01:00
|
|
|
if (issetuid ())
|
2007-07-16 22:01:15 +02:00
|
|
|
return ImpersonateLoggedOnUser (primary_token ());
|
2006-12-12 17:27:32 +01:00
|
|
|
return true;
|
2003-06-30 15:07:36 +02:00
|
|
|
}
|
2003-07-14 19:04:21 +02:00
|
|
|
bool has_impersonation_tokens ()
|
2004-10-28 03:46:01 +02:00
|
|
|
{ return external_token != NO_IMPERSONATION
|
|
|
|
|| internal_token != NO_IMPERSONATION
|
2005-04-03 15:06:43 +02:00
|
|
|
|| curr_primary_token != NO_IMPERSONATION; }
|
2003-06-30 15:07:36 +02:00
|
|
|
void close_impersonation_tokens ()
|
|
|
|
{
|
2007-07-16 22:01:15 +02:00
|
|
|
if (curr_imp_token != NO_IMPERSONATION)
|
|
|
|
CloseHandle (curr_imp_token);
|
2005-04-03 15:06:43 +02:00
|
|
|
if (curr_primary_token != NO_IMPERSONATION
|
2011-06-06 07:02:13 +02:00
|
|
|
&& curr_primary_token != external_token
|
2005-04-03 15:06:43 +02:00
|
|
|
&& curr_primary_token != internal_token)
|
|
|
|
CloseHandle (curr_primary_token);
|
2004-10-28 03:46:01 +02:00
|
|
|
if (external_token != NO_IMPERSONATION)
|
2005-04-03 15:06:43 +02:00
|
|
|
CloseHandle (external_token);
|
2004-10-28 03:46:01 +02:00
|
|
|
if (internal_token != NO_IMPERSONATION)
|
2005-04-03 15:06:43 +02:00
|
|
|
CloseHandle (internal_token);
|
2002-06-17 01:34:43 +02:00
|
|
|
}
|
2008-02-14 17:47:11 +01:00
|
|
|
PWCHAR get_windows_id (PWCHAR buf)
|
|
|
|
{
|
|
|
|
return effec_cygsid.string (buf);
|
|
|
|
}
|
|
|
|
char *get_windows_id (char *buf)
|
2004-11-20 20:09:19 +01:00
|
|
|
{
|
2007-02-22 11:54:47 +01:00
|
|
|
return effec_cygsid.string (buf);
|
2004-11-20 20:09:19 +01:00
|
|
|
}
|
2005-02-13 19:17:29 +01:00
|
|
|
|
2013-01-21 05:34:52 +01:00
|
|
|
const char __reg3 *test_uid (char *&, const char *, size_t);
|
2000-11-15 01:13:09 +01:00
|
|
|
};
|
2000-11-14 06:53:32 +01:00
|
|
|
|
2001-04-17 05:52:08 +02:00
|
|
|
/* cwd cache stuff. */
|
|
|
|
|
2012-03-05 12:50:25 +01:00
|
|
|
enum fcwd_version_t {
|
|
|
|
FCWD_OLD,
|
|
|
|
FCWD_W7,
|
|
|
|
FCWD_W8
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This class is used to store the CWD starting with Windows Vista.
|
|
|
|
The CWD storage in the RTL_USER_PROCESS_PARAMETERS block is only
|
|
|
|
an afterthought now. The actual CWD storage is a FAST_CWD structure
|
|
|
|
which is allocated on the process heap. The new method only requires
|
|
|
|
minimal locking and it's much more multi-thread friendly. Presumably
|
|
|
|
it minimizes contention when accessing the CWD.
|
|
|
|
The class fcwd_access_t is supposed to encapsulate the gory implementation
|
2013-04-23 11:44:36 +02:00
|
|
|
details depending on OS version from the calling functions.
|
|
|
|
The layout of all structures has been tested on 32 and 64 bit. */
|
2012-03-05 12:50:25 +01:00
|
|
|
class fcwd_access_t {
|
2013-04-23 11:44:36 +02:00
|
|
|
/* This is the layout used in Windows 8. */
|
2012-03-05 12:50:25 +01:00
|
|
|
struct FAST_CWD_8 {
|
|
|
|
LONG ReferenceCount; /* Only release when this is 0. */
|
|
|
|
HANDLE DirectoryHandle;
|
|
|
|
ULONG OldDismountCount; /* Reflects the system DismountCount
|
|
|
|
at the time the CWD has been set. */
|
|
|
|
UNICODE_STRING Path; /* Path's Buffer member always refers
|
|
|
|
to the following Buffer array. */
|
|
|
|
LONG FSCharacteristics; /* Taken from FileFsDeviceInformation */
|
2013-04-23 11:44:36 +02:00
|
|
|
WCHAR Buffer[MAX_PATH] __attribute ((aligned (8)));
|
2012-03-05 12:50:25 +01:00
|
|
|
};
|
|
|
|
/* This is the layout used in Windows 7 and Vista. */
|
|
|
|
struct FAST_CWD_7 {
|
|
|
|
UNICODE_STRING Path; /* Path's Buffer member always refers
|
|
|
|
to the following Buffer array. */
|
|
|
|
HANDLE DirectoryHandle;
|
|
|
|
LONG FSCharacteristics; /* Taken from FileFsDeviceInformation */
|
|
|
|
LONG ReferenceCount; /* Only release when this is 0. */
|
|
|
|
ULONG OldDismountCount; /* Reflects the system DismountCount
|
|
|
|
at the time the CWD has been set. */
|
2013-04-23 11:44:36 +02:00
|
|
|
WCHAR Buffer[MAX_PATH] __attribute ((aligned (8)));
|
2012-03-05 12:50:25 +01:00
|
|
|
};
|
|
|
|
/* This is the old FAST_CWD structure up to the patch from KB 2393802,
|
|
|
|
release in February 2011. */
|
|
|
|
struct FAST_CWD_OLD {
|
|
|
|
LONG ReferenceCount; /* Only release when this is 0. */
|
|
|
|
HANDLE DirectoryHandle;
|
|
|
|
ULONG OldDismountCount; /* Reflects the system DismountCount
|
|
|
|
at the time the CWD has been set. */
|
|
|
|
UNICODE_STRING Path; /* Path's Buffer member always refers
|
|
|
|
to the following Buffer array. */
|
|
|
|
WCHAR Buffer[MAX_PATH];
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
FAST_CWD_OLD fold;
|
|
|
|
FAST_CWD_7 f7;
|
|
|
|
FAST_CWD_8 f8;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define IMPLEMENT(type, name) \
|
|
|
|
type name () { \
|
|
|
|
switch (fast_cwd_version ()) { \
|
|
|
|
case FCWD_OLD: \
|
|
|
|
default: \
|
|
|
|
return fold.name; \
|
|
|
|
case FCWD_W7: \
|
|
|
|
return f7.name; \
|
|
|
|
case FCWD_W8: \
|
|
|
|
return f8.name; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
IMPLEMENT (LONG &, ReferenceCount)
|
|
|
|
IMPLEMENT (HANDLE &, DirectoryHandle)
|
|
|
|
IMPLEMENT (ULONG &, OldDismountCount)
|
|
|
|
IMPLEMENT (UNICODE_STRING &, Path)
|
|
|
|
IMPLEMENT (WCHAR *, Buffer)
|
|
|
|
void SetFSCharacteristics (LONG val);
|
|
|
|
static fcwd_version_t &fast_cwd_version (void);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void CopyPath (UNICODE_STRING &target);
|
|
|
|
void Free (PVOID heap);
|
|
|
|
void FillIn (HANDLE dir, PUNICODE_STRING name, ULONG old_dismount_count);
|
|
|
|
static void SetDirHandleFromBufferPointer (PWCHAR buf_p, HANDLE dir);
|
|
|
|
static void SetVersionFromPointer (PBYTE buf_p, bool is_buffer);
|
|
|
|
};
|
2001-04-17 05:52:08 +02:00
|
|
|
|
2009-05-13 17:00:06 +02:00
|
|
|
class cwdstuff
|
2001-04-17 05:52:08 +02:00
|
|
|
{
|
2009-05-13 17:00:06 +02:00
|
|
|
private:
|
2001-04-17 05:52:08 +02:00
|
|
|
char *posix;
|
2008-01-31 15:18:49 +01:00
|
|
|
HANDLE dir;
|
2010-08-13 13:51:54 +02:00
|
|
|
DWORD drive_length;
|
|
|
|
int error; /* This contains an errno number which corresponds
|
|
|
|
to the problem with this path when trying to start
|
|
|
|
a native Win32 application. See cwdstuff::set for
|
2011-11-14 02:29:49 +01:00
|
|
|
how it gets set. See child_info_spawn::worker for how
|
|
|
|
it's evaluated. */
|
2012-03-05 12:50:25 +01:00
|
|
|
|
|
|
|
friend class fcwd_access_t;
|
|
|
|
/* fast_cwd_ptr is a pointer to the global RtlpCurDirRef pointer in
|
|
|
|
ntdll.dll pointing to the FAST_CWD structure which constitutes the CWD.
|
|
|
|
Unfortunately RtlpCurDirRef is not exported from ntdll.dll. */
|
|
|
|
fcwd_access_t **fast_cwd_ptr;
|
|
|
|
/* Type of FAST_CWD used on this system. Keeping this information
|
|
|
|
available in shared memory avoids to test for the version every time
|
|
|
|
around. Default to new version. */
|
|
|
|
fcwd_version_t fast_cwd_version;
|
2010-10-09 12:54:13 +02:00
|
|
|
void override_win32_cwd (bool, ULONG);
|
|
|
|
|
2009-05-13 17:00:06 +02:00
|
|
|
public:
|
|
|
|
UNICODE_STRING win32;
|
2005-04-05 06:31:00 +02:00
|
|
|
static muto cwd_lock;
|
2009-09-21 21:29:16 +02:00
|
|
|
const char *get_posix () const { return posix; };
|
|
|
|
void reset_posix (wchar_t *);
|
2008-03-07 12:24:51 +01:00
|
|
|
char *get (char *, int = 1, int = 0, unsigned = NT_MAX_PATH);
|
2008-01-31 15:18:49 +01:00
|
|
|
HANDLE get_handle () { return dir; }
|
2005-02-13 19:17:29 +01:00
|
|
|
DWORD get_drive (char * dst)
|
2004-09-08 01:26:28 +02:00
|
|
|
{
|
2007-10-23 18:26:28 +02:00
|
|
|
cwd_lock.acquire ();
|
2008-02-14 17:47:11 +01:00
|
|
|
DWORD ret = sys_wcstombs (dst, NT_MAX_PATH, win32.Buffer, drive_length);
|
2005-04-05 06:31:00 +02:00
|
|
|
cwd_lock.release ();
|
2007-10-23 18:26:28 +02:00
|
|
|
return ret;
|
2004-09-08 01:26:28 +02:00
|
|
|
}
|
2010-08-13 13:51:54 +02:00
|
|
|
int get_error () const { return error; }
|
|
|
|
const char *get_error_desc () const;
|
2001-04-17 05:52:08 +02:00
|
|
|
void init ();
|
2010-08-13 13:51:54 +02:00
|
|
|
int set (path_conv *, const char *);
|
2001-04-17 05:52:08 +02:00
|
|
|
};
|
|
|
|
|
2002-07-13 22:00:27 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
struct cygheap_debug
|
|
|
|
{
|
|
|
|
handle_list starth;
|
|
|
|
handle_list freeh[500];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-05-14 21:49:37 +02:00
|
|
|
struct cygheap_locale
|
|
|
|
{
|
|
|
|
mbtowc_p mbtowc;
|
|
|
|
wctomb_p wctomb;
|
|
|
|
char charset[ENCODING_LEN + 1];
|
|
|
|
};
|
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
struct user_heap_info
|
|
|
|
{
|
|
|
|
void *base;
|
|
|
|
void *ptr;
|
|
|
|
void *top;
|
2003-08-22 21:25:56 +02:00
|
|
|
void *max;
|
2013-04-23 11:44:36 +02:00
|
|
|
SIZE_T chunk;
|
2002-10-22 18:18:55 +02:00
|
|
|
};
|
|
|
|
|
2005-03-22 20:00:31 +01:00
|
|
|
struct hook_chain
|
|
|
|
{
|
|
|
|
void **loc;
|
|
|
|
const void *func;
|
|
|
|
struct hook_chain *next;
|
|
|
|
};
|
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
struct mini_cygheap
|
|
|
|
{
|
|
|
|
cygheap_locale locale;
|
|
|
|
};
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
#define NBUCKETS 40
|
|
|
|
|
2009-06-08 05:53:40 +02:00
|
|
|
struct init_cygheap: public mini_cygheap
|
2000-11-14 06:53:32 +01:00
|
|
|
{
|
|
|
|
_cmalloc_entry *chain;
|
2013-04-23 11:44:36 +02:00
|
|
|
unsigned bucket_val[NBUCKETS];
|
|
|
|
char *buckets[NBUCKETS];
|
2012-02-14 12:27:43 +01:00
|
|
|
WCHAR installation_root[PATH_MAX];
|
|
|
|
UNICODE_STRING installation_key;
|
|
|
|
WCHAR installation_key_buf[18];
|
2000-11-15 01:13:09 +01:00
|
|
|
cygheap_root root;
|
|
|
|
cygheap_user user;
|
2002-10-22 18:18:55 +02:00
|
|
|
user_heap_info user_heap;
|
2000-11-14 06:53:32 +01:00
|
|
|
mode_t umask;
|
2010-04-20 12:44:52 +02:00
|
|
|
unsigned long rlim_core;
|
2001-01-28 06:51:15 +01:00
|
|
|
HANDLE console_h;
|
2001-04-17 05:52:08 +02:00
|
|
|
cwdstuff cwd;
|
2001-04-18 23:10:15 +02:00
|
|
|
dtable fdtab;
|
2002-07-13 22:00:27 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
cygheap_debug debug;
|
|
|
|
#endif
|
2003-09-01 04:05:32 +02:00
|
|
|
struct sigaction *sigs;
|
2003-12-07 23:37:12 +01:00
|
|
|
|
2011-04-17 21:56:25 +02:00
|
|
|
fhandler_termios *ctty; /* Current tty */
|
2004-02-12 04:01:58 +01:00
|
|
|
struct _cygtls **threadlist;
|
2013-04-23 11:44:36 +02:00
|
|
|
uint32_t sthreads;
|
2004-09-12 05:47:57 +02:00
|
|
|
pid_t pid; /* my pid */
|
2008-03-24 15:48:58 +01:00
|
|
|
struct { /* Equivalent to using LIST_HEAD. */
|
|
|
|
struct inode_t *lh_first;
|
|
|
|
} inode_list; /* Global inode pointer for adv. locking. */
|
2005-03-22 20:00:31 +01:00
|
|
|
hook_chain hooks;
|
2004-01-14 16:45:37 +01:00
|
|
|
void close_ctty ();
|
2012-02-14 12:27:43 +01:00
|
|
|
void init_installation_root ();
|
2013-01-21 05:34:52 +01:00
|
|
|
void __reg1 init_tls_list ();;
|
|
|
|
void __reg2 add_tls (_cygtls *);
|
|
|
|
void __reg3 remove_tls (_cygtls *, DWORD);
|
2013-03-31 14:35:44 +02:00
|
|
|
_cygtls __reg3 *find_tls (int, bool&);
|
2000-11-14 06:53:32 +01:00
|
|
|
};
|
|
|
|
|
2005-11-14 05:28:45 +01:00
|
|
|
|
2004-04-11 06:07:18 +02:00
|
|
|
#define _CYGHEAPSIZE_SLOP (128 * 1024)
|
2004-03-18 20:30:51 +01:00
|
|
|
#define CYGHEAPSIZE (sizeof (init_cygheap) + (20000 * sizeof (fhandler_union)) + _CYGHEAPSIZE_SLOP)
|
|
|
|
#define CYGHEAPSIZE_MIN (sizeof (init_cygheap) + (10000 * sizeof (fhandler_union)))
|
2001-04-18 23:10:15 +02:00
|
|
|
|
2000-11-14 06:53:32 +01:00
|
|
|
extern init_cygheap *cygheap;
|
|
|
|
extern void *cygheap_max;
|
|
|
|
|
2001-10-16 01:39:33 +02:00
|
|
|
class cygheap_fdmanip
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
int fd;
|
|
|
|
bool locked;
|
|
|
|
public:
|
2010-08-09 18:53:35 +02:00
|
|
|
cygheap_fdmanip (): fd (-1), locked (false) {}
|
2001-10-16 01:39:33 +02:00
|
|
|
virtual ~cygheap_fdmanip ()
|
|
|
|
{
|
|
|
|
if (locked)
|
2004-01-14 16:45:37 +01:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 01:39:33 +02:00
|
|
|
}
|
2011-12-17 01:03:31 +01:00
|
|
|
virtual void release () { cygheap->fdtab.release (fd); }
|
2001-10-16 01:39:33 +02:00
|
|
|
operator int &() {return fd;}
|
2010-08-09 18:53:35 +02:00
|
|
|
operator fhandler_base* &() {return cygheap->fdtab[fd];}
|
|
|
|
operator fhandler_socket* () const {return reinterpret_cast<fhandler_socket *> (cygheap->fdtab[fd]);}
|
|
|
|
operator fhandler_pipe* () const {return reinterpret_cast<fhandler_pipe *> (cygheap->fdtab[fd]);}
|
|
|
|
void operator = (fhandler_base *fh) {cygheap->fdtab[fd] = fh;}
|
|
|
|
fhandler_base *operator -> () const {return cygheap->fdtab[fd];}
|
2001-11-02 00:48:34 +01:00
|
|
|
bool isopen () const
|
|
|
|
{
|
2010-08-09 18:53:35 +02:00
|
|
|
if (cygheap->fdtab[fd])
|
2001-11-02 00:48:34 +01:00
|
|
|
return true;
|
|
|
|
set_errno (EBADF);
|
|
|
|
return false;
|
|
|
|
}
|
2001-10-16 01:39:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class cygheap_fdnew : public cygheap_fdmanip
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cygheap_fdnew (int seed_fd = -1, bool lockit = true)
|
|
|
|
{
|
|
|
|
if (lockit)
|
2004-01-14 16:45:37 +01:00
|
|
|
cygheap->fdtab.lock ();
|
2001-10-16 01:39:33 +02:00
|
|
|
if (seed_fd < 0)
|
|
|
|
fd = cygheap->fdtab.find_unused_handle ();
|
|
|
|
else
|
|
|
|
fd = cygheap->fdtab.find_unused_handle (seed_fd + 1);
|
|
|
|
if (fd >= 0)
|
2010-08-09 18:53:35 +02:00
|
|
|
locked = lockit;
|
2001-10-16 01:39:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (EMFILE);
|
|
|
|
if (lockit)
|
2004-01-14 16:45:37 +01:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 01:39:33 +02:00
|
|
|
locked = false;
|
|
|
|
}
|
|
|
|
}
|
2011-12-17 01:03:31 +01:00
|
|
|
~cygheap_fdnew ()
|
|
|
|
{
|
|
|
|
if (cygheap->fdtab[fd])
|
2012-06-03 20:02:45 +02:00
|
|
|
cygheap->fdtab[fd]->inc_refcnt ();
|
2011-12-17 01:03:31 +01:00
|
|
|
}
|
2010-08-09 18:53:35 +02:00
|
|
|
void operator = (fhandler_base *fh) {cygheap->fdtab[fd] = fh;}
|
2001-10-16 01:39:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class cygheap_fdget : public cygheap_fdmanip
|
|
|
|
{
|
2011-12-17 01:03:31 +01:00
|
|
|
fhandler_base *fh;
|
|
|
|
public:
|
2001-10-16 05:31:50 +02:00
|
|
|
cygheap_fdget (int fd, bool lockit = false, bool do_set_errno = true)
|
2001-10-16 01:39:33 +02:00
|
|
|
{
|
|
|
|
if (lockit)
|
2004-01-14 16:45:37 +01:00
|
|
|
cygheap->fdtab.lock ();
|
2010-08-09 18:53:35 +02:00
|
|
|
if (fd >= 0 && fd < (int) cygheap->fdtab.size && cygheap->fdtab[fd] != NULL)
|
2001-10-16 01:39:33 +02:00
|
|
|
{
|
|
|
|
this->fd = fd;
|
|
|
|
locked = lockit;
|
2011-12-17 01:03:31 +01:00
|
|
|
fh = cygheap->fdtab[fd];
|
2012-06-03 20:02:45 +02:00
|
|
|
fh->inc_refcnt ();
|
2001-10-16 01:39:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->fd = -1;
|
2001-10-16 05:31:50 +02:00
|
|
|
if (do_set_errno)
|
2001-10-16 16:43:36 +02:00
|
|
|
set_errno (EBADF);
|
2001-10-16 01:39:33 +02:00
|
|
|
if (lockit)
|
2004-01-14 16:45:37 +01:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 01:39:33 +02:00
|
|
|
locked = false;
|
2011-12-17 01:03:31 +01:00
|
|
|
fh = NULL;
|
2001-10-16 01:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
2011-12-17 01:03:31 +01:00
|
|
|
~cygheap_fdget ()
|
|
|
|
{
|
2012-06-03 20:02:45 +02:00
|
|
|
if (fh && fh->dec_refcnt () <= 0)
|
2011-12-17 01:03:31 +01:00
|
|
|
{
|
2012-01-22 07:45:08 +01:00
|
|
|
debug_only_printf ("deleting fh %p", fh);
|
2011-12-17 01:03:31 +01:00
|
|
|
delete fh;
|
|
|
|
}
|
|
|
|
}
|
2012-01-22 08:27:57 +01:00
|
|
|
void release () { cygheap->fdtab.release (fd); }
|
2001-10-16 01:39:33 +02:00
|
|
|
};
|
|
|
|
|
2005-01-31 11:28:55 +01:00
|
|
|
class cygheap_fdenum : public cygheap_fdmanip
|
|
|
|
{
|
|
|
|
public:
|
2005-10-24 17:09:07 +02:00
|
|
|
cygheap_fdenum (bool lockit = false)
|
2005-01-31 11:28:55 +01:00
|
|
|
{
|
2005-10-24 01:47:45 +02:00
|
|
|
locked = lockit;
|
2005-01-31 11:28:55 +01:00
|
|
|
if (lockit)
|
|
|
|
cygheap->fdtab.lock ();
|
2005-10-24 17:09:07 +02:00
|
|
|
fd = -1;
|
2005-01-31 11:28:55 +01:00
|
|
|
}
|
|
|
|
int next ()
|
|
|
|
{
|
|
|
|
while (++fd < (int) cygheap->fdtab.size)
|
2010-08-09 18:53:35 +02:00
|
|
|
if (cygheap->fdtab[fd] != NULL)
|
2005-05-02 05:50:11 +02:00
|
|
|
return fd;
|
2005-01-31 11:28:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void rewind ()
|
|
|
|
{
|
2005-10-24 17:09:07 +02:00
|
|
|
fd = -1;
|
2005-01-31 11:28:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-06-15 23:59:32 +02:00
|
|
|
void __stdcall cygheap_fixup_in_child (bool);
|
2000-10-02 04:26:04 +02:00
|
|
|
void __stdcall cygheap_init ();
|
2005-06-01 05:46:56 +02:00
|
|
|
extern char _cygheap_start[] __attribute__((section(".idata")));
|