1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*-------------------------------------------------------------------------
*
* shm.c
* System V Shared Memory Emulation
*
* Copyright (c) 1999, repas AEG Automation GmbH
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/port/qnx/Attic/shm.c,v 1.1 1999/12/16 01:25:06 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/shm.h>
#define MODE 0777
#define SHMMAX 1024
struct shm_info {
int shmid;
key_t key;
size_t size;
void *addr;
};
static struct shm_info *ShmInfo;
static int shm_putinfo( struct shm_info *info );
static int shm_updinfo( int i, struct shm_info *info );
static int shm_getinfo( int shmid, struct shm_info *info );
static int shm_getinfobyaddr( const void *addr, struct shm_info *info );
static int shm_putinfo( struct shm_info *info )
{
int i;
if( ShmInfo == NULL ) {
ShmInfo = calloc( SHMMAX, sizeof( struct shm_info ) );
if( ShmInfo == NULL ) return -1;
/* initialize ShmInfo */
for( i = 0; i < SHMMAX; i++ ) {
ShmInfo[i].shmid = -1;
}
}
/* search first free element */
i = 0;
while( i < SHMMAX && ShmInfo[i].shmid != -1 ) i++;
if( i >= SHMMAX ) {
errno = ENOSPC;
return -1;
}
memcpy( &ShmInfo[i], info, sizeof( struct shm_info ) );
return i;
}
static int shm_updinfo( int i, struct shm_info *info )
{
if( i >= SHMMAX ) return -1;
if( ShmInfo == NULL ) return -1;
memcpy( &ShmInfo[i], info, sizeof( struct shm_info ) );
return i;
}
static int shm_getinfo( int shmid, struct shm_info *info )
{
int i;
if( ShmInfo == NULL ) return -1;
/* search element */
i = 0;
while( i < SHMMAX && ShmInfo[i].shmid != shmid ) i++;
if( i >= SHMMAX ) return -1;
memcpy( info, &ShmInfo[i], sizeof( struct shm_info ) );
return i;
}
static int shm_getinfobyaddr( const void *addr, struct shm_info *info )
{
int i;
if( ShmInfo == ( struct shm_info * )-1 ) return -1;
/* search element */
i = 0;
while( i < SHMMAX && ShmInfo[i].addr != addr ) i++;
if( i >= SHMMAX ) return -1;
memcpy( info, &ShmInfo[i], sizeof( struct shm_info ) );
return i;
}
void *shmat( int shmid, const void *shmaddr, int shmflg )
{
struct shm_info info;
int i;
i = shm_getinfo( shmid, &info );
if( i == -1 ) {
errno = EACCES;
return ( void * )-1;
}
info.addr = mmap( ( void * )shmaddr, info.size,
PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0 );
if( info.addr == MAP_FAILED ) return info.addr;
if( shm_updinfo( i, &info ) == -1 ) {
errno = EACCES;
return ( void * )-1;
}
return info.addr;
}
int shmdt( const void *addr )
{
struct shm_info info;
if( shm_getinfobyaddr( addr, &info ) == -1 ) {
errno = EACCES;
return -1;
}
return munmap( ( void * )addr, info.size );
}
int shmctl( int shmid, int cmd, struct shmid_ds *buf )
{
struct shm_info info;
char name[NAME_MAX+1];
/* IPC_RMID supported only */
if( cmd != IPC_RMID ) {
errno = EINVAL;
return -1;
}
if( shm_getinfo( shmid, &info ) == -1 ) {
errno = EACCES;
return -1;
}
return shm_unlink( itoa( info.key, name, 16 ) );
}
int shmget( key_t key, size_t size, int flags )
{
char name[NAME_MAX+1];
int oflag = 0;
struct shm_info info;
if( flags & IPC_CREAT ) oflag |= O_CREAT;
if( flags & IPC_EXCL ) oflag |= O_EXCL;
if( flags & SHM_R ) {
if( flags & SHM_W ) oflag |= O_RDWR;
else oflag |= O_RDONLY;
}
info.shmid = shm_open( itoa( key, name, 16 ), oflag, MODE );
/* store shared memory information */
if( info.shmid != -1 ) {
info.key = key;
info.size = size;
info.addr = NULL;
if( shm_putinfo( &info ) == -1 ) return -1;
}
/* The size may only be set once. Ignore errors. */
ltrunc( info.shmid, size, SEEK_SET );
return info.shmid;
}
|