X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FSharedMem.c;h=16cae770d9c4dc8afddf2bb4b58a97eaa51f7cc1;hp=eab28f471368364e0ce34b950f81edd22712858f;hb=HEAD;hpb=730b0bb045c1794c4f4675605dc106b756e82d69 diff --git a/sources/SharedMem.c b/sources/SharedMem.c index eab28f4..16cae77 100644 --- a/sources/SharedMem.c +++ b/sources/SharedMem.c @@ -26,19 +26,19 @@ * * Author: S. Piccardi * - * $Id: SharedMem.c,v 1.3 2003/02/02 20:35:33 piccardi Exp $ - * ***************************************************************/ -#include /* SysV IPC shared memory declarations */ -#include -#include -#include /* standard I/O functions */ -#include -#include /* signal handling declarations */ -#include +#include /* SysV shared memory */ +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ +#include /* standard I/O library */ +#include /* file control functions */ +#include /* signal constants, types and functions */ +#include /* unix standard library */ #include -#include -#include +#include /* C strings library */ +#include /* error definitions and routines */ + +#include "macros.h" /* ************************************************************************* * * Functions for SysV shared memory @@ -62,7 +62,7 @@ * the fill value * Return: the address of the shared memory segment (NULL on error) */ -void * ShmCreate(key_t ipc_key, int shm_size, int perm, char fill) +void * ShmCreate(key_t ipc_key, int shm_size, int perm, int fill) { void * shm_ptr; int shm_id; /* ID of the IPC shared memory segment */ @@ -100,7 +100,7 @@ void * ShmFind(key_t ipc_key, int shm_size) } /* * Function ShmRemove: - * Scheudle removal for a SysV shared memory segment + * Schedule removal for a SysV shared memory segment * Input: an IPC key value * the shared memory segment size * Return: 0 on success, -1 on error @@ -140,24 +140,27 @@ int ShmRemove(key_t ipc_key, void * shm_ptr) * the fill value * Return: the address of the shared memory segment (NULL on error) */ -void * CreateShm(char * shm_name, int shm_size, int perm, char fill) +void * CreateShm(char * shm_name, off_t shm_size, mode_t perm, int fill) { void * shm_ptr; int fd; int flag; /* first open the object, creating it if not existent */ - flag = O_RDWR|O_TRUNC|O_CREAT|O_EXCL; + flag = O_CREAT|O_EXCL|O_RDWR; fd = shm_open(shm_name, flag, perm); /* get object file descriptor */ if (fd < 0) { + perror("errore in shm_open"); return NULL; } /* set the object size */ if (ftruncate(fd, shm_size)) { + perror("errore in ftruncate"); return NULL; } /* map it in the process address space */ shm_ptr = mmap(NULL, shm_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0); if (shm_ptr == MAP_FAILED) { + perror("errore in mmap"); return NULL; } memset((void *) shm_ptr, fill, shm_size); /* fill segment */ @@ -170,12 +173,13 @@ void * CreateShm(char * shm_name, int shm_size, int perm, char fill) * the shared memory segment size * Return: the address of the segment (NULL on error) */ -void * FindShm(char * shm_name, int shm_size) +void * FindShm(char * shm_name, off_t shm_size) { void * shm_ptr; int fd; /* ID of the IPC shared memory segment */ /* find shared memory ID */ if ((fd = shm_open(shm_name, O_RDWR|O_EXCL, 0)) < 0) { + debug("Cannot open %s\n", shm_name); return NULL; } /* take the pointer to it */ @@ -193,6 +197,5 @@ void * FindShm(char * shm_name, int shm_size) */ int RemoveShm(char * shm_name) { - shm_unlink(shm_name); - return 0; + return shm_unlink(shm_name); }