1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
17
18 // interfaces for accessing shared memory segments
19
20 #if defined(_WIN32) && !defined(__STDWX_H__)
21 #include "boinc_win.h"
22 #elif defined(_WIN32) && defined(__STDWX_H__)
23 #include "stdwx.h"
24 #endif
25
26 #ifdef _MSC_VER
27 #define snprintf _snprintf
28 #endif
29
30 #ifdef __EMX__
31 #define INCL_DOS
32 #define INCL_DOSERRORS
33 #include <os2.h>
34 extern "C" int debug_printf(const char *fmt, ...);
35 #endif
36
37 #ifndef _WIN32
38 #include "config.h"
39 #include <cstdio>
40 #include <cstring>
41 #include <sys/types.h>
42 #include <assert.h>
43
44 #if HAVE_SYS_IPC_H
45 #include <sys/ipc.h>
46 #endif
47 #if HAVE_SYS_SHM_H
48 #if __FreeBSD__
49 #include <sys/param.h>
50 #endif
51 #include <sys/shm.h>
52 #endif
53 #endif
54
55 #if(!defined (_WIN32) && !defined (__EMX__))
56 #include <fcntl.h>
57 #include <unistd.h>
58 #include <sys/stat.h>
59 #include <sys/mman.h>
60
61 #include "error_numbers.h"
62
63 #include "shmem.h"
64
65 // MAP_FILE isn't defined on most operating systems, and even then, it
66 // is often defined just for the sake of compatibility. On those that
67 // don't define it, we will....
68 #ifndef MAP_FILE
69 #define MAP_FILE 0
70 #endif
71 #endif
72
73 #ifdef _USING_FCGI_
74 #include "boinc_fcgi.h"
75 using FCGI::fprintf;
76 #define perror FCGI::perror
77 #else
78 using std::fprintf;
79 using std::perror;
80 #endif
81
82
83 #ifdef _WIN32
84
create_shmem(LPCTSTR seg_name,int size,void ** pp,bool try_global)85 HANDLE create_shmem(LPCTSTR seg_name, int size, void** pp, bool try_global) {
86 HANDLE hMap = NULL;
87 DWORD dwError = 0;
88 DWORD dwRes = 0;
89 PSID pEveryoneSID = NULL;
90 PACL pACL = NULL;
91 PSECURITY_DESCRIPTOR pSD = NULL;
92 EXPLICIT_ACCESS ea;
93 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
94 SECURITY_ATTRIBUTES sa;
95 OSVERSIONINFO osvi;
96 char global_seg_name[256];
97
98 // Win9X doesn't like any reference to a security descriptor.
99 // So if we detect that we are running on the Win9X platform pass
100 // a NULL value for it.
101 //
102 osvi.dwOSVersionInfoSize = sizeof(osvi);
103 GetVersionEx(&osvi);
104 if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
105
106 hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, seg_name);
107 dwError = GetLastError();
108
109 } else {
110 // Create a well-known SID for the Everyone group.
111 if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
112 SECURITY_WORLD_RID,
113 0, 0, 0, 0, 0, 0, 0,
114 &pEveryoneSID)
115 ) {
116 fprintf(stderr, "AllocateAndInitializeSid Error %u\n", GetLastError());
117 goto Cleanup;
118 }
119
120 // Initialize an EXPLICIT_ACCESS structure for an ACE.
121 // The ACE will allow Everyone all access to the shared memory object.
122 ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
123 ea.grfAccessPermissions = FILE_MAP_ALL_ACCESS;
124 ea.grfAccessMode = SET_ACCESS;
125 ea.grfInheritance= NO_INHERITANCE;
126 ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
127 ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
128 ea.Trustee.ptstrName = (LPTSTR) pEveryoneSID;
129
130 // Create a new ACL that contains the new ACEs.
131 dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
132 if (ERROR_SUCCESS != dwRes) {
133 fprintf(stderr, "SetEntriesInAcl Error %u\n", GetLastError());
134 goto Cleanup;
135 }
136
137 // Initialize a security descriptor.
138 pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
139 if (NULL == pSD) {
140 fprintf(stderr, "LocalAlloc Error %u\n", GetLastError());
141 goto Cleanup;
142 }
143
144 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) {
145 fprintf(stderr, "InitializeSecurityDescriptor Error %u\n", GetLastError());
146 goto Cleanup;
147 }
148
149 // Add the ACL to the security descriptor.
150 if (!SetSecurityDescriptorDacl(pSD,
151 TRUE, // bDaclPresent flag
152 pACL,
153 FALSE) // not a default DACL
154 ) {
155 fprintf(stderr,
156 "SetSecurityDescriptorDacl Error %u\n", GetLastError()
157 );
158 goto Cleanup;
159 }
160
161 // Initialize a security attributes structure.
162 sa.nLength = sizeof (SECURITY_ATTRIBUTES);
163 sa.lpSecurityDescriptor = pSD;
164 sa.bInheritHandle = FALSE;
165
166 // Use the security attributes to set the security descriptor
167 // when you create a shared file mapping.
168
169 // Try using 'Global' so that it can cross terminal server sessions
170 // The 'Global' prefix must be included in the shared memory
171 // name if the shared memory segment is going to cross
172 // terminal server session boundaries.
173 //
174 if (try_global) {
175 snprintf(global_seg_name, sizeof(global_seg_name), "Global\\%s", seg_name);
176 hMap = CreateFileMapping(
177 INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0,
178 size, global_seg_name
179 );
180 dwError = GetLastError();
181 if (!hMap && (ERROR_ACCESS_DENIED == dwError)) {
182 // Couldn't use the 'Global' tag, so try the original name.
183 try_global = false;
184 }
185 }
186 if (!try_global) {
187 hMap = CreateFileMapping(
188 INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, size, seg_name
189 );
190 dwError = GetLastError();
191 }
192 }
193
194 if (hMap) {
195 if (GetLastError() == ERROR_ALREADY_EXISTS) {
196 CloseHandle(hMap);
197 hMap = NULL;
198 } else {
199 *pp = MapViewOfFile( hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
200 }
201 }
202
203 Cleanup:
204
205 if (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
206 if (pEveryoneSID)
207 FreeSid(pEveryoneSID);
208 if (pACL)
209 LocalFree(pACL);
210 if (pSD)
211 LocalFree(pSD);
212 }
213
214 return hMap;
215 }
216
attach_shmem(LPCTSTR seg_name,void ** pp)217 HANDLE attach_shmem(LPCTSTR seg_name, void** pp) {
218 HANDLE hMap;
219 char global_seg_name[256];
220
221 // The 'Global' prefix must be included in the shared memory
222 // name if the shared memory segment is going to cross
223 // terminal server session boundries.
224 //
225 snprintf(global_seg_name, sizeof(global_seg_name), "Global\\%s", seg_name);
226
227 // Try using 'Global' so that it can cross terminal server sessions
228 //
229 hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, global_seg_name);
230 if (!hMap) {
231 // Couldn't use the 'Global' tag, so just attempt to use the original
232 // name.
233 //
234 hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, seg_name);
235 }
236 if (!hMap) return NULL;
237
238 if (pp) *pp = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
239
240 return hMap;
241 }
242
detach_shmem(HANDLE hMap,void * p)243 int detach_shmem(HANDLE hMap, void* p) {
244 if (p) UnmapViewOfFile(p);
245 CloseHandle(hMap);
246
247 return 0;
248 }
249
250 #elif defined(__EMX__)
251
create_shmem(key_t key,int size,void ** pp)252 int create_shmem(key_t key, int size, void** pp) {
253 APIRET rc;
254 char buf[256];
255
256 snprintf(buf, sizeof(buf), "\\SHAREMEM\\BOINC\\%d", key);
257 //debug_printf( "create_shmem %s, %d, %p\n", buf, size, pp);
258 rc = DosAllocSharedMem(pp, (PSZ)buf, size, PAG_READ | PAG_WRITE | PAG_EXECUTE | PAG_COMMIT | OBJ_ANY);
259 if (rc == ERROR_ALREADY_EXISTS)
260 return attach_shmem( key, pp);
261 if (rc)
262 rc = DosAllocSharedMem(pp, (PSZ)buf, size, PAG_READ | PAG_WRITE | PAG_EXECUTE | PAG_COMMIT);
263
264 if (rc) {
265 //debug_printf( "DosAllocSharedMem %s failed, rc=%d\n", buf, rc);
266 return ERR_SHMGET;
267 }
268 //debug_printf( "create_shmem %p\n", *pp);
269 return 0;
270
271 }
272
destroy_shmem(key_t key)273 int destroy_shmem(key_t key){
274 APIRET rc;
275 void* pp;
276
277 //debug_printf( "destroy_shmem %d\n", key);
278 attach_shmem( key, &pp);
279 rc = DosFreeMem(pp);
280 if (rc) {
281 //debug_printf( "DosFreeMem %d failed, rc=%d\n", key, rc);
282 return ERR_SHMCTL;
283 }
284 //debug_printf( "destroy_shmem %d done\n", key);
285 return 0;
286 }
287
attach_shmem(key_t key,void ** pp)288 int attach_shmem(key_t key, void** pp){
289 APIRET rc;
290 char buf[256];
291
292 snprintf(buf, sizeof(buf), "\\SHAREMEM\\BOINC\\%d", key);
293 //debug_printf( "attach_shmem %s, %p\n", buf, pp);
294 rc = DosGetNamedSharedMem(pp, (PSZ) buf, PAG_READ | PAG_WRITE);
295 if (rc) {
296 //debug_printf( "DosGetNamedSharedMem %s failed, rc=%d\n", buf, rc);
297 return ERR_SHMAT;
298 }
299 //debug_printf( "attach_shmem %p\n", *pp);
300 return 0;
301 }
302
detach_shmem(void * p)303 int detach_shmem(void* p) {
304 /* dummy */
305 //debug_printf( "detach_shmem %p not supported\n", p);
306 return 0;
307 }
308
309 #else
310
311 // V6 mmap() shared memory for Unix/Linux/Mac
312 //
create_shmem_mmap(const char * path,size_t size,void ** pp)313 int create_shmem_mmap(const char *path, size_t size, void** pp) {
314 int fd, retval;
315 struct stat sbuf;
316
317 // Return NULL pointer if create_shmem fails
318 *pp = 0;
319 if (size == 0) return ERR_SHMGET;
320
321 // NOTE: in principle it should be 0660, not 0666
322 // (i.e. Apache should belong to the same group as the
323 // project admin user, and should therefore be able to access the seg.
324 // However, this doesn't seem to work on some Linux systems.
325 // I don't have time to figure this out (31 July 07)
326 // it's a big headache for anyone it affects,
327 // and it's not a significant security issue.
328 //
329 fd = open(path, O_RDWR | O_CREAT, 0666);
330 if (fd < 0) return ERR_SHMGET;
331
332 retval = fstat(fd, &sbuf);
333 if (retval) {
334 close(fd);
335 return ERR_SHMGET;
336 }
337 if (sbuf.st_size < (long)size) {
338 // The following 2 lines extend the file and clear its new
339 // area to all zeros because they write beyond the old EOF.
340 // See the lseek man page for details.
341 lseek(fd, size-1, SEEK_SET);
342 write(fd, "\0", 1);
343 }
344
345 *pp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
346
347 close(fd);
348
349 if (*pp == MAP_FAILED) {
350 *pp = 0;
351 return ERR_SHMGET;
352 }
353
354 return 0;
355 }
356
destroy_shmem_mmap(key_t)357 int destroy_shmem_mmap(key_t /*key*/){
358 return 0;
359 }
360
361
attach_shmem_mmap(const char * path,void ** pp)362 int attach_shmem_mmap(const char *path, void** pp) {
363 int fd, retval;
364 struct stat sbuf;
365
366 // Return NULL pointer if attach_shmem fails
367 *pp = 0;
368 fd = open(path, O_RDWR);
369 if (fd < 0) return ERR_SHMGET;
370
371 retval = fstat(fd, &sbuf);
372 if (retval) {
373 close(fd);
374 return ERR_SHMGET;
375 }
376 if (sbuf.st_size == 0) {
377 close(fd);
378 return ERR_SHMGET;
379 }
380
381 *pp = mmap(NULL, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
382
383 close(fd);
384
385 if (*pp == MAP_FAILED) {
386 *pp = 0;
387 return ERR_SHMGET;
388 }
389
390 return 0;
391 }
392
393
detach_shmem_mmap(void * p,size_t size)394 int detach_shmem_mmap(void* p, size_t size) {
395 return munmap((char *)p, size);
396 }
397
398 #if HAVE_SYS_SHM_H && !defined(ANDROID)
399
400 // Compatibility routines for Unix/Linux/Mac V5 applications
401 //
create_shmem(key_t key,int size,gid_t gid,void ** pp)402 int create_shmem(key_t key, int size, gid_t gid, void** pp) {
403 int id;
404
405 // try 0666, then SHM_R|SHM_W
406 // seems like some platforms require one or the other
407 // (this may be superstition)
408 //
409 // NOTE: in principle it should be 0660, not 0666
410 // (i.e. Apache should belong to the same group as the
411 // project admin user, and should therefore be able to access the seg.
412 // However, this doesn't seem to work on some Linux systems.
413 // I don't have time to figure this out (31 July 07)
414 // it's a big headache for anyone it affects,
415 // and it's not a significant security issue.
416 //
417 id = shmget(key, size, IPC_CREAT|0666);
418 if (id < 0) {
419 id = shmget(key, size, IPC_CREAT|SHM_R|SHM_W);
420 }
421 if (id < 0) {
422 perror("shmget");
423 fprintf(stderr, "shmem size: %d\n", size);
424 return ERR_SHMGET;
425 }
426
427 // set group ownership if requested
428 //
429 if (gid) {
430 int retval;
431 struct shmid_ds buf;
432 // Set the shmem segment's group ID
433 retval = shmctl(id, IPC_STAT, &buf);
434 if (retval) {
435 perror("shmget: shmctl STAT");
436 return ERR_SHMGET;
437 }
438 buf.shm_perm.gid = gid;
439 retval = shmctl(id, IPC_SET, &buf);
440 if (retval) {
441 perror("shmget: shmctl IPC_SET");
442 return ERR_SHMGET;
443 }
444 }
445 return attach_shmem(key, pp);
446 }
447
448 // Mark the shared memory segment so it will be released after
449 // the last attached process detaches or exits.
450 // On Mac OS X and some other systems, not doing this causes
451 // shared memory leaks if BOINC crashes or exits suddenly.
452 // On Mac OS X and some other systems, this command also
453 // prevents any more processes from attaching (by clearing
454 // the key in the shared memory structure), so BOINC does it
455 // only after we are completey done with the segment.
456 //
destroy_shmem(key_t key)457 int destroy_shmem(key_t key){
458 struct shmid_ds buf;
459 int id, retval;
460
461 id = shmget(key, 0, 0);
462 if (id < 0) return 0; // assume it doesn't exist
463 retval = shmctl(id, IPC_STAT, &buf);
464 if (retval) {
465 perror("shmctl STAT");
466 return ERR_SHMCTL;
467 }
468 retval = shmctl(id, IPC_RMID, 0);
469 if (retval) {
470 perror("shmctl RMID");
471 return ERR_SHMCTL;
472 }
473 return 0;
474 }
475
attach_shmem(key_t key,void ** pp)476 int attach_shmem(key_t key, void** pp){
477 void* p;
478 int id;
479
480 id = shmget(key, 0, 0);
481 if (id < 0) {
482 perror("shmget in attach_shmem");
483 return ERR_SHMGET;
484 }
485 p = shmat(id, 0, 0);
486 if ((long)p == -1) {
487 perror("shmat");
488 return ERR_SHMAT;
489 }
490 *pp = p;
491 return 0;
492 }
493
detach_shmem(void * p)494 int detach_shmem(void* p) {
495 int retval;
496 retval = shmdt((char *)p);
497 return retval;
498 }
499
print_shmem_info(key_t key)500 int print_shmem_info(key_t key) {
501 int id;
502 struct shmid_ds buf;
503
504 id = shmget(key, 0, 0);
505 if (id < 0) {
506 return ERR_SHMGET;
507 }
508 shmctl(id, IPC_STAT, &buf);
509 fprintf(
510 stderr, "shmem key: %x\t\tid: %d, size: %d, nattach: %d\n",
511 (unsigned int)key, id, (int)buf.shm_segsz, (int)buf.shm_nattch
512 );
513
514 return 0;
515 }
516
517 #else // HAVE_SYS_SHM_H
518
519 // Platforms that don't have sys/shm.h will need stubs,
520 // or alternate implementations
521
create_shmem(key_t,int size,gid_t gid,void **)522 int create_shmem(key_t, int size, gid_t gid, void**) {
523 perror("create_shmem: not supported on this platform");
524 return ERR_SHMGET;
525 }
attach_shmem(key_t,void **)526 int attach_shmem(key_t, void**) {
527 perror("attach_shmem: not supported on this platform");
528 return ERR_SHMGET;
529 }
detach_shmem(void *)530 int detach_shmem(void*) {
531 perror("detach_shmem: not supported on this platform");
532 return ERR_SHMGET;
533 }
destroy_shmem(key_t)534 int destroy_shmem(key_t) {
535 perror("destroy_shmem: not supported on this platform");
536 return ERR_SHMCTL;
537 }
538
539 #endif // !HAVE_SYS_SHM_H
540
541 #endif // !defined(_WIN32) && !defined(__EMX__)
542
543