| 1 |
#include <stdio.h> |
| 2 |
#include <memory.h> |
| 3 |
#include <string.h> |
| 4 |
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) |
| 5 |
#include <stdlib.h> |
| 6 |
#else |
| 7 |
#include <malloc.h> |
| 8 |
#endif |
| 9 |
#include "fitsio.h" /* needed to define LONGLONG */ |
| 10 |
#include "drvrsmem.h" /* uses LONGLONG */ |
| 11 |
|
| 12 |
int main(int argc, char **argv) |
| 13 |
{ int cmdok, listmode, longlistmode, recovermode, deletemode, id; |
| 14 |
int status; |
| 15 |
char *address; |
| 16 |
|
| 17 |
listmode = longlistmode = recovermode = deletemode = 0; |
| 18 |
id = -1; |
| 19 |
cmdok = 1; |
| 20 |
|
| 21 |
switch (argc) |
| 22 |
{ case 1: listmode = 1; |
| 23 |
break; |
| 24 |
case 2: |
| 25 |
if (0 == strcmp("-l", argv[1])) longlistmode = 1; |
| 26 |
else if (0 == strcmp("-r", argv[1])) recovermode = 1; |
| 27 |
else if (0 == strcmp("-d", argv[1])) deletemode = 1; |
| 28 |
else cmdok = 0; |
| 29 |
break; |
| 30 |
case 3: |
| 31 |
if (0 == strcmp("-r", argv[1])) recovermode = 1; |
| 32 |
else if (0 == strcmp("-d", argv[1])) deletemode = 1; |
| 33 |
else |
| 34 |
{ cmdok = 0; /* signal invalid cmd line syntax */ |
| 35 |
break; |
| 36 |
} |
| 37 |
if (1 != sscanf(argv[2], "%d", &id)) cmdok = 0; |
| 38 |
break; |
| 39 |
default: |
| 40 |
cmdok = 0; |
| 41 |
break; |
| 42 |
} |
| 43 |
|
| 44 |
if (0 == cmdok) |
| 45 |
{ printf("usage :\n\n"); |
| 46 |
printf("smem - list all shared memory segments\n"); |
| 47 |
printf("\t!\tcouldn't obtain RDONLY lock - info unreliable\n"); |
| 48 |
printf("\tIdx\thandle of shared memory segment (visible by application)\n"); |
| 49 |
printf("\tKey\tcurrent system key of shared memory segment. Key\n"); |
| 50 |
printf("\t\tchanges whenever shmem segment is reallocated. Use\n"); |
| 51 |
printf("\t\tipcs (or ipcs -a) to view all shmem segments\n"); |
| 52 |
printf("\tNproc\tnumber of processes attached to segment\n"); |
| 53 |
printf("\tSize\tsize of shmem segment in bytes\n"); |
| 54 |
printf("\tFlags\tRESIZABLE - realloc allowed, PERSIST - segment is not\n"); |
| 55 |
printf("\t\tdeleted after shared_free called by last process attached\n"); |
| 56 |
printf("\t\tto it.\n"); |
| 57 |
printf("smem -d - delete all shared memory segments (may block)\n"); |
| 58 |
printf("smem -d id - delete specific shared memory segment (may block)\n"); |
| 59 |
printf("smem -r - unconditionally reset all shared memory segments\n\t\t(does not block, recovers zombie handles left by kill -9)\n"); |
| 60 |
printf("smem -r id - unconditionally reset specific shared memory segment\n"); |
| 61 |
} |
| 62 |
|
| 63 |
if (shared_init(0)) |
| 64 |
{ printf("couldn't initialize shared memory, aborting ...\n"); |
| 65 |
return(10); |
| 66 |
} |
| 67 |
|
| 68 |
if (listmode) shared_list(id); |
| 69 |
else if (recovermode) shared_recover(id); |
| 70 |
else if (deletemode) shared_uncond_delete(id); |
| 71 |
|
| 72 |
for (id = 0; id <16; id++) { |
| 73 |
status = shared_getaddr(id, &address); |
| 74 |
if (!status) printf("id, status, address %d %d %p %.30s\n", id, status, (void *) address, address); |
| 75 |
} |
| 76 |
return(0); |
| 77 |
} |