sharestuff.c

filter: Statementstmt 56.8% 170 / 299 Branchbran 40.3% 79 / 196 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 50.3% 249 / 495 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ShareLite.xs ↑ Summary
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <sys/types.h>
6 #include <sys/ipc.h>
7 #include <sys/shm.h>
8 #include <sys/sem.h>
9 #include <errno.h>
10 #include "sharestuff.h"
11  
12 #ifndef errno
13 extern int errno;
14 #endif
15  
16 #include "EXTERN.h"
17 #include "perl.h"
18 #include "XSUB.h"
19  
20 /* Use Perl's memory management */
21  
22 #ifndef Newxz
23 #define Newxz(pointer, number, type) \
24 Newz(1, pointer, number, type)
25 #endif
26  
27 #ifdef HAS_UNION_SEMUN
28 #define SEMUN union semun
29 #else
30 union my_semun {
31 int val;
32 struct semid_ds *buf;
33 unsigned short *array;
34 };
35 #define SEMUN union my_semun
36 #endif
37  
38 /* --- DEFINE MACROS FOR SEMAPHORE OPERATIONS --- */
39  
40 #define GET_EX_LOCK(A) semop((A), &ex_lock[0], 3)
41 #define GET_EX_LOCK_NB(A) semop((A), &ex_lock_nb[0], 3)
42 #define RM_EX_LOCK(A) semop((A), &ex_unlock[0], 1)
43 #define GET_SH_LOCK(A) semop((A), &sh_lock[0], 2)
44 #define GET_SH_LOCK_NB(A) semop((A), &sh_lock_nb[0], 2)
45 #define RM_SH_LOCK(A) semop((A), &sh_unlock[0], 1)
46  
47 /* --- DEFINE STRUCTURES FOR MANIPULATING SEMAPHORES --- */
48  
49 static struct sembuf ex_lock[3] = {
50 {1, 0, 0}, /* wait for readers to finish */
51 {2, 0, 0}, /* wait for writers to finish */
52 {2, 1, SEM_UNDO} /* assert write lock */
53 };
54  
55 static struct sembuf ex_lock_nb[3] = {
56 {1, 0, IPC_NOWAIT}, /* wait for readers to finish */
57 {2, 0, IPC_NOWAIT}, /* wait for writers to finish */
58 {2, 1, ( SEM_UNDO | IPC_NOWAIT )} /* assert write lock */
59 };
60  
61 static struct sembuf ex_unlock[1] = {
62 {2, -1, ( SEM_UNDO | IPC_NOWAIT )} /* remove write lock */
63 };
64  
65 static struct sembuf sh_lock[2] = {
66 {2, 0, 0}, /* wait for writers to finish */
67 {1, 1, SEM_UNDO} /* assert shared read lock */
68 };
69  
70 static struct sembuf sh_lock_nb[2] = {
71 {2, 0, IPC_NOWAIT}, /* wait for writers to finish */
72 {1, 1, ( SEM_UNDO | IPC_NOWAIT )} /* assert shared read lock */
73 };
74  
75 static struct sembuf sh_unlock[1] = {
76 {1, -1, ( SEM_UNDO | IPC_NOWAIT )} /* remove shared read lock */
77 };
78  
79 FILE *log_fh = NULL;
80 #define LOG_ARGS const char *file, int line, const char *fmt, ...
81 #define LOG0(fmt) sharelite_log(__FILE__, __LINE__, fmt)
82 #define LOG1(fmt, a1) sharelite_log(__FILE__, __LINE__, fmt, a1)
83 #define LOG2(fmt, a1, a2) sharelite_log(__FILE__, __LINE__, fmt, a1, a2)
84 #define LOG3(fmt, a1, a2, a3) sharelite_log(__FILE__, __LINE__, fmt, a1, a2, a3)
85  
86 static void sharelite_log_active( LOG_ARGS );
87 static void sharelite_log_nop( LOG_ARGS );
88  
89 static void ( *sharelite_log ) ( LOG_ARGS ) = sharelite_log_active;
90  
91 static void
92 0 sharelite_log_nop( LOG_ARGS ) {
93 0 }
94  
95 static void
96 0 sharelite_log_active( LOG_ARGS ) {
97 0 ▶ if ( NULL == log_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( NULL == log_fh ) { 0 0 0
98 0 const char *log_file = getenv( "IPC_SHARELITE_LOG" );
99 0 ▶ if ( NULL == log_file
Branch: 1 branch
BranchTrueFalseTotal
if ( NULL == log_file 0 0 0
100 0 ▶ || ( log_fh = fopen( log_file, "a" ), NULL == log_fh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
|| ( log_fh = fopen( log_file, "a" ), NULL == log_fh ) ) { 0 0 0
101 0 sharelite_log = sharelite_log_nop;
102 0 return;
103 }
104 }
105 {
106 struct timeval now;
107 char timebuf[40];
108 va_list ap;
109  
110 0 gettimeofday( &now, NULL );
111 0 strftime( timebuf, sizeof( timebuf ), "%Y/%m/%d %H:%M:%S",
112 0 gmtime( &now.tv_sec ) );
113 0 fprintf( log_fh, "%s.%06lu %s, %d : ", timebuf,
114 0 ( unsigned long ) now.tv_usec, file, line );
115 0 va_start( ap, fmt );
116 0 vfprintf( log_fh, fmt, ap );
117 0 va_end( ap );
118 0 fprintf( log_fh, "\n" );
119 0 fflush( log_fh );
120 }
121 }
122  
123 /* USER INITIATED LOCK */
124  
125 /* returns 0 on success -- requested operation performed *
126 * returns -1 on error *
127 * returns 1 if LOCK_NB specified and operation would block */
128 int
129 2000 sharelite_lock( Share * share, int flags ) {
130  
131 /* try to obtain exclusive lock by default */
132 2000 ▶ if ( !flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !flags ) { 0 2000 2000
133 0 flags = LOCK_EX;
134 }
135  
136 /* Check for invalid combination of flags. Invalid combinations *
137 * are attempts to obtain *both* an exclusive and shared lock or *
138 * to both obtain and release a lock at the same time */
139 2000 ▶ if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) ||
Branch: 2 branches
BranchTrueFalseTotal
if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) || 2000 0 2000
if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) || 2000 0 2000
140 2000 ▶ ( ( flags & LOCK_UN )
Branch: 1 branch
BranchTrueFalseTotal
( ( flags & LOCK_UN ) 0 2000 2000
141 0 ▶ && ( ( flags & LOCK_EX ) || ( flags & LOCK_SH ) ) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
&& ( ( flags & LOCK_EX ) || ( flags & LOCK_SH ) ) ) ) { 0 0 0
&& ( ( flags & LOCK_EX ) || ( flags & LOCK_SH ) ) ) ) { 0 0 0
142 0 return -1;
143 }
144  
145 2000 ▶ if ( flags & LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( flags & LOCK_EX ) { 2000 0 2000
146 /*** WANTS EXCLUSIVE LOCK ***/
147 /* If they already have an exclusive lock, just return */
148 2000 ▶ if ( share->lock & LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_EX ) { 0 2000 2000
149 0 return 0;
150 }
151 /* If they currently have a shared lock, remove it */
152 2000 ▶ if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_SH ) { 0 2000 2000
153 0 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 0 0
154 0 return -1;
155 }
156 0 share->lock = 0;
157 }
158 2000 ▶ if ( flags & LOCK_NB ) { /* non-blocking request */
Branch: 1 branch
BranchTrueFalseTotal
if ( flags & LOCK_NB ) { /* non-blocking request */ 0 2000 2000
159 0 ▶ if ( GET_EX_LOCK_NB( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_EX_LOCK_NB( share->semid ) < 0 ) { 0 0 0
160 0 ▶ if ( errno == EAGAIN ) { /* would we have blocked? */
Branch: 1 branch
BranchTrueFalseTotal
if ( errno == EAGAIN ) { /* would we have blocked? */ 0 0 0
161 0 return 1;
162 }
163 0 return -1;
164 }
165 }
166 else { /* blocking request */
167 2000 ▶ if ( GET_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_EX_LOCK( share->semid ) < 0 ) { 0 2000 2000
168 0 return -1;
169 }
170 }
171 2000 share->lock = LOCK_EX;
172 2000 return 0;
173 }
174 0 ▶ else if ( flags & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( flags & LOCK_SH ) { 0 0 0
175 /*** WANTS SHARED LOCK ***/
176 /* If they already have a shared lock, just return */
177 0 ▶ if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_SH ) { 0 0 0
178 0 return 0;
179 }
180 /* If they currently have an exclusive lock, remove it */
181 0 ▶ if ( share->lock & LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_EX ) { 0 0 0
182 0 ▶ if ( RM_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( share->semid ) < 0 ) { 0 0 0
183 0 return -1;
184 }
185 0 share->lock = 0;
186 }
187 0 ▶ if ( flags & LOCK_NB ) { /* non-blocking request */
Branch: 1 branch
BranchTrueFalseTotal
if ( flags & LOCK_NB ) { /* non-blocking request */ 0 0 0
188 0 ▶ if ( GET_SH_LOCK_NB( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_SH_LOCK_NB( share->semid ) < 0 ) { 0 0 0
189 0 ▶ if ( errno == EAGAIN ) { /* would we have blocked? */
Branch: 1 branch
BranchTrueFalseTotal
if ( errno == EAGAIN ) { /* would we have blocked? */ 0 0 0
190 0 return 1;
191 }
192 0 return -1;
193 }
194 }
195 else { /* blocking request */
196 0 ▶ if ( GET_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_SH_LOCK( share->semid ) < 0 ) { 0 0 0
197 0 return -1;
198 }
199 }
200 0 share->lock = LOCK_SH;
201 0 return 0;
202 }
203 0 ▶ else if ( flags & LOCK_UN ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( flags & LOCK_UN ) { 0 0 0
204 /*** WANTS TO RELEASE LOCK ***/
205 0 ▶ if ( share->lock & LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_EX ) { 0 0 0
206 0 ▶ if ( RM_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( share->semid ) < 0 ) { 0 0 0
207 0 return -1;
208 }
209 }
210 0 ▶ else if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( share->lock & LOCK_SH ) { 0 0 0
211 0 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 0 0
212 0 return -1;
213 }
214 }
215 }
216  
217 0 return 0;
218 }
219  
220 int
221 2000 sharelite_unlock( Share * share ) {
222 2000 ▶ if ( share->lock & LOCK_EX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_EX ) { 2000 0 2000
223 2000 ▶ if ( RM_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( share->semid ) < 0 ) { 0 2000 2000
224 0 return -1;
225 }
226 }
227 0 ▶ else if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( share->lock & LOCK_SH ) { 0 0 0
228 0 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 0 0
229 0 return -1;
230 }
231 }
232 2000 share->lock = 0;
233 2000 return 0;
234 }
235  
236 Node *
237 4 _add_segment( Share * share ) {
238 Node *node;
239 int flags;
240  
241 4 Newxz( node, 1, Node );
242  
243 4 node->next = NULL;
244  
245 /* Does another shared memory segment already exist? */
246 4 ▶ if ( share->tail->shmaddr->next_shmid >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->tail->shmaddr->next_shmid >= 0 ) { 0 4 4
247 0 node->shmid = share->tail->shmaddr->next_shmid;
248 0 ▶ if ( ( node->shmaddr =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node->shmaddr = 0 0 0
249 0 ( Header * ) shmat( node->shmid, ( char * ) 0,
250 0 ) ) == ( Header * ) - 1 ) {
251 0 return NULL;
252 }
253 0 share->tail->next = node;
254 0 share->tail = node;
255 0 return node;
256 }
257  
258 4 flags = share->flags | IPC_CREAT | IPC_EXCL;
259  
260 /* We need to create a new segment */
261 while ( 1 ) {
262 4 node->shmid = shmget( share->next_key++, share->segment_size, flags );
263 4 ▶ if ( node->shmid >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node->shmid >= 0 ) { 4 0 4
264 4 break;
265 }
266 #ifdef EIDRM
267 0 ▶ if ( errno == EEXIST || errno == EIDRM ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( errno == EEXIST || errno == EIDRM ) { 0 0 0
if ( errno == EEXIST || errno == EIDRM ) { 0 0 0
268 0 continue;
269 }
270 #else
271 if ( errno == EEXIST ) {
272 continue;
273 }
274 #endif
275 0 return NULL;
276 }
277  
278 4 share->tail->shmaddr->next_shmid = node->shmid;
279 4 share->tail->next = node;
280 4 share->tail = node;
281 4 ▶ if ( ( node->shmaddr =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node->shmaddr = 0 4 4
282 4 ( Header * ) shmat( node->shmid, ( char * ) 0,
283 0 ) ) == ( Header * ) - 1 ) {
284 0 return NULL;
285 }
286 4 node->shmaddr->next_shmid = -1;
287 4 node->shmaddr->length = 0;
288  
289 4 return node;
290 }
291  
292 int
293 6 _detach_segments( Node * node ) {
294 Node *next_node;
295  
296 12 ▶ while ( node != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( node != NULL ) { 6 6 12
297 6 next_node = node->next;
298 6 ▶ if ( shmdt( ( char * ) node->shmaddr ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( shmdt( ( char * ) node->shmaddr ) < 0 ) { 0 6 6
299 0 return -1;
300 }
301 6 Safefree( node );
302 6 node = next_node;
303 }
304 6 return 0;
305 }
306  
307 int
308 3 _remove_segments( int shmid ) {
309 int next_shmid;
310 Header *shmaddr;
311  
312 8 ▶ while ( shmid >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( shmid >= 0 ) { 5 3 8
313 5 ▶ if ( ( shmaddr =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmaddr = 0 5 5
314 5 ( Header * ) shmat( shmid, ( char * ) 0,
315 0 ) ) == ( Header * ) - 1 ) {
316 0 return -1;
317 }
318 5 next_shmid = shmaddr->next_shmid;
319 5 ▶ if ( shmdt( ( char * ) shmaddr ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( shmdt( ( char * ) shmaddr ) < 0 ) { 0 5 5
320 0 return -1;
321 }
322 5 ▶ if ( shmctl( shmid, IPC_RMID, ( struct shmid_ds * ) 0 ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( shmctl( shmid, IPC_RMID, ( struct shmid_ds * ) 0 ) < 0 ) { 0 5 5
323 0 return -1;
324 }
325 5 shmid = next_shmid;
326 }
327  
328 3 return 0;
329 }
330  
331 int
332 2 _invalidate_segments( Share * share ) {
333  
334 2 ▶ if ( _detach_segments( share->head->next ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _detach_segments( share->head->next ) < 0 ) { 0 2 2
335 0 return -1;
336 }
337 2 share->head->next = NULL;
338 2 share->tail = share->head;
339 2 share->shm_state = share->head->shmaddr->shm_state;
340  
341 2 return 0;
342 }
343  
344 int
345 2006 write_share( Share * share, char *data, int length ) {
346 char *shmaddr;
347 int segments;
348 int left;
349 int chunk_size;
350 Node *node;
351 int shmid;
352  
353 2006 ▶ if ( data == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( data == NULL ) { 0 2006 2006
354 0 return -1;
355 }
356  
357 2006 ▶ if ( !( share->lock & LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !( share->lock & LOCK_EX ) ) { 6 2000 2006
358 6 ▶ if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_SH ) { 0 6 6
359 0 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 0 0
360 0 return -1;
361 }
362 }
363 6 ▶ if ( GET_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_EX_LOCK( share->semid ) < 0 ) { 0 6 6
364 0 return -1;
365 }
366 }
367  
368 2006 ▶ if ( share->shm_state != share->head->shmaddr->shm_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->shm_state != share->head->shmaddr->shm_state ) { 0 2006 2006
369 0 ▶ if ( _invalidate_segments( share ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _invalidate_segments( share ) < 0 ) { 0 0 0
370 0 return -1;
371 }
372 }
373  
374 /* set the data length to zero. if we are interrupted or encounter *
375 * an error during the write, this guarantees that we won't *
376 * receive corrupt data in future reads. */
377 2006 share->head->shmaddr->length = 0;
378  
379 /* compute number of segments necessary to hold data */
380 2006 segments =
381 2006 ( length / share->data_size ) +
382 2006 ( length % share->data_size ? 1 : 0 );
383  
384 2006 node = share->head;
385 2006 left = length;
386 4016 ▶ while ( segments-- ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( segments-- ) { 2010 2006 4016
387 2010 ▶ if ( node == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node == NULL ) { 4 2006 2010
388 4 ▶ if ( ( node = _add_segment( share ) ) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _add_segment( share ) ) == NULL ) { 0 4 4
389 0 return -1;
390 }
391 }
392 2010 chunk_size = ( left > share->data_size ? share->data_size : left );
393 2010 shmaddr = ( char * ) node->shmaddr + sizeof( Header );
394 2010 memcpy( shmaddr, data, chunk_size );
395 2010 left -= chunk_size;
396 2010 data += chunk_size;
397 2010 ▶ if ( segments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( segments ) { 4 2006 2010
398 4 node = node->next;
399 }
400 }
401  
402 /* set new length in header of first segment */
403 2006 share->head->shmaddr->length = length;
404  
405 /* garbage collection -- remove unused segments */
406 2006 ▶ if ( node->shmaddr->next_shmid >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node->shmaddr->next_shmid >= 0 ) { 2 2004 2006
407 2 shmid = node->shmaddr->next_shmid;
408 2 ▶ if ( _detach_segments( node->next ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _detach_segments( node->next ) < 0 ) { 0 2 2
409 0 return -1;
410 }
411 2 ▶ if ( _remove_segments( shmid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _remove_segments( shmid ) < 0 ) { 0 2 2
412 0 return -1;
413 }
414 2 node->shmaddr->next_shmid = -1;
415 2 node->next = NULL;
416 2 share->tail = node;
417 2 share->head->shmaddr->shm_state++;
418 }
419  
420 2006 ++share->head->shmaddr->version;
421  
422 2006 ▶ if ( !( share->lock & LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !( share->lock & LOCK_EX ) ) { 6 2000 2006
423 6 ▶ if ( RM_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( share->semid ) < 0 ) { 0 6 6
424 0 return -1;
425 }
426 6 ▶ if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_SH ) { 0 6 6
427 0 ▶ if ( GET_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_SH_LOCK( share->semid ) < 0 ) { 0 0 0
428 0 return -1;
429 }
430 }
431 }
432  
433 2006 return 0;
434 }
435  
436 int
437 2005 read_share( Share * share, char **data ) {
438 char *shmaddr;
439 char *pos;
440 Node *node;
441 int length;
442 int left;
443 int chunk_size;
444  
445 2005 ▶ if ( !share->lock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !share->lock ) { 5 2000 2005
446 5 ▶ if ( GET_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_SH_LOCK( share->semid ) < 0 ) { 0 5 5
447 0 return -1;
448 }
449 }
450  
451 2005 ▶ if ( share->shm_state != share->head->shmaddr->shm_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->shm_state != share->head->shmaddr->shm_state ) { 2 2003 2005
452 2 ▶ if ( _invalidate_segments( share ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _invalidate_segments( share ) < 0 ) { 0 2 2
453 0 return -1;
454 }
455 }
456  
457 2005 node = share->head;
458 2005 left = length = node->shmaddr->length;
459  
460 /* Allocate extra byte for a null at the end */
461 2005 Newxz( *data, length + 1, char );
462 2005 pos = *data;
463  
464 2005 pos[length] = '\0';
465  
466 4014 ▶ while ( left ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( left ) { 2009 2005 4014
467 2009 ▶ if ( node == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( node == NULL ) { 0 2009 2009
468 0 ▶ if ( ( node = _add_segment( share ) ) == NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node = _add_segment( share ) ) == NULL ) { 0 0 0
469 0 goto fail;
470 }
471 }
472 2009 chunk_size = ( left > share->data_size ? share->data_size : left );
473 2009 shmaddr = ( char * ) node->shmaddr + sizeof( Header );
474 2009 memcpy( pos, shmaddr, chunk_size );
475 2009 pos += chunk_size;
476 2009 left -= chunk_size;
477 2009 node = node->next;
478 }
479  
480 2005 ▶ if ( !share->lock ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !share->lock ) { 5 2000 2005
481 5 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 5 5
482 0 goto fail;
483 }
484 }
485  
486 2005 return length;
487  
488 0 fail:
489 0 Safefree( *data );
490 0 return -1;
491 }
492  
493 Share *
494 2 new_share( key_t key, int segment_size, int flags ) {
495 Share *share;
496 Node *node;
497 int semid;
498 struct shmid_ds shmctl_arg;
499 SEMUN semun_arg;
500  
501 2 again:
502 2 ▶ if ( ( semid = semget( key, 3, flags ) ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( semid = semget( key, 3, flags ) ) < 0 ) { 0 2 2
503 0 LOG1( "semget failed (%d)", errno );
504 0 return NULL;
505 }
506  
507 /* It's possible for another process to obtain the semaphore, lock it, *
508 * and remove it from the system before we have a chance to lock it. *
509 * In this case (EINVAL) we just try to create it again. */
510 2 ▶ if ( GET_EX_LOCK( semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_EX_LOCK( semid ) < 0 ) { 0 2 2
511 0 ▶ if ( errno == EINVAL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( errno == EINVAL ) { 0 0 0
512 0 goto again;
513 }
514 0 LOG1( "GET_EX_LOCK failed (%d)", errno );
515 0 return NULL;
516 }
517  
518 /* XXX IS THIS THE RIGHT THING TO DO? */
519 2 ▶ if ( segment_size <= sizeof( Header ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( segment_size <= sizeof( Header ) ) { 0 2 2
520 0 segment_size = SHM_SEGMENT_SIZE;
521 }
522  
523 2 Newxz( node, 1, Node );
524  
525 2 ▶ if ( ( node->shmid = shmget( key, segment_size, flags ) ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node->shmid = shmget( key, segment_size, flags ) ) < 0 ) { 0 2 2
526 0 LOG1( "shmget failed (%d)", errno );
527 0 return NULL;
528 }
529  
530 2 ▶ if ( ( node->shmaddr =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( node->shmaddr = 0 2 2
531 2 ( Header * ) shmat( node->shmid, ( char * ) 0,
532 0 ) ) == ( Header * ) - 1 ) {
533 0 LOG1( "shmat failed (%d)", errno );
534 0 return NULL;
535 }
536  
537 2 node->next = NULL;
538  
539 2 Newxz( share, 1, Share );
540  
541 2 share->key = key;
542 2 share->next_key = key + 1;
543 2 share->flags = flags;
544 2 share->semid = semid;
545 2 share->lock = 0;
546 2 share->head = node;
547 2 share->tail = node;
548  
549 /* is this a newly created segment? if so, initialize it */
550 2 ▶ if ( ( semun_arg.val =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( semun_arg.val = 0 2 2
551 2 semctl( share->semid, 0, GETVAL, semun_arg ) ) < 0 ) {
552 0 LOG1( "shmctl failed (%d)", errno );
553 0 return NULL;
554 }
555  
556 2 ▶ if ( semun_arg.val == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( semun_arg.val == 0 ) { 2 0 2
557 2 semun_arg.val = 1;
558 2 ▶ if ( semctl( share->semid, 0, SETVAL, semun_arg ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( semctl( share->semid, 0, SETVAL, semun_arg ) < 0 ) { 0 2 2
559 0 LOG1( "shmctl failed (%d)", errno );
560 0 return NULL;
561 }
562 2 share->head->shmaddr->length = 0;
563 2 share->head->shmaddr->next_shmid = -1;
564 2 share->head->shmaddr->shm_state = 1;
565 2 share->head->shmaddr->version = 1;
566 }
567  
568 2 share->shm_state = share->head->shmaddr->shm_state;
569 2 share->version = share->head->shmaddr->version;
570  
571 /* determine the true length of the segment. this may disagree *
572 * with what the user requested, since shmget() calls will *
573 * succeed if the requested size <= the existing size */
574 2 ▶ if ( shmctl( share->head->shmid, IPC_STAT, &shmctl_arg ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( shmctl( share->head->shmid, IPC_STAT, &shmctl_arg ) < 0 ) { 0 2 2
575 0 LOG1( "shmctl failed (%d)", errno );
576 0 return NULL;
577 }
578  
579 2 share->segment_size = shmctl_arg.shm_segsz;
580 2 share->data_size = share->segment_size - sizeof( Header );
581  
582 2 ▶ if ( RM_EX_LOCK( semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( semid ) < 0 ) { 0 2 2
583 0 LOG1( "RM_EX_LOCK failed (%d)", errno );
584 0 return NULL;
585 }
586  
587 2 return share;
588 }
589  
590 unsigned int
591 9 sharelite_version( Share * share ) {
592 9 return share->head->shmaddr->version;
593 }
594  
595 int
596 2 destroy_share( Share * share, int rmid ) {
597 int semid;
598 SEMUN semctl_arg;
599  
600 2 ▶ if ( !( share->lock & LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !( share->lock & LOCK_EX ) ) { 2 0 2
601 2 ▶ if ( share->lock & LOCK_SH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( share->lock & LOCK_SH ) { 0 2 2
602 0 ▶ if ( RM_SH_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_SH_LOCK( share->semid ) < 0 ) { 0 0 0
603 0 return -1;
604 }
605 }
606 2 ▶ if ( GET_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( GET_EX_LOCK( share->semid ) < 0 ) { 0 2 2
607 0 return -1;
608 }
609 }
610  
611 2 semid = share->head->shmid;
612 2 ▶ if ( _detach_segments( share->head ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _detach_segments( share->head ) < 0 ) { 0 2 2
613 0 return -1;
614 }
615  
616 2 ▶ if ( rmid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( rmid ) { 1 1 2
617 1 ▶ if ( _remove_segments( semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( _remove_segments( semid ) < 0 ) { 0 1 1
618 0 return -1;
619 }
620 1 semctl_arg.val = 0;
621 1 ▶ if ( semctl( share->semid, 0, IPC_RMID, semctl_arg ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( semctl( share->semid, 0, IPC_RMID, semctl_arg ) < 0 ) { 0 1 1
622 0 return -1;
623 }
624 }
625 else {
626 1 ▶ if ( RM_EX_LOCK( share->semid ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( RM_EX_LOCK( share->semid ) < 0 ) { 0 1 1
627 0 return -1;
628 }
629 }
630  
631 2 Safefree( share );
632  
633 2 return 0;
634 }
635  
636 int
637 2 sharelite_num_segments( Share * share ) {
638 2 int count = 0;
639 int shmid;
640 Header *shmaddr;
641  
642 2 shmid = share->head->shmid;
643 8 ▶ while ( shmid >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( shmid >= 0 ) { 6 2 8
644 6 count++;
645 6 ▶ if ( ( shmaddr =
Branch: 1 branch
BranchTrueFalseTotal
if ( ( shmaddr = 0 6 6
646 6 ( Header * ) shmat( shmid, ( char * ) 0,
647 0 ) ) == ( Header * ) - 1 ) {
648 0 return -1;
649 }
650 6 shmid = shmaddr->next_shmid;
651 6 ▶ if ( shmdt( ( char * ) shmaddr ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( shmdt( ( char * ) shmaddr ) < 0 ) { 0 6 6
652 0 return -1;
653 }
654 }
655  
656 2 return count;
657 }
658  
659 void
660 0 _dump_list( Share * share ) {
661 Node *node;
662  
663 0 node = share->head;
664 0 ▶ while ( node != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( node != NULL ) { 0 0 0
665 0 printf( "shmid: %i\n", node->shmid );
666 0 node = node->next;
667 }
668 0 }
« ShareLite.xs ↑ Summary