mmd: rename dbinfo to selector_info
[paraslash.git] / server.c
index 044792f13c40807477cd984501deab4d0d0611e2..d881868c09cc826e561c2d7d49b5bb03de8fd4ea 100644 (file)
--- a/server.c
+++ b/server.c
@@ -21,8 +21,9 @@
 
 /** \mainpage Paraslash API Reference
  *
- *  Good starting points for reading are probably \ref dbtool, \ref sender,
- *  \ref receiver, \ref receiver_node, \ref filter, \ref filter_node.
+ *  Good starting points for reading are probably \ref audio_file_selector,
+ *  \ref sender, \ref receiver, \ref receiver_node, \ref filter, \ref
+ *  filter_node.
  *
  */
 
 #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;
@@ -47,11 +48,11 @@ INIT_SERVER_ERRLISTS;
 /** shut down non-authorized connections after that many seconds */
 #define ALARM_TIMEOUT 10
 
-/* these are exported to afs/command/dbtool */
+/* these are exported to afs.c. command.c and to all selectors */
 struct misc_meta_data *mmd;
 /** the configuration of para_server
  *
- * It also contains the options for all database tools and all supported
+ * It also contains the options for all audio file selectors and all supported
  * senders.
 */
 struct gengetopt_args_info conf;
@@ -60,18 +61,27 @@ extern void http_send_init(struct sender *);
 extern void ortp_send_init(struct sender *);
 extern struct audio_format afl[];
 
-/** the list of supported database tools */
-struct dbtool dblist[] = {
+/** the list of supported audio file selectors */
+struct audio_file_selector dblist[] = {
        {
                .name = "random",
-               .init = random_dbtool_init,
+               .init = random_selector_init,
                .update_audio_file = NULL,
        },
+       {
+               .name = "playlist",
+               .init = playlist_selector_init,
+               .update_audio_file = NULL,
+               .pre_select = NULL,
+               .post_select = NULL,
+       },
 #ifdef HAVE_MYSQL
        {
                .name = "mysql",
-               .init = mysql_dbtool_init,
+               .init = mysql_selector_init,
                .update_audio_file = NULL,
+               .pre_select = NULL,
+               .post_select = NULL,
        },
 #endif
        {
@@ -99,7 +109,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;
 
 /**
@@ -142,27 +152,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;
@@ -175,21 +185,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);
 }
 
 /**
@@ -199,19 +198,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);
 }
 
 /**
@@ -219,16 +206,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)
@@ -288,51 +269,35 @@ 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);
        }
 }
 
-static void init_dbtool(void)
+static void init_selector(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.selector_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.selector_arg))
+                       continue;
+               PARA_NOTICE_LOG("initializing %s audio file selector\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 selector\n");
 random:
        mmd->dbt_num = 0;
        dblist[0].init(&dblist[0]); /* always successful */
@@ -387,7 +352,7 @@ static unsigned do_inits(int argc, char **argv)
        /* become daemon */
        if (conf.daemon_given)
                daemon_init();
-       init_dbtool();
+       init_selector();
        PARA_NOTICE_LOG("%s", "initializing audio file sender\n");
        /* audio file sender */
        afs_init();
@@ -417,7 +382,7 @@ static void handle_dbt_change(void)
                return;
        }
        /* init failed */
-       PARA_ERROR_LOG("%s -- switching to the random dbtool\n", PARA_STRERROR(-ret));
+       PARA_ERROR_LOG("%s -- switching to the random selector\n", PARA_STRERROR(-ret));
        dblist[0].init(&dblist[0]);
        mmd->dbt_num = 0;
 }
@@ -431,8 +396,8 @@ static void handle_sighup(void)
        close_log(logfile); /* gets reopened if necessary by parse_config */
        logfile = NULL;
        parse_config(1); /* reopens log */
-       mmd->dbt_change = mmd->dbt_num; /* do not change dbtool */
-       handle_dbt_change(); /* force reloading dbtool */
+       mmd->dbt_change = mmd->dbt_num; /* do not change selector */
+       handle_dbt_change(); /* reload selector */
 }
 
 static void status_refresh(void)
@@ -494,6 +459,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);
@@ -502,6 +471,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) {
@@ -536,6 +507,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);
                }
        }