]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - afs.c
Introduce template commands for command_util.sh.
[paraslash.git] / afs.c
diff --git a/afs.c b/afs.c
index c5673c6542835d669691b36bb616f4e357748d11..0a3d1038560f8591fddc8072d59dd3ef3df7fd78 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -1,14 +1,19 @@
 #include "para.h"
+#include "afh.h"
 #include "error.h"
 #include <dirent.h> /* readdir() */
 #include <sys/mman.h>
 #include <sys/time.h>
-
+//#include <inttypes.h>
 
 #include "net.h"
 #include "afs.h"
 #include "ipc.h"
 #include "string.h"
+#include "list.h"
+#include "sched.h"
+#include "signal.h"
+#include "fd.h"
 
 /** \file afs.c Paraslash's audio file selector. */
 
@@ -64,6 +69,18 @@ enum afs_table_num {
 
 static struct table_info afs_tables[NUM_AFS_TABLES];
 
+struct command_task {
+       /** The file descriptor for the local socket. */
+       int fd;
+       /**
+        * Value sent by the command handlers to identify themselves as
+        * children of the running para_server.
+        */
+       uint32_t cookie;
+       /** The associated task structure. */
+       struct task task;
+};
+
 
 /**
  * A wrapper for strtol(3).
@@ -127,6 +144,18 @@ struct callback_data {
        int sma_ret;
 };
 
+struct callback_query {
+       /** The function to be called. */
+       callback_function *handler;
+       /** The number of bytes of the query */
+       size_t query_size;
+};
+
+struct callback_result {
+       /** The number of bytes of the result. */
+       size_t result_size;
+};
+
 static struct callback_data *shm_callback_data;
 static int callback_mutex;
 static int child_mutex;
@@ -365,16 +394,6 @@ int stdin_command(struct osl_object *arg_obj, callback_function *f,
        return ret;
 }
 
-static void para_init_random_seed(void)
-{
-       struct timeval now;
-       unsigned int seed;
-
-       gettimeofday(&now, NULL);
-       seed = now.tv_usec;
-       srand(seed);
-}
-
 /**
  * Open the audio file with highest score.
  *
@@ -416,6 +435,7 @@ int close_audio_file(struct audio_file_data *afd)
        return para_munmap(afd->map.data, afd->map.size);
 }
 
+#if 0
 static void play_loop(enum play_mode current_play_mode)
 {
        int i, ret;
@@ -433,6 +453,8 @@ static void play_loop(enum play_mode current_play_mode)
                close_audio_file(&afd);
        }
 }
+#endif
+
 
 static enum play_mode init_admissible_files(void)
 {
@@ -463,18 +485,239 @@ static enum play_mode init_admissible_files(void)
        return PLAY_MODE_MOOD;
 }
 
-static int afs_init(void)
+static int setup_command_socket_or_die(void)
+{
+       int ret;
+       char *socket_name = "/tmp/afs_command_socket";
+       struct sockaddr_un unix_addr;
+
+       unlink(socket_name);
+       ret = create_local_socket(socket_name, &unix_addr,
+               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
+       if (ret < 0)
+               exit(EXIT_FAILURE);
+       if (listen(ret , 5) < 0) {
+               PARA_EMERG_LOG("%s", "can not listen on socket\n");
+               exit(EXIT_FAILURE);
+       }
+       PARA_INFO_LOG("listening on command socket %s (fd %d)\n", socket_name,
+               ret);
+       return ret;
+}
+
+static int server_socket;
+
+void loop(void)
+{
+       for (;;)
+               sleep(1);
+}
+
+static void afs_shutdown(enum osl_close_flags flags)
+{
+       PARA_NOTICE_LOG("cleaning up\n");
+       score_shutdown(flags);
+       attribute_shutdown(flags);
+       mood_close();
+       playlist_close();
+       moods_shutdown(flags);
+       playlists_shutdown(flags);
+       lyrics_shutdown(flags);
+       images_shutdown(flags);
+       aft_shutdown(flags);
+}
+
+static void signal_pre_select(struct sched *s, struct task *t)
+{
+       struct signal_task *st = t->private_data;
+       t->ret = 1;
+       para_fd_set(st->fd, &s->rfds, &s->max_fileno);
+}
+
+static void signal_post_select(struct sched *s, struct task *t)
+{
+       struct signal_task *st = t->private_data;
+       t->ret = 1;
+       if (!FD_ISSET(st->fd, &s->rfds))
+               return;
+       st->signum = para_next_signal();
+       PARA_NOTICE_LOG("caught signal %d\n", st->signum);
+       t->ret = 1;
+       if (st->signum == SIGUSR1)
+               return; /* ignore SIGUSR1 */
+       afs_shutdown(OSL_MARK_CLEAN);
+       t->ret = -E_SIGNAL_CAUGHT;
+}
+
+static void register_signal_task(void)
+{
+       static struct signal_task signal_task_struct;
+       struct signal_task *st = &signal_task_struct;
+       st->fd = para_signal_init();
+       PARA_INFO_LOG("signal pipe: fd %d\n", st->fd);
+       para_install_sighandler(SIGINT);
+       para_install_sighandler(SIGTERM);
+       para_install_sighandler(SIGPIPE);
+
+       st->task.pre_select = signal_pre_select;
+       st->task.post_select = signal_post_select;
+       st->task.private_data = st;
+       sprintf(st->task.status, "signal task");
+       register_task(&st->task);
+}
+
+static void command_pre_select(struct sched *s, struct task *t)
+{
+       struct command_task *ct = t->private_data;
+       t->ret = 1;
+       para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
+}
+
+/*
+ * On errors, negative value is written to fd.
+ * On success: If query produced a result, the result_shmid is written to fd.
+ * Otherwise, zero is written.
+ */
+static int call_callback(int fd, int query_shmid)
+{
+       void *query_shm, *result_shm;
+       struct callback_query *cq;
+       struct callback_result *cr;
+       struct osl_object query, result = {.data = NULL};
+       int result_shmid = -1, ret, ret2;
+
+       ret = shm_attach(query_shmid, ATTACH_RO, &query_shm);
+       if (ret < 0)
+               goto out;
+       cq = query_shm;
+       query.data = (char *)query_shm + sizeof(*cq);
+       query.size = cq->query_size;
+       ret = cq->handler(&query, &result);
+       ret2 = shm_detach(query_shm);
+       if (ret2 < 0 && ret >= 0)
+               ret = ret2;
+       if (ret < 0)
+               goto out;
+       ret = 0;
+       if (!result.data || !result.size)
+               goto out;
+       ret = shm_new(result.size + sizeof(struct callback_result));
+       if (ret < 0)
+               goto out;
+       result_shmid = ret;
+       ret = shm_attach(result_shmid, ATTACH_RW, &result_shm);
+       if (ret < 0)
+               goto out;
+       cr = result_shm;
+       cr->result_size = result.size;
+       memcpy(result_shm + sizeof(*cr), result.data, result.size);
+       ret = shm_detach(result_shm);
+       if (ret < 0)
+               goto out;
+       ret = result_shmid;
+out:
+       free(result.data);
+       ret2 = send_bin_buffer(fd, (char *)ret, sizeof(int));
+       if (ret < 0 || ret2 < 0) {
+               if (result_shmid >= 0)
+                       if (shm_destroy(result_shmid) < 0)
+                               PARA_ERROR_LOG("destroy result failed\n");
+               if (ret >= 0)
+                       ret = ret2;
+       }
+       return ret;
+}
+
+static void command_post_select(struct sched *s, struct task *t)
+{
+       struct command_task *ct = t->private_data;
+       struct sockaddr_un unix_addr;
+       char buf[sizeof(uint32_t) + sizeof(int)];
+       uint32_t cookie;
+       int query_shmid, fd;
+
+       t->ret = 1;
+       if (!FD_ISSET(ct->fd, &s->rfds))
+               return;
+       t->ret = para_accept(ct->fd, &unix_addr, sizeof(unix_addr));
+       if (t->ret < 0)
+               return;
+       /*
+        * The following errors may be caused by a malicious local user. So do
+        * not return an error in this case as this would terminate  para_afs
+        * and para_server.
+        */
+       fd = t->ret;
+       t->ret = recv_bin_buffer(ct->fd, buf, sizeof(buf));
+       if (t->ret < 0) {
+               PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
+               t->ret = 1;
+               goto out;
+       }
+       if (t->ret != sizeof(buf)) {
+               PARA_NOTICE_LOG("short read (%d bytes, expected %d)\n",
+                       t->ret, sizeof(buf));
+               t->ret = 1;
+               goto out;
+       }
+       cookie = *(uint32_t *)buf;
+       if (cookie != ct->cookie) {
+               PARA_NOTICE_LOG("received invalid cookie(got %u, expected %u)\n",
+                       (unsigned)cookie, (unsigned)ct->cookie);
+               t->ret = 1;
+               goto out;
+       }
+       query_shmid = *(int *)(buf + sizeof(cookie));
+       if (query_shmid < 0) {
+               PARA_WARNING_LOG("received invalid query shmid %d)\n",
+                       query_shmid);
+               t->ret = 1;
+               goto out;
+       }
+       t->ret = call_callback(fd, query_shmid);
+       if (t->ret < 0) {
+               PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
+               t->ret = 1;
+               goto out;
+       }
+out:
+       close(fd);
+}
+
+static void register_command_task(uint32_t cookie)
+{
+       static struct command_task command_task_struct;
+       struct command_task *ct = &command_task_struct;
+       ct->fd = setup_command_socket_or_die();
+       ct->cookie = cookie;
+
+       ct->task.pre_select = command_pre_select;
+       ct->task.post_select = command_post_select;
+       ct->task.private_data = ct;
+       sprintf(ct->task.status, "command task");
+       register_task(&ct->task);
+}
+
+void register_tasks(uint32_t cookie)
 {
-       int ret, shmid;
-       void *shm_area;
+       register_signal_task();
+       register_command_task(cookie);
+}
+
+__noreturn int afs_init(uint32_t cookie, int socket_fd)
+{
+       int ret;
+//     void *shm_area;
        enum play_mode current_play_mode;
+       struct sched s;
 
-       para_init_random_seed();
+       server_socket = socket_fd;
+       PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
+               server_socket, (unsigned) cookie);
 
        ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES]);
-       PARA_DEBUG_LOG("ret %d\n", ret);
        if (ret < 0)
-               return ret;
+               goto attribute_init_error;
        ret = moods_init(&afs_tables[TBLNUM_MOODS]);
        if (ret < 0)
                goto moods_init_error;
@@ -495,8 +738,12 @@ static int afs_init(void)
                goto aft_init_error;
 
        current_play_mode = init_admissible_files();
-       play_loop(current_play_mode);
+       register_tasks(cookie);
+       s.default_timeout.tv_sec = 0;
+       s.default_timeout.tv_usec = 99 * 1000;
+       sched(&s);
 
+#if 0
        ret = shm_new(sizeof(struct callback_data));
        if (ret < 0)
                return ret;
@@ -518,7 +765,7 @@ static int afs_init(void)
                return ret;
        result_mutex = ret;
        mutex_lock(result_mutex);
-       return 1;
+#endif 
 aft_init_error:
        score_shutdown(OSL_MARK_CLEAN);
 score_init_error:
@@ -531,68 +778,10 @@ playlists_init_error:
        moods_shutdown(OSL_MARK_CLEAN);
 moods_init_error:
        attribute_shutdown(OSL_MARK_CLEAN);
-       return ret;
-}
-
-static uint32_t afs_socket_cookie;
-static int para_random(unsigned max)
-{
-       return ((max + 0.0) * (rand() / (RAND_MAX + 1.0)));
-}
-
-int setup(void)
-{
-       int ret, afs_server_socket[2];
-
-       para_init_random_seed();
-       ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, afs_server_socket);
-       if (ret < 0)
-               exit(EXIT_FAILURE);
-       afs_socket_cookie = para_random((uint32_t)-1);
-       ret = fork();
-       if (ret < 0)
-               exit(EXIT_FAILURE);
-       if (!ret) { /* child (afs) */
-               char *socket_name = "/tmp/afs_command_socket";
-               struct sockaddr_un unix_addr;
-               int fd;
-
-               unlink(socket_name);
-               ret = create_local_socket(socket_name, &unix_addr,
-                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
-               if (ret < 0)
-                       exit(EXIT_FAILURE);
-               fd = ret;
-               if (listen(fd , 5) < 0) {
-                       PARA_EMERG_LOG("%s", "can not listen on socket\n");
-                       exit(EXIT_FAILURE);
-               }
-               ret = afs_init();
-               if (ret < 0)
-                       exit(EXIT_FAILURE);
-               PARA_NOTICE_LOG("accepting\n");
-               ret = para_accept(fd, &unix_addr, sizeof(struct sockaddr_un));
-               return ret;
-       }
-       ret = fork();
-       if (ret < 0)
-               exit(EXIT_FAILURE);
-       if (!ret) { /* child (handler) */
-               PARA_NOTICE_LOG("reading stdin\n");
-               for (;;) {
-                       char buf[255];
-                       read(0, buf, 255);
-                       PARA_NOTICE_LOG("read: %s\n", buf);
-               }
-
-       }
-       for (;;) {
-               sleep(10);
-               PARA_NOTICE_LOG("sending next requerst\n");
-       }
+attribute_init_error:
+       exit(EXIT_FAILURE);
 }
 
-
 static int create_all_tables(void)
 {
        int i, ret;
@@ -610,7 +799,7 @@ static int create_all_tables(void)
 }
 
 /* TODO load tables after init */
-static int com_init(__a_unused int fd, int argc, const char **argv)
+int com_init(__a_unused int fd, int argc, const char **argv)
 {
        int i, j, ret;
        if (argc == 1)
@@ -634,6 +823,8 @@ static int com_init(__a_unused int fd, int argc, const char **argv)
        }
        return 1;
 }
+
+#if 0
 /** Describes a command of para_server. */
 struct command {
        /** The name of the command. */
@@ -642,7 +833,7 @@ struct command {
        int (*handler)(int fd, int argc, const char **argv);
 };
 
-static struct command cmd[] = {
+static struct command afs_cmds[] = {
 {
        .name = "add",
        .handler = com_add,
@@ -816,28 +1007,7 @@ out:
        mutex_unlock(result_mutex); /* wake up child */
 }
 
-static void dummy(__a_unused int s)
-{}
-
-static void afs_shutdown(enum osl_close_flags flags)
-{
-       score_shutdown(flags);
-       attribute_shutdown(flags);
-       mood_close();
-       playlist_close();
-       moods_shutdown(flags);
-       playlists_shutdown(flags);
-       lyrics_shutdown(flags);
-       images_shutdown(flags);
-       aft_shutdown(flags);
-}
-
 static int got_sigchld;
-static void sigchld_handler(__a_unused int s)
-{
-       got_sigchld = 1;
-}
-
 static void server_loop(int child_pid)
 {
 //     int status;
@@ -860,7 +1030,6 @@ static void server_loop(int child_pid)
        }
 }
 
-#if 0
 int main(int argc, const char **argv)
 {
        int i, ret = -E_AFS_SYNTAX;