summaryrefslogtreecommitdiff
path: root/tool/mqtool.c
diff options
context:
space:
mode:
authorZoRo <dos21h@gmail.com>2017-01-26 22:23:28 +0000
committerZoRo <dos21h@gmail.com>2017-01-26 22:23:28 +0000
commit67860598185d248756316549a7522968f7294990 (patch)
treeb329576eca4f5dccbfec2bf7a4952abc93b8b9c8 /tool/mqtool.c
parenta588aa017512d3cc70dde6627d1218020e755259 (diff)
downloadagni-67860598185d248756316549a7522968f7294990.tar.gz
agni-67860598185d248756316549a7522968f7294990.zip
Made working basic mq IPC communication
Diffstat (limited to 'tool/mqtool.c')
-rw-r--r--tool/mqtool.c183
1 files changed, 183 insertions, 0 deletions
diff --git a/tool/mqtool.c b/tool/mqtool.c
new file mode 100644
index 0000000..61e78af
--- /dev/null
+++ b/tool/mqtool.c
@@ -0,0 +1,183 @@
+#define _GNU_SOURCE
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <sys/syscall.h>
+#include <sched.h>
+#include <stdatomic.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+#include <sys/stat.h>
+#include <sched.h>
+#include <mqueue.h>
+
+#define DEVMQPATH "/dev/mqueue/"
+#define AGNIPATH_IN "agni-%d-in"
+#define AGNIPATH_OUT "agni-%d-out"
+
+//------------------------------------------------------------------------------
+int main(int argc, char **argv)
+{
+ //generic variables
+ int c;
+ int i,j;
+ int iRet; //any func return param
+
+ //for directory listing
+ DIR *dirPath;
+ struct dirent *entryPath;
+
+ //list all mqs
+ int flagListAll = 0;
+ //show mq param
+ int flagMqParam = 0;
+ char* mqName = NULL;
+ //clear message queue
+ int flagClear = 0;
+ char* mqNameClear = NULL;
+ //clear print extra info if there is
+ int flagExtra = 0;
+
+
+ //get arguments
+ while ((c = getopt(argc, argv, "elp:c:")) != -1)
+ switch(c)
+ {
+ case 'l':
+ flagListAll = 1;
+ break;
+ case 'p':
+ flagMqParam = 1;
+ mqName = optarg;
+ break;
+ case 'c':
+ flagClear = 1;
+ mqNameClear = optarg;
+ break;
+ case 'e':
+ flagExtra = 1;
+ break;
+ default:
+ break;
+ }
+
+ //----------------------------------------------------------------------
+ //main logic
+
+ //list all
+ if (flagListAll)
+ {
+ dirPath = opendir(DEVMQPATH);
+ if (dirPath != NULL)
+ {
+ while (entryPath = readdir(dirPath))
+ {
+ int id;
+ if (entryPath->d_type == DT_REG)
+ {
+ if(sscanf(entryPath->d_name, "agni-%d", &id) == 1)
+ {
+ printf("%s\n",entryPath->d_name);
+ }
+ }
+ //should free entryPath? maybe
+ }
+ closedir(dirPath);
+ } else
+ {
+ perror("Cant open directory");
+ }
+
+ exit(0);
+ }
+
+ //get mqueue params
+ if (flagMqParam)
+ {
+ struct mq_attr mqAttr;
+ int mqFd = -1;
+
+ mqFd = mq_open(mqName, O_RDWR, 0666, NULL);
+ if (mqFd != -1)
+ {
+ iRet = mq_getattr(mqFd, &mqAttr);
+ if (iRet != -1)
+ {
+ printf("FLAGS %8ld, ", mqAttr.mq_flags);
+ printf("MSG SIZE %8ld, ", mqAttr.mq_msgsize);
+ printf("CUR MSGS %ld/%ld\n", mqAttr.mq_curmsgs,mqAttr.mq_maxmsg);
+ }
+ } else {
+ printf("Cant open %s\n", mqName);
+ }
+ exit(0);
+ }
+
+ //clear mq from messages
+ if (flagClear)
+ {
+ struct mq_attr mqAttr;
+ int mqFd = -1;
+ char *cBuf = NULL;
+ int numRead = -1;
+ int prio = 0;
+ int iCountMsg = 0;
+
+ mqFd = mq_open(mqNameClear, O_RDWR, 0666, NULL);
+ if (mqFd == -1)
+ {
+ printf("Cant open %s\n", mqName);
+ exit(1);
+ }
+
+ iRet = mq_getattr(mqFd, &mqAttr);
+ if (iRet == -1)
+ {
+ perror("Cant get attributes");
+ exit(1);
+ }
+
+ cBuf = malloc(mqAttr.mq_msgsize);
+ if (cBuf == NULL)
+ {
+ exit(1);
+ }
+
+ for (i=0; i<mqAttr.mq_curmsgs; i++)
+ {
+ numRead = mq_receive(mqFd, cBuf, mqAttr.mq_msgsize, &prio);
+ if (numRead == -1)
+ {
+ free(cBuf);
+ exit(0);
+ }
+ iCountMsg += 1;
+
+ //extra output of message value
+ if (flagExtra)
+ {
+ for (j=0; j<numRead; j++)
+ {
+ printf("%c",cBuf[j]);
+ }
+ printf("\n");
+ }
+ }
+
+ free(cBuf);
+
+ printf("Readed %d messages from %ld\n", iCountMsg, mqAttr.mq_curmsgs);
+
+
+ exit(0);
+ }
+
+} \ No newline at end of file