OSDN Git Service

Replace valid memory checks with new myfault class "exception handling", almost
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / shared.cc
1 /* shared.cc: shared data area support.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "winsup.h"
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <grp.h>
17 #include <pwd.h>
18 #include "cygerrno.h"
19 #include "pinfo.h"
20 #include "security.h"
21 #include "path.h"
22 #include "fhandler.h"
23 #include "dtable.h"
24 #include "cygheap.h"
25 #include "heap.h"
26 #include "shared_info_magic.h"
27 #include "registry.h"
28 #include "cygwin_version.h"
29 #include "child_info.h"
30 #include "mtinfo.h"
31
32 shared_info NO_COPY *cygwin_shared;
33 user_info NO_COPY *user_shared;
34 HANDLE NO_COPY cygwin_user_h;
35
36 char * __stdcall
37 shared_name (char *ret_buf, const char *str, int num)
38 {
39   extern bool _cygwin_testing;
40
41   __small_sprintf (ret_buf, "%s%s.%s.%d", cygheap->shared_prefix,
42                    cygwin_version.shared_id, str, num);
43   if (_cygwin_testing)
44     strcat (ret_buf, cygwin_version.dll_build_date);
45   return ret_buf;
46 }
47
48 #define page_const (65535)
49 #define pround(n) (((size_t) (n) + page_const) & ~page_const)
50
51 static char *offsets[] =
52 {
53   (char *) cygwin_shared_address,
54   (char *) cygwin_shared_address
55     + pround (sizeof (shared_info)),
56   (char *) cygwin_shared_address
57     + pround (sizeof (shared_info))
58     + pround (sizeof (user_info)),
59   (char *) cygwin_shared_address
60     + pround (sizeof (shared_info))
61     + pround (sizeof (user_info))
62     + pround (sizeof (console_state)),
63   (char *) cygwin_shared_address
64     + pround (sizeof (shared_info))
65     + pround (sizeof (user_info))
66     + pround (sizeof (console_state))
67     + pround (sizeof (_pinfo)),
68   (char *) cygwin_shared_address
69     + pround (sizeof (shared_info))
70     + pround (sizeof (user_info))
71     + pround (sizeof (console_state))
72     + pround (sizeof (_pinfo))
73     + pround (sizeof (mtinfo))
74 };
75
76 void * __stdcall
77 open_shared (const char *name, int n, HANDLE& shared_h, DWORD size,
78              shared_locations& m, PSECURITY_ATTRIBUTES psa, DWORD access)
79 {
80   void *shared;
81
82   void *addr;
83   if ((m == SH_JUSTCREATE || m == SH_JUSTOPEN)
84       || !wincap.needs_memory_protection () && offsets[0])
85     addr = NULL;
86   else
87     {
88       addr = offsets[m];
89       (void) VirtualFree (addr, 0, MEM_RELEASE);
90     }
91
92   if (shared_h)
93     m = SH_JUSTOPEN;
94   else
95     {
96       char *mapname;
97       char map_buf[CYG_MAX_PATH];
98       if (!name)
99         mapname = NULL;
100       else
101         mapname = shared_name (map_buf, name, n);
102       if (m == SH_JUSTOPEN)
103         shared_h = OpenFileMapping (access, FALSE, mapname);
104       else
105         {
106           shared_h = CreateFileMapping (INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
107                                         0, size, mapname);
108           if (GetLastError () == ERROR_ALREADY_EXISTS)
109             m = SH_JUSTOPEN;
110         }
111       if (shared_h)
112         /* ok! */;
113       else if (m != SH_JUSTOPEN)
114         api_fatal ("CreateFileMapping %s, %E.  Terminating.", mapname);
115       else
116         return NULL;
117     }
118
119   shared = (shared_info *)
120     MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
121
122   if (!shared && addr)
123     {
124       /* Probably win95, so try without specifying the address.  */
125       shared = (shared_info *) MapViewOfFileEx (shared_h,
126                                        FILE_MAP_READ|FILE_MAP_WRITE,
127                                        0, 0, 0, NULL);
128 #ifdef DEBUGGING
129       if (wincap.is_winnt ())
130         system_printf ("relocating shared object %s(%d) from %p to %p on Windows NT", name, n, addr, shared);
131 #endif
132       offsets[0] = NULL;
133     }
134
135   if (!shared)
136     api_fatal ("MapViewOfFileEx '%s'(%p), %E.  Terminating.", name, shared_h);
137
138   if (m == SH_CYGWIN_SHARED && offsets[0] && wincap.needs_memory_protection ())
139     {
140       unsigned delta = (char *) shared - offsets[0];
141       offsets[0] = (char *) shared;
142       for (int i = SH_CYGWIN_SHARED + 1; i < SH_TOTAL_SIZE; i++)
143         {
144           unsigned size = offsets[i + 1] - offsets[i];
145           offsets[i] += delta;
146           if (!VirtualAlloc (offsets[i], size, MEM_RESERVE, PAGE_NOACCESS))
147             continue;  /* oh well */
148         }
149       offsets[SH_TOTAL_SIZE] += delta;
150
151 #if 0
152       if (!child_proc_info && wincap.needs_memory_protection ())
153         for (DWORD s = 0x950000; s <= 0xa40000; s += 0x1000)
154           VirtualAlloc ((void *) s, 4, MEM_RESERVE, PAGE_NOACCESS);
155 #endif
156     }
157
158   debug_printf ("name %s, shared %p (wanted %p), h %p", name, shared, addr, shared_h);
159
160   return shared;
161 }
162
163 void
164 user_shared_initialize (bool reinit)
165 {
166   char name[UNLEN + 1] = ""; /* Large enough for SID */
167
168   if (reinit)
169     {
170       if (!UnmapViewOfFile (user_shared))
171         debug_printf("UnmapViewOfFile %E");
172       if (!ForceCloseHandle (cygwin_user_h))
173         debug_printf("CloseHandle %E");
174       cygwin_user_h = NULL;
175     }
176
177   if (!cygwin_user_h)
178     cygheap->user.get_windows_id (name);
179
180   shared_locations sh_user_shared = SH_USER_SHARED;
181   user_shared = (user_info *) open_shared (name, USER_VERSION,
182                                             cygwin_user_h, sizeof (user_info),
183                                             sh_user_shared, &sec_none);
184   debug_printf ("opening user shared for '%s' at %p", name, user_shared);
185   ProtectHandleINH (cygwin_user_h);
186   debug_printf ("user shared version %x", user_shared->version);
187
188   DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &user_shared->version, USER_VERSION_MAGIC);
189   /* Initialize the Cygwin per-user shared, if necessary */
190   if (!sversion)
191     {
192       debug_printf ("initializing user shared");
193       user_shared->mountinfo.init ();   /* Initialize the mount table.  */
194       user_shared->delqueue.init (); /* Initialize the queue of deleted files.  */
195       user_shared->cb =  sizeof (*user_shared);
196     }
197   else
198     {
199       while (!user_shared->cb)
200         low_priority_sleep (0); // Should be hit only very very rarely
201       if (user_shared->version != sversion)
202         multiple_cygwin_problem ("user shared memory version", user_shared->version, sversion);
203       else if (user_shared->cb != sizeof (*user_shared))
204         multiple_cygwin_problem ("user shared memory size", user_shared->cb, sizeof (*user_shared));
205     }
206 }
207
208 void
209 shared_info::initialize ()
210 {
211   DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &version, SHARED_VERSION_MAGIC);
212   if (sversion)
213     {
214       if (sversion != SHARED_VERSION_MAGIC)
215         {
216           InterlockedExchange ((LONG *) &version, sversion);
217           multiple_cygwin_problem ("system shared memory version", sversion, SHARED_VERSION_MAGIC);
218         }
219       while (!cb)
220         low_priority_sleep (0); // Should be hit only very very rarely
221     }
222
223   heap_init ();
224
225   if (!sversion)
226     {
227       tty.init ();              /* Initialize tty table.  */
228       cb = sizeof (*this);      /* Do last, after all shared memory initialization */
229     }
230
231   if (cb != SHARED_INFO_CB)
232     system_printf ("size of shared memory region changed from %u to %u",
233                    SHARED_INFO_CB, cb);
234 }
235
236 void __stdcall
237 memory_init ()
238 {
239   getpagesize ();
240
241   /* Initialize the Cygwin heap, if necessary */
242   if (!cygheap)
243     {
244       cygheap_init ();
245       cygheap->user.init ();
246     }
247
248   /* Initialize general shared memory */
249   shared_locations sh_cygwin_shared = SH_CYGWIN_SHARED;
250   cygwin_shared = (shared_info *) open_shared ("shared",
251                                                CYGWIN_VERSION_SHARED_DATA,
252                                                cygheap->shared_h,
253                                                sizeof (*cygwin_shared),
254                                                sh_cygwin_shared);
255
256   cygwin_shared->initialize ();
257   ProtectHandleINH (cygheap->shared_h);
258
259   user_shared_initialize (false);
260   mtinfo_init ();
261 }
262
263 unsigned
264 shared_info::heap_chunk_size ()
265 {
266   if (!heap_chunk)
267     {
268       /* Fetch from registry, first user then local machine.  */
269       for (int i = 0; i < 2; i++)
270         {
271           reg_key reg (i, KEY_READ, NULL);
272
273           /* Note that reserving a huge amount of heap space does not result in
274              the use of swap since we are not committing it. */
275           /* FIXME: We should not be restricted to a fixed size heap no matter
276              what the fixed size is. */
277
278           if ((heap_chunk = reg.get_int ("heap_chunk_in_mb", 0)))
279             break;
280           heap_chunk = 384; /* Default */
281         }
282
283       if (heap_chunk < 4)
284         heap_chunk = 4 * 1024 * 1024;
285       else
286         heap_chunk <<= 20;
287       if (!heap_chunk)
288         heap_chunk = 384 * 1024 * 1024;
289       debug_printf ("fixed heap size is %u", heap_chunk);
290     }
291
292   return heap_chunk;
293 }