| 1 |
|
| /* |
| 2 |
|
| * sharelite.h - part of IPC::Shm::Simple |
| 3 |
|
| * |
| 4 |
|
| * Originally part of IPC::ShareLite by Maurice Aubrey. |
| 5 |
|
| * |
| 6 |
|
| * Adapted 2/2005 by Kevin Cody-Little <kcody@cpan.org> |
| 7 |
|
| * |
| 8 |
|
| * This code may be modified or redistributed under the terms |
| 9 |
|
| * of either the Artistic or GNU General Public licenses, at |
| 10 |
|
| * the modifier or redistributor's discretion. |
| 11 |
|
| * |
| 12 |
|
| */ |
| 13 |
|
| |
| 14 |
|
| #include <stdlib.h> |
| 15 |
|
| #include <string.h> |
| 16 |
|
| #include <errno.h> |
| 17 |
|
| |
| 18 |
|
| #include <sys/types.h> |
| 19 |
|
| #include <sys/file.h> |
| 20 |
|
| #include <sys/ipc.h> |
| 21 |
|
| #include <sys/shm.h> |
| 22 |
|
| |
| 23 |
|
| #ifndef errno |
| 24 |
|
| extern int errno; |
| 25 |
|
| #endif |
| 26 |
|
| |
| 27 |
|
| |
| 28 |
|
| #include "sharelite.h" |
| 29 |
|
| #include "sharelite_shm.h" |
| 30 |
|
| #include "sharelite_sem.h" |
| 31 |
|
| |
| 32 |
|
| #define CALL_NEEDS_SHARE(A) \ |
| 33 |
|
| if ( share == NULL ) { \ |
| 34 |
|
| errno = EINVAL; \ |
| 35 |
|
| return (A); \ |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| #define CALL_NEEDS_SHARE_AND_HEAD(A) \ |
| 39 |
|
| if ( ( share == NULL ) \ |
| 40 |
|
| || ( share->head == NULL ) ) { \ |
| 41 |
|
| errno = EINVAL; \ |
| 42 |
|
| return (A); \ |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| |
| 46 |
|
| /* CREATE / ATTACH SHARED SEGMENT FUNCTIONS */ |
| 47 |
|
| |
| 48 |
|
| inline |
| 49 |
|
| Share *_sharelite_shmat( key_t key, int shmid ) { |
| 50 |
|
| Share *share; |
| 51 |
|
| |
| 52 |
|
| /* _sharelite_shm_attach expects a blank share object */ |
| 53 |
|
| if ( ( share = (Share *) calloc( 1, sizeof( Share ) ) ) == NULL ) |
| 54 |
|
| return NULL; |
| 55 |
|
| |
| 56 |
|
| /* both are expected; key is used if shmid == -1 */ |
| 57 |
|
| share->key = key; |
| 58 |
|
| share->shmid = shmid; |
| 59 |
|
| |
| 60 |
|
| /* can't attach to an unknown anonymous segment */ |
| 61 |
|
| if ( ( shmid == -1 ) && ( key == IPC_PRIVATE ) ) { |
| 62 |
|
| errno = EINVAL; |
| 63 |
|
| return NULL; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| /* fetching the top node also inits the list pointers in share */ |
| 67 |
|
| if ( _sharelite_shm_attach( share ) < 0 ) { |
| 68 |
|
| free( share ); |
| 69 |
|
| return NULL; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| /* initialize the process private state variables */ |
| 73 |
|
| share->key = key; /* only to provide a default */ |
| 74 |
|
| share->lock = LOCK_UN; |
| 75 |
|
| |
| 76 |
|
| share->flags = share->head->shminfo->seg_perms; |
| 77 |
|
| share->semid = share->head->shminfo->seg_semid; |
| 78 |
|
| |
| 79 |
|
| share->size_data = share->head->shminfo->size_topseg |
| 80 |
|
| - ( sizeof( Header ) + sizeof( Descriptor ) ); |
| 81 |
|
| |
| 82 |
|
| return share; |
| 83 |
|
| } |
| 84 |
|
| |
| 85 |
0 |
| Share *sharelite_shmat( int shmid ) { |
| 86 |
|
| |
| 87 |
0 |
| return _sharelite_shmat( IPC_PRIVATE, shmid ); |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
0 |
| Share *sharelite_attach( key_t key ) { |
| 91 |
|
| |
| 92 |
0 |
| return _sharelite_shmat( key, -1 ); |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
0 |
| Share *sharelite_create( key_t key, int segsize, int flags ) { |
| 96 |
|
| Share *share; |
| 97 |
|
| Node *node; |
| 98 |
|
| int semid; |
| 99 |
|
| |
| 100 |
|
| /* unsure what the minimum size is, but there -must- be one... */ |
| 101 |
0 |
▶ | if ( segsize < SHARELITE_MIN_SIZE ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( segsize < SHARELITE_MIN_SIZE ) |
0 |
0 |
0 |
|
| 102 |
0 |
| segsize = SHARELITE_SEG_SIZE; |
| 103 |
|
| |
| 104 |
|
| /* _sharelite_seg_create expects a blank share object */ |
| 105 |
0 |
▶ | if ( ( share = calloc( 1, sizeof( Share ) ) ) == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ( share = calloc( 1, sizeof( Share ) ) ) == NULL ) |
0 |
0 |
0 |
|
| 106 |
0 |
| return NULL; |
| 107 |
|
| |
| 108 |
|
| /* get a new locked semaphore to go with our segment */ |
| 109 |
0 |
▶ | if ( ( semid = _sharelite_sem_create( flags ) ) == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ( semid = _sharelite_sem_create( flags ) ) == -1 ) { |
0 |
0 |
0 |
|
| 110 |
0 |
| free( share ); |
| 111 |
0 |
| return NULL; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| /* no error checking - any ipckey is valid, plus IPC_PRIVATE */ |
| 115 |
0 |
| share->key = key; |
| 116 |
|
| |
| 117 |
|
| /* mask out all but the lower nine bits - permissions */ |
| 118 |
0 |
| share->flags = flags & 0x01FF; |
| 119 |
|
| |
| 120 |
|
| /* fetching the new top node also inits the list pointers in share */ |
| 121 |
0 |
▶ | if ( _sharelite_shm_create( share, segsize ) < 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( _sharelite_shm_create( share, segsize ) < 0 ) { |
0 |
0 |
0 |
|
| 122 |
0 |
| _sharelite_sem_remove( semid ); |
| 123 |
0 |
| free( share ); |
| 124 |
0 |
| return NULL; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
0 |
| node = share->head; |
| 128 |
|
| |
| 129 |
|
| /* initialize the process private state variables */ |
| 130 |
0 |
| share->lock = LOCK_EX; |
| 131 |
0 |
| share->semid = semid; |
| 132 |
|
| |
| 133 |
|
| /* the existence of this value marks the segment as valid */ |
| 134 |
0 |
| node->shminfo->seg_semid = semid; |
| 135 |
|
| |
| 136 |
0 |
| return share; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| |
| 140 |
|
| /* DETACH / DESTROY SHARED SEGMENT FUNCTIONS */ |
| 141 |
|
| #include <stdio.h> |
| 142 |
|
| /* ignores error conditions; this would be called by a destructor */ |
| 143 |
0 |
| int sharelite_shmdt( Share *share ) { |
| 144 |
|
| |
| 145 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 146 |
|
| |
| 147 |
0 |
▶ | if ( share->remove ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->remove ) { |
0 |
0 |
0 |
|
| 148 |
0 |
▶ | REQ_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
|
| 149 |
0 |
| _sharelite_sem_remove( share->semid ); |
| 150 |
0 |
| _sharelite_shm_remove( share, NULL ); |
| 151 |
|
| /* no END_EX_LOCK; we just removed the semaphore */ |
| 152 |
|
| } else { |
| 153 |
0 |
▶ | if ( share->lock & LOCK_EX ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->lock & LOCK_EX ) |
0 |
0 |
0 |
|
| 154 |
0 |
| REL_EX_LOCK(share); |
| 155 |
0 |
▶ | if ( share->lock & LOCK_SH ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->lock & LOCK_SH ) |
0 |
0 |
0 |
|
| 156 |
0 |
| REL_SH_LOCK(share); |
| 157 |
0 |
| _sharelite_shm_forget( share, NULL ); |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
0 |
| free( share ); |
| 161 |
|
| |
| 162 |
0 |
| return 0; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| /* just sets a flag; removal happens on detach, just like the syscall */ |
| 166 |
0 |
| int sharelite_remove( Share *share ) { |
| 167 |
|
| |
| 168 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 169 |
|
| |
| 170 |
0 |
| share->remove = 1; |
| 171 |
|
| |
| 172 |
0 |
| return 0; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /* USER INITIATED LOCK */ |
| 176 |
|
| |
| 177 |
|
| /* returns 0 on success -- requested operation performed * |
| 178 |
|
| * returns -1 on error * |
| 179 |
|
| * returns 1 if LOCK_NB specified and operation would block */ |
| 180 |
0 |
| int sharelite_lock(Share *share, int flags) { |
| 181 |
|
| int nonblock, lockmode, rc; |
| 182 |
|
| |
| 183 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 184 |
|
| |
| 185 |
|
| /* at this layer we demand an argument */ |
| 186 |
0 |
▶ | if ( ! flags ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! flags ) { |
0 |
0 |
0 |
|
| 187 |
0 |
| errno = EINVAL; |
| 188 |
0 |
| return -1; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| /* Check for invalid combination of flags. Invalid combinations * |
| 192 |
|
| * are attempts to obtain *both* an exclusive and shared lock or * |
| 193 |
|
| * to both obtain and release a lock at the same time */ |
| 194 |
0 |
▶ | if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) || |
0 |
0 |
0 |
| if ( ( ( flags & LOCK_EX ) && ( flags & LOCK_SH ) ) || |
0 |
0 |
0 |
|
| 195 |
0 |
▶ | ( ( flags & LOCK_UN ) && |
Branch: 1 branch
| Branch | True | False | Total |
| ( ( flags & LOCK_UN ) && |
0 |
0 |
0 |
|
| 196 |
0 |
▶ | ( ( flags & LOCK_SH ) || ( flags & LOCK_EX ) ) ) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| ( ( flags & LOCK_SH ) || ( flags & LOCK_EX ) ) ) ) { |
0 |
0 |
0 |
| ( ( flags & LOCK_SH ) || ( flags & LOCK_EX ) ) ) ) { |
0 |
0 |
0 |
|
| 197 |
0 |
| errno = EINVAL; |
| 198 |
0 |
| return -1; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
0 |
| nonblock = ( flags & LOCK_NB ) == LOCK_NB; |
| 202 |
|
| |
| 203 |
0 |
| lockmode = flags & ~ LOCK_NB; |
| 204 |
|
| |
| 205 |
|
| /* succeed if we already have the requested lock type */ |
| 206 |
0 |
▶ | if ( share->lock == lockmode ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->lock == lockmode ) |
0 |
0 |
0 |
|
| 207 |
0 |
| return 0; |
| 208 |
|
| |
| 209 |
|
| /* release a lock; this is simpler logic than asserting one */ |
| 210 |
0 |
▶ | if ( lockmode & LOCK_UN ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( lockmode & LOCK_UN ) { |
0 |
0 |
0 |
|
| 211 |
|
| |
| 212 |
0 |
▶ | if ( share->lock & LOCK_SH ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->lock & LOCK_SH ) |
0 |
0 |
0 |
|
| 213 |
0 |
| rc = REL_SH_LOCK(share); |
| 214 |
|
| else |
| 215 |
0 |
| rc = REL_EX_LOCK(share); |
| 216 |
|
| |
| 217 |
0 |
▶ | if ( rc == 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) |
0 |
0 |
0 |
|
| 218 |
0 |
| share->lock = LOCK_UN; |
| 219 |
|
| |
| 220 |
0 |
| return rc; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| /* clear the old incorrect lock if necessary */ |
| 224 |
0 |
▶ | if ( ! ( share->lock & LOCK_UN ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! ( share->lock & LOCK_UN ) ) { |
0 |
0 |
0 |
|
| 225 |
|
| |
| 226 |
0 |
| rc = 0; |
| 227 |
|
| |
| 228 |
0 |
▶ | if ( share->lock & LOCK_SH ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->lock & LOCK_SH ) |
0 |
0 |
0 |
|
| 229 |
0 |
| rc = REL_SH_LOCK(share); |
| 230 |
|
| else |
| 231 |
0 |
| rc = REL_EX_LOCK(share); |
| 232 |
|
| |
| 233 |
0 |
▶ | if ( rc < 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc < 0 ) |
0 |
0 |
0 |
|
| 234 |
0 |
| return -1; |
| 235 |
|
| |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
|
| /* set the new lock */ |
| 239 |
0 |
▶ | if ( lockmode & LOCK_EX ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( lockmode & LOCK_EX ) { |
0 |
0 |
0 |
|
| 240 |
|
| |
| 241 |
0 |
| rc = nonblock |
| 242 |
0 |
| ? GET_EX_LOCK_NB(share) |
| 243 |
0 |
▶ | : GET_EX_LOCK(share); |
Branch: 1 branch
| Branch | True | False | Total |
| : GET_EX_LOCK(share); |
0 |
0 |
0 |
|
| 244 |
|
| |
| 245 |
0 |
▶ | } else if ( lockmode & LOCK_SH ) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( lockmode & LOCK_SH ) { |
0 |
0 |
0 |
|
| 246 |
|
| |
| 247 |
0 |
| rc = nonblock |
| 248 |
0 |
| ? GET_SH_LOCK_NB(share) |
| 249 |
0 |
▶ | : GET_SH_LOCK(share); |
Branch: 1 branch
| Branch | True | False | Total |
| : GET_SH_LOCK(share); |
0 |
0 |
0 |
|
| 250 |
|
| |
| 251 |
|
| } else { |
| 252 |
|
| |
| 253 |
0 |
| errno = EINVAL; |
| 254 |
0 |
| rc = -1; |
| 255 |
|
| |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
0 |
▶ | if ( nonblock && ( rc < 0 ) && ( errno == EAGAIN ) ) |
Branch: 3 branches
| Branch | True | False | Total |
| if ( nonblock && ( rc < 0 ) && ( errno == EAGAIN ) ) |
0 |
0 |
0 |
| if ( nonblock && ( rc < 0 ) && ( errno == EAGAIN ) ) |
0 |
0 |
0 |
| if ( nonblock && ( rc < 0 ) && ( errno == EAGAIN ) ) |
0 |
0 |
0 |
|
| 259 |
0 |
| return 1; |
| 260 |
|
| |
| 261 |
0 |
▶ | if ( rc == 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( rc == 0 ) |
0 |
0 |
0 |
|
| 262 |
0 |
| share->lock = lockmode; |
| 263 |
|
| |
| 264 |
0 |
| return rc; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
|
| |
| 268 |
0 |
| int sharelite_locked( Share *share, int flags ) { |
| 269 |
|
| |
| 270 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 271 |
|
| |
| 272 |
0 |
| return share->lock == flags; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| |
| 276 |
|
| /* SHARELITE INPUT-OUTPUT FUNCTIONS */ |
| 277 |
|
| |
| 278 |
0 |
| int sharelite_store( Share *share, char *data, int length ) { |
| 279 |
|
| char *srcaddr; |
| 280 |
|
| char *dstaddr; |
| 281 |
|
| Node *node; |
| 282 |
|
| int left, size, chunk; |
| 283 |
|
| |
| 284 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 285 |
|
| |
| 286 |
0 |
▶ | REQ_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
|
| 287 |
|
| |
| 288 |
0 |
| node = share->head; |
| 289 |
|
| |
| 290 |
|
| /* set to zero so if the write bombs, we don't get corrupt data */ |
| 291 |
0 |
| node->shminfo->data_length = 0; |
| 292 |
|
| |
| 293 |
|
| /* fetch the first and subsequent chunk sizes */ |
| 294 |
0 |
| size = share->size_data; |
| 295 |
0 |
| chunk = node->shminfo->size_chunkseg - sizeof( Header ); |
| 296 |
|
| |
| 297 |
|
| /* initialize the copy pointers */ |
| 298 |
0 |
| dstaddr = node->shmdata; |
| 299 |
0 |
| srcaddr = data; |
| 300 |
|
| |
| 301 |
|
| /* decide if the data will fit in the top segment */ |
| 302 |
0 |
▶ | if ( length < size ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( length < size ) { |
0 |
0 |
0 |
|
| 303 |
0 |
| size = length; |
| 304 |
0 |
| left = 0; |
| 305 |
|
| } else |
| 306 |
0 |
| left = length - size; |
| 307 |
|
| |
| 308 |
|
| /* copy data to top segment */ |
| 309 |
0 |
▶ | if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
0 |
0 |
0 |
|
| 310 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 311 |
0 |
| return -1; |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
|
| /* copy any chunks to their own segments */ |
| 315 |
0 |
▶ | while ( left ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( left ) { |
0 |
0 |
0 |
|
| 316 |
|
| |
| 317 |
|
| /* catches other processes freeing shared segments */ |
| 318 |
0 |
▶ | if ( node->next != NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next != NULL ) |
0 |
0 |
0 |
|
| 319 |
0 |
▶ | if ( node->next->shmid != node->shmhead->next_shmid ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next->shmid != node->shmhead->next_shmid ) |
0 |
0 |
0 |
|
| 320 |
0 |
▶ | if ( _sharelite_shm_forget( share, node ) == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( _sharelite_shm_forget( share, node ) == -1 ) { |
0 |
0 |
0 |
|
| 321 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 322 |
0 |
| return -1; |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
|
| /* catches running into the end of the attached segments */ |
| 326 |
0 |
▶ | if ( node->next == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next == NULL ) |
0 |
0 |
0 |
|
| 327 |
0 |
▶ | if ( _sharelite_shm_append( share ) == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( _sharelite_shm_append( share ) == -1 ) { |
0 |
0 |
0 |
|
| 328 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 329 |
0 |
| return -1; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
|
| /* advance the pointers */ |
| 333 |
0 |
| node = node->next; |
| 334 |
0 |
| dstaddr = node->shmdata; |
| 335 |
0 |
| srcaddr += size; |
| 336 |
0 |
| size = ( left > chunk ) ? chunk : left; |
| 337 |
|
| |
| 338 |
|
| /* copy a chunk to its segment */ |
| 339 |
0 |
▶ | if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
0 |
0 |
0 |
|
| 340 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 341 |
0 |
| return -1; |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
|
| /* adjust remaining data size */ |
| 345 |
0 |
| left -= size; |
| 346 |
|
| |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| /* deallocate unneeded chunk segments */ |
| 350 |
0 |
| _sharelite_shm_remove( share, node ); |
| 351 |
|
| |
| 352 |
|
| /* increment serial and save length */ |
| 353 |
0 |
| node->shminfo->data_serial++; |
| 354 |
0 |
| node->shminfo->data_length = length; |
| 355 |
|
| |
| 356 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 357 |
|
| |
| 358 |
0 |
| return 0; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
0 |
| int sharelite_fetch( Share *share, char **data ) { |
| 362 |
|
| char *srcaddr; |
| 363 |
|
| char *dstaddr; |
| 364 |
|
| Node *node; |
| 365 |
|
| int length, left, size, chunk; |
| 366 |
|
| |
| 367 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 368 |
|
| |
| 369 |
0 |
| node = share->head; |
| 370 |
0 |
| length = node->shminfo->data_length; |
| 371 |
|
| |
| 372 |
0 |
▶ | if ( ( *data = dstaddr = (char *) malloc( length ) ) == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ( *data = dstaddr = (char *) malloc( length ) ) == NULL ) |
0 |
0 |
0 |
|
| 373 |
0 |
| return -1; |
| 374 |
|
| |
| 375 |
0 |
▶ | REQ_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| REQ_SH_LOCK(share); |
0 |
0 |
0 |
| REQ_SH_LOCK(share); |
0 |
0 |
0 |
|
| 376 |
|
| |
| 377 |
0 |
| size = share->size_data; |
| 378 |
0 |
| chunk = node->shminfo->size_chunkseg - sizeof( Header ); |
| 379 |
0 |
| srcaddr = node->shmdata; |
| 380 |
|
| |
| 381 |
0 |
▶ | if ( length < size ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( length < size ) { |
0 |
0 |
0 |
|
| 382 |
0 |
| size = length; |
| 383 |
0 |
| left = 0; |
| 384 |
|
| } else |
| 385 |
0 |
| left = length - size; |
| 386 |
|
| |
| 387 |
0 |
▶ | if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
0 |
0 |
0 |
|
| 388 |
0 |
▶ | END_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| END_SH_LOCK(share); |
0 |
0 |
0 |
| END_SH_LOCK(share); |
0 |
0 |
0 |
|
| 389 |
0 |
| return -1; |
| 390 |
|
| } |
| 391 |
|
| |
| 392 |
0 |
▶ | while ( left ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( left ) { |
0 |
0 |
0 |
|
| 393 |
|
| |
| 394 |
0 |
▶ | if ( node->next != NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next != NULL ) |
0 |
0 |
0 |
|
| 395 |
0 |
▶ | if ( node->next->shmid != node->shmhead->next_shmid ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next->shmid != node->shmhead->next_shmid ) |
0 |
0 |
0 |
|
| 396 |
0 |
▶ | if ( _sharelite_shm_forget( share, node ) == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( _sharelite_shm_forget( share, node ) == -1 ) { |
0 |
0 |
0 |
|
| 397 |
0 |
▶ | END_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| END_SH_LOCK(share); |
0 |
0 |
0 |
| END_SH_LOCK(share); |
0 |
0 |
0 |
|
| 398 |
0 |
| return -1; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
0 |
▶ | if ( node->next == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->next == NULL ) |
0 |
0 |
0 |
|
| 402 |
0 |
▶ | if ( _sharelite_shm_append( share ) == -1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( _sharelite_shm_append( share ) == -1 ) { |
0 |
0 |
0 |
|
| 403 |
0 |
▶ | END_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| END_SH_LOCK(share); |
0 |
0 |
0 |
| END_SH_LOCK(share); |
0 |
0 |
0 |
|
| 404 |
0 |
| return -1; |
| 405 |
|
| } |
| 406 |
|
| |
| 407 |
0 |
| node = node->next; |
| 408 |
0 |
| srcaddr = node->shmdata; |
| 409 |
0 |
| dstaddr += size; |
| 410 |
0 |
| size = ( left > chunk ) ? chunk : left; |
| 411 |
|
| |
| 412 |
0 |
▶ | if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( memcpy( dstaddr, srcaddr, size ) == NULL ) { |
0 |
0 |
0 |
|
| 413 |
0 |
▶ | END_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| END_SH_LOCK(share); |
0 |
0 |
0 |
| END_SH_LOCK(share); |
0 |
0 |
0 |
|
| 414 |
0 |
| return -1; |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
0 |
| left -= size; |
| 418 |
|
| |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
0 |
▶ | END_SH_LOCK(share); |
Branch: 2 branches
| Branch | True | False | Total |
| END_SH_LOCK(share); |
0 |
0 |
0 |
| END_SH_LOCK(share); |
0 |
0 |
0 |
|
| 422 |
|
| |
| 423 |
0 |
| return length; |
| 424 |
|
| } |
| 425 |
|
| |
| 426 |
|
| |
| 427 |
|
| /* SHARELITE OBJECT ACCESSOR FUNCTIONS */ |
| 428 |
|
| |
| 429 |
0 |
| int sharelite_key( Share *share ) { |
| 430 |
|
| |
| 431 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 432 |
|
| |
| 433 |
0 |
| return share->key; |
| 434 |
|
| } |
| 435 |
|
| |
| 436 |
0 |
| int sharelite_shmid( Share *share ) { |
| 437 |
|
| |
| 438 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 439 |
|
| |
| 440 |
0 |
| return share->shmid; |
| 441 |
|
| } |
| 442 |
|
| |
| 443 |
0 |
| int sharelite_flags( Share *share ) { |
| 444 |
|
| |
| 445 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 446 |
|
| |
| 447 |
0 |
| return share->flags; |
| 448 |
|
| } |
| 449 |
|
| |
| 450 |
0 |
| int sharelite_length( Share *share ) { |
| 451 |
|
| |
| 452 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 453 |
|
| |
| 454 |
0 |
| return share->head->shminfo->data_length; |
| 455 |
|
| } |
| 456 |
|
| |
| 457 |
0 |
| int sharelite_serial( Share *share ) { |
| 458 |
|
| |
| 459 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 460 |
|
| |
| 461 |
0 |
| return share->head->shminfo->data_serial; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
0 |
| int sharelite_nsegments( Share *share ) { |
| 465 |
|
| |
| 466 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 467 |
|
| |
| 468 |
0 |
| return share->head->shminfo->data_chunks + 1; |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
0 |
| int sharelite_top_seg_size( Share *share ) { |
| 472 |
|
| |
| 473 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 474 |
|
| |
| 475 |
0 |
| return share->head->shminfo->size_topseg; |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
0 |
| int sharelite_chunk_seg_size( Share *share, int segsize ) { |
| 479 |
|
| |
| 480 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 481 |
|
| |
| 482 |
0 |
▶ | if ( segsize > 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( segsize > 0 ) { |
0 |
0 |
0 |
|
| 483 |
|
| /* trying to set a new segment size */ |
| 484 |
|
| |
| 485 |
0 |
▶ | REQ_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
|
| 486 |
|
| |
| 487 |
0 |
▶ | if ( share->head->shminfo->data_length > share->size_data ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( share->head->shminfo->data_length > share->size_data ) { |
0 |
0 |
0 |
|
| 488 |
|
| /* there are chunk segments defined already */ |
| 489 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 490 |
0 |
| errno = EINVAL; |
| 491 |
0 |
| return -1; |
| 492 |
|
| } |
| 493 |
|
| |
| 494 |
0 |
| share->head->shminfo->size_chunkseg = segsize; |
| 495 |
|
| |
| 496 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 497 |
|
| |
| 498 |
|
| } |
| 499 |
|
| |
| 500 |
0 |
| return share->head->shminfo->size_chunkseg; |
| 501 |
|
| } |
| 502 |
|
| |
| 503 |
0 |
| int sharelite_nconns( Share *share ) { |
| 504 |
|
| |
| 505 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 506 |
|
| |
| 507 |
0 |
| return _sharelite_shm_nconns( share ); |
| 508 |
|
| } |
| 509 |
|
| |
| 510 |
0 |
| int sharelite_nrefs( Share *share ) { |
| 511 |
|
| |
| 512 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 513 |
|
| |
| 514 |
0 |
| return share->head->shminfo->nrefs; |
| 515 |
|
| } |
| 516 |
|
| |
| 517 |
0 |
| int sharelite_incref( Share *share ) { |
| 518 |
|
| |
| 519 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 520 |
|
| |
| 521 |
0 |
▶ | REQ_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
|
| 522 |
0 |
| share->head->shminfo->nrefs++; |
| 523 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 524 |
|
| |
| 525 |
0 |
| return share->head->shminfo->nrefs; |
| 526 |
|
| } |
| 527 |
|
| |
| 528 |
0 |
| int sharelite_decref( Share *share ) { |
| 529 |
|
| |
| 530 |
0 |
▶ | CALL_NEEDS_SHARE_AND_HEAD(-1); |
Branch: 2 branches
| Branch | True | False | Total |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
| CALL_NEEDS_SHARE_AND_HEAD(-1); |
0 |
0 |
0 |
|
| 531 |
|
| |
| 532 |
0 |
▶ | REQ_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
| REQ_EX_LOCK(share); |
0 |
0 |
0 |
|
| 533 |
0 |
| share->head->shminfo->nrefs--; |
| 534 |
0 |
▶ | END_EX_LOCK(share); |
Branch: 4 branches
| Branch | True | False | Total |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
| END_EX_LOCK(share); |
0 |
0 |
0 |
|
| 535 |
|
| |
| 536 |
0 |
| return share->head->shminfo->nrefs; |
| 537 |
|
| } |
| 538 |
|
| |
| 539 |
0 |
| int sharelite_is_valid( Share *share ) { |
| 540 |
|
| |
| 541 |
0 |
▶ | CALL_NEEDS_SHARE(-1); |
Branch: 1 branch
| Branch | True | False | Total |
| CALL_NEEDS_SHARE(-1); |
0 |
0 |
0 |
|
| 542 |
|
| |
| 543 |
0 |
| return _sharelite_sem_access( share->semid ) == 0; |
| 544 |
|
| } |
| 545 |
|
| |