From bad57eee70cdca87e8ba1af08d4e2ef6d2878620 Mon Sep 17 00:00:00 2001 From: Andre Date: Tue, 21 Feb 2006 08:12:46 +0100 Subject: [PATCH] The new ipc subsystem Contains functions for dealing with mutexes and shared memory. --- configure.ac | 2 +- error.h | 9 ++++- ipc.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ ipc.h | 7 ++++ 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 ipc.c create mode 100644 ipc.h diff --git a/configure.ac b/configure.ac index 46ba57e5..5e11767e 100644 --- a/configure.ac +++ b/configure.ac @@ -73,7 +73,7 @@ audiod_ldflags="" server_cmdline_objs="server.cmdline" server_errlist_objs="server mp3 afs command net string signal random_dbtool time daemon stat - crypt http_send db close_on_fork" + crypt http_send db close_on_fork ipc" server_ldflags="" ########################################################################### ssl diff --git a/error.h b/error.h index 028dd7f2..7532509a 100644 --- a/error.h +++ b/error.h @@ -24,7 +24,7 @@ enum para_subsystem {SS_RECV, SS_STRING, SS_DAEMON, SS_STAT, SS_TIME, SS_GRAB_CLIENT, SS_HTTP_RECV, SS_RECV_COMMON, SS_FILTER_CHAIN, SS_WAV, SS_COMPRESS, SS_OGGDEC, SS_FILTER, SS_COMMAND, SS_RANDOM_DBTOOL, SS_CRYPT, SS_HTTP_SEND, SS_ORTP_SEND, SS_DB, SS_OGG, - SS_MP3, SS_MP3DEC, SS_SERVER, SS_AFS, SS_MYSQL, SS_RINGBUFFER}; + SS_MP3, SS_MP3DEC, SS_SERVER, SS_AFS, SS_MYSQL, SS_IPC, SS_RINGBUFFER}; #define NUM_SS (SS_RINGBUFFER + 1) extern const char **para_errlist[]; /** \endcond */ @@ -224,6 +224,12 @@ extern const char **para_errlist[]; PARA_ERROR(LOCK, "lock error"), \ PARA_ERROR(SENDER_CMD, "command not supported by this sender"), \ +#define IPC_ERRORS \ + PARA_ERROR(SEM_GET, "failed to create semaphore"), \ + PARA_ERROR(SEM_REMOVE, "can not remove semaphore"), \ + PARA_ERROR(SHM_GET, "failed to allocate shared memory area"), \ + PARA_ERROR(SHM_DESTROY, "failed to destroy shared memory area"), \ + PARA_ERROR(SHM_DETACH, "can not detach shared memory area"), \ /* these do not need error handling (yet) */ #define SERVER_ERRORS @@ -337,6 +343,7 @@ SS_ENUM(HTTP_SEND); SS_ENUM(ORTP_SEND); SS_ENUM(DB); SS_ENUM(MYSQL); +SS_ENUM(IPC); SS_ENUM(RINGBUFFER); /** \endcond */ #undef PARA_ERROR diff --git a/ipc.c b/ipc.c new file mode 100644 index 00000000..9ae2b3a0 --- /dev/null +++ b/ipc.c @@ -0,0 +1,105 @@ +#include "para.h" +#include "error.h" +#include "ipc.h" +#include +#include + + +int mutex_new(void) +{ + int ret = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666); + return ret < 0? -E_SEM_GET : ret; +} + +int mutex_remove(int id) +{ + int ret = semctl(id, 0, IPC_RMID); + return ret < 0? -E_SEM_REMOVE : 1; +} + +static void para_semop(int id, struct sembuf *sops, int num) +{ + if (semop(id, sops, num) >= 0) + return; + PARA_WARNING_LOG("semop failed (%s), retrying\n", strerror(errno)); + while (semop(id, sops, num) < 0) + ; /* nothing */ +} + +/** + * lock the given mutex + * + * \sa semop(2), struct misc_meta_data + */ +void mutex_lock(int id) +{ + struct sembuf sops[2] = { + { + .sem_num = 0, + .sem_op = 0, + .sem_flg = SEM_UNDO + }, + { + .sem_num = 0, + .sem_op = 1, + .sem_flg = SEM_UNDO + } + }; + para_semop(id, sops, 2); +} + +/** + * unlock a mutex + * + * \sa semop(2), struct misc_meta_data + */ +void mutex_unlock(int id) +{ + struct sembuf sops[1] = { + { + .sem_num = 0, + .sem_op = -1, + .sem_flg = SEM_UNDO + }, + }; + para_semop(id, sops, 1); +} + +/** + * create a new shared memory area of given size + * + * \sa shmget(2) + */ +int shm_new(size_t size) +{ + int ret = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | 0600); + return ret < 0 ? -E_SHM_GET : ret; +} + +/** + * destroy the given shared memory area + * \sa shmctl(2) + **/ +int shm_destroy(int id) +{ + struct shmid_ds shm_desc; + int ret = shmctl(id, IPC_RMID, &shm_desc); + return ret < 0? -E_SHM_DESTROY : ret; +} + +/** + * attach a shared memory area + * + * \sa semop(2) + */ +void *shm_attach(int id, enum shm_attach_mode mode) +{ + if (mode == ATTACH_RW) + return shmat(id, NULL, 0); + return shmat(id, NULL, SHM_RDONLY); +} +int shm_detach(void *addr) +{ + int ret = shmdt(addr); + return ret < 0? -E_SHM_DETACH : 1; +} diff --git a/ipc.h b/ipc.h new file mode 100644 index 00000000..25c56d26 --- /dev/null +++ b/ipc.h @@ -0,0 +1,7 @@ +/** \file ipc.h inter process communication and shared memory routines */ + +enum shm_attach_mode {ATTACH_RO, ATTACH_RW}; + +int mutex_new(void); +void mutex_lock(int id); +void mutex_unlock(int id); -- 2.39.5