2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-09-26 18:11:06 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-10 18:42:45 +00:00
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-07-10 18:42:45 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-10 18:42:45 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
// interfaces for accessing shared memory segments
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
int create_shmem(key_t key, int size, void** pp) {
|
2002-05-24 04:29:10 +00:00
|
|
|
int id;
|
2002-07-11 01:09:53 +00:00
|
|
|
assert(pp!=NULL);
|
2002-05-24 04:29:10 +00:00
|
|
|
id = shmget(key, size, IPC_CREAT|0777);
|
|
|
|
if (id < 0) {
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMGET;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
return attach_shmem(key, pp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2003-10-21 04:06:55 +00:00
|
|
|
if (retval) return ERR_SHMCTL;
|
2002-05-24 04:29:10 +00:00
|
|
|
retval = shmctl(id, IPC_RMID, 0);
|
|
|
|
if (retval) {
|
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;
|
2002-07-11 01:09:53 +00:00
|
|
|
assert(pp!=NULL);
|
2002-05-24 04:29:10 +00:00
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) {
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_SHMGET;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
|
|
|
p = shmat(id, 0, 0);
|
2003-10-21 04:06:55 +00:00
|
|
|
if ((long)p == ERR_SHMAT) {
|
|
|
|
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-07-11 01:09:53 +00:00
|
|
|
assert(p!=NULL);
|
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",
|
|
|
|
(unsigned int)key, id, buf.shm_segsz, (int)buf.shm_nattch
|
|
|
|
);
|
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
|
|
|
|
|
|
|
#endif
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_f835f078de = "$Id$";
|