1 /* shared.cc: shared data area support.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
26 #include "shared_info_magic.h"
28 #include "cygwin_version.h"
29 #include "child_info.h"
32 shared_info NO_COPY *cygwin_shared;
33 user_info NO_COPY *user_shared;
34 HANDLE NO_COPY cygwin_user_h;
37 shared_name (char *ret_buf, const char *str, int num)
39 extern bool _cygwin_testing;
41 __small_sprintf (ret_buf, "%s%s.%s.%d", cygheap->shared_prefix,
42 cygwin_version.shared_id, str, num);
44 strcat (ret_buf, cygwin_version.dll_build_date);
48 #define page_const (65535)
49 #define pround(n) (((size_t) (n) + page_const) & ~page_const)
51 static char *offsets[] =
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))
77 open_shared (const char *name, int n, HANDLE& shared_h, DWORD size,
78 shared_locations& m, PSECURITY_ATTRIBUTES psa, DWORD access)
83 if ((m == SH_JUSTCREATE || m == SH_JUSTOPEN)
84 || !wincap.needs_memory_protection () && offsets[0])
89 (void) VirtualFree (addr, 0, MEM_RELEASE);
97 char map_buf[CYG_MAX_PATH];
101 mapname = shared_name (map_buf, name, n);
102 if (m == SH_JUSTOPEN)
103 shared_h = OpenFileMapping (access, FALSE, mapname);
106 shared_h = CreateFileMapping (INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
108 if (GetLastError () == ERROR_ALREADY_EXISTS)
113 else if (m != SH_JUSTOPEN)
114 api_fatal ("CreateFileMapping %s, %E. Terminating.", mapname);
119 shared = (shared_info *)
120 MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
124 /* Probably win95, so try without specifying the address. */
125 shared = (shared_info *) MapViewOfFileEx (shared_h,
126 FILE_MAP_READ|FILE_MAP_WRITE,
129 if (wincap.is_winnt ())
130 system_printf ("relocating shared object %s(%d) from %p to %p on Windows NT", name, n, addr, shared);
136 api_fatal ("MapViewOfFileEx '%s'(%p), %E. Terminating.", name, shared_h);
138 if (m == SH_CYGWIN_SHARED && offsets[0] && wincap.needs_memory_protection ())
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++)
144 unsigned size = offsets[i + 1] - offsets[i];
146 if (!VirtualAlloc (offsets[i], size, MEM_RESERVE, PAGE_NOACCESS))
147 continue; /* oh well */
149 offsets[SH_TOTAL_SIZE] += delta;
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);
158 debug_printf ("name %s, shared %p (wanted %p), h %p", name, shared, addr, shared_h);
164 user_shared_initialize (bool reinit)
166 char name[UNLEN + 1] = ""; /* Large enough for SID */
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;
178 cygheap->user.get_windows_id (name);
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);
188 DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &user_shared->version, USER_VERSION_MAGIC);
189 /* Initialize the Cygwin per-user shared, if necessary */
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);
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));
209 shared_info::initialize ()
211 DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &version, SHARED_VERSION_MAGIC);
214 if (sversion != SHARED_VERSION_MAGIC)
216 InterlockedExchange ((LONG *) &version, sversion);
217 multiple_cygwin_problem ("system shared memory version", sversion, SHARED_VERSION_MAGIC);
220 low_priority_sleep (0); // Should be hit only very very rarely
227 tty.init (); /* Initialize tty table. */
228 cb = sizeof (*this); /* Do last, after all shared memory initialization */
231 if (cb != SHARED_INFO_CB)
232 system_printf ("size of shared memory region changed from %u to %u",
241 /* Initialize the Cygwin heap, if necessary */
245 cygheap->user.init ();
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,
253 sizeof (*cygwin_shared),
256 cygwin_shared->initialize ();
257 ProtectHandleINH (cygheap->shared_h);
259 user_shared_initialize (false);
264 shared_info::heap_chunk_size ()
268 /* Fetch from registry, first user then local machine. */
269 for (int i = 0; i < 2; i++)
271 reg_key reg (i, KEY_READ, NULL);
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. */
278 if ((heap_chunk = reg.get_int ("heap_chunk_in_mb", 0)))
280 heap_chunk = 384; /* Default */
284 heap_chunk = 4 * 1024 * 1024;
288 heap_chunk = 384 * 1024 * 1024;
289 debug_printf ("fixed heap size is %u", heap_chunk);