OSDN Git Service

* shared.cc (open_shared): Drop useless attempt from 2006-08-11.
[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    2006 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 static shared_info cygwin_shared_area __attribute__((section (".cygwin_dll_common"), shared));
33 shared_info NO_COPY *cygwin_shared;
34 user_info NO_COPY *user_shared;
35 HANDLE NO_COPY cygwin_user_h;
36
37 char * __stdcall
38 shared_name (char *ret_buf, const char *str, int num)
39 {
40   extern bool _cygwin_testing;
41
42   __small_sprintf (ret_buf, "%s%s.%s.%d", cygheap->shared_prefix,
43                    cygwin_version.shared_id, str, num);
44   if (_cygwin_testing)
45     strcat (ret_buf, cygwin_version.dll_build_date);
46   return ret_buf;
47 }
48
49 #define page_const (65535)
50 #define pround(n) (((size_t) (n) + page_const) & ~page_const)
51
52 static ptrdiff_t offsets[] =
53 {
54   - pround (sizeof (user_info))
55   - pround (sizeof (console_state))
56   - pround (sizeof (_pinfo)),
57   - pround (sizeof (console_state))
58   - pround (sizeof (_pinfo)),
59   - pround (sizeof (_pinfo)),
60   0
61 };
62
63 #define off_addr(x)     ((void *)((caddr_t) cygwin_hmodule + offsets[x]))
64
65 void * __stdcall
66 open_shared (const char *name, int n, HANDLE& shared_h, DWORD size,
67              shared_locations& m, PSECURITY_ATTRIBUTES psa, DWORD access)
68 {
69   void *shared;
70
71   void *addr;
72   if ((m == SH_JUSTCREATE || m == SH_JUSTOPEN)
73       || !wincap.needs_memory_protection () && offsets[0])
74     addr = NULL;
75   else
76     {
77       addr = off_addr (m);
78       VirtualFree (addr, 0, MEM_RELEASE);
79     }
80
81   char map_buf[CYG_MAX_PATH];
82   char *mapname = NULL;
83
84   if (shared_h)
85     m = SH_JUSTOPEN;
86   else
87     {
88       if (name)
89         mapname = shared_name (map_buf, name, n);
90       if (m == SH_JUSTOPEN)
91         shared_h = OpenFileMapping (access, FALSE, mapname);
92       else
93         {
94           shared_h = CreateFileMapping (INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
95                                         0, size, mapname);
96           if (GetLastError () == ERROR_ALREADY_EXISTS)
97             m = SH_JUSTOPEN;
98         }
99       if (shared_h)
100         /* ok! */;
101       else if (m != SH_JUSTOPEN)
102         api_fatal ("CreateFileMapping %s, %E.  Terminating.", mapname);
103       else
104         return NULL;
105     }
106
107   shared = (shared_info *)
108     MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
109
110   if (!shared && addr)
111     {
112       /* Probably win95, so try without specifying the address.  */
113       shared = (shared_info *) MapViewOfFileEx (shared_h,
114                                        FILE_MAP_READ|FILE_MAP_WRITE,
115                                        0, 0, 0, NULL);
116 #ifdef DEBUGGING
117       if (wincap.is_winnt ())
118         system_printf ("relocating shared object %s(%d) from %p to %p on Windows NT", name, n, addr, shared);
119 #endif
120       offsets[0] = 0;
121     }
122
123   if (!shared)
124     api_fatal ("MapViewOfFileEx '%s'(%p), %E.  Terminating.", mapname, shared_h);
125
126   if (m == SH_USER_SHARED && offsets[0] && wincap.needs_memory_protection ())
127     {
128       ptrdiff_t delta = (caddr_t) shared - (caddr_t) off_addr (0);
129       offsets[0] = (caddr_t) shared - (caddr_t) cygwin_hmodule;
130       for (int i = SH_USER_SHARED + 1; i < SH_TOTAL_SIZE; i++)
131         {
132           unsigned size = offsets[i + 1] - offsets[i];
133           offsets[i] += delta;
134           if (!VirtualAlloc (off_addr (i), size, MEM_RESERVE, PAGE_NOACCESS))
135             continue;  /* oh well */
136         }
137       offsets[SH_TOTAL_SIZE] += delta;
138     }
139
140   debug_printf ("name %s, n %d, shared %p (wanted %p), h %p", mapname, n, shared, addr, shared_h);
141
142   return shared;
143 }
144
145 void
146 user_shared_initialize (bool reinit)
147 {
148   char name[UNLEN + 1] = ""; /* Large enough for SID */
149
150   if (reinit)
151     {
152       if (!UnmapViewOfFile (user_shared))
153         debug_printf("UnmapViewOfFile %E");
154       if (!ForceCloseHandle (cygwin_user_h))
155         debug_printf("CloseHandle %E");
156       cygwin_user_h = NULL;
157     }
158
159   if (!cygwin_user_h)
160     cygheap->user.get_windows_id (name);
161
162   shared_locations sh_user_shared = SH_USER_SHARED;
163   user_shared = (user_info *) open_shared (name, USER_VERSION,
164                                             cygwin_user_h, sizeof (user_info),
165                                             sh_user_shared, &sec_none);
166   debug_printf ("opening user shared for '%s' at %p", name, user_shared);
167   ProtectHandleINH (cygwin_user_h);
168   debug_printf ("user shared version %x", user_shared->version);
169
170   DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &user_shared->version, USER_VERSION_MAGIC);
171   /* Initialize the Cygwin per-user shared, if necessary */
172   if (!sversion)
173     {
174       debug_printf ("initializing user shared");
175       user_shared->mountinfo.init ();   /* Initialize the mount table.  */
176       user_shared->delqueue.init (); /* Initialize the queue of deleted files.  */
177       user_shared->cb =  sizeof (*user_shared);
178     }
179   else
180     {
181       while (!user_shared->cb)
182         low_priority_sleep (0); // Should be hit only very very rarely
183       if (user_shared->version != sversion)
184         multiple_cygwin_problem ("user shared memory version", user_shared->version, sversion);
185       else if (user_shared->cb != sizeof (*user_shared))
186         multiple_cygwin_problem ("user shared memory size", user_shared->cb, sizeof (*user_shared));
187     }
188 }
189
190 void
191 shared_info::initialize ()
192 {
193   DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &version, SHARED_VERSION_MAGIC);
194   if (sversion)
195     {
196       if (sversion != SHARED_VERSION_MAGIC)
197         {
198           InterlockedExchange ((LONG *) &version, sversion);
199           multiple_cygwin_problem ("system shared memory version", sversion, SHARED_VERSION_MAGIC);
200         }
201       while (!cb)
202         low_priority_sleep (0); // Should be hit only very very rarely
203     }
204
205   heap_init ();
206
207   if (!sversion)
208     {
209       tty.init ();              /* Initialize tty table.  */
210       cb = sizeof (*this);      /* Do last, after all shared memory initialization */
211     }
212
213   if (cb != SHARED_INFO_CB)
214     system_printf ("size of shared memory region changed from %u to %u",
215                    SHARED_INFO_CB, cb);
216 }
217
218 void __stdcall
219 memory_init ()
220 {
221   getpagesize ();
222
223   /* Initialize the Cygwin heap, if necessary */
224   if (!cygheap)
225     {
226       cygheap_init ();
227       cygheap->user.init ();
228     }
229
230   cygwin_shared = &cygwin_shared_area;
231   cygwin_shared->initialize ();
232
233   user_shared_initialize (false);
234   mtinfo_init ();
235 }
236
237 unsigned
238 shared_info::heap_chunk_size ()
239 {
240   if (!heap_chunk)
241     {
242       /* Fetch from registry, first user then local machine.  */
243       for (int i = 0; i < 2; i++)
244         {
245           reg_key reg (i, KEY_READ, NULL);
246
247           /* Note that reserving a huge amount of heap space does not result in
248              the use of swap since we are not committing it. */
249           /* FIXME: We should not be restricted to a fixed size heap no matter
250              what the fixed size is. */
251
252           if ((heap_chunk = reg.get_int ("heap_chunk_in_mb", 0)))
253             break;
254           heap_chunk = 384; /* Default */
255         }
256
257       if (heap_chunk < 4)
258         heap_chunk = 4 * 1024 * 1024;
259       else
260         heap_chunk <<= 20;
261       if (!heap_chunk)
262         heap_chunk = 384 * 1024 * 1024;
263       debug_printf ("fixed heap size is %u", heap_chunk);
264     }
265
266   return heap_chunk;
267 }