1 /* shared.cc: shared data area support.
3 Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
20 #include "shared_info.h"
22 #include "cygwin_version.h"
25 #define SHAREDVER (unsigned)(cygwin_version.api_major << 16 | \
26 cygwin_version.api_minor)
28 shared_info NO_COPY *cygwin_shared = NULL;
30 /* The handle of the shared data area. */
31 HANDLE cygwin_shared_h = NULL;
33 /* General purpose security attribute objects for global use. */
34 SECURITY_ATTRIBUTES NO_COPY sec_none;
35 SECURITY_ATTRIBUTES NO_COPY sec_none_nih;
36 SECURITY_ATTRIBUTES NO_COPY sec_all;
37 SECURITY_ATTRIBUTES NO_COPY sec_all_nih;
40 shared_name (const char *str, int num)
42 static NO_COPY char buf[MAX_PATH] = {0};
45 __small_sprintf (buf, "%s.%s.%d", cygwin_version.shared_id, str, num);
46 if (GetEnvironmentVariable("CYGWIN_TESTING", envbuf, 5))
47 strcat(buf, cygwin_version.dll_build_date);
51 /* Open the shared memory map. */
53 open_shared_file_map ()
55 cygwin_shared = (shared_info *) open_shared ("shared",
57 sizeof (*cygwin_shared),
59 ProtectHandle (cygwin_shared);
63 open_shared (const char *name, HANDLE &shared_h, DWORD size, void *addr)
74 mapname = shared_name (name, 0);
75 shared_h = OpenFileMappingA (FILE_MAP_READ | FILE_MAP_WRITE,
79 !(shared_h = CreateFileMappingA ((HANDLE) 0xffffffff,
85 api_fatal ("CreateFileMappingA, %E. Terminating.");
88 shared = (shared_info *) MapViewOfFileEx (shared_h,
89 FILE_MAP_READ | FILE_MAP_WRITE,
94 /* Probably win95, so try without specifying the address. */
95 shared = (shared_info *) MapViewOfFileEx (shared_h,
96 FILE_MAP_READ|FILE_MAP_WRITE,
101 api_fatal ("MapViewOfFileEx, %E. Terminating.");
103 debug_printf ("name %s, shared %p, h %p", name, shared, shared_h);
105 /* FIXME: I couldn't find anywhere in the documentation a note about
106 whether the memory is initialized to zero. The code assumes it does
107 and since this part seems to be working, we'll leave it as is. */
112 shared_info::initialize ()
114 /* Ya, Win32 provides a way for a dll to watch when it's first loaded.
115 We may eventually want to use it but for now we have this. */
118 if (inited != SHAREDVER)
119 api_fatal ("Shared region version mismatch. Version %x != %x.\n"
120 "Are you using multiple versions of cygwin1.dll?\n"
121 "Run 'cygcheck -r -s -v' to find out.",
126 /* Initialize the mount table. */
129 /* Initialize the queue of deleted files. */
132 /* Initialize tty table. */
135 /* Fetch misc. registry entries. */
137 reg_key reg (KEY_READ, NULL);
139 /* Note that reserving a huge amount of heap space does not result in
140 swapping since we are not committing it. */
141 /* FIXME: We should not be restricted to a fixed size heap no matter
142 what the fixed size is. */
144 heap_chunk_in_mb = reg.get_int ("heap_chunk_in_mb", 128);
145 if (heap_chunk_in_mb < 4)
147 heap_chunk_in_mb = 4;
148 reg.set_int ("heap_chunk_in_mb", heap_chunk_in_mb);
157 open_shared_file_map ();
159 cygwin_shared->initialize ();
166 ForceCloseHandle (cygwin_shared_h);
170 shared_info::heap_chunk_size ()
172 return heap_chunk_in_mb << 20;
175 /* For apps that wish to access the shared data. */
180 return cygwin_shared;
184 * Function to return a common SECURITY_DESCRIPTOR * that
188 static NO_COPY SECURITY_DESCRIPTOR *null_sdp = 0;
190 SECURITY_DESCRIPTOR *__stdcall
193 static NO_COPY SECURITY_DESCRIPTOR sd;
197 InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
198 SetSecurityDescriptorDacl (&sd, TRUE, 0, FALSE);
204 extern PSID get_admin_sid ();
205 extern PSID get_system_sid ();
206 extern PSID get_creator_owner_sid ();
208 PSECURITY_ATTRIBUTES __stdcall
209 sec_user (PVOID sa_buf, PSID sid2, BOOL inherit)
212 return inherit ? &sec_none_nih : &sec_none;
214 PSECURITY_ATTRIBUTES psa = (PSECURITY_ATTRIBUTES) sa_buf;
215 PSECURITY_DESCRIPTOR psd = (PSECURITY_DESCRIPTOR)
216 ((char *) sa_buf + sizeof (*psa));
217 PACL acl = (PACL) ((char *) sa_buf + sizeof (*psa) + sizeof (*psd));
219 char sid_buf[MAX_SID_LEN];
220 PSID sid = (PSID) sid_buf;
222 if (myself->use_psid)
223 CopySid (MAX_SID_LEN, sid, myself->psid);
224 else if (! lookup_name (getlogin (), myself->logsrv, sid))
225 return inherit ? &sec_none_nih : &sec_none;
227 size_t acl_len = sizeof (ACL)
228 + 4 * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD))
230 + GetLengthSid (get_admin_sid ())
231 + GetLengthSid (get_system_sid ())
232 + GetLengthSid (get_creator_owner_sid ());
234 acl_len += sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD)
235 + GetLengthSid (sid2);
237 if (! InitializeAcl (acl, acl_len, ACL_REVISION))
238 debug_printf("InitializeAcl %E");
240 if (! AddAccessAllowedAce (acl, ACL_REVISION,
241 SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
243 debug_printf("AddAccessAllowedAce(%s) %E", getlogin());
245 if (! AddAccessAllowedAce (acl, ACL_REVISION,
246 SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
248 debug_printf("AddAccessAllowedAce(admin) %E");
250 if (! AddAccessAllowedAce (acl, ACL_REVISION,
251 SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
253 debug_printf("AddAccessAllowedAce(system) %E");
255 if (! AddAccessAllowedAce (acl, ACL_REVISION,
256 SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
257 get_creator_owner_sid ()))
258 debug_printf("AddAccessAllowedAce(creator_owner) %E");
261 if (! AddAccessAllowedAce (acl, ACL_REVISION,
262 SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
264 debug_printf("AddAccessAllowedAce(sid2) %E");
266 if (! InitializeSecurityDescriptor (psd,
267 SECURITY_DESCRIPTOR_REVISION))
268 debug_printf("InitializeSecurityDescriptor %E");
271 * Setting the owner lets the created security attribute not work
272 * on NT4 SP3 Server. Don't know why, but the function still does
273 * what it should do also if the owner isn't set.
276 if (! SetSecurityDescriptorOwner (psd, sid, FALSE))
277 debug_printf("SetSecurityDescriptorOwner %E");
280 if (! SetSecurityDescriptorDacl (psd, TRUE, acl, FALSE))
281 debug_printf("SetSecurityDescriptorDacl %E");
283 psa->nLength = sizeof (SECURITY_ATTRIBUTES);
284 psa->lpSecurityDescriptor = psd;
285 psa->bInheritHandle = inherit;
289 SECURITY_ATTRIBUTES *__stdcall
290 sec_user_nih (PVOID sa_buf, PSID sid2)
292 return sec_user (sa_buf, sid2, FALSE);