2000-09-03 12:16:35 +08:00
|
|
|
/* cygheap.h: Cygwin heap manager.
|
|
|
|
|
2010-04-20 18:44:52 +08:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2011-04-18 19:26:37 +08:00
|
|
|
2010, 2011 Red Hat, Inc.
|
2000-09-03 12:16:35 +08: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-05 04:45:56 +08:00
|
|
|
#include "hires.h"
|
2009-08-02 03:52:46 +08:00
|
|
|
#include "cygheap_malloc.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-02 10:26:04 +08:00
|
|
|
#define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap_max)))
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-11-14 13:53:32 +08:00
|
|
|
struct _cmalloc_entry
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
DWORD b;
|
|
|
|
char *ptr;
|
|
|
|
};
|
|
|
|
struct _cmalloc_entry *prev;
|
|
|
|
char data[0];
|
|
|
|
};
|
|
|
|
|
2001-06-03 10:31:16 +08:00
|
|
|
struct cygheap_root_mount_info
|
|
|
|
{
|
2003-11-15 07:40:06 +08:00
|
|
|
char posix_path[CYG_MAX_PATH];
|
2001-06-03 10:31:16 +08:00
|
|
|
unsigned posix_pathlen;
|
2003-11-15 07:40:06 +08:00
|
|
|
char native_path[CYG_MAX_PATH];
|
2001-06-03 10:31:16 +08:00
|
|
|
unsigned native_pathlen;
|
2008-07-17 04:20:45 +08:00
|
|
|
bool caseinsensitive;
|
2001-06-03 10:31:16 +08:00
|
|
|
};
|
|
|
|
|
2001-06-04 09:28:09 +08:00
|
|
|
/* CGF: FIXME This doesn't belong here */
|
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
class cygheap_root
|
|
|
|
{
|
|
|
|
/* Root directory information.
|
|
|
|
This is used after a chroot is called. */
|
2001-06-03 10:31:16 +08:00
|
|
|
struct cygheap_root_mount_info *m;
|
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
public:
|
2001-06-03 10:31:16 +08:00
|
|
|
bool posix_ok (const char *path)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return 1;
|
2008-07-17 04:20:45 +08:00
|
|
|
return path_prefix_p (m->posix_path, path, m->posix_pathlen,
|
|
|
|
m->caseinsensitive);
|
2001-06-03 10:31:16 +08:00
|
|
|
}
|
|
|
|
bool ischroot_native (const char *path)
|
|
|
|
{
|
|
|
|
if (!m)
|
|
|
|
return 1;
|
|
|
|
return strncasematch (m->native_path, path, m->native_pathlen)
|
2001-09-08 05:32:07 +08:00
|
|
|
&& (path[m->native_pathlen] == '\\' || !path[m->native_pathlen]);
|
2001-06-03 10:31:16 +08: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-17 04:20:45 +08:00
|
|
|
void set (const char *, const char *, bool);
|
2001-06-03 10:31:16 +08: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 08:13:09 +08:00
|
|
|
};
|
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
enum homebodies
|
|
|
|
{
|
|
|
|
CH_HOMEDRIVE,
|
|
|
|
CH_HOMEPATH,
|
|
|
|
CH_HOME
|
|
|
|
};
|
|
|
|
|
2001-01-17 22:57:09 +08:00
|
|
|
class cygheap_user
|
|
|
|
{
|
2000-11-15 08:13:09 +08:00
|
|
|
/* Extendend user information.
|
|
|
|
The information is derived from the internal_getlogin call
|
|
|
|
when on a NT system. */
|
2009-07-01 05:18:44 +08:00
|
|
|
char *pname; /* user's name */
|
2000-11-16 05:04:02 +08:00
|
|
|
char *plogsrv; /* Logon server, may be FQDN */
|
|
|
|
char *pdomain; /* Logon domain of the user */
|
2002-06-12 13:13:54 +08:00
|
|
|
char *homedrive; /* User's home drive */
|
|
|
|
char *homepath; /* User's home path */
|
2004-10-08 05:28:57 +08:00
|
|
|
char *psystemroot; /* Value of SYSTEMROOT */
|
2002-06-29 10:36:08 +08:00
|
|
|
char *pwinname; /* User's name as far as Windows knows it */
|
2002-06-28 04:44:27 +08:00
|
|
|
char *puserprof; /* User profile */
|
2003-09-27 09:56:36 +08:00
|
|
|
cygsid effec_cygsid; /* buffer for user's SID */
|
|
|
|
cygsid saved_cygsid; /* Remains intact even after impersonation */
|
2000-11-15 08:13:09 +08:00
|
|
|
public:
|
2003-09-16 17:24:52 +08:00
|
|
|
__uid32_t saved_uid; /* Remains intact even after impersonation */
|
|
|
|
__gid32_t saved_gid; /* Ditto */
|
2002-05-29 23:04:29 +08:00
|
|
|
__uid32_t real_uid; /* Remains intact on seteuid, replaced by setuid */
|
Change internal gid datatype from __gid16_t to __gid32_t
throughout.
* cygwin.din: Export new symbols chown32, fchown32, getegid32,
getgid32, getgrgid32, getgrnam32, getgroups32, initgroups32, lchown32,
setgid32, setegid32, getgrent32.
* grp.cc (grp32togrp16): New static function.
(getgrgid32): New function.
(getgrnam32): Ditto.
(getgrent32): Ditto.
(getgroups32): Change name of internal function from getgroups.
(getgroups32): New function.
(initgroups32): Ditto.
* syscalls.cc (chown32): Ditto.
(lchown32): Ditto.
(fchown32): Ditto.
(setegid32): Ditto.
(setgid32): Ditto.
* uinfo.cc (getgid32): Ditto.
(getegid32): Ditto.
* include/cygwin/grp.h: Remove declaration of getgrgid() and getgrnam().
Declare getgrgid32() and getgrnam32() instead. Declare getgid32().
2002-05-28 22:10:55 +08:00
|
|
|
__gid32_t real_gid; /* Ditto */
|
2002-07-29 20:51:52 +08:00
|
|
|
user_groups groups; /* Primary and supp SIDs */
|
2000-11-15 08:13:09 +08:00
|
|
|
|
2000-11-16 05:04:02 +08: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 21:07:36 +08:00
|
|
|
HANDLE external_token;
|
|
|
|
HANDLE internal_token;
|
2005-04-03 21:06:43 +08:00
|
|
|
HANDLE curr_primary_token;
|
2007-07-17 04:01:15 +08:00
|
|
|
HANDLE curr_imp_token;
|
2009-10-13 18:23:31 +08: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-16 05:04:02 +08:00
|
|
|
|
2002-06-28 04:44:27 +08: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-08 06:37:12 +08:00
|
|
|
I've removed the constructor entirely. Please reinstate this if this
|
2002-06-28 04:44:27 +08:00
|
|
|
situation ever changes.
|
2001-06-04 22:29:54 +08:00
|
|
|
cygheap_user () : pname (NULL), plogsrv (NULL), pdomain (NULL),
|
2003-09-16 17:24:52 +08:00
|
|
|
homedrive (NULL), homepath (NULL),
|
2002-06-28 04:44:27 +08:00
|
|
|
token (INVALID_HANDLE_VALUE) {}
|
|
|
|
*/
|
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
~cygheap_user ();
|
|
|
|
|
2003-09-11 05:01:40 +08:00
|
|
|
void init ();
|
2000-11-15 08:13:09 +08:00
|
|
|
void set_name (const char *new_name);
|
|
|
|
const char *name () const { return pname; }
|
|
|
|
|
2002-06-29 10:36:08 +08: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-08 05:28:57 +08:00
|
|
|
const char *env_systemroot (const char *, size_t);
|
2000-11-15 08:13:09 +08:00
|
|
|
|
2002-06-29 10:36:08 +08:00
|
|
|
const char *logsrv ()
|
|
|
|
{
|
2002-06-30 06:05:30 +08:00
|
|
|
const char *p = env_logsrv ("LOGONSERVER=", sizeof ("LOGONSERVER=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 10:36:08 +08:00
|
|
|
}
|
|
|
|
const char *winname ()
|
|
|
|
{
|
2002-06-30 06:05:30 +08:00
|
|
|
const char *p = env_name ("USERNAME=", sizeof ("USERNAME=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 10:36:08 +08:00
|
|
|
}
|
|
|
|
const char *domain ()
|
|
|
|
{
|
2002-06-30 06:05:30 +08:00
|
|
|
const char *p = env_domain ("USERDOMAIN=", sizeof ("USERDOMAIN=") - 1);
|
|
|
|
return (p == almost_null) ? NULL : p;
|
2002-06-29 10:36:08 +08:00
|
|
|
}
|
2003-09-27 09:56:36 +08:00
|
|
|
BOOL set_sid (PSID new_sid) {return (BOOL) (effec_cygsid = new_sid);}
|
|
|
|
BOOL set_saved_sid () { return (BOOL) (saved_cygsid = effec_cygsid); }
|
|
|
|
PSID sid () { return effec_cygsid; }
|
|
|
|
PSID saved_sid () { return saved_cygsid; }
|
2002-06-12 13:13:54 +08:00
|
|
|
const char *ontherange (homebodies what, struct passwd * = NULL);
|
2004-10-28 09:46:01 +08:00
|
|
|
#define NO_IMPERSONATION NULL
|
2007-07-17 04:01:15 +08:00
|
|
|
bool issetuid () const { return curr_imp_token != NO_IMPERSONATION; }
|
2005-04-03 21:06:43 +08:00
|
|
|
HANDLE primary_token () { return curr_primary_token; }
|
2007-07-17 04:01:15 +08:00
|
|
|
HANDLE imp_token () { return curr_imp_token; }
|
2003-06-30 21:07:36 +08:00
|
|
|
void deimpersonate ()
|
|
|
|
{
|
2007-01-24 18:50:47 +08:00
|
|
|
RevertToSelf ();
|
2003-06-30 21:07:36 +08:00
|
|
|
}
|
2005-04-03 21:06:43 +08:00
|
|
|
bool reimpersonate ()
|
2003-06-30 21:07:36 +08:00
|
|
|
{
|
2006-12-13 00:27:32 +08:00
|
|
|
if (issetuid ())
|
2007-07-17 04:01:15 +08:00
|
|
|
return ImpersonateLoggedOnUser (primary_token ());
|
2006-12-13 00:27:32 +08:00
|
|
|
return true;
|
2003-06-30 21:07:36 +08:00
|
|
|
}
|
2003-07-15 01:04:21 +08:00
|
|
|
bool has_impersonation_tokens ()
|
2004-10-28 09:46:01 +08:00
|
|
|
{ return external_token != NO_IMPERSONATION
|
|
|
|
|| internal_token != NO_IMPERSONATION
|
2005-04-03 21:06:43 +08:00
|
|
|
|| curr_primary_token != NO_IMPERSONATION; }
|
2003-06-30 21:07:36 +08:00
|
|
|
void close_impersonation_tokens ()
|
|
|
|
{
|
2007-07-17 04:01:15 +08:00
|
|
|
if (curr_imp_token != NO_IMPERSONATION)
|
|
|
|
CloseHandle (curr_imp_token);
|
2005-04-03 21:06:43 +08:00
|
|
|
if (curr_primary_token != NO_IMPERSONATION
|
2011-06-06 13:02:13 +08:00
|
|
|
&& curr_primary_token != external_token
|
2005-04-03 21:06:43 +08:00
|
|
|
&& curr_primary_token != internal_token)
|
|
|
|
CloseHandle (curr_primary_token);
|
2004-10-28 09:46:01 +08:00
|
|
|
if (external_token != NO_IMPERSONATION)
|
2005-04-03 21:06:43 +08:00
|
|
|
CloseHandle (external_token);
|
2004-10-28 09:46:01 +08:00
|
|
|
if (internal_token != NO_IMPERSONATION)
|
2005-04-03 21:06:43 +08:00
|
|
|
CloseHandle (internal_token);
|
2002-06-17 07:34:43 +08:00
|
|
|
}
|
2008-02-15 00:47:11 +08:00
|
|
|
PWCHAR get_windows_id (PWCHAR buf)
|
|
|
|
{
|
|
|
|
return effec_cygsid.string (buf);
|
|
|
|
}
|
|
|
|
char *get_windows_id (char *buf)
|
2004-11-21 03:09:19 +08:00
|
|
|
{
|
2007-02-22 18:54:47 +08:00
|
|
|
return effec_cygsid.string (buf);
|
2004-11-21 03:09:19 +08:00
|
|
|
}
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2006-07-18 03:30:30 +08:00
|
|
|
const char *test_uid (char *&, const char *, size_t)
|
2002-06-29 10:36:08 +08:00
|
|
|
__attribute__ ((regparm (3)));
|
2000-11-15 08:13:09 +08:00
|
|
|
};
|
2000-11-14 13:53:32 +08:00
|
|
|
|
2001-04-17 11:52:08 +08:00
|
|
|
/* cwd cache stuff. */
|
|
|
|
|
|
|
|
class muto;
|
|
|
|
|
2009-05-13 23:00:06 +08:00
|
|
|
class cwdstuff
|
2001-04-17 11:52:08 +08:00
|
|
|
{
|
2009-05-13 23:00:06 +08:00
|
|
|
private:
|
2001-04-17 11:52:08 +08:00
|
|
|
char *posix;
|
2008-01-31 22:18:49 +08:00
|
|
|
HANDLE dir;
|
2010-08-13 19:51:54 +08: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
|
|
|
|
how it gets set. See spawn_guts for how it's
|
|
|
|
evaluated. */
|
2010-10-09 18:54:13 +08:00
|
|
|
void override_win32_cwd (bool, ULONG);
|
|
|
|
|
2009-05-13 23:00:06 +08:00
|
|
|
public:
|
|
|
|
UNICODE_STRING win32;
|
2005-04-05 12:31:00 +08:00
|
|
|
static muto cwd_lock;
|
2009-09-22 03:29:16 +08:00
|
|
|
const char *get_posix () const { return posix; };
|
|
|
|
void reset_posix (wchar_t *);
|
2008-03-07 19:24:51 +08:00
|
|
|
char *get (char *, int = 1, int = 0, unsigned = NT_MAX_PATH);
|
2008-01-31 22:18:49 +08:00
|
|
|
HANDLE get_handle () { return dir; }
|
2005-02-14 02:17:29 +08:00
|
|
|
DWORD get_drive (char * dst)
|
2004-09-08 07:26:28 +08:00
|
|
|
{
|
2007-10-24 00:26:28 +08:00
|
|
|
cwd_lock.acquire ();
|
2008-02-15 00:47:11 +08:00
|
|
|
DWORD ret = sys_wcstombs (dst, NT_MAX_PATH, win32.Buffer, drive_length);
|
2005-04-05 12:31:00 +08:00
|
|
|
cwd_lock.release ();
|
2007-10-24 00:26:28 +08:00
|
|
|
return ret;
|
2004-09-08 07:26:28 +08:00
|
|
|
}
|
2010-08-13 19:51:54 +08:00
|
|
|
int get_error () const { return error; }
|
|
|
|
const char *get_error_desc () const;
|
2001-04-17 11:52:08 +08:00
|
|
|
void init ();
|
2010-08-13 19:51:54 +08:00
|
|
|
int set (path_conv *, const char *);
|
2001-04-17 11:52:08 +08:00
|
|
|
};
|
|
|
|
|
2002-07-14 04:00:27 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
struct cygheap_debug
|
|
|
|
{
|
|
|
|
handle_list starth;
|
|
|
|
handle_list freeh[500];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-05-15 03:49:37 +08:00
|
|
|
struct cygheap_locale
|
|
|
|
{
|
|
|
|
mbtowc_p mbtowc;
|
|
|
|
wctomb_p wctomb;
|
|
|
|
char charset[ENCODING_LEN + 1];
|
|
|
|
};
|
|
|
|
|
2002-10-23 00:18:55 +08:00
|
|
|
struct user_heap_info
|
|
|
|
{
|
|
|
|
void *base;
|
|
|
|
void *ptr;
|
|
|
|
void *top;
|
2003-08-23 03:25:56 +08:00
|
|
|
void *max;
|
2002-10-23 00:18:55 +08:00
|
|
|
unsigned chunk;
|
2006-11-01 02:41:16 +08:00
|
|
|
unsigned slop;
|
2002-10-23 00:18:55 +08:00
|
|
|
};
|
|
|
|
|
2005-03-23 03:00:31 +08:00
|
|
|
struct hook_chain
|
|
|
|
{
|
|
|
|
void **loc;
|
|
|
|
const void *func;
|
|
|
|
struct hook_chain *next;
|
|
|
|
};
|
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
struct mini_cygheap
|
|
|
|
{
|
|
|
|
cygheap_locale locale;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct init_cygheap: public mini_cygheap
|
2000-11-14 13:53:32 +08:00
|
|
|
{
|
|
|
|
_cmalloc_entry *chain;
|
2001-09-06 11:39:18 +08:00
|
|
|
char *buckets[32];
|
2000-11-15 08:13:09 +08:00
|
|
|
cygheap_root root;
|
|
|
|
cygheap_user user;
|
2002-10-23 00:18:55 +08:00
|
|
|
user_heap_info user_heap;
|
2000-11-14 13:53:32 +08:00
|
|
|
mode_t umask;
|
2010-04-20 18:44:52 +08:00
|
|
|
unsigned long rlim_core;
|
2001-01-28 13:51:15 +08:00
|
|
|
HANDLE console_h;
|
2001-04-17 11:52:08 +08:00
|
|
|
cwdstuff cwd;
|
2001-04-19 05:10:15 +08:00
|
|
|
dtable fdtab;
|
2002-07-14 04:00:27 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
cygheap_debug debug;
|
|
|
|
#endif
|
2003-09-01 10:05:32 +08:00
|
|
|
struct sigaction *sigs;
|
2003-12-08 06:37:12 +08:00
|
|
|
|
2011-04-18 03:56:25 +08:00
|
|
|
fhandler_termios *ctty; /* Current tty */
|
2004-01-24 07:05:33 +08:00
|
|
|
#ifdef NEWVFORK
|
2004-01-04 02:15:03 +08:00
|
|
|
fhandler_tty_slave *ctty_on_hold;
|
2004-01-24 07:05:33 +08:00
|
|
|
#endif
|
2004-02-12 11:01:58 +08:00
|
|
|
struct _cygtls **threadlist;
|
2004-01-14 23:45:37 +08:00
|
|
|
size_t sthreads;
|
2004-09-12 11:47:57 +08:00
|
|
|
pid_t pid; /* my pid */
|
2004-11-26 12:15:10 +08:00
|
|
|
HANDLE pid_handle; /* handle for my pid */
|
2008-03-24 22:48:58 +08:00
|
|
|
struct { /* Equivalent to using LIST_HEAD. */
|
|
|
|
struct inode_t *lh_first;
|
|
|
|
} inode_list; /* Global inode pointer for adv. locking. */
|
2005-03-23 03:00:31 +08:00
|
|
|
hook_chain hooks;
|
2004-01-14 23:45:37 +08:00
|
|
|
void close_ctty ();
|
2005-11-14 12:28:45 +08:00
|
|
|
int manage_console_count (const char *, int, bool = false) __attribute__ ((regparm (3)));
|
|
|
|
private:
|
|
|
|
int console_count;
|
2000-11-14 13:53:32 +08:00
|
|
|
};
|
|
|
|
|
2005-11-14 12:28:45 +08:00
|
|
|
|
2004-04-11 12:07:18 +08:00
|
|
|
#define _CYGHEAPSIZE_SLOP (128 * 1024)
|
2004-03-19 03:30:51 +08:00
|
|
|
#define CYGHEAPSIZE (sizeof (init_cygheap) + (20000 * sizeof (fhandler_union)) + _CYGHEAPSIZE_SLOP)
|
|
|
|
#define CYGHEAPSIZE_MIN (sizeof (init_cygheap) + (10000 * sizeof (fhandler_union)))
|
2001-04-19 05:10:15 +08:00
|
|
|
|
2000-11-14 13:53:32 +08:00
|
|
|
extern init_cygheap *cygheap;
|
|
|
|
extern void *cygheap_max;
|
|
|
|
|
2001-10-16 07:39:33 +08:00
|
|
|
class cygheap_fdmanip
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
int fd;
|
|
|
|
bool locked;
|
|
|
|
public:
|
2010-08-10 00:53:35 +08:00
|
|
|
cygheap_fdmanip (): fd (-1), locked (false) {}
|
2001-10-16 07:39:33 +08:00
|
|
|
virtual ~cygheap_fdmanip ()
|
|
|
|
{
|
|
|
|
if (locked)
|
2004-01-14 23:45:37 +08:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 07:39:33 +08:00
|
|
|
}
|
|
|
|
void release ()
|
|
|
|
{
|
|
|
|
cygheap->fdtab.release (fd);
|
|
|
|
}
|
|
|
|
operator int &() {return fd;}
|
2010-08-10 00:53:35 +08: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 07:48:34 +08:00
|
|
|
bool isopen () const
|
|
|
|
{
|
2010-08-10 00:53:35 +08:00
|
|
|
if (cygheap->fdtab[fd])
|
2001-11-02 07:48:34 +08:00
|
|
|
return true;
|
|
|
|
set_errno (EBADF);
|
|
|
|
return false;
|
|
|
|
}
|
2001-10-16 07:39:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class cygheap_fdnew : public cygheap_fdmanip
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cygheap_fdnew (int seed_fd = -1, bool lockit = true)
|
|
|
|
{
|
|
|
|
if (lockit)
|
2004-01-14 23:45:37 +08:00
|
|
|
cygheap->fdtab.lock ();
|
2001-10-16 07:39:33 +08: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-10 00:53:35 +08:00
|
|
|
locked = lockit;
|
2001-10-16 07:39:33 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (EMFILE);
|
|
|
|
if (lockit)
|
2004-01-14 23:45:37 +08:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 07:39:33 +08:00
|
|
|
locked = false;
|
|
|
|
}
|
|
|
|
}
|
2010-08-10 00:53:35 +08:00
|
|
|
void operator = (fhandler_base *fh) {cygheap->fdtab[fd] = fh;}
|
2001-10-16 07:39:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class cygheap_fdget : public cygheap_fdmanip
|
|
|
|
{
|
|
|
|
public:
|
2001-10-16 11:31:50 +08:00
|
|
|
cygheap_fdget (int fd, bool lockit = false, bool do_set_errno = true)
|
2001-10-16 07:39:33 +08:00
|
|
|
{
|
|
|
|
if (lockit)
|
2004-01-14 23:45:37 +08:00
|
|
|
cygheap->fdtab.lock ();
|
2010-08-10 00:53:35 +08:00
|
|
|
if (fd >= 0 && fd < (int) cygheap->fdtab.size && cygheap->fdtab[fd] != NULL)
|
2001-10-16 07:39:33 +08:00
|
|
|
{
|
|
|
|
this->fd = fd;
|
|
|
|
locked = lockit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->fd = -1;
|
2001-10-16 11:31:50 +08:00
|
|
|
if (do_set_errno)
|
2001-10-16 22:43:36 +08:00
|
|
|
set_errno (EBADF);
|
2001-10-16 07:39:33 +08:00
|
|
|
if (lockit)
|
2004-01-14 23:45:37 +08:00
|
|
|
cygheap->fdtab.unlock ();
|
2001-10-16 07:39:33 +08:00
|
|
|
locked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-01-31 18:28:55 +08:00
|
|
|
class cygheap_fdenum : public cygheap_fdmanip
|
|
|
|
{
|
|
|
|
public:
|
2005-10-24 23:09:07 +08:00
|
|
|
cygheap_fdenum (bool lockit = false)
|
2005-01-31 18:28:55 +08:00
|
|
|
{
|
2005-10-24 07:47:45 +08:00
|
|
|
locked = lockit;
|
2005-01-31 18:28:55 +08:00
|
|
|
if (lockit)
|
|
|
|
cygheap->fdtab.lock ();
|
2005-10-24 23:09:07 +08:00
|
|
|
fd = -1;
|
2005-01-31 18:28:55 +08:00
|
|
|
}
|
|
|
|
int next ()
|
|
|
|
{
|
|
|
|
while (++fd < (int) cygheap->fdtab.size)
|
2010-08-10 00:53:35 +08:00
|
|
|
if (cygheap->fdtab[fd] != NULL)
|
2005-05-02 11:50:11 +08:00
|
|
|
return fd;
|
2005-01-31 18:28:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void rewind ()
|
|
|
|
{
|
2005-10-24 23:09:07 +08:00
|
|
|
fd = -1;
|
2005-01-31 18:28:55 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-06-16 05:59:32 +08:00
|
|
|
void __stdcall cygheap_fixup_in_child (bool);
|
2000-10-02 10:26:04 +08:00
|
|
|
void __stdcall cygheap_init ();
|
2005-06-01 11:46:56 +08:00
|
|
|
extern char _cygheap_start[] __attribute__((section(".idata")));
|