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 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;
38 shared_name (char *ret_buf, const char *str, int num)
40 extern bool _cygwin_testing;
42 __small_sprintf (ret_buf, "%s%s.%s.%d", cygheap->shared_prefix,
43 cygwin_version.shared_id, str, num);
45 strcat (ret_buf, cygwin_version.dll_build_date);
49 #define page_const (65535)
50 #define pround(n) (((size_t) (n) + page_const) & ~page_const)
52 static ptrdiff_t offsets[] =
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)),
63 #define off_addr(x) ((void *)((caddr_t) cygwin_hmodule + offsets[x]))
66 open_shared (const char *name, int n, HANDLE& shared_h, DWORD size,
67 shared_locations& m, PSECURITY_ATTRIBUTES psa, DWORD access)
72 if ((m == SH_JUSTCREATE || m == SH_JUSTOPEN)
73 || !wincap.needs_memory_protection () && offsets[0])
78 VirtualFree (addr, 0, MEM_RELEASE);
81 char map_buf[CYG_MAX_PATH];
89 mapname = shared_name (map_buf, name, n);
91 shared_h = OpenFileMapping (access, FALSE, mapname);
94 shared_h = CreateFileMapping (INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
96 if (GetLastError () == ERROR_ALREADY_EXISTS)
101 else if (m != SH_JUSTOPEN)
102 api_fatal ("CreateFileMapping %s, %E. Terminating.", mapname);
107 shared = (shared_info *)
108 MapViewOfFileEx (shared_h, access, 0, 0, 0, addr);
112 /* Probably win95, so try without specifying the address. */
113 shared = (shared_info *) MapViewOfFileEx (shared_h,
114 FILE_MAP_READ|FILE_MAP_WRITE,
117 if (wincap.is_winnt ())
118 system_printf ("relocating shared object %s(%d) from %p to %p on Windows NT", name, n, addr, shared);
124 api_fatal ("MapViewOfFileEx '%s'(%p), %E. Terminating.", mapname, shared_h);
126 if (m == SH_USER_SHARED && offsets[0] && wincap.needs_memory_protection ())
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++)
132 unsigned size = offsets[i + 1] - offsets[i];
134 if (!VirtualAlloc (off_addr (i), size, MEM_RESERVE, PAGE_NOACCESS))
135 continue; /* oh well */
137 offsets[SH_TOTAL_SIZE] += delta;
140 debug_printf ("name %s, n %d, shared %p (wanted %p), h %p", mapname, n, shared, addr, shared_h);
146 user_shared_initialize (bool reinit)
148 char name[UNLEN + 1] = ""; /* Large enough for SID */
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;
160 cygheap->user.get_windows_id (name);
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);
170 DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &user_shared->version, USER_VERSION_MAGIC);
171 /* Initialize the Cygwin per-user shared, if necessary */
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);
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));
191 shared_info::initialize ()
193 DWORD sversion = (DWORD) InterlockedExchange ((LONG *) &version, SHARED_VERSION_MAGIC);
196 if (sversion != SHARED_VERSION_MAGIC)
198 InterlockedExchange ((LONG *) &version, sversion);
199 multiple_cygwin_problem ("system shared memory version", sversion, SHARED_VERSION_MAGIC);
202 low_priority_sleep (0); // Should be hit only very very rarely
209 tty.init (); /* Initialize tty table. */
210 cb = sizeof (*this); /* Do last, after all shared memory initialization */
213 if (cb != SHARED_INFO_CB)
214 system_printf ("size of shared memory region changed from %u to %u",
223 /* Initialize the Cygwin heap, if necessary */
227 cygheap->user.init ();
230 cygwin_shared = &cygwin_shared_area;
231 cygwin_shared->initialize ();
233 user_shared_initialize (false);
238 shared_info::heap_chunk_size ()
242 /* Fetch from registry, first user then local machine. */
243 for (int i = 0; i < 2; i++)
245 reg_key reg (i, KEY_READ, NULL);
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. */
252 if ((heap_chunk = reg.get_int ("heap_chunk_in_mb", 0)))
254 heap_chunk = 384; /* Default */
258 heap_chunk = 4 * 1024 * 1024;
262 heap_chunk = 384 * 1024 * 1024;
263 debug_printf ("fixed heap size is %u", heap_chunk);