#include "afs.h"
#include "config.h"
#include "close_on_fork.h"
-#include <sys/mman.h> /* MAP_SHARED, PROT_READ, PROT_WRITE */
#include "send.h"
#include "error.h"
#include "net.h"
#include "daemon.h"
#include "string.h"
+#include "ipc.h"
/** define the array of error lists needed by para_server */
INIT_SERVER_ERRLISTS;
/* global variables for server-internal use */
static FILE *logfile;
-static int mmd_semid;
+static int mmd_mutex, mmd_shm_id;
static int signal_pipe;
/**
va_end(argp);
}
-
/*
- * setup shared memory area and get semaphore for locking
+ * setup shared memory area and get mutex for locking
*/
static void shm_init(void)
{
- int fd;
- caddr_t area;
+ void *shm;
+ int ret = shm_new(sizeof(struct misc_meta_data));
- if ((fd = open("/dev/zero", O_RDWR)) < 0) {
- PARA_EMERG_LOG("%s", "failed to open /dev/zero\n");
- exit(EXIT_FAILURE);
- }
- if ((area = mmap(0, sizeof(struct misc_meta_data),
- PROT_READ | PROT_WRITE,
- MAP_SHARED, fd, 0)) == (caddr_t) - 1) {
- PARA_EMERG_LOG("%s", "mmap error\n");
- exit(EXIT_FAILURE);
- }
- close(fd); /* we dont need /dev/zero anymore */
- mmd = (struct misc_meta_data *)area;
+ if (ret < 0)
+ goto err_out;
+
+ ret = shm_attach(ret, ATTACH_RW, &shm);
+ if (ret < 0)
+ goto err_out;
+ mmd = shm;
+ mmd_shm_id = ret;
+
+ ret = mutex_new();
+ if (ret < 0)
+ goto err_out;
+ mmd_mutex = ret;
mmd->dbt_num = 0;
mmd->num_played = 0;
mmd->afs_status_flags = AFS_NEXT;
mmd->new_afs_status_flags = AFS_NEXT;
mmd->sender_cmd_data.cmd_num = -1;
-
- mmd_semid = semget(42, 1, IPC_CREAT | 0666);
- if (mmd_semid == -1) {
- PARA_EMERG_LOG("%s", "semget failed\n");
- exit(EXIT_FAILURE);
- }
-}
-
-static void do_semop(struct sembuf *sops, int num)
-{
- if (semop(mmd_semid, sops, num) >= 0)
- return;
- PARA_WARNING_LOG("semop failed (%s), retrying\n", strerror(errno));
- while (semop(mmd_semid, sops, num) < 0)
- ; /* nothing */
+ return;
+err_out:
+ PARA_EMERG_LOG("%s", PARA_STRERROR(-ret));
+ exit(EXIT_FAILURE);
}
/**
*/
void mmd_lock(void)
{
- struct sembuf sops[2] = {
- {
- .sem_num = 0,
- .sem_op = 0,
- .sem_flg = SEM_UNDO
- },
- {
- .sem_num = 0,
- .sem_op = 1,
- .sem_flg = SEM_UNDO
- }
- };
- do_semop(sops, 2);
+ mutex_lock(mmd_mutex);
}
/**
*
* \sa semop(2), struct misc_meta_data
*/
+
void mmd_unlock(void)
{
- struct sembuf sops[1] = {
- {
- .sem_num = 0,
- .sem_op = -1,
- .sem_flg = SEM_UNDO
- },
- };
- do_semop(sops, 1);
+ mutex_unlock(mmd_mutex);
}
static void parse_config(int override)
case SIGTERM:
PARA_EMERG_LOG("terminating on signal %d\n", sig);
kill(0, SIGTERM);
+ dblist[mmd->dbt_num].shutdown();
+ mutex_destroy(mmd_mutex);
+ shm_detach(mmd);
+ shm_destroy(mmd_shm_id);
exit(EXIT_FAILURE);
}
}