summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZoRo <dos21h@gmail.com>2020-08-22 23:13:49 +0100
committerZoRo <dos21h@gmail.com>2020-08-22 23:13:49 +0100
commit10b377468b0a4bd2230cd203fa1745163ad39f96 (patch)
treeb1125b30b8a38ade2e799234cee2101263cf250b
downloadmqueue_examples-master.tar.gz
mqueue_examples-master.zip
Initial commitHEADmaster
-rw-r--r--Makefile9
-rwxr-xr-xdaemon.sh46
-rw-r--r--mqueue_create.c95
-rw-r--r--mqueue_info.c103
-rw-r--r--mqueue_pop.c113
-rw-r--r--mqueue_push.c110
-rw-r--r--mqueue_remove.c85
-rwxr-xr-xsend_command.sh15
8 files changed, 576 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..85eeee7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+make:
+ gcc mqueue_create.c -o mqueue_create -lrt
+ gcc mqueue_pop.c -o mqueue_pop -lrt
+ gcc mqueue_push.c -o mqueue_push -lrt
+ gcc mqueue_remove.c -o mqueue_remove -lrt
+ gcc mqueue_info.c -o mqueue_info -lrt
+
+clean:
+ rm -f mqueue_create mqueue_pop mqueue_push mqueue_remove mqueue_info \ No newline at end of file
diff --git a/daemon.sh b/daemon.sh
new file mode 100755
index 0000000..5d0784b
--- /dev/null
+++ b/daemon.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+
+QUEUE_NAME=/music-player-daemon
+
+POP_CMD()
+{
+ ./mqueue_pop -m $QUEUE_NAME
+}
+
+CREATE_QUEUE()
+{
+ ./mqueue_create -m $QUEUE_NAME
+}
+
+REMOVE_QUEUE()
+{
+ ./mqueue_remove -m $QUEUE_NAME
+}
+
+
+
+if [ ! -f "/dev/mqueue$QUEUE_NAME" ]; then
+ CREATE_QUEUE
+fi
+
+state="1"
+while [ $state -ne 0 ]; do
+ get_cmd=$(POP_CMD)
+ case "$get_cmd" in
+ stop)
+ state="0"
+ ;;
+ ls)
+ ls /dev
+ ;;
+ aloha)
+ echo "aloha"
+ ;;
+ esac
+ sleep 1
+done
+
+if [ -f "/dev/mqueue$QUEUE_NAME" ]; then
+ REMOVE_QUEUE
+fi
diff --git a/mqueue_create.c b/mqueue_create.c
new file mode 100644
index 0000000..492e796
--- /dev/null
+++ b/mqueue_create.c
@@ -0,0 +1,95 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <mqueue.h>
+#include <errno.h>
+
+typedef struct {
+ char *f_mqname;
+ int f_verbose;
+ int f_helper;
+} mqueue_params;
+
+mqueue_params g_params;
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS]\n\n"
+ "Version: 0.0.1 \n"
+ "-m mqueue name\n"
+ "-v verbose output\n"
+ "-h help options\n"
+ "\n"
+ , progname);
+}
+
+#define full_mqpath_len 1024
+
+
+int main(int argc, char **argv)
+{
+ //stack params
+ int c,err;
+ int i;
+ int mq_fd;
+ char full_mqpath[full_mqpath_len];
+
+
+ memset(&g_params,0,sizeof(g_params));
+
+ //process arguments
+ while ((c = getopt(argc, argv, "m:vh")) != -1)
+ {
+ switch (c)
+ {
+ case 'm':
+ g_params.f_mqname = optarg;
+ break;
+ case 'v':
+ g_params.f_verbose = 1;
+ break;
+ case 'h':
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if (g_params.f_helper)
+ {
+ helper(argv[0]);
+ return 0;
+ }
+
+ //set mqueue name
+ if (g_params.f_mqname == NULL)
+ {
+ g_params.f_mqname = "/stack-0";
+ }
+
+ //compose string
+ memset(&full_mqpath[0], 0, full_mqpath_len);
+ snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
+ if (g_params.f_verbose)
+ {
+ printf("Full path: %s\n",&full_mqpath);
+ }
+
+ mq_fd = mq_open(g_params.f_mqname,O_RDWR | O_CREAT, 0666, NULL);
+ err = errno;
+ if (mq_fd == -1)
+ {
+ if (g_params.f_verbose)
+ {
+ printf("ERROR:%d,%s\n",err,strerror(err));
+ }
+ return -1;
+ }
+ printf("%s\n",g_params.f_mqname);
+
+ close(mq_fd);
+
+ return 0;
+} \ No newline at end of file
diff --git a/mqueue_info.c b/mqueue_info.c
new file mode 100644
index 0000000..9374ae5
--- /dev/null
+++ b/mqueue_info.c
@@ -0,0 +1,103 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <mqueue.h>
+#include <errno.h>
+
+typedef struct {
+ char *f_mqname;
+ int f_verbose;
+ int f_helper;
+} mqueue_params;
+
+mqueue_params g_params;
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS] VALUE\n\n"
+ "Version: 0.0.1 \n"
+ "-m mqueue name\n"
+ "-v verbose output\n"
+ "-h help options\n"
+ "\n"
+ , progname);
+}
+
+#define full_mqpath_len 1024
+
+int main(int argc, char **argv)
+{
+ //stack params
+ int ret=0;
+ int c,err;
+ int i;
+ int mq_fd;
+ char full_mqpath[full_mqpath_len];
+ int prio;
+ struct mq_attr attr;
+ char *pop_val=NULL;
+ int pop_size;
+
+
+ memset(&g_params,0,sizeof(g_params));
+
+ //process arguments
+ while ((c = getopt(argc, argv, "m:vh")) != -1)
+ {
+ switch (c)
+ {
+ case 'm':
+ g_params.f_mqname = optarg;
+ break;
+ case 'v':
+ g_params.f_verbose = 1;
+ break;
+ case 'h':
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if (g_params.f_helper)
+ {
+ helper(argv[0]);
+ return 0;
+ }
+
+ //set mqueue name
+ if (g_params.f_mqname == NULL)
+ {
+ g_params.f_mqname = "/stack-0";
+ }
+
+ //compose string
+ memset(&full_mqpath[0], 0, full_mqpath_len);
+ snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
+ if (g_params.f_verbose)
+ {
+ printf("Full path: %s\n",&full_mqpath);
+ }
+
+ mq_fd = mq_open(g_params.f_mqname,O_RDWR | O_CREAT, 0666, NULL);
+ err = errno;
+ if (mq_fd == -1)
+ {
+ if (g_params.f_verbose)
+ {
+ printf("ERROR:%d,%s\n",err,strerror(err));
+ }
+ return -1;
+ }
+
+ mq_getattr(mq_fd, &attr);
+
+ printf("%d\n",attr.mq_curmsgs);
+
+
+ close(mq_fd);
+
+ return ret;
+} \ No newline at end of file
diff --git a/mqueue_pop.c b/mqueue_pop.c
new file mode 100644
index 0000000..32cf6db
--- /dev/null
+++ b/mqueue_pop.c
@@ -0,0 +1,113 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <mqueue.h>
+#include <errno.h>
+
+typedef struct {
+ char *f_mqname;
+ int f_verbose;
+ int f_helper;
+} mqueue_params;
+
+mqueue_params g_params;
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS] VALUE\n\n"
+ "Version: 0.0.1 \n"
+ "-m mqueue name\n"
+ "-v verbose output\n"
+ "-h help options\n"
+ "\n"
+ , progname);
+}
+
+#define full_mqpath_len 1024
+
+int main(int argc, char **argv)
+{
+ //stack params
+ int ret=0;
+ int c,err;
+ int i;
+ int mq_fd;
+ char full_mqpath[full_mqpath_len];
+ int prio;
+ struct mq_attr attr;
+ char *pop_val=NULL;
+ int pop_size;
+
+
+ memset(&g_params,0,sizeof(g_params));
+
+ //process arguments
+ while ((c = getopt(argc, argv, "m:vh")) != -1)
+ {
+ switch (c)
+ {
+ case 'm':
+ g_params.f_mqname = optarg;
+ break;
+ case 'v':
+ g_params.f_verbose = 1;
+ break;
+ case 'h':
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if (g_params.f_helper)
+ {
+ helper(argv[0]);
+ return 0;
+ }
+
+ //set mqueue name
+ if (g_params.f_mqname == NULL)
+ {
+ g_params.f_mqname = "/stack-0";
+ }
+
+ //compose string
+ memset(&full_mqpath[0], 0, full_mqpath_len);
+ snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
+ if (g_params.f_verbose)
+ {
+ printf("Full path: %s\n",&full_mqpath);
+ }
+
+ mq_fd = mq_open(g_params.f_mqname,O_RDWR | O_CREAT, 0666, NULL);
+ err = errno;
+ if (mq_fd == -1)
+ {
+ if (g_params.f_verbose)
+ {
+ printf("ERROR:%d,%s\n",err,strerror(err));
+ }
+ return -1;
+ }
+ //printf("%s\n",g_params.f_mqname);
+
+ mq_getattr(mq_fd, &attr);
+
+ pop_val = malloc(attr.mq_msgsize+1);
+ memset(pop_val, 0, attr.mq_msgsize+1);
+
+ if (attr.mq_curmsgs > 0)
+ {
+ pop_size = mq_receive(mq_fd, pop_val, attr.mq_msgsize, &prio);
+ printf("%s\n",pop_val);
+ } else {
+ ret = -1;
+ }
+
+ close(mq_fd);
+ free(pop_val);
+
+ return ret;
+} \ No newline at end of file
diff --git a/mqueue_push.c b/mqueue_push.c
new file mode 100644
index 0000000..4f0e976
--- /dev/null
+++ b/mqueue_push.c
@@ -0,0 +1,110 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <mqueue.h>
+#include <errno.h>
+
+typedef struct {
+ char *f_mqname;
+ char *f_push;
+ int f_verbose;
+ int f_helper;
+} mqueue_params;
+
+mqueue_params g_params;
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS] VALUE\n\n"
+ "Version: 0.0.1 \n"
+ "-m mqueue name\n"
+ "-v verbose output\n"
+ "-p push value\n"
+ "-h help options\n"
+ "\n"
+ , progname);
+}
+
+#define full_mqpath_len 1024
+
+int main(int argc, char **argv)
+{
+ //stack params
+ int c,err;
+ int i;
+ int mq_fd;
+ char full_mqpath[full_mqpath_len];
+ int prio=10;
+ char *push_val="NONE";
+ int push_size=strlen(push_val);
+
+
+ memset(&g_params,0,sizeof(g_params));
+
+ //process arguments
+ while ((c = getopt(argc, argv, "m:vhp:")) != -1)
+ {
+ switch (c)
+ {
+ case 'm':
+ g_params.f_mqname = optarg;
+ break;
+ case 'v':
+ g_params.f_verbose = 1;
+ break;
+ case 'p':
+ g_params.f_push = optarg;
+ break;
+ case 'h':
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if (g_params.f_helper)
+ {
+ helper(argv[0]);
+ return 0;
+ }
+
+ //set mqueue name
+ if (g_params.f_mqname == NULL)
+ {
+ g_params.f_mqname = "/stack-0";
+ }
+
+ //compose string
+ memset(&full_mqpath[0], 0, full_mqpath_len);
+ snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
+ if (g_params.f_verbose)
+ {
+ printf("Full path: %s\n",&full_mqpath);
+ }
+
+ mq_fd = mq_open(g_params.f_mqname,O_RDWR | O_CREAT, 0666, NULL);
+ err = errno;
+ if (mq_fd == -1)
+ {
+ if (g_params.f_verbose)
+ {
+ printf("ERROR:%d,%s\n",err,strerror(err));
+ }
+ return -1;
+ }
+ printf("%s\n",g_params.f_mqname);
+
+ if (g_params.f_push != NULL)
+ {
+ push_val = g_params.f_push;
+ push_size = strlen(push_val);
+ }
+
+ mq_send(mq_fd, push_val, push_size, prio);
+
+ close(mq_fd);
+
+ return 0;
+} \ No newline at end of file
diff --git a/mqueue_remove.c b/mqueue_remove.c
new file mode 100644
index 0000000..e0ad775
--- /dev/null
+++ b/mqueue_remove.c
@@ -0,0 +1,85 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <mqueue.h>
+#include <errno.h>
+
+typedef struct {
+ char *f_mqname;
+ int f_verbose;
+ int f_helper;
+} mqueue_params;
+
+mqueue_params g_params;
+
+void helper(char *progname)
+{
+ printf("Usage: %s [OPTS] VALUE\n\n"
+ "Version: 0.0.1 \n"
+ "-m mqueue name\n"
+ "-v verbose output\n"
+ "-h help options\n"
+ "\n"
+ , progname);
+}
+
+#define full_mqpath_len 1024
+
+int main(int argc, char **argv)
+{
+ //stack params
+ int ret=0;
+ int c,err;
+ int i;
+ int mq_fd;
+ char full_mqpath[full_mqpath_len];
+
+
+ memset(&g_params,0,sizeof(g_params));
+
+ //process arguments
+ while ((c = getopt(argc, argv, "m:vh")) != -1)
+ {
+ switch (c)
+ {
+ case 'm':
+ g_params.f_mqname = optarg;
+ break;
+ case 'v':
+ g_params.f_verbose = 1;
+ break;
+ case 'h':
+ default:
+ helper(argv[0]);
+ exit(1);
+ }
+ }
+
+ if (g_params.f_helper)
+ {
+ helper(argv[0]);
+ return 0;
+ }
+
+ //set mqueue name
+ if (g_params.f_mqname == NULL)
+ {
+ g_params.f_mqname = "/stack-0";
+ }
+
+ //compose string
+ memset(&full_mqpath[0], 0, full_mqpath_len);
+ snprintf(&full_mqpath[0], full_mqpath_len-1, "%s", g_params.f_mqname);
+ if (g_params.f_verbose)
+ {
+ printf("Full path: %s\n",&full_mqpath);
+ }
+
+ mq_unlink(g_params.f_mqname);
+
+ close(mq_fd);
+
+ return ret;
+} \ No newline at end of file
diff --git a/send_command.sh b/send_command.sh
new file mode 100755
index 0000000..59219b6
--- /dev/null
+++ b/send_command.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+QUEUE_NAME=/music-player-daemon
+
+SEND_CMD()
+{
+ ./mqueue_push -m $QUEUE_NAME -p "$1"
+}
+
+echo "Send command to daemon"
+if [ -f "/dev/mqueue$QUEUE_NAME" ]; then
+ SEND_CMD $1
+else
+ echo "No queue to send"
+fi