First draft of the dccp sender/receiver pair.
[paraslash.git] / server.c
index c185b3e..67d9bce 100644 (file)
--- a/server.c
+++ b/server.c
 #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;
@@ -56,6 +56,7 @@ struct misc_meta_data *mmd;
 */
 struct gengetopt_args_info conf;
 char *user_list = NULL;
+extern void dccp_send_init(struct sender *);
 extern void http_send_init(struct sender *);
 extern void ortp_send_init(struct sender *);
 extern struct audio_format afl[];
@@ -94,6 +95,10 @@ struct sender senders[] = {
                .name = "http",
                .init = http_send_init,
        },
+       {
+               .name = "dccp",
+               .init = dccp_send_init,
+       },
 #ifdef HAVE_ORTP
        {
                .name = "ortp",
@@ -108,7 +113,7 @@ struct sender senders[] = {
 
 /* global variables for server-internal use */
 static FILE *logfile;
-static int mmd_semid;
+static int mmd_mutex, mmd_shm_id;
 static int signal_pipe;
 
 /**
@@ -151,27 +156,27 @@ void para_log(int ll, char* fmt,...)
        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;
@@ -184,21 +189,10 @@ static void shm_init(void)
        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);
 }
 
 /**
@@ -208,19 +202,7 @@ static void do_semop(struct sembuf *sops, int num)
  */
 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);
 }
 
 /**
@@ -228,16 +210,10 @@ void mmd_lock(void)
  *
  * \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)
@@ -297,9 +273,9 @@ static void setup_signal_handling(void)
        ret += para_install_sighandler(SIGTERM);
        ret += para_install_sighandler(SIGHUP);
        ret += para_install_sighandler(SIGCHLD);
+       ret += para_install_sighandler(SIGUSR1);
        signal(SIGPIPE, SIG_IGN);
-       signal(SIGUSR1, SIG_IGN);
-       if (ret != 4) {
+       if (ret != 5) {
                PARA_EMERG_LOG("%s", "could not install signal handlers\n");
                exit(EXIT_FAILURE);
        }
@@ -307,41 +283,25 @@ static void setup_signal_handling(void)
 
 static void init_dbtool(void)
 {
-       int i;
+       int i, ret;
 
        mmd->dbt_change = -1; /* no change nec., set to new dbt num by com_cdt */
-       if (!dblist[1].name)
-               goto random;
-       if (conf.dbtool_given) {
-               for (i = 0; dblist[i].name; i++) {
-                       if (strcmp(dblist[i].name, conf.dbtool_arg))
-                               continue;
-                       PARA_NOTICE_LOG("initializing %s database tool\n",
-                               dblist[i].name);
-                       if (dblist[i].init(&dblist[i]) < 0) {
-                               PARA_WARNING_LOG("init %s failed",
-                                       dblist[i].name);
-                               goto random;
-                       }
-                       mmd->dbt_num = i;
-                       return;
-               }
-               PARA_WARNING_LOG("%s", "no such dbtool, switching to random\n");
+       if (!conf.dbtool_given)
                goto random;
-       }
-       /* use the first dbtool that works
-        * (assuming that random always works)
-        */
-       for (i = 1; dblist[i].name; i++) {
-               int ret = dblist[i].init(&dblist[i]);
-               if (ret >= 0) {
-                       PARA_INFO_LOG("initialized %s\n", dblist[i].name);
-                       mmd->dbt_num = i;
-                       return;
+       for (i = 0; dblist[i].name; i++) {
+               if (strcmp(dblist[i].name, conf.dbtool_arg))
+                       continue;
+               PARA_NOTICE_LOG("initializing %s database tool\n",
+                       dblist[i].name);
+               ret = dblist[i].init(&dblist[i]);
+               if (ret < 0) {
+                       PARA_WARNING_LOG("%s", PARA_STRERROR(-ret));
+                       break;
                }
-               PARA_CRIT_LOG("%s init failed: %s\n", dblist[i].name,
-                       PARA_STRERROR(-ret));
+               mmd->dbt_num = i;
+               return;
        }
+       PARA_WARNING_LOG("%s", "falling back to the random dbtool\n");
 random:
        mmd->dbt_num = 0;
        dblist[0].init(&dblist[0]); /* always successful */
@@ -361,7 +321,7 @@ static void init_random_seed(void)
        int fd, ret = -1, len = sizeof(unsigned int);
        unsigned int seed;
 
-       fd = open("/dev/random", O_RDONLY);
+       fd = open("/dev/urandom", O_RDONLY);
        if (fd < 0)
                goto out;
        ret = -2;
@@ -503,6 +463,10 @@ repeat:
                        &max_fileno,
                        &rfds, &wfds);
        }
+       if (dblist[mmd->dbt_num].pre_select) {
+               ret = dblist[mmd->dbt_num].pre_select(&rfds, &wfds);
+               max_fileno = MAX(max_fileno, ret);
+       }
        mmd_unlock();
 //     PARA_DEBUG_LOG("%s: select (max = %i)\n", __func__, max_fileno);
        ret = select(max_fileno + 1, &rfds, &wfds, NULL, timeout);
@@ -511,6 +475,8 @@ repeat:
        mmd_lock();
        if (mmd->dbt_change >= 0)
                handle_dbt_change();
+       if (dblist[mmd->dbt_num].post_select)
+               dblist[mmd->dbt_num].post_select(&rfds, &wfds);
        if (ret < 0 && err == EINTR)
                goto repeat;
        if (ret < 0) {
@@ -545,6 +511,10 @@ repeat:
                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);
                }
        }