-/***************************************************************
- *
- * File wrappers.h: define a set of macro and inlined
- * functions for signal, shared memory and semaphore handling
- *
- * Author: S. Piccardi
- *
- * $Id: wrappers.h,v 1.5 2002/12/02 23:06:59 piccardi Exp $
- *
- ***************************************************************/
-#include <sys/sem.h> /* IPC semaphore declarations */
-#include <sys/shm.h> /* IPC shared memory declarations */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <signal.h> /* signal handling declarations */
-/**
- ** Semaphore definition; used to implement a MutexXXXX API
- ** To create a Mutex use an underlaying semaphore and init it;
- ** we put here all the needed data structires
- **/
-/* put this definition, get from the man pages */
-#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
-/* union semun is defined by including <sys/sem.h> */
-#else
-/* according to X/OPEN we have to define it ourselves */
-union semun {
- int val; /* value for SETVAL */
- struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
- unsigned short int *array; /* array for GETALL, SETALL */
- struct seminfo *__buf; /* buffer for IPC_INFO */
-};
-#endif
-/*
- * Function MutexCreate: create a mutex/semaphore
- *
- * First call create a semaphore, using the given key.
- * We want only one semaphore so we set second argument to 1; third
- * parameter is the flag argument, and is set to create a semaphore
- * with R/W privilege for the user.
- * Second call initialize the semaphore to 1 (unlocked)
- *
- * Input: an IPC key value (to create an unique semaphore)
- * Return: the semaphore id# or -1 on error
- */
-inline int MutexCreate(key_t ipc_key)
-{
- const union semun semunion={1}; /* semaphore union structure */
- int sem_id, ret;
- sem_id = semget(ipc_key, 1, IPC_CREAT|0666); /* get semaphore ID */
- if (sem_id == -1) { /* if error return code */
- return sem_id;
- }
- ret = semctl(sem_id, 0, SETVAL, semunion); /* init semaphore */
- if (ret == -1) {
- return ret;
- }
- return sem_id;
-}
-/*
- * Function MutexFind: get the semaphore/mutex Id given the IPC key value
- *
- * Input: an IPC key value
- */
-inline int MutexFind(key_t ipc_key)
-{
- return semget(ipc_key,1,0);
-}
-/*
- * Function MutexRead: read the current value of the mutex/semaphore
- *
- * Input: a semaphore id #
- * Return: the semaphore value
- */
-inline int MutexRead(int sem_id)
-{
- return semctl(sem_id, 0, GETVAL);
-}
-/*
- * Define sembuf structures to lock and unlock the semaphore
- * (used to implement a mutex)
- */
-struct sembuf sem_lock={ /* to lock semaphore */
- 0, /* semaphore number (only one so 0) */
- -1, /* operation (-1 to use resource) */
- SEM_UNDO}; /* flag (set for undo at exit) */
-struct sembuf sem_ulock={ /* to unlock semaphore */
- 0, /* semaphore number (only one so 0) */
- 1, /* operation (1 to release resource) */
- SEM_UNO}; /* flag (in this case 0) */
-/*
- * Function MutexLock: to lock a mutex/semaphore
- *
- * Input: a semaphore id #
- * Output: semop return code (0 OK, -1 KO)
- */
-inline int MutexLock(int sem_id)
-{
- return semop(sem_id, &sem_lock, 1);
-}
-/*
- * Function MutexUnlock: to unlock a mutex/semaphore
- *
- * Input: a semaphore id #
- * Return: semop return code (0 OK, -1 KO)
- */
-inline int MutexUnlock(int sem_id)
-{
- return semop(sem_id, &sem_ulock, 1);
-}
-/*
- * Function ShmCreate:
- * Allocate a shared memory segment.
- * First call get a shared memory segment with KEY key access and size SIZE,
- * by creating it with R/W privilege for the user (this is the meaning of
- * the ored flags). The function return an identifier shmid used for any
- * further reference to the shared memory segment.
- * Second call attach the shared memory segment to this process and return a
- * pointer to it (of char * type).
- * Then initialize shared memory:
- * Set all to 0x55 (means 0101 un binary notation
- *
- * Input: an IPC key value
- * the shared memory segment size
- * Return: the address of the segment
- */
-inline char * ShmCreate(key_t ipc_key, int shm_size)
-{
- char * shptr;
- int shmid; /* ID of the IPC shared memory segment */
- if ((shmid=shmget(ipc_key,shm_size,IPC_CREAT|0666))<0){ /* get shm ID */
- perror("cannot find shared memory");
- exit(1);
- }
- if ( (shptr=shmat(shmid,0,0)) < 0 ){ /* take the pointer to it */
- perror("cannot attach shared memory");
- exit(1);
- }
- memset((void *)shptr,0x55,shm_size); /* second couter starts from "0" */
- return shptr;
-}
-/*
- * Function ShmFind:
- * Find a shared memory segment
- * Input: an IPC key value
- * the shared memory segment size
- * Return: the address of the segment
- */
-inline char * ShmFind(key_t ipc_key, int shm_size)
-{
- char * shptr;
- int shmid; /* ID of the IPC shared memory segment */
- if ( (shmid=shmget(ipc_key,shm_size,0))<0 ){ /* find shared memory ID */
- perror("cannot find shared memory");
- exit(1);
- }
- if ( (shptr=shmat(shmid,0,0)) < 0 ){ /* take the pointer to it */
- perror("cannot attach shared memory");
- exit(1);
- }
- return shptr;
-}
-/*
- * Function LockFile & UnlockFile:
- * Create and remove a lockfile of the given pathname.
- * Fail and exit in case of error or existence of the same lock
- * file, using unlink do not need to remove the file,
- */
-inline int LockFile(const char* path_name)
-{
- return open(path_name, O_EXCL|O_CREAT);
-}
-inline int UnlockFile(const char* path_name)
-{
- return unlink(path_name);
-}
-/*
- * Function Signal
- * Initialize a signal handler.
- * To enable the signal handling a process we need to tell it to
- * kernel; this is done writing all needed info to a sigaction structure
- * named sigact, and then callind sigaction() system call passing the
- * information stored in the sigact structure variable.
- *
- * Input: the signal to handle
- * the signal handler function
- * Return: the previous sigaction structure
- */
-typedef void SigFunc(int);
-inline SigFunc * Signal(int signo, SigFunc *func)
-{
- struct sigaction new_handl, old_handl;
- new_handl.sa_handler=func;
- /* clear signal mask: no signal blocked during execution of func */
- if (sigemptyset(&new_handl.sa_mask)!=0){ /* initialize signal set */
- perror("cannot initializes the signal set to empty"); /* see mess. */
- exit(1);
- }
- new_handl.sa_flags=0; /* init to 0 all flags */
- /* change action for signo signal */
- if (sigaction(signo,&new_handl,&old_handl)){
- perror("sigaction failed on signal action setting");
- exit(1);
- }
- return (old_handl.sa_handler);
-}
-
-/**
- ** Defining prototypes for the all other functions
- **/
-ssize_t SockRead(int fd, void *buf, size_t count);
-ssize_t SockWrite(int fd, const void *buf, size_t count);
-
-void HandSIGCHLD(int sig);