2005-01-20 23:22:22 +00:00
|
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-07-10 18:42:45 +00:00
|
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2003-07-10 18:42:45 +00:00
|
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
|
// 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
|
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-09-26 18:11:06 +00:00
|
|
|
|
|
|
|
|
|
// interfaces for accessing shared memory segments
|
|
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
2004-06-16 23:16:08 +00:00
|
|
|
|
#include "boinc_win.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-08-27 09:17:38 +00:00
|
|
|
|
#ifdef USE_FILE_MAPPED_SHMEM
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
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_
|
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
|
HANDLE create_shmem(LPCTSTR seg_name, int size, void** pp, bool disable_mapview) {
|
2003-03-18 19:37:09 +00:00
|
|
|
|
SECURITY_ATTRIBUTES security;
|
2004-09-01 04:59:32 +00:00
|
|
|
|
HANDLE hMap;
|
|
|
|
|
DWORD dwError = 0;
|
2003-06-20 21:10:37 +00:00
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
|
security.nLength = sizeof(security);
|
|
|
|
|
security.lpSecurityDescriptor = NULL;
|
|
|
|
|
security.bInheritHandle = TRUE;
|
|
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
|
hMap = CreateFileMapping(INVALID_HANDLE_VALUE, &security, PAGE_READWRITE, 0, size, seg_name);
|
|
|
|
|
dwError = GetLastError();
|
|
|
|
|
if (disable_mapview && (NULL != hMap) && (ERROR_ALREADY_EXISTS == dwError)) {
|
|
|
|
|
CloseHandle(hMap);
|
|
|
|
|
hMap = NULL;
|
|
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
|
if (!disable_mapview && (NULL != hMap) && 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HANDLE attach_shmem(LPCTSTR seg_name, void** pp) {
|
2004-09-01 04:59:32 +00:00
|
|
|
|
HANDLE hMap;
|
2003-03-18 19:37:09 +00:00
|
|
|
|
|
2004-09-01 04:59:32 +00:00
|
|
|
|
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, seg_name);
|
|
|
|
|
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
|
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
|
#ifdef USE_FILE_MAPPED_SHMEM
|
|
|
|
|
|
|
|
|
|
int create_shmem(char *path, size_t size, void** pp) {
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
// try 0666, then SHM_R|SHM_W
|
|
|
|
|
// seems like some platforms require one or the other
|
|
|
|
|
// (this may be superstition)
|
|
|
|
|
//
|
|
|
|
|
// 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);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return ERR_SHMGET;
|
|
|
|
|
|
|
|
|
|
*pp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
|
|
|
|
|
|
|
|
|
|
// Now close the file. The kernel doesn<73>t use our file descriptor.
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
if (*pp == MAP_FAILED)
|
|
|
|
|
return ERR_SHMGET;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int destroy_shmem(key_t key){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int detach_shmem(void* p, size_t size) {
|
|
|
|
|
int retval;
|
|
|
|
|
retval = shmdt((char *)p);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else // USE_FILE_MAPPED_SHMEM
|
|
|
|
|
|
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.
|
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) {
|
2006-06-06 18:45:40 +00:00
|
|
|
|
perror("shmget");
|
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
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
|
#endif // USE_FILE_MAPPED_SHMEM
|
|
|
|
|
|
2007-06-25 11:46:15 +00:00
|
|
|
|
// For debugging shared memory logic
|
|
|
|
|
// For testing on Apple, Linux, UNIX systems with limited number
|
|
|
|
|
// of shared memory segments per process and / or system-wide
|
|
|
|
|
// Mac OS X has a default limit of 8 segments per process, 32 system-wide
|
2007-06-26 03:41:32 +00:00
|
|
|
|
// If
|
|
|
|
|
void stress_shmem(short reduce_by) {
|
2007-08-27 09:17:38 +00:00
|
|
|
|
#ifndef USE_FILE_MAPPED_SHMEM
|
2007-06-25 11:46:15 +00:00
|
|
|
|
int retval;
|
2007-06-26 03:41:32 +00:00
|
|
|
|
void * shmaddr[16];
|
2007-06-27 20:09:18 +00:00
|
|
|
|
key_t key[] = {
|
|
|
|
|
'BNC0', 'BNC1', 'BNC2', 'BNC3', 'BNC4', 'BNC5', 'BNC6', 'BNC7',
|
|
|
|
|
'BNC8', 'BNC9', 'BNCA', 'BNCB', 'BNCC', 'BNCD', 'BNCE', 'BNCF'
|
|
|
|
|
};
|
2007-06-25 11:46:15 +00:00
|
|
|
|
int i, id;
|
|
|
|
|
|
2007-06-26 03:41:32 +00:00
|
|
|
|
if (reduce_by > 16) reduce_by = 16;
|
|
|
|
|
|
2007-06-25 11:46:15 +00:00
|
|
|
|
// Tie up 5 of the 8 shared memory segments each process may have
|
2007-06-26 03:41:32 +00:00
|
|
|
|
for (i=0; i<reduce_by; i++) {
|
2007-06-25 11:46:15 +00:00
|
|
|
|
retval = create_shmem(key[i], 1024, 0, &shmaddr[i]);
|
2007-06-26 03:41:32 +00:00
|
|
|
|
id = shmget(key[i], 0, 0);
|
|
|
|
|
// Mark it for automatic destruction when BOINC exits
|
|
|
|
|
if (id >= 0) {
|
|
|
|
|
retval = shmctl(id, IPC_RMID, 0);
|
2007-06-25 11:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-27 09:17:38 +00:00
|
|
|
|
#endif // !defined(USE_FILE_MAPPED_SHMEM)
|
2007-06-25 11:46:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-27 09:17:38 +00:00
|
|
|
|
#endif // !defined(_WIN32) && !defined(__EMX__)
|
2004-12-08 00:40:19 +00:00
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
|
const char *BOINC_RCSID_f835f078de = "$Id$";
|