From 67860598185d248756316549a7522968f7294990 Mon Sep 17 00:00:00 2001 From: ZoRo Date: Thu, 26 Jan 2017 22:23:28 +0000 Subject: Made working basic mq IPC communication --- tool/Makefile | 2 + tool/mqtool.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 tool/Makefile create mode 100644 tool/mqtool.c (limited to 'tool') diff --git a/tool/Makefile b/tool/Makefile new file mode 100644 index 0000000..915bf78 --- /dev/null +++ b/tool/Makefile @@ -0,0 +1,2 @@ +make: + gcc mqtool.c -o mqtool -lrt \ No newline at end of file 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#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