From c2fb35a0dc211c047a33f9f92ee0777373686a5c Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 13 May 2020 18:57:37 +0200
Subject: [PATCH] Creation is successfull, but not send

---
 inc/common/queue.h |  2 +-
 src/commands.c     |  2 ++
 src/queue.c        | 24 +++++++++++++++++-------
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/inc/common/queue.h b/inc/common/queue.h
index d80806a5..14187549 100644
--- a/inc/common/queue.h
+++ b/inc/common/queue.h
@@ -4,7 +4,7 @@
 #include <sys/stat.h>
 #include <mqueue.h>
 
-#define LEKTORD_QUEUE_NAME "LEKTORD_QUEUE"
+#define LEKTORD_QUEUE_NAME "/LEKTORD_QUEUE"
 
 enum lkt_event_type {
     lkt_event_null = 0,         /* NULL         */
diff --git a/src/commands.c b/src/commands.c
index f4f0c7cf..9a1b7c40 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -31,6 +31,7 @@ command_restart(struct lkt_state *srv, size_t c)
     }
     close(srv->fds[0].fd);
     database_queue_state(srv->db, &sta);
+    lkt_queue_close(srv->queue);
     env_set(LKT_ENV_RESTART, "1");
     int len = long_length(sta.current);
     if (len > 0) {
@@ -89,6 +90,7 @@ command_kill(struct lkt_state *srv, size_t c)
     RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false);
     LOG_INFO_SCT("GENERAL", "%s", "Stopping lektord");
     close(srv->fds[0].fd);
+    lkt_queue_close(srv->queue);
     database_close_all();
     exit(EXIT_SUCCESS);
 }
diff --git a/src/queue.c b/src/queue.c
index 07bec454..a7ae48a3 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -12,7 +12,14 @@ mqd_t
 lkt_queue_open(void)
 {
     errno = 0;
-    mqd_t ret = mq_open(LEKTORD_QUEUE_NAME, O_RDONLY | O_CREAT | O_NONBLOCK); /* Check EAGAIN */
+    struct mq_attr attr = {
+        .mq_flags   = O_NONBLOCK,
+        .mq_maxmsg  = 8,
+        .mq_msgsize = sizeof(void *),
+        .mq_curmsgs = 1,
+    };
+    UNUSED(&attr);
+    mqd_t ret = mq_open(LEKTORD_QUEUE_NAME, O_RDWR | O_CREAT, 0622, &attr);
 
     switch (errno) {
     case 0:
@@ -20,17 +27,17 @@ lkt_queue_open(void)
 
     case EACCES:
         LOG_ERROR_SCT("QUEUE", "Queue %s already exists", LEKTORD_QUEUE_NAME);
-        return 0;
+        return ret;
 
     case ENOMEM:
         LOG_ERROR_SCT("QUEUE", "%s", "Out of memory");
         exit(EXIT_FAILURE);
 
     default:
-        LOG_ERROR_SCT("QUEUE", "Unhandled error '%s' (%d)", strerror(errno), errno);
+        LOG_ERROR_SCT("QUEUE", "Unhandled error '%s' (%d)",
+                      strerror(errno), errno);
         exit(EXIT_FAILURE);
     }
-
 }
 
 void
@@ -43,13 +50,16 @@ lkt_queue_close(mqd_t queue)
 void
 lkt_queue_send(enum lkt_event_type type, void *attr)
 {
+    errno = 0;
     mqd_t queue = mq_open(LEKTORD_QUEUE_NAME, O_WRONLY);
-    if (queue >= 0) {
+    if (queue != (mqd_t) -1) {
         if (mq_send(queue, attr, sizeof(void *), type))
-            LOG_WARN_SCT("QUEUE", "Failed to send msg of type %d", type);
+            LOG_WARN_SCT("QUEUE", "Failed to send msg of type %d: %s",
+                         type, strerror(errno));
         mq_close(queue);
     } else
-        LOG_ERROR_SCT("QUEUE", "Failed to open queue '%s'", LEKTORD_QUEUE_NAME);
+        LOG_ERROR_SCT("QUEUE", "Failed to open queue '%s': %s",
+                      LEKTORD_QUEUE_NAME, strerror(errno));
 }
 
 lkt_event
-- 
GitLab