OSDN Git Service

Change internal uid datatype from __uid16_t to __uid32_t
[pf3gnuchains/sourceware.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 class cygheap_root
52 {
53   /* Root directory information.
54      This is used after a chroot is called. */
55   struct cygheap_root_mount_info *m;
56
57 public:
58   bool posix_ok (const char *path)
59   {
60     if (!m)
61       return 1;
62     return path_prefix_p (m->posix_path, path, m->posix_pathlen);
63   }
64   bool ischroot_native (const char *path)
65   {
66     if (!m)
67       return 1;
68     return strncasematch (m->native_path, path, m->native_pathlen)
69             && (path[m->native_pathlen] == '\\' || !path[m->native_pathlen]);
70   }
71   const char *unchroot (const char *path)
72   {
73     if (!m)
74       return path;
75     const char *p = path + m->posix_pathlen;
76     if (!*p)
77       p = "/";
78     return p;
79   }
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; }
86 };
87
88 class cygheap_user
89 {
90   /* Extendend user information.
91      The information is derived from the internal_getlogin call
92      when on a NT system. */
93   char  *pname;         /* user's name */
94   char  *plogsrv;       /* Logon server, may be FQDN */
95   char  *pdomain;       /* Logon domain of the user */
96   PSID   psid;          /* buffer for user's SID */
97   PSID   orig_psid;     /* Remains intact even after impersonation */
98 public:
99   __uid32_t orig_uid;      /* Remains intact even after impersonation */
100   __gid32_t orig_gid;      /* Ditto */
101   __uid32_t real_uid;      /* Remains intact on seteuid, replaced by setuid */
102   __gid32_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   PSID orig_sid () const { return orig_psid; }
125
126   void operator =(cygheap_user &user)
127   {
128     set_name (user.name ());
129     set_logsrv (user.logsrv ());
130     set_domain (user.domain ());
131     set_sid (user.sid ());
132   }
133 };
134
135 /* cwd cache stuff.  */
136
137 class muto;
138
139 struct cwdstuff
140 {
141   char *posix;
142   char *win32;
143   DWORD hash;
144   muto *cwd_lock;
145   char *get (char *buf, int need_posix = 1, int with_chroot = 0, unsigned ulen = MAX_PATH);
146   DWORD get_hash ();
147   void init ();
148   void fixup_after_exec (char *win32, char *posix, DWORD hash);
149   bool get_initial ();
150   void set (const char *win32_cwd, const char *posix_cwd = NULL);
151 };
152
153 struct init_cygheap
154 {
155   _cmalloc_entry *chain;
156   char *buckets[32];
157   struct /* User heap stuff. */
158     {
159       void *heapbase;
160       void *heapptr;
161       void *heaptop;
162     };
163   cygheap_root root;
164   cygheap_user user;
165   mode_t umask;
166   HANDLE shared_h;
167   HANDLE console_h;
168   HANDLE etc_changed_h;
169   char *cygwin_regname;
170   cwdstuff cwd;
171   dtable fdtab;
172
173   bool etc_changed ();
174 };
175
176 #define CYGHEAPSIZE (sizeof (init_cygheap) + (4000 * sizeof (fhandler_union)) + (2 * 65536))
177
178 extern init_cygheap *cygheap;
179 extern void *cygheap_max;
180
181 class cygheap_fdmanip
182 {
183  protected:
184   int fd;
185   fhandler_base **fh;
186   bool locked;
187  public:
188   cygheap_fdmanip (): fh (NULL) {}
189   virtual ~cygheap_fdmanip ()
190   {
191     if (locked)
192       ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdmanip");
193   }
194   void release ()
195   {
196     cygheap->fdtab.release (fd);
197   }
198   operator int &() {return fd;}
199   operator fhandler_base* &() {return *fh;}
200   void operator = (fhandler_base *fh) {*this->fh = fh;}
201   fhandler_base *operator -> () const {return *fh;}
202   bool isopen () const
203   {
204     if (*fh)
205       return true;
206     set_errno (EBADF);
207     return false;
208   }
209 };
210
211 class cygheap_fdnew : public cygheap_fdmanip
212 {
213  public:
214   cygheap_fdnew (int seed_fd = -1, bool lockit = true)
215   {
216     if (lockit)
217       SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
218     if (seed_fd < 0)
219       fd = cygheap->fdtab.find_unused_handle ();
220     else
221       fd = cygheap->fdtab.find_unused_handle (seed_fd + 1);
222     if (fd >= 0)
223       {
224         locked = lockit;
225         fh = cygheap->fdtab + fd;
226       }
227     else
228       {
229         set_errno (EMFILE);
230         if (lockit)
231           ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygheap_fdnew");
232         locked = false;
233       }
234   }
235   void operator = (fhandler_base *fh) {*this->fh = fh;}
236 };
237
238 class cygheap_fdget : public cygheap_fdmanip
239 {
240  public:
241   cygheap_fdget (int fd, bool lockit = false, bool do_set_errno = true)
242   {
243     if (lockit)
244       SetResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
245     if (fd >= 0 && fd < (int) cygheap->fdtab.size
246         && *(fh = cygheap->fdtab + fd) != NULL)
247       {
248         this->fd = fd;
249         locked = lockit;
250       }
251     else
252       {
253         this->fd = -1;
254         if (do_set_errno)
255           set_errno (EBADF);
256         if (lockit)
257           ReleaseResourceLock (LOCK_FD_LIST, READ_LOCK, "cygheap_fdget");
258         locked = false;
259       }
260   }
261 };
262
263 class child_info;
264 void *__stdcall cygheap_setup_for_child (child_info *ci, bool dup_later) __attribute__ ((regparm(2)));
265 void __stdcall cygheap_setup_for_child_cleanup (void *, child_info *, bool) __attribute__ ((regparm(3)));
266 void __stdcall cygheap_fixup_in_child (child_info *, bool);
267 extern "C" {
268 void __stdcall cfree (void *) __attribute__ ((regparm(1)));
269 void *__stdcall cmalloc (cygheap_types, DWORD) __attribute__ ((regparm(2)));
270 void *__stdcall crealloc (void *, DWORD) __attribute__ ((regparm(2)));
271 void *__stdcall ccalloc (cygheap_types, DWORD, DWORD) __attribute__ ((regparm(3)));
272 char *__stdcall cstrdup (const char *) __attribute__ ((regparm(1)));
273 char *__stdcall cstrdup1 (const char *) __attribute__ ((regparm(1)));
274 void __stdcall cygheap_init ();
275 }