2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2003-07-10 18:42:45 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2003-07-10 18:42:45 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-09-26 18:11:06 +00:00
|
|
|
|
|
|
|
// interfaces for accessing shared memory segments
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
|
|
|
|
2005-12-02 22:29:35 +00:00
|
|
|
#ifdef __EMX__
|
|
|
|
#define INCL_DOS
|
|
|
|
#define INCL_DOSERRORS
|
|
|
|
#include <os2.h>
|
|
|
|
extern "C" int debug_printf(const char *fmt, ...);
|
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2003-10-02 00:24:39 +00:00
|
|
|
#include <sys/types.h>
|
2004-06-12 18:44:53 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#if HAVE_SYS_IPC_H
|
2002-05-24 04:29:10 +00:00
|
|
|
#include <sys/ipc.h>
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
2003-03-20 18:42:16 +00:00
|
|
|
#if HAVE_SYS_SHM_H
|
2003-08-13 21:35:26 +00:00
|
|
|
#if __FreeBSD__
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2003-03-20 18:42:16 +00:00
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2002-05-24 04:29:10 +00:00
|
|
|
|
2007-09-26 10:16:42 +00:00
|
|
|
#if(!defined (_WIN32) && !defined (__EMX__))
|
2007-08-27 09:17:38 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2007-08-27 11:34:32 +00:00
|
|
|
#include <sys/stat.h>
|
2007-08-27 09:17:38 +00:00
|
|
|
#include <sys/mman.h>
|
2008-05-11 23:11:16 +00:00
|
|
|
|
2008-04-01 21:44:12 +00:00
|
|
|
// MAP_FILE isn't defined on most operating systems, and even then, it
|
|
|
|
// is often defined just for the sake of compatibility. On those that
|
|
|
|
// don't define it, we will....
|
|
|
|
#ifndef MAP_FILE
|
|
|
|
#define MAP_FILE 0
|
|
|
|
#endif
|
2007-08-27 09:17:38 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
|
2003-10-21 04:06:55 +00:00
|
|
|
#include "error_numbers.h"
|
2002-05-24 04:29:10 +00:00
|
|
|
#include "shmem.h"
|
|
|
|
|
2004-07-03 21:38:15 +00:00
|
|
|
#ifdef _USING_FCGI_
|
2008-09-09 19:10:42 +00:00
|
|
|
#include "boinc_fcgi.h"
|
2009-02-26 00:23:23 +00:00
|
|
|
using FCGI::fprintf;
|
|
|
|
#define perror FCGI::perror
|
|
|
|
#else
|
|
|
|
using std::fprintf;
|
|
|
|
using std::perror;
|
2004-07-03 21:38:15 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-26 00:23:23 +00:00
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2008-06-20 21:33:02 +00:00
|
|
|
HANDLE create_shmem(LPCTSTR seg_name, int size, void** pp, bool try_global) {
|
2008-03-12 18:23:48 +00:00
|
|
|
HANDLE hMap = NULL;
|
2008-03-14 02:41:56 +00:00
|
|
|
DWORD dwError = 0;
|
|
|
|
DWORD dwRes = 0;
|
2008-03-12 18:23:48 +00:00
|
|
|
PSID pEveryoneSID = NULL;
|
|
|
|
PACL pACL = NULL;
|
|
|
|
PSECURITY_DESCRIPTOR pSD = NULL;
|
|
|
|
EXPLICIT_ACCESS ea;
|
|
|
|
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
2008-05-08 20:10:20 +00:00
|
|
|
OSVERSIONINFO osvi;
|
2008-03-14 18:46:18 +00:00
|
|
|
char global_seg_name[256];
|
2003-06-20 21:10:37 +00:00
|
|
|
|
2008-05-11 23:11:16 +00:00
|
|
|
// Win9X doesn't like any reference to a security descriptor.
|
|
|
|
// So if we detect that we are running on the Win9X platform pass
|
|
|
|
// a NULL value for it.
|
|
|
|
//
|
2008-05-08 20:10:20 +00:00
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2008-05-08 20:10:20 +00:00
|
|
|
hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, seg_name);
|
|
|
|
dwError = GetLastError();
|
2008-03-14 02:41:56 +00:00
|
|
|
|
2008-05-08 20:10:20 +00:00
|
|
|
} else {
|
|
|
|
// Create a well-known SID for the Everyone group.
|
|
|
|
if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
|
2008-05-11 23:11:16 +00:00
|
|
|
SECURITY_WORLD_RID,
|
|
|
|
0, 0, 0, 0, 0, 0, 0,
|
|
|
|
&pEveryoneSID)
|
|
|
|
) {
|
2008-05-08 20:10:20 +00:00
|
|
|
fprintf(stderr, "AllocateAndInitializeSid Error %u\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize an EXPLICIT_ACCESS structure for an ACE.
|
|
|
|
// The ACE will allow Everyone all access to the shared memory object.
|
|
|
|
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
|
|
|
|
ea.grfAccessPermissions = FILE_MAP_ALL_ACCESS;
|
|
|
|
ea.grfAccessMode = SET_ACCESS;
|
|
|
|
ea.grfInheritance= NO_INHERITANCE;
|
|
|
|
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
|
|
|
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
|
|
|
|
ea.Trustee.ptstrName = (LPTSTR) pEveryoneSID;
|
|
|
|
|
|
|
|
// Create a new ACL that contains the new ACEs.
|
|
|
|
dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
|
2008-05-12 20:29:57 +00:00
|
|
|
if (ERROR_SUCCESS != dwRes) {
|
2008-05-08 20:10:20 +00:00
|
|
|
fprintf(stderr, "SetEntriesInAcl Error %u\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a security descriptor.
|
|
|
|
pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
|
2008-05-11 23:11:16 +00:00
|
|
|
if (NULL == pSD) {
|
2008-05-08 20:10:20 +00:00
|
|
|
fprintf(stderr, "LocalAlloc Error %u\n", GetLastError());
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:11:16 +00:00
|
|
|
if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) {
|
|
|
|
fprintf(stderr, "InitializeSecurityDescriptor Error %u\n",
|
|
|
|
GetLastError()
|
|
|
|
);
|
2008-05-08 20:10:20 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the ACL to the security descriptor.
|
|
|
|
if (!SetSecurityDescriptorDacl(pSD,
|
|
|
|
TRUE, // bDaclPresent flag
|
|
|
|
pACL,
|
2008-05-11 23:11:16 +00:00
|
|
|
FALSE) // not a default DACL
|
|
|
|
) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"SetSecurityDescriptorDacl Error %u\n", GetLastError()
|
|
|
|
);
|
2008-05-08 20:10:20 +00:00
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a security attributes structure.
|
|
|
|
sa.nLength = sizeof (SECURITY_ATTRIBUTES);
|
|
|
|
sa.lpSecurityDescriptor = pSD;
|
|
|
|
sa.bInheritHandle = FALSE;
|
|
|
|
|
|
|
|
// Use the security attributes to set the security descriptor
|
|
|
|
// when you create a shared file mapping.
|
|
|
|
|
2008-06-20 21:33:02 +00:00
|
|
|
// Try using 'Global' so that it can cross terminal server sessions
|
2008-05-08 20:10:20 +00:00
|
|
|
// The 'Global' prefix must be included in the shared memory
|
|
|
|
// name if the shared memory segment is going to cross
|
2008-06-20 21:33:02 +00:00
|
|
|
// terminal server session boundaries.
|
2008-05-08 20:10:20 +00:00
|
|
|
//
|
2008-06-20 21:33:02 +00:00
|
|
|
if (try_global) {
|
|
|
|
sprintf(global_seg_name, "Global\\%s", seg_name);
|
|
|
|
hMap = CreateFileMapping(
|
|
|
|
INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0,
|
|
|
|
size, global_seg_name
|
|
|
|
);
|
|
|
|
dwError = GetLastError();
|
|
|
|
if (!hMap && (ERROR_ACCESS_DENIED == dwError)) {
|
|
|
|
// Couldn't use the 'Global' tag, so try the original name.
|
|
|
|
try_global = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!try_global) {
|
|
|
|
hMap = CreateFileMapping(
|
|
|
|
INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, size, seg_name
|
|
|
|
);
|
2008-05-08 20:10:20 +00:00
|
|
|
dwError = GetLastError();
|
|
|
|
}
|
2008-03-14 02:41:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 20:29:57 +00:00
|
|
|
if (hMap) {
|
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS) {
|
|
|
|
CloseHandle(hMap);
|
|
|
|
hMap = NULL;
|
|
|
|
} else {
|
|
|
|
*pp = MapViewOfFile( hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
|
|
|
|
}
|
2004-09-01 04:59:32 +00:00
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2008-03-12 18:23:48 +00:00
|
|
|
Cleanup:
|
|
|
|
|
2008-05-08 20:10:20 +00:00
|
|
|
if (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
|
|
|
|
if (pEveryoneSID)
|
|
|
|
FreeSid(pEveryoneSID);
|
|
|
|
if (pACL)
|
|
|
|
LocalFree(pACL);
|
|
|
|
if (pSD)
|
|
|
|
LocalFree(pSD);
|
|
|
|
}
|
2008-03-12 18:23:48 +00:00
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
return hMap;
|
2003-03-18 19:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE attach_shmem(LPCTSTR seg_name, void** pp) {
|
2004-09-01 04:59:32 +00:00
|
|
|
HANDLE hMap;
|
2008-03-14 18:46:18 +00:00
|
|
|
char global_seg_name[256];
|
2008-03-14 02:41:56 +00:00
|
|
|
|
|
|
|
// The 'Global' prefix must be included in the shared memory
|
|
|
|
// name if the shared memory segment is going to cross
|
|
|
|
// terminal server session boundries.
|
|
|
|
//
|
2008-03-14 18:46:18 +00:00
|
|
|
sprintf(global_seg_name, "Global\\%s", seg_name);
|
2008-03-14 02:41:56 +00:00
|
|
|
|
|
|
|
// Try using 'Global' so that it can cross terminal server sessions
|
|
|
|
//
|
|
|
|
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, global_seg_name);
|
|
|
|
if (!hMap) {
|
|
|
|
// Couldn't use the 'Global' tag, so just attempt to use the original
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, seg_name);
|
|
|
|
}
|
2004-09-01 04:59:32 +00:00
|
|
|
if (!hMap) return NULL;
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
if (pp) *pp = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
return hMap;
|
2003-03-18 19:37:09 +00:00
|
|
|
}
|
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
int detach_shmem(HANDLE hMap, void* p) {
|
2004-08-31 19:15:17 +00:00
|
|
|
if (p) UnmapViewOfFile(p);
|
2004-09-01 04:59:32 +00:00
|
|
|
CloseHandle(hMap);
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2003-06-06 22:08:46 +00:00
|
|
|
return 0;
|
2003-03-18 19:37:09 +00:00
|
|
|
}
|
|
|
|
|
2005-12-02 22:29:35 +00:00
|
|
|
#elif defined(__EMX__)
|
|
|
|
|
|
|
|
int create_shmem(key_t key, int size, void** pp) {
|
|
|
|
APIRET rc;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
sprintf(buf, "\\SHAREMEM\\BOINC\\%d", key);
|
|
|
|
//debug_printf( "create_shmem %s, %d, %p\n", buf, size, pp);
|
|
|
|
rc = DosAllocSharedMem(pp, (PSZ)buf, size, PAG_READ | PAG_WRITE | PAG_EXECUTE | PAG_COMMIT | OBJ_ANY);
|
|
|
|
if (rc == ERROR_ALREADY_EXISTS)
|
|
|
|
return attach_shmem( key, pp);
|
|
|
|
if (rc)
|
|
|
|
rc = DosAllocSharedMem(pp, (PSZ)buf, size, PAG_READ | PAG_WRITE | PAG_EXECUTE | PAG_COMMIT);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
//debug_printf( "DosAllocSharedMem %s failed, rc=%d\n", buf, rc);
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
//debug_printf( "create_shmem %p\n", *pp);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int destroy_shmem(key_t key){
|
|
|
|
APIRET rc;
|
|
|
|
void* pp;
|
|
|
|
|
|
|
|
//debug_printf( "destroy_shmem %d\n", key);
|
|
|
|
attach_shmem( key, &pp);
|
|
|
|
rc = DosFreeMem(pp);
|
|
|
|
if (rc) {
|
|
|
|
//debug_printf( "DosFreeMem %d failed, rc=%d\n", key, rc);
|
|
|
|
return ERR_SHMCTL;
|
|
|
|
}
|
|
|
|
//debug_printf( "destroy_shmem %d done\n", key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attach_shmem(key_t key, void** pp){
|
|
|
|
APIRET rc;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
sprintf(buf, "\\SHAREMEM\\BOINC\\%d", key);
|
|
|
|
//debug_printf( "attach_shmem %s, %p\n", buf, pp);
|
|
|
|
rc = DosGetNamedSharedMem(pp, (PSZ) buf, PAG_READ | PAG_WRITE);
|
|
|
|
if (rc) {
|
|
|
|
//debug_printf( "DosGetNamedSharedMem %s failed, rc=%d\n", buf, rc);
|
|
|
|
return ERR_SHMAT;
|
|
|
|
}
|
|
|
|
//debug_printf( "attach_shmem %p\n", *pp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int detach_shmem(void* p) {
|
|
|
|
/* dummy */
|
|
|
|
//debug_printf( "detach_shmem %p not supported\n", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#else
|
|
|
|
|
2008-03-27 18:25:29 +00:00
|
|
|
// V6 mmap() shared memory for Unix/Linux/Mac
|
|
|
|
//
|
- server code: at some point I made a global var "SCHED_CONFIG config",
mostly so that the parse function could assume
that everything was initially zero.
However, various back-end functions pass around SCHED_CONFIG&
as an argument (also named "config").
This creates a shadow, which is always bad.
Worse is the possibility that some projects have back-end programs
that have a SCHED_CONFIG variable that's automatic,
and therefore isn't zero initially,
and therefore isn't parsing correctly.
To fix this, I changed the 2 vectors in SCHED_CONFIG into pointers,
and have the parse routine zero the structure.
I was tempted to remove the SCHED_CONFIG& args to back-end functions,
but this would have broken some projects' code.
I did, however, change the name from config to config_loc
to avoid shadowing.
Also fixed various other compiler warnings.
svn path=/trunk/boinc/; revision=15541
2008-07-02 17:24:53 +00:00
|
|
|
int create_shmem_mmap(const char *path, size_t size, void** pp) {
|
2007-08-27 11:34:32 +00:00
|
|
|
int fd, retval;
|
|
|
|
struct stat sbuf;
|
2007-08-27 09:17:38 +00:00
|
|
|
|
2007-09-04 10:33:50 +00:00
|
|
|
// Return NULL pointer if create_shmem fails
|
|
|
|
*pp = 0;
|
|
|
|
if (size == 0) return ERR_SHMGET;
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
// NOTE: in principle it should be 0660, not 0666
|
|
|
|
// (i.e. Apache should belong to the same group as the
|
|
|
|
// project admin user, and should therefore be able to access the seg.
|
|
|
|
// However, this doesn't seem to work on some Linux systems.
|
|
|
|
// I don't have time to figure this out (31 July 07)
|
|
|
|
// it's a big headache for anyone it affects,
|
|
|
|
// and it's not a significant security issue.
|
|
|
|
//
|
|
|
|
fd = open(path, O_RDWR | O_CREAT, 0666);
|
2007-09-04 07:41:25 +00:00
|
|
|
if (fd < 0) return ERR_SHMGET;
|
2007-08-27 09:17:38 +00:00
|
|
|
|
2007-08-27 11:34:32 +00:00
|
|
|
retval = fstat(fd, &sbuf);
|
2009-10-01 20:02:06 +00:00
|
|
|
if (retval) {
|
|
|
|
close(fd);
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
2007-10-25 09:34:16 +00:00
|
|
|
if (sbuf.st_size < (long)size) {
|
2007-09-04 07:41:25 +00:00
|
|
|
// The following 2 lines extend the file and clear its new
|
|
|
|
// area to all zeros because they write beyond the old EOF.
|
|
|
|
// See the lseek man page for details.
|
|
|
|
lseek(fd, size-1, SEEK_SET);
|
|
|
|
write(fd, "\0", 1);
|
2007-08-27 11:34:32 +00:00
|
|
|
}
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
*pp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2007-09-04 07:41:25 +00:00
|
|
|
if (*pp == MAP_FAILED) {
|
|
|
|
*pp = 0;
|
2007-08-27 09:17:38 +00:00
|
|
|
return ERR_SHMGET;
|
2007-09-04 07:41:25 +00:00
|
|
|
}
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-26 10:16:42 +00:00
|
|
|
int destroy_shmem_mmap(key_t key){
|
2007-08-27 09:17:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-01 21:44:12 +00:00
|
|
|
|
- server code: at some point I made a global var "SCHED_CONFIG config",
mostly so that the parse function could assume
that everything was initially zero.
However, various back-end functions pass around SCHED_CONFIG&
as an argument (also named "config").
This creates a shadow, which is always bad.
Worse is the possibility that some projects have back-end programs
that have a SCHED_CONFIG variable that's automatic,
and therefore isn't zero initially,
and therefore isn't parsing correctly.
To fix this, I changed the 2 vectors in SCHED_CONFIG into pointers,
and have the parse routine zero the structure.
I was tempted to remove the SCHED_CONFIG& args to back-end functions,
but this would have broken some projects' code.
I did, however, change the name from config to config_loc
to avoid shadowing.
Also fixed various other compiler warnings.
svn path=/trunk/boinc/; revision=15541
2008-07-02 17:24:53 +00:00
|
|
|
int attach_shmem_mmap(const char *path, void** pp) {
|
2007-09-04 07:41:25 +00:00
|
|
|
int fd, retval;
|
|
|
|
struct stat sbuf;
|
|
|
|
|
2007-09-04 10:33:50 +00:00
|
|
|
// Return NULL pointer if attach_shmem fails
|
2007-09-20 11:21:35 +00:00
|
|
|
*pp = 0;
|
|
|
|
fd = open(path, O_RDWR);
|
2007-09-04 07:41:25 +00:00
|
|
|
if (fd < 0) return ERR_SHMGET;
|
|
|
|
|
|
|
|
retval = fstat(fd, &sbuf);
|
2009-10-01 20:02:06 +00:00
|
|
|
if (retval) {
|
2009-10-01 23:21:03 +00:00
|
|
|
close(fd);
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
if (sbuf.st_size == 0) {
|
|
|
|
close(fd);
|
2009-10-01 20:02:06 +00:00
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
2007-09-04 07:41:25 +00:00
|
|
|
|
|
|
|
*pp = mmap(NULL, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (*pp == MAP_FAILED) {
|
|
|
|
*pp = 0;
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-26 10:16:42 +00:00
|
|
|
int detach_shmem_mmap(void* p, size_t size) {
|
2009-02-26 00:23:23 +00:00
|
|
|
return munmap((char *)p, size);
|
2007-08-27 09:17:38 +00:00
|
|
|
}
|
|
|
|
|
2009-05-28 03:54:47 +00:00
|
|
|
#if HAVE_SYS_SHM_H
|
|
|
|
|
2007-09-26 10:16:42 +00:00
|
|
|
// Compatibility routines for Unix/Linux/Mac V5 applications
|
2008-03-27 18:25:29 +00:00
|
|
|
//
|
2006-06-09 23:57:41 +00:00
|
|
|
int create_shmem(key_t key, int size, gid_t gid, void** pp) {
|
2002-05-24 04:29:10 +00:00
|
|
|
int id;
|
2006-06-09 23:57:41 +00:00
|
|
|
|
2007-08-01 15:35:08 +00:00
|
|
|
// try 0666, then SHM_R|SHM_W
|
2006-06-10 05:27:28 +00:00
|
|
|
// seems like some platforms require one or the other
|
|
|
|
// (this may be superstition)
|
|
|
|
//
|
2007-08-01 15:35:08 +00:00
|
|
|
// NOTE: in principle it should be 0660, not 0666
|
|
|
|
// (i.e. Apache should belong to the same group as the
|
|
|
|
// project admin user, and should therefore be able to access the seg.
|
|
|
|
// However, this doesn't seem to work on some Linux systems.
|
|
|
|
// I don't have time to figure this out (31 July 07)
|
|
|
|
// it's a big headache for anyone it affects,
|
|
|
|
// and it's not a significant security issue.
|
|
|
|
//
|
2007-02-22 09:33:08 +00:00
|
|
|
id = shmget(key, size, IPC_CREAT|0666);
|
2005-07-23 08:10:39 +00:00
|
|
|
if (id < 0) {
|
2006-06-10 05:27:28 +00:00
|
|
|
id = shmget(key, size, IPC_CREAT|SHM_R|SHM_W);
|
|
|
|
}
|
|
|
|
if (id < 0) {
|
|
|
|
perror("shmget");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set group ownership if requested
|
|
|
|
//
|
|
|
|
if (gid) {
|
|
|
|
int retval;
|
|
|
|
struct shmid_ds buf;
|
2006-06-09 23:57:41 +00:00
|
|
|
// Set the shmem segment's group ID
|
|
|
|
retval = shmctl(id, IPC_STAT, &buf);
|
|
|
|
if (retval) {
|
|
|
|
perror("shmget: shmctl STAT");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
buf.shm_perm.gid = gid;
|
|
|
|
retval = shmctl(id, IPC_SET, &buf);
|
|
|
|
if (retval) {
|
|
|
|
perror("shmget: shmctl IPC_SET");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
return attach_shmem(key, pp);
|
|
|
|
}
|
|
|
|
|
2007-06-27 10:29:31 +00:00
|
|
|
// Mark the shared memory segment so it will be released after
|
|
|
|
// the last attached process detaches or exits.
|
|
|
|
// On Mac OS X and some other systems, not doing this causes
|
|
|
|
// shared memory leaks if BOINC crashes or exits suddenly.
|
|
|
|
// On Mac OS X and some other systems, this command also
|
|
|
|
// prevents any more processes from attaching (by clearing
|
|
|
|
// the key in the shared memory structure), so BOINC does it
|
2007-06-28 11:09:07 +00:00
|
|
|
// only after we are completey done with the segment.
|
2008-03-27 18:25:29 +00:00
|
|
|
//
|
2002-05-24 04:29:10 +00:00
|
|
|
int destroy_shmem(key_t key){
|
|
|
|
struct shmid_ds buf;
|
|
|
|
int id, retval;
|
|
|
|
|
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) return 0; // assume it doesn't exist
|
|
|
|
retval = shmctl(id, IPC_STAT, &buf);
|
2006-06-06 18:45:40 +00:00
|
|
|
if (retval) {
|
|
|
|
perror("shmctl STAT");
|
|
|
|
return ERR_SHMCTL;
|
|
|
|
}
|
2002-05-24 04:29:10 +00:00
|
|
|
retval = shmctl(id, IPC_RMID, 0);
|
|
|
|
if (retval) {
|
2006-06-06 18:45:40 +00:00
|
|
|
perror("shmctl RMID");
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMCTL;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attach_shmem(key_t key, void** pp){
|
|
|
|
void* p;
|
|
|
|
int id;
|
2005-07-23 08:10:39 +00:00
|
|
|
|
2002-05-24 04:29:10 +00:00
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) {
|
2008-03-27 18:25:29 +00:00
|
|
|
perror("shmget in attach_shmem");
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMGET;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
p = shmat(id, 0, 0);
|
2005-12-03 07:51:04 +00:00
|
|
|
if ((long)p == -1) {
|
2006-06-06 18:45:40 +00:00
|
|
|
perror("shmat");
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMAT;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
*pp = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
int detach_shmem(void* p) {
|
2002-05-24 04:29:10 +00:00
|
|
|
int retval;
|
2002-06-03 19:15:19 +00:00
|
|
|
retval = shmdt((char *)p);
|
2002-05-24 04:29:10 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2003-03-17 19:24:38 +00:00
|
|
|
|
2004-08-20 19:52:34 +00:00
|
|
|
int print_shmem_info(key_t key) {
|
2003-03-17 19:24:38 +00:00
|
|
|
int id;
|
|
|
|
struct shmid_ds buf;
|
2003-06-20 21:10:37 +00:00
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) {
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMGET;
|
2003-03-17 19:24:38 +00:00
|
|
|
}
|
|
|
|
shmctl(id, IPC_STAT, &buf);
|
2004-08-20 19:52:34 +00:00
|
|
|
fprintf(
|
|
|
|
stderr, "shmem key: %x\t\tid: %d, size: %d, nattach: %d\n",
|
2006-02-07 01:08:02 +00:00
|
|
|
(unsigned int)key, id, (int)buf.shm_segsz, (int)buf.shm_nattch
|
2004-08-20 19:52:34 +00:00
|
|
|
);
|
2003-06-20 21:10:37 +00:00
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
|
2009-05-28 03:54:47 +00:00
|
|
|
#else // HAVE_SYS_SHM_H
|
|
|
|
|
|
|
|
// Platforms that don't have sys/shm.h will need stubs,
|
|
|
|
// or alternate implementations
|
|
|
|
|
|
|
|
int create_shmem(key_t, int size, gid_t gid, void**) {
|
|
|
|
perror("create_shmem: not supported on this platform");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
int attach_shmem(key_t, void**) {
|
|
|
|
perror("attach_shmem: not supported on this platform");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
int detach_shmem(void*) {
|
|
|
|
perror("detach_shmem: not supported on this platform");
|
|
|
|
return ERR_SHMGET;
|
|
|
|
}
|
|
|
|
int destroy_shmem(key_t) {
|
|
|
|
perror("destroy_shmem: not supported on this platform");
|
|
|
|
return ERR_SHMCTL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !HAVE_SYS_SHM_H
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
#endif // !defined(_WIN32) && !defined(__EMX__)
|
2004-12-08 00:40:19 +00:00
|
|
|
|