342 lines
10 KiB
C
342 lines
10 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <node.h>
|
|
#define SM_MODE 0 /* Utilisation des API sécurisés */
|
|
#include <shmem.h>
|
|
|
|
VER_INFO_EXPORT (smadmin, "$Revision: 1.1 $", "$Name: $", __FILE__, "$Author: smas $")
|
|
|
|
#define USAGE "Usage : %s [ --help | --version [-v] | --create | --destroy ]\n"
|
|
|
|
#define QUIT 0
|
|
#define BASE_INIT 1
|
|
#define BASE_OPEN 2
|
|
#define BASE_INFO 3
|
|
#define BASE_CLOSE 4
|
|
#define BASE_END 5
|
|
#define MHH_INIT 6
|
|
#define MHH_OPEN 7
|
|
#define MHH_CHECK 8
|
|
#define MHH_COMPRESS 9
|
|
#define MHH_CLOSE 10
|
|
#define MHH_END 11
|
|
#define ALLOC_CHUNK 12
|
|
#define WRITE_CHUNK 13
|
|
#define READ_CHUNK 14
|
|
#define FREE_CHUNK 15
|
|
#define DSR_DUMP 16
|
|
#define ACR_DUMP 17
|
|
#define FCR_DUMP 18
|
|
|
|
char menu [1000];
|
|
char tmp [100];
|
|
|
|
void init_menu (void);
|
|
int print_menu (void);
|
|
|
|
int main (int argc, char ** argv)
|
|
{
|
|
int choice;
|
|
char name [100];
|
|
int Mode, Locked;
|
|
void * ptr;
|
|
char answer [10];
|
|
SMT_Heap * Heap;
|
|
int Nb_Detected, Nb_Corrected;
|
|
size_t size;
|
|
|
|
/* Lancement de commande d'administration */
|
|
|
|
if (argc >= 2)
|
|
{
|
|
if (!strcmp (argv[1], "--help"))
|
|
{
|
|
fprintf (stderr, USAGE, argv[0]);
|
|
return -1;
|
|
}
|
|
else if (!strcmp (argv[1], "--version"))
|
|
{
|
|
if (argc >= 3 && !strcmp (argv[2], "-v"))
|
|
return VER_Object_Print (stdout, VERD_VERBOSE);
|
|
else
|
|
return VER_Object_Print (stdout, VERD_MINIMAL);
|
|
}
|
|
else if (!strcmp (argv[1], "--create"))
|
|
{
|
|
if (SM_Library_Open (0, NULL, SMD_CREATE | SMD_DEBUG_ALL) != SMS_OK)
|
|
{
|
|
fprintf (stderr, "=> Impossible de créer l'instance de la librairie LIBSHMEM\n");
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
else if (!strcmp (argv[1], "--destroy"))
|
|
{
|
|
if (SM_Library_Open (0, NULL, SMD_OPEN | SMD_DEBUG_ALL) != SMS_OK || SM_Library_Close (SMD_DESTROY) != SMS_OK)
|
|
{
|
|
fprintf (stderr, "=> Impossible de détruire l'instance de la librairie LIBSHMEM\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
fprintf (stderr, USAGE, argv[0]);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/* Lancement du menu intercatif */
|
|
|
|
init_menu ();
|
|
|
|
choice = print_menu ();
|
|
|
|
while (choice != QUIT)
|
|
{
|
|
strcpy (answer, "yes");
|
|
|
|
switch (choice)
|
|
{
|
|
case BASE_INIT:
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Library_Open (0, NULL, SMD_CREATE | SMD_DEBUG_ALL) == SMS_OK ? "OK" : "NOK" );
|
|
break;
|
|
|
|
case BASE_OPEN:
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Library_Open (0, NULL, SMD_OPEN | SMD_DEBUG_ALL) == SMS_OK ? "OK" : "NOK" );
|
|
break;
|
|
|
|
case BASE_END:
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Library_Close (SMD_DESTROY) == SMS_OK ? "OK" : "NOK" );
|
|
break;
|
|
|
|
case BASE_CLOSE:
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Library_Close (SMD_CLOSE) == SMS_OK ? "OK" : "NOK" );
|
|
break;
|
|
|
|
case BASE_INFO:
|
|
SM_Library_Dump (stderr);
|
|
break;
|
|
|
|
case MHH_INIT:
|
|
fprintf (stdout, "\nNew heap name ? ");
|
|
gets (name);
|
|
fprintf (stdout, "\nHeap segment size ? ");
|
|
gets (tmp);
|
|
size = atoi (tmp);
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Heap_Open (name, &Heap, size, \
|
|
SMD_CREATE, &Locked) == SMS_OK ? "OK" : "NOK" );
|
|
break;
|
|
|
|
case MHH_OPEN:
|
|
fprintf (stdout, "\nHeap name to open ? ");
|
|
gets (name);
|
|
fprintf (stdout, "\nOpening mode (read=1 write=2) ? ");
|
|
gets (tmp);
|
|
if (tmp[0] == '1') Mode = SMD_READ;
|
|
else Mode = SMD_WRITE;
|
|
if (!strcmp (name, HEAP_SYSTEM))
|
|
{
|
|
fprintf (stdout, \
|
|
"\n*** Warning : you can not make this operation on the system heap ***\n");
|
|
break;
|
|
}
|
|
|
|
fprintf (stdout, "\nReturn code = %s\n", SM_Heap_Open (name, \
|
|
&Heap, 0, SMD_OPEN | Mode, &Locked) == SMS_OK ? "OK" : "NOK" );
|
|
|
|
break;
|
|
|
|
case MHH_COMPRESS:
|
|
fprintf (stdout, "\nHeap name à compresser ? ");
|
|
gets (name);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_WRITE, &Locked) == SMS_OK)
|
|
{
|
|
size_t Compress;
|
|
SM_Heap_Compress (Heap, &Compress);
|
|
fprintf (stdout, "\nCompression size = %d byte(s)\n", (int)Compress);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
break;
|
|
|
|
case MHH_END:
|
|
fprintf (stdout, "\nHeap name to remove ? ");
|
|
gets (name);
|
|
if (!strcmp (name, HEAP_SYSTEM))
|
|
{
|
|
fprintf (stdout, \
|
|
"\n*** Warning : you can not make this operation on the system heap ***\n");
|
|
break;
|
|
}
|
|
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Heap_End (name) == SMS_OK ? "OK" : "NOK" );
|
|
|
|
break;
|
|
|
|
case MHH_CLOSE:
|
|
fprintf (stdout, "\nHeap name to close ? ");
|
|
gets (name);
|
|
if (!strcmp (name, HEAP_SYSTEM))
|
|
{
|
|
fprintf (stdout, \
|
|
"\n*** Warning : you can not make this operation on the system heap ***\n");
|
|
break;
|
|
}
|
|
if (SM_Heap_IsOpen (name, &Heap) == SMS_OK)
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Heap_Close (Heap) == SMS_OK ? "OK" : "NOK" );
|
|
else
|
|
fprintf (stdout, "\nHeap %s is not opened\n", name);
|
|
|
|
break;
|
|
|
|
case MHH_CHECK:
|
|
fprintf (stdout, "\nHeap name to check/recover ? ");
|
|
gets (name);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_WRITE, &Locked) == SMS_OK)
|
|
{
|
|
Nb_Detected = Nb_Corrected = 0;
|
|
SM_Heap_Check (Heap, &Nb_Detected, &Nb_Corrected, stderr);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
break;
|
|
|
|
case ALLOC_CHUNK:
|
|
fprintf (stdout, "\nHeap name ? ");
|
|
gets (name);
|
|
fprintf (stdout, "\nAllocation size ? ");
|
|
gets (tmp);
|
|
size = atoi (tmp);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_WRITE, &Locked) == SMS_OK)
|
|
{
|
|
SM_Chunk_Alloc (Heap, size, &ptr);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
fprintf (stdout, "\nAddress = 0x%p (%d)\n", ptr, (unsigned int)ptr);
|
|
break;
|
|
|
|
case WRITE_CHUNK:
|
|
fprintf (stdout, "\nAddress ? ");
|
|
gets (tmp);
|
|
ptr = (void *)atoi (tmp);
|
|
fprintf (stdout, "\nString to put in ? ");
|
|
gets (tmp);
|
|
strcpy ( (char *)ptr, tmp);
|
|
fprintf (stdout, "\nOK\n");
|
|
break;
|
|
|
|
case READ_CHUNK:
|
|
fprintf (stdout, "\nAddress ? ");
|
|
gets (tmp);
|
|
ptr = (void *)atoi (tmp);
|
|
fprintf (stdout, "\nValeur du pointeur = %s\n", (char *)ptr);
|
|
break;
|
|
|
|
case FREE_CHUNK:
|
|
fprintf (stdout, "\nHeap name ? ");
|
|
gets (name);
|
|
fprintf (stdout, "\nAddress ? ");
|
|
gets (tmp);
|
|
ptr = (void *)atoi (tmp);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_WRITE, &Locked) == SMS_OK)
|
|
{
|
|
fprintf (stdout, "\nReturn code = %s\n", \
|
|
SM_Chunk_Free (Heap, ptr) == SMS_OK ? "OK" : "NOK" );
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
|
|
break;
|
|
|
|
case DSR_DUMP:
|
|
fprintf (stdout, "\nHeap name ? ");
|
|
gets (name);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_READ, &Locked) == SMS_OK)
|
|
{
|
|
ND_DataStruct_Info_Print (Heap->MHH->DSR, stdout);
|
|
ND_DataStruct_Print (Heap->MHH->DSR, stdout);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
break;
|
|
|
|
case ACR_DUMP:
|
|
fprintf (stdout, "\nHeap name ? ");
|
|
gets (name);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_READ, &Locked) == SMS_OK)
|
|
{
|
|
ND_DataStruct_Info_Print (Heap->MHH->ACR, stdout);
|
|
ND_DataStruct_Print (Heap->MHH->ACR, stdout);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
break;
|
|
|
|
case FCR_DUMP:
|
|
fprintf (stdout, "\nHeap name ? ");
|
|
gets (name);
|
|
if (SM_Heap_Open (name, &Heap, 0, SMD_OPEN | SMD_READ, &Locked) == SMS_OK)
|
|
{
|
|
ND_DataStruct_Info_Print (Heap->MHH->FCR, stdout);
|
|
ND_DataStruct_Print (Heap->MHH->FCR, stdout);
|
|
if (Locked == TRUE) SM_Heap_Unlock (Heap);
|
|
}
|
|
break;
|
|
|
|
case QUIT:
|
|
fprintf (stdout, "\nExiting program ...\n");
|
|
break;
|
|
|
|
default:
|
|
fprintf (stdout, "\nUndefined choice %d\n", choice);
|
|
}
|
|
choice = print_menu ();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void init_menu (void)
|
|
{
|
|
sprintf (menu, "Menu :\n");
|
|
sprintf (tmp, " - %2d) %-18s\n", QUIT, "Quit"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", BASE_INIT, "Init library"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", BASE_OPEN, "Open library"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", BASE_INFO, "Info library"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", BASE_CLOSE, "Close library"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", BASE_END, "End library"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", MHH_INIT, "Init heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", MHH_OPEN, "Open heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", MHH_CHECK, "Check/recover heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", MHH_COMPRESS, "Compress heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", MHH_CLOSE, "Close heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", MHH_END, "End heap"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", ALLOC_CHUNK, "Allocate chunk"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", WRITE_CHUNK, "Write chunk"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", READ_CHUNK, "Read chunk"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", FREE_CHUNK, "Free chunk"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", DSR_DUMP, "Dump DSR"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s", ACR_DUMP, "Dump ACR"); strcat (menu, tmp);
|
|
sprintf (tmp, " - %2d) %-18s\n", FCR_DUMP, "Dump FCR"); strcat (menu, tmp);
|
|
}
|
|
|
|
int print_menu (void)
|
|
{
|
|
fprintf (stdout, "------------------------------------------------------\n%s", menu);
|
|
|
|
tmp[0] = '\0';
|
|
while (tmp[0] == '\0')
|
|
{
|
|
printf ("\nChoice ? ");
|
|
gets (tmp);
|
|
}
|
|
|
|
return atoi (tmp);
|
|
}
|
|
|