sharelite_shm.c

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 173 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sharelite.c ↑ Summary
1  
2 /*
3 * sharelite_shm.c - part of IPC::Shm::Simple
4 *
5 * Derived from parts of IPC::ShareLite by Maurice Aubrey.
6 *
7 * Copyright (c) 2/2005 by Kevin Cody-Little <kcody@cpan.org>
8 *
9 * This code may be modified or redistributed under the terms
10 * of either the Artistic or GNU General Public licenses, at
11 * the modifier or redistributor's discretion.
12 *
13 */
14  
15 #include <stdlib.h>
16 #include <errno.h>
17  
18 #include <sys/types.h>
19 #include <sys/ipc.h>
20 #include <sys/shm.h>
21  
22 #include "sharelite.h"
23 #include "sharelite_shm.h"
24  
25 /* --- SHARED SEGMENT NODE FUNCTIONS --- */
26  
27 /* shmat to an existing segment shmid and return a valid Node structure */
28 0 Node *_shmseg_shmat( int shmid ) {
29 char *shmaddr;
30 Node *node;
31  
32 0 ▶ if ( ( shmaddr = shmat( shmid, (char *) NULL, 0 ) ) == (void *) -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmaddr = shmat( shmid, (char *) NULL, 0 ) ) == (void *) -1 ) 0 0 0
33 0 return NULL;
34  
35 0 ▶ if ( ( node = malloc( sizeof( Node ) ) ) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = malloc( sizeof( Node ) ) ) == NULL ) { 0 0 0
36 0 shmid = errno;
37 0 shmdt( shmaddr );
38 0 errno = shmid; /* does malloc define this? */
39 0 return NULL;
40 }
41  
42 0 node->shmid = shmid;
43 0 node->shmhead = (Header *) shmaddr;
44 0 node->shmdata = shmaddr + sizeof( Header );
45  
46 0 node->next = NULL;
47 0 node->shminfo = NULL;
48  
49 0 return node;
50 }
51  
52 /* shmdt from a segment and free its Node structure, possibly remove segment */
53 0 int _shmseg_shmdt( Node *node, int remove ) {
54  
55 0 ▶ if ( remove ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( remove ) { 0 0 0
56  
57 0 node->shminfo->data_chunks--;
58  
59 0 ▶ if ( shmctl( node->shmid, IPC_RMID, NULL ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( shmctl( node->shmid, IPC_RMID, NULL ) == -1 ) 0 0 0
60 0 ▶ if ( shmctl( node->shmid, IPC_RMID, NULL ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( shmctl( node->shmid, IPC_RMID, NULL ) == -1 ) 0 0 0
61 0 return -1;
62  
63 }
64  
65 0 ▶ if ( shmdt( node->shmhead ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( shmdt( node->shmhead ) == -1 ) 0 0 0
66 0 return -1;
67  
68 0 free( node );
69  
70 0 return 0;
71 }
72  
73 /* allocate and zero a new segment and return a valid Node structure */
74 0 Node *_shmseg_alloc( key_t key, int size, int flags, int is_top_node ) {
75 Node *node;
76 int myflags, shmid;
77  
78 0 flags = flags & 0x01FF; /* only want lower nine bits - perms */
79 0 myflags = flags | IPC_CREAT | IPC_EXCL;
80  
81 0 ▶ if ( ( shmid = shmget( key, size, myflags ) ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmid = shmget( key, size, myflags ) ) == -1 ) 0 0 0
82 0 return NULL;
83  
84 0 ▶ if ( ( node = _shmseg_shmat( shmid ) ) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _shmseg_shmat( shmid ) ) == NULL ) { 0 0 0
85 0 shmctl( shmid, IPC_RMID, NULL );
86 0 return NULL;
87 }
88  
89 0 node->shmhead->shm_magic = SHARELITE_MAGIC;
90 0 node->shmhead->next_shmid = -1;
91  
92 /* FIXME: fetch actual size via shmctl */
93  
94 0 ▶ if ( is_top_node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( is_top_node ) { 0 0 0
95  
96 0 node->shminfo = (Descriptor *) node->shmdata;
97 0 node->shmdata += sizeof( Descriptor );
98  
99 0 node->shminfo->seg_perms = flags;
100 0 node->shminfo->size_topseg = size;
101 0 node->shminfo->size_chunkseg = size;
102  
103 0 node->shminfo->seg_semid = -1;
104 0 node->shminfo->data_chunks = 0;
105 0 node->shminfo->data_serial = 1;
106 0 node->shminfo->data_length = 0;
107 0 node->shminfo->nrefs = 0;
108  
109 0 ▶ if ( key != IPC_PRIVATE )
Branch: 1 branch
BranchTrueFalseTotal
if ( key != IPC_PRIVATE ) 0 0 0
110 0 node->shminfo->nrefs = 1;
111  
112 }
113  
114 0 return node;
115 }
116  
117  
118 /* --- SHARED SEGMENT LIST FUNCTIONS --- */
119  
120 /* attach to an existing top segment given a shmid or ipckey */
121 0 int _sharelite_shm_attach( Share *share ) {
122 Node *node;
123 int shmid;
124  
125 0 ▶ if ( ( shmid = share->shmid ) == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmid = share->shmid ) == -1 ) { 0 0 0
126  
127 0 ▶ if ( ( shmid = shmget( share->key, 0, 0 ) ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmid = shmget( share->key, 0, 0 ) ) == -1 ) 0 0 0
128 0 return -1;
129  
130 0 share->shmid = shmid;
131  
132 }
133  
134 0 ▶ if ( ( node = _shmseg_shmat( shmid ) ) == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _shmseg_shmat( shmid ) ) == NULL ) 0 0 0
135 0 return -1;
136  
137 /* the shmid doesn't point to a sharelite segment */
138 0 ▶ if ( node->shmhead->shm_magic != SHARELITE_MAGIC ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node->shmhead->shm_magic != SHARELITE_MAGIC ) { 0 0 0
139 0 _shmseg_shmdt( node, 0 );
140 0 errno = EFAULT;
141 0 return -1;
142 }
143  
144 0 node->shminfo = (Descriptor *) node->shmdata;
145 0 node->shmdata += sizeof( Descriptor );
146  
147 0 share->head = share->tail = node;
148  
149 0 return 0;
150 }
151  
152 /* create a new top segment given an ipckey (possibly IPC_PRIVATE) */
153 0 int _sharelite_shm_create( Share *share, int size ) {
154 Node *node;
155 int flags;
156  
157 0 flags = share->flags;
158  
159 0 ▶ if ( ( node = _shmseg_alloc( share->key, size, flags, 1 ) ) == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _shmseg_alloc( share->key, size, flags, 1 ) ) == NULL ) 0 0 0
160 0 return -1;
161  
162 0 share->shmid = node->shmid;
163 0 share->size_data = node->shminfo->size_topseg
164 0 - ( sizeof( Header ) + sizeof( Descriptor ) );
165  
166 0 share->head = share->tail = node;
167  
168 0 return 0;
169 }
170  
171 /* attach the next segment, creating one if necessary */
172 0 int _sharelite_shm_append( Share *share ) {
173 Node *node;
174 int shmid;
175  
176 0 ▶ if ( ( shmid = share->tail->shmhead->next_shmid ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmid = share->tail->shmhead->next_shmid ) != -1 ) { 0 0 0
177 /* attach an existing linked segment */
178  
179 0 ▶ if ( ( node = _shmseg_shmat( shmid ) ) == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _shmseg_shmat( shmid ) ) == NULL ) 0 0 0
180 0 return -1;
181  
182 /* the shmid doesn't point to a sharelite segment */
183 0 ▶ if ( node->shmhead->shm_magic != SHARELITE_MAGIC ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node->shmhead->shm_magic != SHARELITE_MAGIC ) { 0 0 0
184 0 _shmseg_shmdt( node, 0 );
185 0 errno = EFAULT;
186 0 return -1;
187 }
188  
189 0 share->tail->next = node;
190 0 share->tail = node;
191  
192 } else {
193 /* create a new linked segment */
194 int key, size, mode;
195  
196 0 key = IPC_PRIVATE;
197 0 size = share->head->shminfo->size_chunkseg;
198 0 mode = share->head->shminfo->seg_perms;
199  
200 0 ▶ if ( ( node = _shmseg_alloc( key, size, mode, 0 ) ) == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _shmseg_alloc( key, size, mode, 0 ) ) == NULL ) 0 0 0
201 0 return -1;
202  
203 /* update shared linked list */
204 0 share->tail->shmhead->next_shmid = node->shmid;
205  
206 /* update process private linked list */
207 0 share->tail->next = node;
208 0 share->tail = node;
209  
210 }
211  
212 0 node->shminfo = share->head->shminfo;
213 0 node->shminfo->data_chunks++;
214  
215 0 return 0;
216 }
217  
218  
219 #define _SHMSEG_TRUNC_SETUP_MACRO_ \
220 if ( last == NULL ) { \
221 node = share->head; \
222 share->head = NULL; \
223 share->tail = NULL; \
224 } else { \
225 node = last->next; \
226 last->next = NULL; \
227 share->tail = last; \
228 }
229  
230 /* nondestructively free stale Node structures */
231 0 int _sharelite_shm_forget( Share *share, Node *last ) {
232 Node *node, *next;
233  
234 0 ▶ if ( share->tail == last )
Branch: 1 branch
BranchTrueFalseTotal
if ( share->tail == last ) 0 0 0
235 0 return 0;
236  
237 0 ▶ _SHMSEG_TRUNC_SETUP_MACRO_
Branch: 1 branch
BranchTrueFalseTotal
_SHMSEG_TRUNC_SETUP_MACRO_ 0 0 0
238  
239 0 ▶ while ( node != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( node != NULL ) { 0 0 0
240 0 next = node->next;
241 0 ▶ if ( _shmseg_shmdt( node, 0 ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( _shmseg_shmdt( node, 0 ) == -1 ) 0 0 0
242 0 return -1;
243 0 node = next;
244 }
245  
246 0 return 0;
247 }
248  
249 /* remove unneeded segments from the system */
250 0 int _sharelite_shm_remove( Share *share, Node *last ) {
251 Node *node, *next;
252  
253 0 ▶ _SHMSEG_TRUNC_SETUP_MACRO_
Branch: 1 branch
BranchTrueFalseTotal
_SHMSEG_TRUNC_SETUP_MACRO_ 0 0 0
254  
255 0 ▶ if ( last != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( last != NULL ) 0 0 0
256 0 last->shmhead->next_shmid = -1;
257  
258 0 ▶ while ( node != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( node != NULL ) { 0 0 0
259 0 next = node->next;
260 0 ▶ if ( _shmseg_shmdt( node, 1 ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( _shmseg_shmdt( node, 1 ) == -1 ) 0 0 0
261 0 return -1;
262 0 node = next;
263 }
264  
265 0 return 0;
266 }
267  
268 /* get the number of active connections */
269 0 int _sharelite_shm_nconns( Share *share ) {
270 struct shmid_ds buf;
271  
272 0 ▶ if ( shmctl( share->shmid, IPC_STAT, &buf ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( shmctl( share->shmid, IPC_STAT, &buf ) == -1 ) 0 0 0
273 0 return -1;
274  
275 0 return (int) buf.shm_nattch;
276 }
277  
« sharelite.c ↑ Summary