~ubuntu-branches/ubuntu/breezy/libipc-sharelite-perl/breezy

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
#include "config.h"
#include "sharelite.h"

#ifndef errno 
extern int errno;
#endif

#ifndef HAS_UNION_SEMUN
union semun {
  int             val;
  struct semid_ds *buf;
  unsigned short  *array;
};
#endif /* HAS_UNION_SEMUN */

/* --- DEFINE MACROS FOR SEMAPHORE OPERATIONS --- */

#define GET_EX_LOCK(A)    semop((A), &ex_lock[0],    3)
#define GET_EX_LOCK_NB(A) semop((A), &ex_lock_nb[0], 3)
#define RM_EX_LOCK(A)     semop((A), &ex_unlock[0],  1)
#define GET_SH_LOCK(A)    semop((A), &sh_lock[0],    2)
#define GET_SH_LOCK_NB(A) semop((A), &sh_lock_nb[0], 2)
#define RM_SH_LOCK(A)     semop((A), &sh_unlock[0],  1) 

/* --- DEFINE STRUCTURES FOR MANIPULATING SEMAPHORES --- */

static struct sembuf ex_lock[3] = {
  { 1, 0, 0 },        /* wait for readers to finish */
  { 2, 0, 0 },        /* wait for writers to finish */
  { 2, 1, SEM_UNDO }  /* assert write lock */
};

static struct sembuf ex_lock_nb[3] = {
  { 1, 0, IPC_NOWAIT },             /* wait for readers to finish */
  { 2, 0, IPC_NOWAIT },             /* wait for writers to finish */
  { 2, 1, (SEM_UNDO | IPC_NOWAIT) } /* assert write lock */     
};

static struct sembuf ex_unlock[1] = {
  { 2, -1, (SEM_UNDO | IPC_NOWAIT) } /* remove write lock */
};

static struct sembuf sh_lock[2] = {
  { 2, 0, 0 },        /* wait for writers to finish */
  { 1, 1, SEM_UNDO }  /* assert shared read lock */
};

static struct sembuf sh_lock_nb[2] = {
  { 2, 0, IPC_NOWAIT },             /* wait for writers to finish */
  { 1, 1, (SEM_UNDO | IPC_NOWAIT) } /* assert shared read lock */
};                

static struct sembuf sh_unlock[1] = {
  { 1, -1, (SEM_UNDO | IPC_NOWAIT) } /* remove shared read lock */
};                                 

/* USER INITIATED LOCK */

/* returns 0  on success -- requested operation performed   *
 * returns -1 on error                                      *
 * returns 1 if LOCK_NB specified and operation would block */
int sharelite_lock(Share *share, int flags) {

  if (!flags) /* try to obtain exclusive lock by default */
    flags = LOCK_EX;

  /* Check for invalid combination of flags.  Invalid combinations *
   * are attempts to obtain *both* an exclusive and shared lock or *
   * to both obtain and release a lock at the same time            */ 
  if (((flags & LOCK_EX) && (flags & LOCK_SH)) ||
     ((flags & LOCK_UN) && ((flags & LOCK_EX) || (flags & LOCK_SH)))) 
    return -1;

  if (flags & LOCK_EX) { /*** WANTS EXCLUSIVE LOCK ***/
    /* If they already have an exclusive lock, just return */
    if (share->lock & LOCK_EX) 
      return 0;
    /* If they currently have a shared lock, remove it */
    if (share->lock & LOCK_SH) { 
      if (RM_SH_LOCK(share->semid) < 0)
        return -1;
      share->lock = 0;
    }
    if (flags & LOCK_NB) { /* non-blocking request */
      if (GET_EX_LOCK_NB(share->semid) < 0) {
        if (errno == EAGAIN) /* would we have blocked? */
          return 1;
        return -1; 
      }
    } else { /* blocking request */
      if (GET_EX_LOCK(share->semid) < 0) 
        return -1;
    }    
    share->lock = LOCK_EX;
    return 0; 
  } else if (flags & LOCK_SH) { /*** WANTS SHARED LOCK ***/ 
    /* If they already have a shared lock, just return */
    if (share->lock & LOCK_SH) 
      return 0;
    /* If they currently have an exclusive lock, remove it */
    if (share->lock & LOCK_EX) {
      if (RM_EX_LOCK(share->semid) < 0) 
        return -1;
      share->lock = 0; 
    }
    if (flags & LOCK_NB) { /* non-blocking request */
      if (GET_SH_LOCK_NB(share->semid) < 0) {
        if (errno == EAGAIN) /* would we have blocked? */
          return 1;
        return -1;
      }              
    } else { /* blocking request */
      if (GET_SH_LOCK(share->semid) < 0) 
        return -1;
    }
    share->lock = LOCK_SH;
    return 0;
  } else if (flags & LOCK_UN) { /*** WANTS TO RELEASE LOCK ***/
    if (share->lock & LOCK_EX) {
      if (RM_EX_LOCK(share->semid) < 0)
        return -1;
    } else if (share->lock & LOCK_SH) {
      if (RM_SH_LOCK(share->semid) < 0)
        return -1;
    }
  }

  return 0;
}

int sharelite_unlock(Share *share) {
  if (share->lock & LOCK_EX) {
    if (RM_EX_LOCK(share->semid) < 0) 
      return -1;
  } else if (share->lock & LOCK_SH) { 
    if (RM_SH_LOCK(share->semid) < 0) 
      return -1; 
  } 
  share->lock = 0;
  return 0;
}

Node *_add_segment(Share *share) {
  Node *node;
  int  flags;

  if ((node = (Node *) malloc(sizeof(Node))) == NULL) 
    return NULL; 
  node->next = NULL;

  /* Does another shared memory segment already exist? */
  if (share->tail->shmaddr->next_shmid >= 0) { 
    node->shmid   = share->tail->shmaddr->next_shmid;
    if ((node->shmaddr = (Header *) shmat(node->shmid, (char *) 0, 0)) == (Header *) -1)
      return NULL;
    share->tail->next = node;
    share->tail       = node;
    return node;
  }

  flags = share->flags|IPC_CREAT|IPC_EXCL;

  /* We need to create a new segment */
  while(1) {
    node->shmid = shmget(share->next_key++, share->segment_size, flags);
    if (node->shmid >= 0) break;
#ifdef EIDRM
    if (errno == EEXIST || errno == EIDRM) continue;
#else
    if (errno == EEXIST) continue;
#endif
    return NULL;                            
  } 

  share->tail->shmaddr->next_shmid = node->shmid; 
  share->tail->next = node;
  share->tail = node;
  if ((node->shmaddr = (Header *) shmat(node->shmid, (char *) 0, 0)) == (Header *) -1) 
    return NULL;
  node->shmaddr->next_shmid = -1;
  node->shmaddr->length     = 0;

  return node;
}

int _detach_segments(Node *node) {
  Node *next_node;

  while(node != NULL) {
    next_node  = node->next;
    if (shmdt((char *) node->shmaddr) < 0) return -1;
    free(node);
    node = next_node;
  }
  return 0;             
}

int _remove_segments(int shmid) {
  int    next_shmid;
  Header *shmaddr;

  while(shmid >= 0) {
    if ((shmaddr = (Header *) shmat(shmid, (char *) 0, 0)) == (Header *) -1) return -1;
    next_shmid = shmaddr->next_shmid;
    if (shmdt((char *) shmaddr) < 0) return -1;   
    if (shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0) < 0) return -1;
    shmid = next_shmid;
  }

  return 0;
}

int _invalidate_segments(Share* share) {

  if (_detach_segments( share->head->next ) < 0) 
    return -1;
  share->head->next = NULL;
  share->tail       = share->head;
  share->shm_state  = share->head->shmaddr->shm_state;
 
  return 0;           
}

int write_share(Share *share, char *data, int length) {
  char *shmaddr;
  int  segments;
  int  left;
  int  chunk_size;
  Node *node;
  int  shmid;

  if (data == NULL) 
    return -1;

  if (! (share->lock & LOCK_EX)) {
    if (share->lock & LOCK_SH) {
      if (RM_SH_LOCK(share->semid) < 0) 
        return -1;
    }
    if (GET_EX_LOCK(share->semid) < 0) 
      return -1;
  }

  if (share->shm_state != share->head->shmaddr->shm_state) {
    if (_invalidate_segments(share) < 0)
      return -1;
  } 

  /* set the data length to zero.  if we are interrupted or encounter *
   * an error during the write, this guarantees that we won't         *
   * receive corrupt data in future reads.                            */
  share->head->shmaddr->length = 0;

  /* compute number of segments necessary to hold data */
  segments = (length / share->data_size) + (length % share->data_size ? 1 : 0); 

  node = share->head;
  left = length;
  while(segments--) {
    if (node == NULL) {
      if ((node = _add_segment(share)) == NULL) 
        return -1;
    }
    chunk_size = (left > share->data_size ? share->data_size : left);
    shmaddr = (char *) node->shmaddr + sizeof(Header);
    if (memcpy(shmaddr, data, chunk_size) == NULL)
      return -1;
    left -= chunk_size;
    data += chunk_size;
    if (segments) 
      node = node->next;
  }

  /* set new length in header of first segment */
  share->head->shmaddr->length = length;       

  /* garbage collection -- remove unused segments */
  if (node->shmaddr->next_shmid >= 0) {
    shmid = node->shmaddr->next_shmid;
    if (_detach_segments(node->next) < 0) 
      return -1;
    if (_remove_segments(shmid) < 0) 
      return -1;   
    node->shmaddr->next_shmid = -1;
    node->next = NULL;
    share->tail = node;
    share->head->shmaddr->shm_state++;
  }

  ++share->head->shmaddr->version;

  if (! (share->lock & LOCK_EX)) {
    if (RM_EX_LOCK(share->semid) < 0) 
      return -1;
    if (share->lock & LOCK_SH) {
      if (GET_SH_LOCK(share->semid) < 0) 
        return -1;
    }
  }

  return 0;
}

int read_share(Share *share, char **data) {
  char *shmaddr;
  char *pos;
  Node *node;
  int  length;
  int  left;
  int  chunk_size;

  if (! share->lock) {
    if (GET_SH_LOCK(share->semid) < 0) 
      return -1;
  }

  if (share->shm_state != share->head->shmaddr->shm_state) {
    if (_invalidate_segments(share) < 0) 
      return -1;
  }             
 
  node   = share->head;
  left = length = node->shmaddr->length;

  if ((pos = *data = (char *) malloc( length )) == NULL)
    return -1;

  while(left) {
    if (node == NULL) {
      if ((node = _add_segment(share)) == NULL) 
        return -1;
    }                       
    chunk_size = (left > share->data_size ? share->data_size : left);
    shmaddr = (char *) node->shmaddr + sizeof(Header);
    if (memcpy(pos, shmaddr, chunk_size) == NULL) 
      return -1;
    pos    += chunk_size;
    left -= chunk_size; 
    node = node->next;  
  }

  if (! share->lock) {
    if (RM_SH_LOCK(share->semid) < 0) 
      return -1;
  }

  return length; 
}

Share *new_share(key_t key, int segment_size, int flags) {
  Share  *share;
  Node   *node;
  int    semid;
  struct shmid_ds shmctl_arg;
  union  semun    semun_arg; 

again:
  if ((semid = semget(key, 3, flags)) < 0) 
    return NULL;

  /* It's possible for another process to obtain the semaphore, lock it, *
   * and remove it from the system before we have a chance to lock it.   *
   * In this case (EINVAL) we just try to create it again.               */
  if (GET_EX_LOCK(semid) < 0) {
    if (errno == EINVAL)
      goto again;
    return NULL;
  }

  /* XXX IS THIS THE RIGHT THING TO DO? */
  if (segment_size <= sizeof(Header)) {
    segment_size = SHM_SEGMENT_SIZE; 
  }

  if ((node = (Node *) malloc(sizeof(Node))) == NULL) 
    return NULL;
  if ((node->shmid = shmget(key, segment_size, flags)) < 0)
    return NULL;
  if ((node->shmaddr = (Header *) shmat(node->shmid, (char *) 0, 0)) == (Header *) -1)
    return NULL;
  node->next = NULL;

  if ((share = (Share *) malloc(sizeof(Share))) == NULL) 
    return NULL;
  share->key       = key;
  share->next_key  = key + 1;
  share->flags     = flags;      
  share->semid     = semid;
  share->lock      = 0;
  share->head      = node;
  share->tail      = node;

  /* is this a newly created segment?  if so, initialize it */
  if ((semun_arg.val = semctl(share->semid, 0, GETVAL, semun_arg)) < 0) 
    return NULL;

  if (semun_arg.val == 0) {
    semun_arg.val = 1;
    if (semctl(share->semid, 0, SETVAL, semun_arg) < 0)
      return NULL;
    share->head->shmaddr->length      = 0;
    share->head->shmaddr->next_shmid  = -1;
    share->head->shmaddr->shm_state   = 1;
    share->head->shmaddr->version     = 1;
  } 

  share->shm_state = share->head->shmaddr->shm_state;
  share->version   = share->head->shmaddr->version;

  /* determine the true length of the segment.  this may disagree *
   * with what the user requested, since shmget() calls will      *
   * succeed if the requested size <= the existing size           */
  if (shmctl(share->head->shmid, IPC_STAT, &shmctl_arg) < 0) 
    return NULL;
  share->segment_size = shmctl_arg.shm_segsz;
  share->data_size    = share->segment_size - sizeof(Header);

  if (RM_EX_LOCK(semid) < 0) 
    return NULL;

  return share;
}

unsigned int sharelite_version(Share *share) {
  return share->head->shmaddr->version;
}

int destroy_share (Share *share, int rmid) {
  int   semid;
  union semun semctl_arg; 

  if (! (share->lock & LOCK_EX)) {
    if (share->lock & LOCK_SH) {
      if (RM_SH_LOCK(share->semid) < 0) 
        return -1;
    }
    if (GET_EX_LOCK(share->semid) < 0) 
      return -1;
  }                   

  semid = share->head->shmid;
  if (_detach_segments(share->head) < 0) 
    return -1;

  if (rmid) {
    if (_remove_segments(semid) < 0) 
      return -1;
    semctl_arg.val = 0;
    if (semctl(share->semid, 0, IPC_RMID, semctl_arg) < 0) 
      return -1;
  } else {
    if (RM_EX_LOCK(share->semid) < 0) 
      return -1;
  }

  free(share);
  
  return 0;
}

int sharelite_num_segments(Share* share) {
  int    count = 0;
  int    shmid;
  Header *shmaddr;

  shmid = share->head->shmid;
  while(shmid >= 0) {
    count++;
    if ((shmaddr = (Header *) shmat(shmid, (char *) 0, 0)) == (Header *) -1) 
      return -1;
    shmid = shmaddr->next_shmid;
    if (shmdt((char *) shmaddr) < 0) 
      return -1;  
  } 
  
  return count; 
}

void _dump_list(Share *share) {
  Node *node;

  node = share->head;
  while(node != NULL) {
    printf("shmid: %i\n", node->shmid);
    node = node->next;
  } 
}