OSDN Git Service

* sync.h (new_muto): Just accept an argument which denotes the name of the
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / cygheap.h
1 /* cygheap.h: Cygwin heap manager.
2
3    Copyright 2000, 2001, 2002 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #undef cfree
12
13 enum cygheap_types
14 {
15   HEAP_FHANDLER,
16   HEAP_STR,
17   HEAP_ARGV,
18   HEAP_BUF,
19   HEAP_MOUNT,
20   HEAP_1_START,
21   HEAP_1_STR,
22   HEAP_1_ARGV,
23   HEAP_1_BUF,
24   HEAP_1_EXEC,
25   HEAP_1_MAX = 100
26 };
27
28 #define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap_max)))
29
30 struct _cmalloc_entry
31 {
32   union
33   {
34     DWORD b;
35     char *ptr;
36   };
37   struct _cmalloc_entry *prev;
38   char data[0];
39 };
40
41 struct cygheap_root_mount_info
42 {
43   char posix_path[MAX_PATH];
44   unsigned posix_pathlen;
45   char native_path[MAX_PATH];
46   unsigned native_pathlen;
47 };
48
49 /* CGF: FIXME This doesn't belong here */
50
51 int path_prefix_p (const char *path1, const char *path2, int len1) __attribute__ ((regparm (3)));
52 class cygheap_root
53 {
54   /* Root directory information.
55      This is used after a chroot is called. */
56   struct cygheap_root_mount_info *m;
57
58 public:
59   bool posix_ok (const char *path)
60   {
61     if (!m)
62       return 1;
63     return path_prefix_p (m->posix_path, path, m->posix_pathlen);
64   }
65   bool ischroot_native (const char *path)
66   {
67     if (!m)
68       return 1;
69     return strncasematch (m->native_path, path, m->native_pathlen)
70             && (path[m->native_pathlen] == '\\' || !path[m->native_pathlen]);
71   }
72   const char *unchroot (const char *path)
73   {
74     if (!m)
75       return path;
76     const char *p = path + m->posix_pathlen;
77     if (!*p)
78       p = "/";
79     return p;
80   }
81   bool exists () {return !!m;}
82   void set (const char *posix, const char *native);
83   size_t posix_length () const { return m->posix_pathlen; }
84   const char *posix_path () const { return m->posix_path; }
85   size_t native_length () const { return m->native_pathlen; }
86   const char *native_path () const { return m->native_path; }
87 };
88
89 class cygheap_user
90 {
91   /* Extendend user information.
92      The information is derived from the internal_getlogin call
93      when on a NT system. */
94   char  *pname;         /* user's name */
95   char  *plogsrv;       /* Logon server, may be FQDN */
96   char  *pdomain;       /* Logon domain of the user */
97   PSID   psid;          /* buffer for user's SID */
98 public:
99   __uid16_t orig_uid;      /* Remains intact even after impersonation */
100   __uid16_t orig_gid;      /* Ditto */
101   __uid16_t real_uid;      /* Remains intact on seteuid, replaced by setuid */
102   __gid16_t real_gid;      /* Ditto */
103
104   /* token is needed if set(e)uid should be called. It can be set by a call
105      to `set_impersonation_token()'. */
106   HANDLE token;
107   BOOL   impersonated;
108
109   cygheap_user () : pname (NULL), plogsrv (NULL), pdomain (NULL),
110                     psid (NULL), token (INVALID_HANDLE_VALUE) {}
111   ~cygheap_user ();
112
113   void set_name (const char *new_name);
114   const char *name () const { return pname; }
115
116   void set_logsrv (const char *new_logsrv);
117   const char *logsrv () const { return plogsrv; }
118
119   void set_domain (const char *new_domain);
120   const char *domain () const { return pdomain; }
121
122   BOOL set_sid (PSID new_sid);
123   PSID sid () const { return psid; }
124
125   void operator =(cygheap_user &user)
126   {
127     set_name (user.name ());
128     set_logsrv (user.logsrv ());
129     set_domain (user.domain ());
130     set_sid (user.sid ());
131   }
132 };
133
134 /* cwd cache stuff.  */
135
136 class muto;
137
138 struct cwdstuff
139 {
140   char *posix;
141   char *win32;
142   DWORD hash;
143   muto *cwd_lock;
144   char *get (char *buf, int need_posix = 1, int with_chroot = 0, unsigned ulen = MAX_PATH);
145   DWORD get_hash ();
146   void init ();
147   void fixup_after_exec (char *win32, char *posix, DWORD hash);
148   bool get_initial ();
149   void set (const char *win32_cwd, const char *posix_cwd = NULL);
150 };
151
152 struct init_cygheap
153 {
154   _cmalloc_entry *chain;
155   char *buckets[32];
156   struct /* User heap stuff. */
157     {
158       void *heapbase;
159       void *heapptr;
160       void *heaptop;
161     };
162   cygheap_root root;
163   cygheap_user user;
164   mode_t umask;
165   HANDLE shared_h;
166   HANDLE console_h;
167   HANDLE etc_changed_h;
168   char *cygwin_regname;
169   cwdstuff cwd;
170   dtable fdtab;
171
172   bool etc_changed ();
173 };
174
175 #define CYGHEAPSIZE (sizeof (init_cygheap) + (4000 * sizeof (fhandler_union)) + (2 * 65536))
176
177 extern init_cygheap *cygheap;
178 extern void *cygheap_max;
179
180 class cygheap_fdmanip
181 {
182  protected:
183   int fd;
184   fhandler_base **fh;
185   bool locked;
186  public:
187   cygheap_fdmanip (): fh (NULL) {}
188   virtual ~cygheap_fdmanip ()
189   {
190     if (locked)
191       ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdmanip");
192   }
193   void release ()
194   {
195     cygheap->fdtab.release (fd);
196   }
197   operator int &() {return fd;}
198   operator fhandler_base* &() {return *fh;}
199   void operator = (fhandler_base *fh) {*this->fh = fh;}
200   fhandler_base *operator -> () const {return *fh;}
201   bool isopen () const
202   {
203     if (*fh)
204       return true;
205     set_errno (EBADF);
206     return false;
207   }
208 };
209
210 class cygheap_fdnew : public cygheap_fdmanip
211 {
212  public:
213   cygheap_fdnew (int seed_fd = -1, bool lockit = true)
214   {
215     if (lockit)
216       SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
217     if (seed_fd < 0)
218       fd = cygheap->fdtab.find_unused_handle ();
219     else
220       fd = cygheap->fdtab.find_unused_handle (seed_fd + 1);
221     if (fd >= 0)
222       {
223         locked = lockit;
224         fh = cygheap->fdtab + fd;
225       }
226     else
227       {
228         set_errno (EMFILE);
229         if (lockit)
230           ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
231         locked = false;
232       }
233   }
234   void operator = (fhandler_base *fh) {*this->fh = fh;}
235 };
236
237 class cygheap_fdget : public cygheap_fdmanip
238 {
239  public:
240   cygheap_fdget (int fd, bool lockit = false, bool do_set_errno = true)
241   {
242     if (lockit)
243       SetResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
244     if (fd >= 0 && fd < (int) cygheap->fdtab.size
245         && *(fh = cygheap->fdtab + fd) != NULL)
246       {
247         this->fd = fd;
248         locked = lockit;
249       }
250     else
251       {
252         this->fd = -1;
253         if (do_set_errno)
254           set_errno (EBADF);
255         if (lockit)
256           ReleaseResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
257         locked = false;
258       }
259   }
260 };
261
262 class child_info;
263 void *__stdcall cygheap_setup_for_child (child_info *ci, bool dup_later) __attribute__ ((regparm(2)));
264 void __stdcall cygheap_setup_for_child_cleanup (void *, child_info *, bool) __attribute__ ((regparm(3)));
265 void __stdcall cygheap_fixup_in_child (child_info *, bool);
266 extern "C" {
267 void __stdcall cfree (void *) __attribute__ ((regparm(1)));
268 void *__stdcall cmalloc (cygheap_types, DWORD) __attribute__ ((regparm(2)));
269 void *__stdcall crealloc (void *, DWORD) __attribute__ ((regparm(2)));
270 void *__stdcall ccalloc (cygheap_types, DWORD, DWORD) __attribute__ ((regparm(3)));
271 char *__stdcall cstrdup (const char *) __attribute__ ((regparm(1)));
272 char *__stdcall cstrdup1 (const char *) __attribute__ ((regparm(1)));
273 void __stdcall cygheap_init ();
274 }