1 /* cygheap.h: Cygwin heap manager.
3 Copyright 2000, 2001, 2002 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
28 #define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap_max)))
37 struct _cmalloc_entry *prev;
41 struct cygheap_root_mount_info
43 char posix_path[MAX_PATH];
44 unsigned posix_pathlen;
45 char native_path[MAX_PATH];
46 unsigned native_pathlen;
49 /* CGF: FIXME This doesn't belong here */
53 /* Root directory information.
54 This is used after a chroot is called. */
55 struct cygheap_root_mount_info *m;
58 bool posix_ok (const char *path)
62 return path_prefix_p (m->posix_path, path, m->posix_pathlen);
64 bool ischroot_native (const char *path)
68 return strncasematch (m->native_path, path, m->native_pathlen)
69 && (path[m->native_pathlen] == '\\' || !path[m->native_pathlen]);
71 const char *unchroot (const char *path)
75 const char *p = path + m->posix_pathlen;
80 bool exists () {return !!m;}
81 void set (const char *posix, const char *native);
82 size_t posix_length () const { return m->posix_pathlen; }
83 const char *posix_path () const { return m->posix_path; }
84 size_t native_length () const { return m->native_pathlen; }
85 const char *native_path () const { return m->native_path; }
98 /* Extendend user information.
99 The information is derived from the internal_getlogin call
100 when on a NT system. */
101 char *pname; /* user's name */
102 char *plogsrv; /* Logon server, may be FQDN */
103 char *pdomain; /* Logon domain of the user */
104 char *homedrive; /* User's home drive */
105 char *homepath; /* User's home path */
106 PSID psid; /* buffer for user's SID */
107 PSID orig_psid; /* Remains intact even after impersonation */
109 __uid32_t orig_uid; /* Remains intact even after impersonation */
110 __gid32_t orig_gid; /* Ditto */
111 __uid32_t real_uid; /* Remains intact on seteuid, replaced by setuid */
112 __gid32_t real_gid; /* Ditto */
114 /* token is needed if set(e)uid should be called. It can be set by a call
115 to `set_impersonation_token()'. */
119 cygheap_user () : pname (NULL), plogsrv (NULL), pdomain (NULL),
120 homedrive (NULL), homepath (NULL),
121 psid (NULL), token (INVALID_HANDLE_VALUE) {}
124 void set_name (const char *new_name);
125 const char *name () const { return pname; }
127 const char *env_logsrv ();
128 const char *env_homepath ();
129 const char *env_homedrive ();
130 const char *env_userprofile ();
131 const char *env_domain ();
132 const char *env_name ();
134 BOOL set_sid (PSID new_sid);
135 BOOL set_orig_sid ();
136 PSID sid () const { return psid; }
137 PSID orig_sid () const { return orig_psid; }
138 const char *ontherange (homebodies what, struct passwd * = NULL);
141 /* cwd cache stuff. */
151 char *get (char *buf, int need_posix = 1, int with_chroot = 0, unsigned ulen = MAX_PATH);
154 void fixup_after_exec (char *win32, char *posix, DWORD hash);
156 void set (const char *win32_cwd, const char *posix_cwd = NULL);
161 _cmalloc_entry *chain;
163 struct /* User heap stuff. */
174 HANDLE etc_changed_h;
175 char *cygwin_regname;
182 #define CYGHEAPSIZE (sizeof (init_cygheap) + (4000 * sizeof (fhandler_union)) + (2 * 65536))
184 extern init_cygheap *cygheap;
185 extern void *cygheap_max;
187 class cygheap_fdmanip
194 cygheap_fdmanip (): fh (NULL) {}
195 virtual ~cygheap_fdmanip ()
198 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdmanip");
202 cygheap->fdtab.release (fd);
204 operator int &() {return fd;}
205 operator fhandler_base* &() {return *fh;}
206 void operator = (fhandler_base *fh) {*this->fh = fh;}
207 fhandler_base *operator -> () const {return *fh;}
217 class cygheap_fdnew : public cygheap_fdmanip
220 cygheap_fdnew (int seed_fd = -1, bool lockit = true)
223 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
225 fd = cygheap->fdtab.find_unused_handle ();
227 fd = cygheap->fdtab.find_unused_handle (seed_fd + 1);
231 fh = cygheap->fdtab + fd;
237 ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
241 void operator = (fhandler_base *fh) {*this->fh = fh;}
244 class cygheap_fdget : public cygheap_fdmanip
247 cygheap_fdget (int fd, bool lockit = false, bool do_set_errno = true)
250 SetResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
251 if (fd >= 0 && fd < (int) cygheap->fdtab.size
252 && *(fh = cygheap->fdtab + fd) != NULL)
263 ReleaseResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
270 void *__stdcall cygheap_setup_for_child (child_info *ci, bool dup_later) __attribute__ ((regparm(2)));
271 void __stdcall cygheap_setup_for_child_cleanup (void *, child_info *, bool) __attribute__ ((regparm(3)));
272 void __stdcall cygheap_fixup_in_child (child_info *, bool);
274 void __stdcall cfree (void *) __attribute__ ((regparm(1)));
275 void *__stdcall cmalloc (cygheap_types, DWORD) __attribute__ ((regparm(2)));
276 void *__stdcall crealloc (void *, DWORD) __attribute__ ((regparm(2)));
277 void *__stdcall ccalloc (cygheap_types, DWORD, DWORD) __attribute__ ((regparm(3)));
278 char *__stdcall cstrdup (const char *) __attribute__ ((regparm(1)));
279 char *__stdcall cstrdup1 (const char *) __attribute__ ((regparm(1)));
280 void __stdcall cygheap_init ();