OSDN Git Service

Break out more header info into separate files. Use appropriate header files
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / shared.cc
1 /* shared.cc: shared data area support.
2
3    Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <grp.h>
16 #include <pwd.h>
17 #include "sync.h"
18 #include "sigproc.h"
19 #include "pinfo.h"
20 #include "shared_info.h"
21 #include "registry.h"
22 #include "cygwin_version.h"
23 #include "security.h"
24
25 #define SHAREDVER (unsigned)(cygwin_version.api_major << 16 | \
26                    cygwin_version.api_minor)
27
28 shared_info NO_COPY *cygwin_shared = NULL;
29
30 /* The handle of the shared data area.  */
31 HANDLE cygwin_shared_h = NULL;
32
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;
38
39 char * __stdcall
40 shared_name (const char *str, int num)
41 {
42   static NO_COPY char buf[MAX_PATH] = {0};
43   char envbuf[6];
44
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);
48   return buf;
49 }
50
51 /* Open the shared memory map.  */
52 static void __stdcall
53 open_shared_file_map ()
54 {
55   cygwin_shared = (shared_info *) open_shared ("shared",
56                                                cygwin_shared_h,
57                                                sizeof (*cygwin_shared),
58                                                (void *)0xa000000);
59   ProtectHandle (cygwin_shared);
60 }
61
62 void * __stdcall
63 open_shared (const char *name, HANDLE &shared_h, DWORD size, void *addr)
64 {
65   void *shared;
66
67   if (!shared_h)
68     {
69       char *mapname;
70       if (!name)
71         mapname = NULL;
72       else
73         {
74           mapname = shared_name (name, 0);
75           shared_h = OpenFileMappingA (FILE_MAP_READ | FILE_MAP_WRITE,
76                                        TRUE, mapname);
77         }
78       if (!shared_h &&
79           !(shared_h = CreateFileMappingA ((HANDLE) 0xffffffff,
80                                            &sec_all,
81                                            PAGE_READWRITE,
82                                            0,
83                                            size,
84                                            mapname)))
85         api_fatal ("CreateFileMappingA, %E.  Terminating.");
86     }
87
88   shared = (shared_info *) MapViewOfFileEx (shared_h,
89                                        FILE_MAP_READ | FILE_MAP_WRITE,
90                                        0, 0, 0, addr);
91
92   if (!shared)
93     {
94       /* Probably win95, so try without specifying the address.  */
95       shared = (shared_info *) MapViewOfFileEx (shared_h,
96                                        FILE_MAP_READ|FILE_MAP_WRITE,
97                                        0,0,0,0);
98     }
99
100   if (!shared)
101     api_fatal ("MapViewOfFileEx, %E.  Terminating.");
102
103   debug_printf ("name %s, shared %p, h %p", name, shared, shared_h);
104
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.  */
108   return shared;
109 }
110
111 void
112 shared_info::initialize ()
113 {
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.  */
116   if (inited)
117     {
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.",
122                    inited, SHAREDVER);
123       return;
124     }
125
126   /* Initialize the mount table.  */
127   mount.init ();
128
129   /* Initialize the queue of deleted files.  */
130   delqueue.init ();
131
132   /* Initialize tty table.  */
133   tty.init ();
134
135   /* Fetch misc. registry entries.  */
136
137   reg_key reg (KEY_READ, NULL);
138
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. */
143
144   heap_chunk_in_mb = reg.get_int ("heap_chunk_in_mb", 128);
145   if (heap_chunk_in_mb < 4)
146     {
147       heap_chunk_in_mb = 4;
148       reg.set_int ("heap_chunk_in_mb", heap_chunk_in_mb);
149     }
150
151   inited = SHAREDVER;
152 }
153
154 void __stdcall
155 shared_init ()
156 {
157   open_shared_file_map ();
158
159   cygwin_shared->initialize ();
160 }
161
162 void __stdcall
163 shared_terminate ()
164 {
165   if (cygwin_shared_h)
166     ForceCloseHandle (cygwin_shared_h);
167 }
168
169 unsigned
170 shared_info::heap_chunk_size ()
171 {
172   return heap_chunk_in_mb << 20;
173 }
174
175 /* For apps that wish to access the shared data.  */
176
177 shared_info *
178 cygwin_getshared ()
179 {
180   return cygwin_shared;
181 }
182
183 /*
184  * Function to return a common SECURITY_DESCRIPTOR * that
185  * allows all access.
186  */
187
188 static NO_COPY SECURITY_DESCRIPTOR *null_sdp = 0;
189
190 SECURITY_DESCRIPTOR *__stdcall
191 get_null_sd ()
192 {
193   static NO_COPY SECURITY_DESCRIPTOR sd;
194
195   if (null_sdp == 0)
196     {
197       InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
198       SetSecurityDescriptorDacl (&sd, TRUE, 0, FALSE);
199       null_sdp = &sd;
200     }
201   return null_sdp;
202 }
203
204 extern PSID get_admin_sid ();
205 extern PSID get_system_sid ();
206 extern PSID get_creator_owner_sid ();
207
208 PSECURITY_ATTRIBUTES __stdcall
209 sec_user (PVOID sa_buf, PSID sid2, BOOL inherit)
210 {
211   if (! sa_buf)
212     return inherit ? &sec_none_nih : &sec_none;
213
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));
218
219   char sid_buf[MAX_SID_LEN];
220   PSID sid = (PSID) sid_buf;
221
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;
226
227   size_t acl_len = sizeof (ACL)
228                    + 4 * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD))
229                    + GetLengthSid (sid)
230                    + GetLengthSid (get_admin_sid ())
231                    + GetLengthSid (get_system_sid ())
232                    + GetLengthSid (get_creator_owner_sid ());
233   if (sid2)
234     acl_len += sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD)
235                + GetLengthSid (sid2);
236
237   if (! InitializeAcl (acl, acl_len, ACL_REVISION))
238     debug_printf("InitializeAcl %E");
239
240   if (! AddAccessAllowedAce (acl, ACL_REVISION,
241                              SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
242                              sid))
243     debug_printf("AddAccessAllowedAce(%s) %E", getlogin());
244
245   if (! AddAccessAllowedAce (acl, ACL_REVISION,
246                              SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
247                              get_admin_sid ()))
248     debug_printf("AddAccessAllowedAce(admin) %E");
249
250   if (! AddAccessAllowedAce (acl, ACL_REVISION,
251                              SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
252                              get_system_sid ()))
253     debug_printf("AddAccessAllowedAce(system) %E");
254
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");
259
260   if (sid2)
261     if (! AddAccessAllowedAce (acl, ACL_REVISION,
262                                SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
263                                sid2))
264       debug_printf("AddAccessAllowedAce(sid2) %E");
265
266   if (! InitializeSecurityDescriptor (psd,
267                                       SECURITY_DESCRIPTOR_REVISION))
268     debug_printf("InitializeSecurityDescriptor %E");
269
270 /*
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.
274 */
275 #if 0
276   if (! SetSecurityDescriptorOwner (psd, sid, FALSE))
277     debug_printf("SetSecurityDescriptorOwner %E");
278 #endif
279
280   if (! SetSecurityDescriptorDacl (psd, TRUE, acl, FALSE))
281     debug_printf("SetSecurityDescriptorDacl %E");
282
283   psa->nLength = sizeof (SECURITY_ATTRIBUTES);
284   psa->lpSecurityDescriptor = psd;
285   psa->bInheritHandle = inherit;
286   return psa;
287 }
288
289 SECURITY_ATTRIBUTES *__stdcall
290 sec_user_nih (PVOID sa_buf, PSID sid2)
291 {
292   return sec_user (sa_buf, sid2, FALSE);
293 }
294